Skip to content

Commit

Permalink
Replace 'exclude_lambda' with 'filter_map'
Browse files Browse the repository at this point in the history
In continuation of the deprecation issue thomasmoelhave#196
  • Loading branch information
SSoelvsten committed Mar 9, 2023
1 parent 59e4bb9 commit 0ef3785
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
1 change: 1 addition & 0 deletions tpie/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ set (HEADERS
pipelining/factory_helpers.h
pipelining/file_stream.h
pipelining/filter.h
pipelining/filter_map.h
pipelining/forwarder.h
pipelining/helpers.h
pipelining/internal_buffer.h
Expand Down
70 changes: 70 additions & 0 deletions tpie/pipelining/filter_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
// vi:set ts=4 sts=4 sw=4 noet :
// Copyright 2022 The TPIE development team
//
// This file is part of TPIE.
//
// TPIE is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with TPIE. If not, see <http://www.gnu.org/licenses/>

#ifndef __TPIE_PIPELINING_FILTER_MAP_H__
#define __TPIE_PIPELINING_FILTER_MAP_H__

#include <tpie/pipelining/map.h>
#include <tpie/pipelining/node.h>
#include <tpie/pipelining/pipe_base.h>
#include <tpie/pipelining/factory_helpers.h>
#include <tpie/pipelining/node_name.h>

namespace tpie::pipelining {
namespace bits {

template <typename dest_t, typename F>
class filter_map_t: public node {
private:
F functor;
dest_t dest;

public:
typedef typename std::decay<typename unary_traits<F>::argument_type>::type item_type;

filter_map_t(dest_t dest, const F & functor):
functor(functor), dest(std::move(dest)) {
set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
}

void push(const item_type & item) {
typename F::result_type t=f(item);
if (t.second) dest.push(t.first);
}
};

} //namespace bits

///////////////////////////////////////////////////////////////////////////////
/// \brief A pipelining node that applies a functor to items and only keeps
/// some of them based on the functor's result.
/// \details The result of the functor must be a pair, e.g.
/// <tt>std::pair<T,bool></tt>: the second item is a boolean indiciating whether
/// item should be pushed to the next node while the first one carries the value
/// itself that is to be pushed.
/// \param functor The functor that should be applied to items
///////////////////////////////////////////////////////////////////////////////
template <typename F, typename = typename std::enable_if<bits::has_argument_type<F>::value>::type>
pipe_middle<tfactory<bits::filter_map_t, Args<F>, F> > filter_map(const F & functor) {
return {functor};
}

} //namespace terrastream::pipelining

#endif //__TPIE_PIPELINING_FILTER_MAP_H__
18 changes: 3 additions & 15 deletions tpie/pipelining/std_glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <tpie/pipelining/pipe_base.h>
#include <tpie/pipelining/factory_helpers.h>
#include <tpie/pipelining/map.h>
#include <tpie/pipelining/filter_map.h>

namespace tpie::pipelining {
namespace bits {
Expand Down Expand Up @@ -101,21 +102,7 @@ template <typename dest_t, typename F>
using lambda_t [[deprecated("Use 'map_t' in 'tpie/pipelining/map.h'.")]] = map_t<dest_t, F>;

template <typename dest_t, typename F>
class exclude_lambda_t: public node {
public:
typedef typename F::argument_type item_type;

exclude_lambda_t(dest_t dest, const F & f): f(f), dest(std::move(dest)) { }

void push(const item_type & item) {
typename F::result_type t=f(item);
if (t.second) dest.push(t.first);
}
private:
F f;
dest_t dest;
};

using exclude_lambda_t [[deprecated("Use 'filter_map_t' in 'tpie/pipelining/map.h'.")]] = filter_map_t<dest_t, F>;

} // namespace bits

Expand Down Expand Up @@ -176,6 +163,7 @@ inline pipe_middle<tfactory<bits::lambda_t, Args<F>, F> > lambda(const F & f) {
/// \param f The functor that should be applied to items
///////////////////////////////////////////////////////////////////////////////
template <typename F>
[[deprecated("Use 'filter_map' in 'tpie/pipelining/filter_map.h'.")]]
inline pipe_middle<tfactory<bits::exclude_lambda_t, Args<F>, F> > exclude_lambda(const F & f) {
return {f};
}
Expand Down

0 comments on commit 0ef3785

Please sign in to comment.