Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #196 : Pipelining duplication lambda_t and map_t #258

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions tpie/pipelining.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include <tpie/pipelining/stdio.h>
#include <tpie/pipelining/uniq.h>
#include <tpie/pipelining/parallel.h>

// Pipelining nodes
#include <tpie/pipelining/map.h>

#endif
10 changes: 5 additions & 5 deletions tpie/pipelining/filter.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// -*- 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 2015 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/>

Expand Down Expand Up @@ -41,7 +41,7 @@ class filter_t: public node {
functor(functor), dest(std::move(dest)) {
set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
}

void push(const item_type & item) {
if (functor(item))
dest.push(item);
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);
SSoelvsten marked this conversation as resolved.
Show resolved Hide resolved
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__
23 changes: 12 additions & 11 deletions tpie/pipelining/map.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// -*- 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 2011, 2012, 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/>

Expand Down Expand Up @@ -58,14 +58,15 @@ class map_t: public node {
private:
F functor;
dest_t dest;

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

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) {
dest.push(functor(item));
}
Expand All @@ -76,12 +77,13 @@ class map_temp_t: public node {
private:
F functor;
dest_t dest;

public:
map_temp_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);
}

template <typename T>
void push(const T & item) {
dest.push(functor(item));
Expand All @@ -99,7 +101,7 @@ class map_sink_t: public node {
functor(functor) {
set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
}

void push(const item_type & item) {
functor(item);
}
Expand All @@ -115,17 +117,16 @@ class pull_map_t: public node {
functor(functor), src(std::move(src)) {
set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
}

auto pull() {
return functor(src.pull());
}

bool can_pull() {
return src.can_pull();
}
};


template <typename T>
struct has_argument_type {
typedef char yes[1];
Expand Down
53 changes: 14 additions & 39 deletions tpie/pipelining/std_glue.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// -*- 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 2011, 2012, 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/>

Expand All @@ -25,6 +25,8 @@
#include <tpie/pipelining/node.h>
#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 @@ -60,7 +62,7 @@ template <typename T, typename A>
class pull_input_vector_t : public node {
public:
typedef T item_type;

pull_input_vector_t(const std::vector<T, A> & input) : input(input) {}

pull_input_vector_t(std::vector<T, A> && input) = delete;
Expand All @@ -73,7 +75,7 @@ class pull_input_vector_t : public node {
bool can_pull() const {return idx < input.size();}
const T & peek() const {return input[idx];}
const T & pull() {return input[idx++];}

private:
size_t idx;
const std::vector<T, A> & input;
Expand All @@ -96,40 +98,11 @@ class output_vector_t : public node {
std::vector<item_type, A> & output;
};


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

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

void push(const item_type & item) {
dest.push(f(item));
}
private:
F f;
dest_t dest;
};
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 @@ -175,6 +148,7 @@ pipe_end<termfactory<bits::output_vector_t<T, A>, std::vector<T, A> &> > output_
/// \param f The functor that should be applied to items
///////////////////////////////////////////////////////////////////////////////
template <typename F>
[[deprecated("Use 'map' in 'tpie/pipelining/map.h'.")]]
inline pipe_middle<tfactory<bits::lambda_t, Args<F>, F> > lambda(const F & f) {
return {f};
}
Expand All @@ -183,12 +157,13 @@ inline pipe_middle<tfactory<bits::lambda_t, Args<F>, F> > lambda(const F & f) {
/// \brief Pipelining nodes that applies to given functor to items in
/// the stream. The functor should have a typedef named argument_type
/// that is the type of the argument given to the call operator. It is required
/// that the functor returns a pair. The first item should be a boolean
/// indicating whether the item should be pushed to the next node. The second
/// that the functor returns a pair. The second item should be a boolean
/// indicating whether the item should be pushed to the next node. The first
/// should be the value itself.
/// \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