TPIE

11a2c2d
ordered_merge.h
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 2017 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_ORDERED_MERGE_H__
21 #define __TPIE_PIPELINING_ORDERED_MERGE_H__
22 
23 #include <tpie/pipelining/pipe_base.h>
24 #include <tpie/pipelining/factory_helpers.h>
25 #include <tpie/pipelining/node.h>
26 
27 namespace tpie::pipelining {
28 namespace bits {
29 
36 
37 template <typename dest_t, typename fact_t, typename comp_t>
38 class ordered_merge_t : public node {
39 public:
40  typedef typename push_type<dest_t>::type item_type;
41  comp_t comp;
42  typename fact_t::constructed_type with;
43  dest_t dest;
44 
45  ordered_merge_t(dest_t dest, fact_t fact, comp_t comp):
46  comp(comp), with(fact.construct()), dest(std::move(dest)) {
48  add_pull_source(with);
49  }
50 
51  void push(const item_type & item) {
52  while (with.can_pull() && comp(with.peek(), item))
53  dest.push(with.pull());
54  dest.push(item);
55  }
56 
57  void end() override {
58  while (with.can_pull())
59  dest.push(with.pull());
60  }
61 };
62 
63 } // namespace bits
64 
69 template <typename fact_t, typename comp_t=std::less<> >
70 inline pipe_middle<tfactory<bits::ordered_merge_t, Args<fact_t, comp_t>, fact_t, comp_t>>
71  ordered_merge(fact_t fact, comp_t comp=std::less<>()) {
72  return {std::move(fact), std::move(comp)};
73 }
74 
75 } // namespace tpie::pipelining
76 
77 #endif //__TPIE_PIPELINING_ORDERED_MERGE_H__
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::pipelining::bits::ordered_merge_t
Definition: ordered_merge.h:38
tpie::pipelining::node::add_pull_source
void add_pull_source(const node_token &dest)
Called by implementers to declare a pull source.
tpie::pipelining::push_type
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:158
tpie::pipelining::node::add_push_destination
void add_push_destination(const node_token &dest)
Called by implementers to declare a push destination.
tpie::pipelining::bits::ordered_merge_t::end
void end() override
End pipeline processing phase.
Definition: ordered_merge.h:57
tpie::pipelining::node
Base class of all nodes.
Definition: node.h:77
tpie::pipelining::ordered_merge
pipe_middle< tfactory< bits::ordered_merge_t, Args< fact_t, comp_t >, fact_t, comp_t > > ordered_merge(fact_t fact, comp_t comp=std::less<>())
A node that merges a pull pipeline into a push pipeline.
Definition: ordered_merge.h:71