chunkedseq
container library for large in-memory data sets
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_bootchunkedseq.cpp
Go to the documentation of this file.
1 
13 /***********************************************************************/
14 
15 // comment out next line for fewer display
16 #define BOOTCHUNKEDSEQ_CHECK 1
17 
18 // comment out next line for fewer display
19 // #define BOOTCHUNKEDSEQ_PRINT 1
20 
21 
22 /***********************************************************************/
23 
24 #include "itemsearch.hpp"
25 #include "cmdline.hpp"
26 //#include "bootchunkedseq.hpp"
27 #include "bootchunkedseqnew.hpp"
28 #include "test_seq.hpp"
29 
30 using namespace pasl::data;
31 using namespace pasl::data::chunkedseq;
32 
33 
34 /***********************************************************************/
35 // Specification of integer items
36 
37 // Structure storing an integer
38 
39 class IntItem {
40 public:
41  int value;
42  IntItem() : value(0) {}
43  size_t get_cached() const {
44  return 1;
45  }
46 };
47 
48 // Helper functions to allocate, deallocate, and print items
49 
50 class IntItemGenerator {
51 public:
52  static IntItem* from_int(int n) {
53  IntItem* x = new IntItem();
54  x->value = n;
55  return x;
56  }
57 
58  static int to_int(const IntItem*& x) {
59  return x->value;
60  }
61 
62  static int to_int(IntItem*& x) {
63  return x->value;
64  }
65 
66  static void print(const IntItem* x) {
67  printf("%d", IntItemGenerator::to_int(x));
68  }
69 
70  static void free(IntItem*& x) {
71  delete x;
72  }
73 
74 };
75 
76 /***********************************************************************/
77 // Specialization of the sequence to pointers on integer items
78 
79 template<int Chunk_capacity>
80 class IntPointerSeqOf {
81 public:
82  using value_type = IntItem*;
83 
84  template <class Item, class Size>
85  class cache_size {
86  public:
87 
88  using size_type = Size;
89  using value_type = Item;
91  using measured_type = typename algebra_type::value_type;
92 
93  class measure_type {
94  public:
95 
96  measured_type operator()(const value_type& v) const {
97  return v->get_cached();
98  }
99 
100  measured_type operator()(const value_type* lo, const value_type* hi) const {
101  measured_type m = algebra_type::identity();
102  for (auto p = lo; p < hi; p++)
103  m = algebra_type::combine(m, operator()(*p));
104  return m;
105  }
106 
107  };
108 
109  static void swap(measured_type& x, measured_type y) {
110  std::swap(x, y);
111  }
112 
113  };
114 
115 
116  using sized_cache_measure = cache_size<value_type, size_t>;
118  using measure_type = typename sized_cache_measure::measure_type;
119  using measured_type = typename sized_cache_measure::measured_type;
120  using size_type = size_t;
121  using self_type = IntPointerSeqOf<Chunk_capacity>;
122 
123  measure_type meas;
124  seq_type seq;
125 
126 public:
127 
128  inline size_t size() {
129  return seq.get_cached();
130  }
131 
132  inline void push_front(const value_type& x) {
133  seq.push_front(x, meas(x));
134  }
135 
136  inline void push_back(const value_type& x) {
137  seq.push_back(x, meas(x));
138  }
139 
140  inline value_type pop_front() {
141  return seq.pop_front();
142  }
143 
144  inline value_type pop_back() {
145  return seq.pop_back();
146  }
147 
148  void concat(self_type& other) {
149  seq.concat(other.seq);
150  }
151 
152  // Todo: fix!
153  class middle_measured_fields {
154  public:
155  static size_type& size(measured_type& m) {
156  return m;
157  }
158  static size_type csize(measured_type m) {
159  return m;
160  }
161  };
162 
163  /* predicate version
164  void split(size_t index, self_type& other) {
165  using pred_type = itemsearch::less_than_or_eq_by_position<measured_type, size_t, middle_measured_fields>;
166  pred_type p(index);
167  value_type v;
168  seq.split(p, 0, v, other.seq);
169  other.push_front(v);
170  }
171  */
172 
173  void split(size_t index, self_type& other) {
174  seq.split(index, other.seq);
175  /* value_type v;
176  seq.split(index, v, other.seq);
177  other.push_front(v);*/
178  }
179 
180  class CachePrinter {
181  public:
182  static void print(typename seq_type::measured_type& c) {
183  printf("%d", c);
184  }
185  };
186 
187  template <class ItemPrinter>
188  void print() {
189  seq.template print<ItemPrinter>();
190  }
191 
192  void check() {
193  seq.check();
194  }
195 
196 };
197 
198 
199 
200 /***********************************************************************/
201 
202 int main(int argc, char** argv) {
203  pasl::util::cmdline::set(argc, argv);
204 
205  size_t chunk_capacity = (size_t) pasl::util::cmdline::parse_or_default_int("chunk_capacity", 2);
206  if (chunk_capacity == 2)
207  TestSeq<IntPointerSeqOf<2>,IntItem*,IntItemGenerator>::execute_test();
208  else if (chunk_capacity == 4)
209  TestSeq<IntPointerSeqOf<4>,IntItem*,IntItemGenerator>::execute_test();
210  else
211  pasl::util::cmdline::die("unsupported capacity");
212 
213  return 0;
214 }
215 
216 
217 /***********************************************************************/
Unit tests for sequences.
int main(int argc, char **argv)
Routines for finding an item in a container via binary search.
const int chunk_capacity
[weighted_split_example]
bytes_8 Item
Definition: do_fifo.cpp:107