chunkedseq
container library for large in-memory data sets
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
chunkedseq_7.cpp
Go to the documentation of this file.
1 
16 #include <iostream>
18 #include <string>
19 #include <assert.h>
20 
21 #include "chunkedseq.hpp"
22 
23 template <class Chunkedseq, class UnaryPredicate>
24 void pcopy_if(typename Chunkedseq::iterator first,
25  typename Chunkedseq::iterator last,
26  Chunkedseq& destination,
27  const UnaryPredicate& pred) {
28  using iterator = typename Chunkedseq::iterator;
29  using value_type = typename Chunkedseq::value_type;
30  using ptr = typename Chunkedseq::const_pointer;
31 
32  const long cutoff = 8192;
33 
34  long sz = last.size() - first.size();
35 
36  if (sz <= cutoff) {
37 
38  // compute result in a sequential fashion
39  Chunkedseq::for_each_segment(first, last, [&] (ptr lo, ptr hi) {
40  for (ptr p = lo; p < hi; p++) {
41  value_type v = *p;
42  if (pred(v))
43  destination.push_back(v);
44  }
45  });
46 
47  } else {
48 
49  // select split position to be the median
50  iterator mid = first + (sz/2);
51 
52  Chunkedseq destination2;
53 
54  // recurse on subproblems
55  // calls can execute in parallel
56  pcopy_if(first, mid, destination, pred);
57  pcopy_if(mid, last, destination2, pred);
58 
59  destination.concat(destination2);
60  }
61 }
62 
63 int main(int argc, const char * argv[]) {
64 
65  const int chunk_size = 2;
67 
68  mydeque_type mydeque = { 0, 1, 2, 3, 4, 5 };
69  mydeque_type mydeque2;
70 
71  pcopy_if(mydeque.begin(), mydeque.end(), mydeque2, [] (int i) { return i%2==0; });
72 
73  std::cout << "mydeque2 contains:";
74  auto p = mydeque2.begin();
75  while (p != mydeque2.end())
76  std::cout << " " << *p++;
77  std::cout << std::endl;
78 
79  return 0;
80 
81 }
82 //! [pcopy_if_example]
iterator begin() const
Returns iterator to beginning.
void pcopy_if(typename Chunkedseq::iterator first, typename Chunkedseq::iterator last, Chunkedseq &destination, const UnaryPredicate &pred)
[pcopy_if_example]
void for_each_segment(iterator begin, iterator end, const Body &f)
iterator end() const
Returns iterator to end.
int main(int argc, const char *argv[])
Chunked-sequence functor.