TPIE

11a2c2d
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::pipelining {
29 namespace bits {
30 
31 template <typename dest_t>
32 class chunker_t: public node {
33 public:
34  typedef typename push_type<dest_t>::type vector_type;
35  typedef typename vector_type::value_type item_type;
36 private:
37  const size_t maxSize;
38  vector_type items;
39  dest_t dest;
40 public:
41  chunker_t(dest_t dest, size_t maxSize)
42  : maxSize(maxSize)
43  , dest(std::move(dest))
44  {
45  set_minimum_memory(sizeof(item_type) * maxSize);
46  set_name("Chunker", PRIORITY_INSIGNIFICANT);
47  }
48 
49  void flush() {
50  dest.push(items);
51  items.clear();
52  }
53 
54  void begin() override {
55  items.reserve(maxSize);
56  }
57 
58  void push(const item_type & item) {
59  if (items.size() == maxSize) flush();
60  items.push_back(item);
61  }
62 
63  void end() override {
64  if (!items.empty()) flush();
65  free_structure_memory(items);
66  }
67 };
68 
69 } //namespace bits
70 
76 
77 } //namespace terrastream::pipelining
78 
79 #endif //__TPIE_PIPELINING_CHUNKER_H__
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::pipelining::bits::chunker_t::begin
void begin() override
Begin pipeline processing phase.
Definition: chunker.h:54
tpie::pipelining::bits::chunker_t
Definition: chunker.h:32
tpie::free_structure_memory
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:217
tpie::pipelining::node::set_minimum_memory
void set_minimum_memory(memory_size_type minimumMemory)
Called by implementers to declare minimum memory requirements.
Definition: node.h:206
tpie::pipelining::push_type
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:158
tpie::pipelining::node::set_name
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
tpie::pipelining::pipe_middle
Definition: pipe_base.h:243
tpie::pipelining::bits::chunker_t::end
void end() override
End pipeline processing phase.
Definition: chunker.h:63
tpie::pipelining::node
Base class of all nodes.
Definition: node.h:77
tpie::pipelining::chunker
pipe_middle< factory< bits::chunker_t, size_t > > chunker
A pipelining node that gathers elements into a vector of some size.
Definition: chunker.h:75