|
| 1 | +// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*- |
| 2 | +// vi:set ts=4 sts=4 sw=4 noet : |
| 3 | +// Copyright 2015 The TPIE development team |
| 4 | +// |
| 5 | +// This file is part of TPIE. |
| 6 | +// |
| 7 | +// TPIE is free software: you can redistribute it and/or modify it under |
| 8 | +// the terms of the GNU Lesser General Public License as published by the |
| 9 | +// Free Software Foundation, either version 3 of the License, or (at your |
| 10 | +// option) any later version. |
| 11 | +// |
| 12 | +// TPIE is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 | +// License for more details. |
| 16 | +// |
| 17 | +// You should have received a copy of the GNU Lesser General Public License |
| 18 | +// along with TPIE. If not, see <http://www.gnu.org/licenses/> |
| 19 | + |
| 20 | +#ifndef __TPIE_PIPELINING_FILTER_H__ |
| 21 | +#define __TPIE_PIPELINING_FILTER_H__ |
| 22 | + |
| 23 | +#include <tpie/pipelining/map.h> |
| 24 | +#include <tpie/pipelining/node.h> |
| 25 | +#include <tpie/pipelining/pipe_base.h> |
| 26 | +#include <tpie/pipelining/factory_helpers.h> |
| 27 | +#include <tpie/pipelining/node_name.h> |
| 28 | + |
| 29 | +namespace tpie::pipelining { |
| 30 | +namespace bits { |
| 31 | + |
| 32 | +template <typename dest_t, typename F> |
| 33 | +class filter_map_t: public node { |
| 34 | +private: |
| 35 | + F functor; |
| 36 | + dest_t dest; |
| 37 | + |
| 38 | +public: |
| 39 | + typedef typename std::decay<typename unary_traits<F>::argument_type>::type item_type; |
| 40 | + |
| 41 | + filter_map_t(dest_t dest, const F & functor): |
| 42 | + functor(functor), dest(std::move(dest)) { |
| 43 | + set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME); |
| 44 | + } |
| 45 | + |
| 46 | + void push(const item_type & item) { |
| 47 | + typename F::result_type t=f(item); |
| 48 | + if (t.second) dest.push(t.first); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +} //namespace bits |
| 53 | + |
| 54 | +/////////////////////////////////////////////////////////////////////////////// |
| 55 | +/// \brief A pipelining node that applies a functor to items and only keeps |
| 56 | +/// some of them based on the functor's result. |
| 57 | +/// \details The result of the functor must be a pair, e.g. |
| 58 | +/// <tt>std::pair<T,bool></tt>: the second item is a boolean indiciating whether |
| 59 | +/// item should be pushed to the next node while the first one carries the value |
| 60 | +/// itself that is to be pushed. |
| 61 | +/// \param functor The functor that should be applied to items |
| 62 | +/////////////////////////////////////////////////////////////////////////////// |
| 63 | +template <typename F, typename = typename std::enable_if<bits::has_argument_type<F>::value>::type> |
| 64 | +pipe_middle<tfactory<bits::filter_map_t, Args<F>, F> > filter_map(const F & functor) { |
| 65 | + return {functor}; |
| 66 | +} |
| 67 | + |
| 68 | +} //namespace terrastream::pipelining |
| 69 | + |
| 70 | +#endif //__TPIE_PIPELINING_FILTER_H__ |
0 commit comments