TPIE

11a2c2d
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::pipelining {
29 namespace bits {
30 
31 template <typename dest_t>
32 class count_consecutive_t : public node {
33 public:
34  typedef uint64_t count_t;
35  typedef typename push_type<dest_t>::type::first_type item_type;
36 
37  count_consecutive_t(dest_t dest)
38  : dest(std::move(dest))
39  , current_count(0)
40  {
41  add_push_destination(this->dest);
42  }
43 
44  void end() override {
45  node::end();
46  flush();
47  }
48 
49  void push(const item_type & item) {
50  if (current_count && item == item_buffer) {
51  ++current_count;
52  } else {
53  flush();
54  item_buffer = item;
55  current_count = 1;
56  }
57  }
58 private:
59  void flush() {
60  if (!current_count) return;
61  dest.push(std::make_pair(item_buffer, current_count));
62  current_count = 0;
63  }
64  dest_t dest;
65  item_type item_buffer;
66  count_t current_count;
67 };
68 
69 class any_type {
70 public:
71  template <typename T>
72  any_type(const T &) {}
73  template <typename T>
74  any_type & operator=(const T &) {return *this;}
75 };
76 
77 template <typename dest_t>
78 class extract_first_t : public node {
79 public:
80  typedef std::pair<typename push_type<dest_t>::type, any_type> item_type;
81 
82  extract_first_t(dest_t dest) : dest(std::move(dest)) {
83  add_push_destination(this->dest);
84  }
85 
86  void push(const item_type & item) {
87  dest.push(item.first);
88  }
89 private:
90  dest_t dest;
91 };
92 
93 } // namespace bits
94 
103 }
104 
105 } //namespace tpie::pipelining
106 
107 #endif //__TPIE_PIPELINING_UNIQ_H__
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::pipelining::factory
Definition: factory_helpers.h:35
tpie::pipelining::bits::extract_first_t
Definition: uniq.h:78
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::pipe_middle
Definition: pipe_base.h:243
tpie::pipelining::bits::count_consecutive_t
Definition: uniq.h:32
tpie::pipelining::bits::any_type
Definition: uniq.h:69
tpie::pipelining::pipeuniq
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:100
tpie::pipelining::node::end
virtual void end()
End pipeline processing phase.
Definition: node.h:328
tpie::pipelining::node
Base class of all nodes.
Definition: node.h:77
tpie::pipelining::bits::count_consecutive_t::end
void end() override
End pipeline processing phase.
Definition: uniq.h:44
tpie::pipelining::bits::pair_factory
Definition: pair_factory.h:147
tpie::pipelining::item_type
pipe_middle< tfactory< bits::item_type_t, Args< T > > > item_type()
Create item type defining identity pipe node.
Definition: helpers.h:654