chunkedseq
container library for large in-memory data sets
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
prelims.hpp
Go to the documentation of this file.
1 
13 #include <deque>
14 #include <vector>
15 #include <assert.h>
16 #include <map>
17 
18 #include "atomic.hpp"
19 #include "chunkedseq.hpp"
20 #include "chunkedbag.hpp"
21 #include "trivbootchunkedseq.hpp"
22 #include "container.hpp"
23 #include "map.hpp"
24 
25 #ifndef _PASL_DATA_TEST_PRELIMS_H_
26 #define _PASL_DATA_TEST_PRELIMS_H_
27 
28 namespace pasl {
29 namespace data {
30 
31 extern bool print_chunkedseq_verbose;
32 
33 /*---------------------------------------------------------------------*/
34 /* Container pair */
35 
36 template < class Key,
37 class T,
38 class Compare,
39 class Alloc
40 >
41 std::ostream& operator<<(std::ostream& out, const std::map<Key, T, Compare, Alloc>& xs);
42 
43 template <class Key,
44 class Item,
45 class Compare,
46 class Key_swap,
47 class Alloc>
48 std::ostream& operator<<(std::ostream& out, const map::map<Key, Item, Compare, Key_swap, Alloc>& xs);
49 
50 template <class Item>
51 std::ostream& operator<<(std::ostream& out, const pasl::data::stl::deque_seq<Item>& seq);
52 
53 template <class T>
55 public:
56  static bool same(const T& x, const T& y) {
57  return x == y;
58  }
59 };
60 
61 template <class T>
63 public:
64  static bool same(const T& x, const T& y) {
65  T xx(x);
66  T yy(y);
67  std::sort(xx.begin(), xx.end());
68  std::sort(yy.begin(), yy.end());
69  return xx == yy;
70  }
71 };
72 
73 // to be used to check consistency between two sequence containers
74 template <
75  class Trusted_container, class Untrusted_container,
76  class Untrusted_to_trusted,
77  class Trusted_same=default_container_same<Trusted_container>
78  >
80 public:
81 
82  using trusted_container_type = Trusted_container;
83  using untrusted_container_type = Untrusted_container;
84  using trusted_same = Trusted_same;
85 
88 
90 
92  assert(ok());
93  }
94 
95  container_pair(const self_type& other)
96  : trusted(other.trusted), untrusted(other.untrusted) {
97  if (! ok()) {
98  std::cout << trusted << std::endl;
99  std::cout << Untrusted_to_trusted::conv(untrusted) << std::endl;
100  }
101  assert(ok());
102  }
103 
104  static bool same(const Trusted_container& t, const Untrusted_container& u) {
105  return Trusted_same::same(t, Untrusted_to_trusted::conv(u));
106  }
107 
108  bool ok() const {
109  bool size_same1 = trusted.size() == untrusted.size();
110  bool size_same2 = trusted.size() == Untrusted_to_trusted::conv(untrusted).size();
111  bool same1 = same(trusted, untrusted);
112  untrusted.check();
113  if (! size_same1)
114  return false;
115  if (! size_same2)
116  return false;
117  if (trusted.empty())
118  assert(untrusted.empty());
119  if (untrusted.empty())
120  assert(trusted.empty());
121  return same1;
122  }
123 
124 };
125 
126 /*---------------------------------------------------------------------*/
127 /* Print routines */
128 
129 static inline void print_dashes() {
130  static constexpr int nb_dashes = 30;
131  for (int i = 0; i < nb_dashes; i++)
132  std::cout << "-";
133  std::cout << std::endl;
134 }
135 
136 template <class Item>
137 std::ostream& operator<<(std::ostream& out, const pasl::data::stl::deque_seq<Item>& seq) {
139 }
140 
141 template <class Item>
142 std::ostream& operator<<(std::ostream& out, const std::vector<Item>& seq) {
143  out << "[";
144  size_t sz = seq.size();
145  for (auto it = seq.begin(); it != seq.end(); it++,sz--) {
146  out << *it;
147  if (sz != 1)
148  out << ", ";
149  }
150  out << "]";
151  return out;
152 }
153 
154 template <class Item, int Chunk_capacity>
155 std::ostream& operator<<(std::ostream& out, const chunkedseq::bootchunkedseq::triv<Item, Chunk_capacity>& seq) {
156  return generic_print_container(out, seq);
157 }
158 
159 template < class Key,
160 class T,
161 class Compare,
162 class Alloc
163 >
164 std::ostream& operator<<(std::ostream& out, const std::map<Key, T, Compare, Alloc>& xs) {
165  out << "[";
166  size_t sz = xs.size();
167  for (auto it = xs.begin(); it != xs.end(); it++, sz--) {
168  auto v = *it;
169  out << "(" << v.first << "," << v.second << ")";
170  if (sz != 1)
171  out << ",";
172  }
173  return out << "]";
174 }
175 
176 template <class Key,
177 class Item,
178 class Compare,
179 class Key_swap,
180 class Alloc>
181 std::ostream& operator<<(std::ostream& out, const map::map<Key, Item, Compare, Key_swap, Alloc>& xs) {
182  return xs.stream(out);
183 }
184 
185 template <class T, class U, class C, class S>
186 std::ostream& operator<<(std::ostream& out, const container_pair<T, U, C, S>& cp) {
187  out << std::endl;
188  out << "trusted (sz= " << cp.trusted.size() << "):" << std::endl;
189  out << cp.trusted << std::endl;
190  out << "untrusted (sz= " << cp.untrusted.size() << "):" << std::endl;
191  out << C::conv(cp.untrusted) << std::endl;
192  out << C::conv(cp.untrusted).size() << std::endl;
193 // out << cp.untrusted << std::endl;
194  return out;
195 }
196 
197 template <class T, class U, class C, class S>
198 bool check_and_print_container_pair(const container_pair<T, U, C, S>& cp, std::string msg = "") {
199  if (cp.ok())
200  return true;
201  print_dashes();
202  std::cout << "check on " << msg << " failed with:" << std::endl;
203  std::cout << cp << std::endl;
204  print_dashes();
205  return false;
206 }
207 
208 /*---------------------------------------------------------------------*/
209 /* Forward declarations */
210 
211 template <class T, class U, class C, class S>
212 void generate(size_t& nb, container_pair<T, U, C, S>& dst);
213 
214 } // end namespace
215 } // end namespace
216 
217 #endif
Trivial instantiation of bootstrapped chunked sequence.
Untrusted_container untrusted_container_type
Definition: prelims.hpp:83
static bool same(const Trusted_container &t, const Untrusted_container &u)
Definition: prelims.hpp:104
trusted_container_type trusted
Definition: prelims.hpp:86
container_pair(const self_type &other)
Definition: prelims.hpp:95
Chunked-bag functor.
std::ostream & generic_print_container(std::ostream &out, const Container &seq)
STL-style map data structure.
Trusted_same trusted_same
Definition: prelims.hpp:84
static bool same(const T &x, const T &y)
Definition: prelims.hpp:56
Definition: algebra.hpp:18
untrusted_container_type untrusted
Definition: prelims.hpp:87
Trusted_container trusted_container_type
Definition: prelims.hpp:82
static bool same(const T &x, const T &y)
Definition: prelims.hpp:64
bool check_and_print_container_pair(const container_pair< T, U, C, S > &cp, std::string msg="")
Definition: prelims.hpp:198
bytes_8 Item
Definition: do_fifo.cpp:107
void generate(size_t &nb, container_pair< pasl::data::stl::deque_seq< Item >, U, C, S > &dst)
Definition: generators.hpp:83
Chunked-sequence functor.