TPIE

2362a60
uniq.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 2011, 2012, 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_UNIQ_H__
21 #define __TPIE_PIPELINING_UNIQ_H__
22 
23 #include <tpie/pipelining/node.h>
24 #include <tpie/pipelining/pipe_base.h>
25 #include <tpie/pipelining/factory_helpers.h>
26 #include <tpie/file_stream.h>
27 
28 namespace tpie {
29 
30 namespace pipelining {
31 
32 namespace bits {
33 
34 template <typename dest_t>
35 class count_consecutive_t : public node {
36 public:
37  typedef uint64_t count_t;
38  typedef typename push_type<dest_t>::type::first_type item_type;
39 
40  inline count_consecutive_t(dest_t dest)
41  : dest(std::move(dest))
42  , current_count(0)
43  {
44  add_push_destination(this->dest);
45  }
46 
47  virtual void end() override {
48  node::end();
49  flush();
50  }
51 
52  inline void push(const item_type & item) {
53  if (current_count && item == item_buffer) {
54  ++current_count;
55  } else {
56  flush();
57  item_buffer = item;
58  current_count = 1;
59  }
60  }
61 private:
62  inline void flush() {
63  if (!current_count) return;
64  dest.push(std::make_pair(item_buffer, current_count));
65  current_count = 0;
66  }
67  dest_t dest;
68  item_type item_buffer;
69  count_t current_count;
70 };
71 
72 class any_type {
73 public:
74  template <typename T>
75  inline any_type(const T &) {}
76  template <typename T>
77  inline any_type & operator=(const T &) {return *this;}
78 };
79 
80 template <typename dest_t>
81 class extract_first_t : public node {
82 public:
83  typedef std::pair<typename push_type<dest_t>::type, any_type> item_type;
84 
85  inline extract_first_t(dest_t dest) : dest(std::move(dest)) {
86  add_push_destination(this->dest);
87  }
88 
89  inline void push(const item_type & item) {
90  dest.push(item.first);
91  }
92 private:
93  dest_t dest;
94 };
95 
96 } // namespace bits
97 
106 }
107 
108 }
109 
110 }
111 
112 #endif
virtual void end() override
End pipeline processing phase.
Definition: uniq.h:47
Base class of all nodes.
Definition: node.h:78
void add_push_destination(const node_token &dest)
Called by implementers to declare a push destination.
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:152
virtual void end()
End pipeline processing phase.
Definition: node.h:329
pipe_middle< bits::pair_factory< factory< bits::count_consecutive_t >, factory< bits::extract_first_t > > > pipeuniq()
A pipelining node that removes duplicate items and create a phase boundary.
Definition: uniq.h:103
Node factory for variadic argument generators.
A pipe_middle class pushes input down the pipeline.
Definition: pipe_base.h:241