TPIE

2362a60
chunker.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 2015 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_CHUNKER_H__
21 #define __TPIE_PIPELINING_CHUNKER_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/pipelining/node_name.h>
27 
28 namespace tpie {
29 namespace pipelining {
30 namespace bits {
31 
32 template <typename dest_t>
33 class chunker_t: public node {
34 public:
35  typedef typename push_type<dest_t>::type vector_type;
36  typedef typename vector_type::value_type item_type;
37 private:
38  const size_t maxSize;
39  vector_type items;
40  dest_t dest;
41 public:
42  chunker_t(dest_t dest, size_t maxSize)
43  : maxSize(maxSize)
44  , dest(std::move(dest))
45  {
46  set_minimum_memory(sizeof(item_type) * maxSize);
47  set_name("Chunker", PRIORITY_INSIGNIFICANT);
48  }
49 
50  void flush() {
51  dest.push(items);
52  items.clear();
53  }
54 
55  void begin() override {
56  items.reserve(maxSize);
57  }
58 
59  void push(const item_type & item) {
60  if (items.size() == maxSize) flush();
61  items.push_back(item);
62  }
63 
64  void end() override {
65  if (!items.empty()) flush();
66  free_structure_memory(items);
67  }
68 };
69 
70 } //namespace bits
71 
77 
78 } //namespace pipelining
79 } //namespace terrastream
80 
81 #endif //__TPIE_PIPELINING_CHUNKER_H__
Base class of all nodes.
Definition: node.h:78
void end() override
End pipeline processing phase.
Definition: chunker.h:64
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:152
pipe_middle< factory< bits::chunker_t, size_t > > chunker
A pipelining node that gathers elements into a vector of some size.
Definition: chunker.h:76
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
void set_minimum_memory(memory_size_type minimumMemory)
Called by implementers to declare minimum memory requirements.
Definition: node.h:207
A pipe_middle class pushes input down the pipeline.
Definition: pipe_base.h:241
void begin() override
Begin pipeline processing phase.
Definition: chunker.h:55
void free_structure_memory(T &v)
Free the memory assosiated with a stl or tpie structure by swapping it with a default constructed str...
Definition: util.h:179