chunkedseq
container library for large in-memory data sets
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
map.hpp
Go to the documentation of this file.
1 
13 #include <iostream>
14 #include <fstream>
15 #include <map>
16 
17 #include "atomic.hpp"
18 #include "chunkedseq.hpp"
19 
20 #ifndef _PASL_DATA_MAP_H_
21 #define _PASL_DATA_MAP_H_
22 
23 namespace pasl {
24 namespace data {
25 namespace map {
26 
27 /***********************************************************************/
28 
29 /*---------------------------------------------------------------------*/
31 template <class Item, class Item_swap>
32 class option {
33 public:
34 
36 
38  bool no_item;
39 
41  : item(), no_item(true) { }
42 
43  option(const Item& item)
44  : item(item), no_item(false) { }
45 
46  option(const option& other)
47  : item(other.item), no_item(other.no_item) { }
48 
49  void swap(option& other) {
50  Item_swap::swap(item, other.item);
51  std::swap(no_item, other.no_item);
52  }
53 
54  bool operator<(const self_type& y) const {
55  if (no_item && y.no_item)
56  return false;
57  if (no_item)
58  return true;
59  if (y.no_item)
60  return false;
61  return item < y.item;
62  }
63 
64  bool operator>=(const self_type& y) const {
65  return ! (*this < y);
66  }
67 
68 };
70 
71 /*---------------------------------------------------------------------*/
73 
74 template <class Item, class Measured>
76 public:
77 
78  using value_type = Item;
79  using key_type = typename value_type::first_type;
80  using measured_type = Measured;
81 
83  key_type key = v.first;
84  return measured_type(key);
85  }
86 
87  measured_type operator()(const value_type* lo, const value_type* hi) const {
88  if (hi - lo == 0)
89  return measured_type();
90  const value_type* last = hi - 1;
91  key_type key = last->first;
92  return measured_type(key);
93  }
94 };
96 
97 /*---------------------------------------------------------------------*/
99 template <class Option>
101 public:
102 
103  using value_type = Option;
104 
105  static constexpr bool has_inverse = false;
106 
107  static value_type identity() {
108  return value_type();
109  }
110 
111  static value_type combine(value_type left, value_type right) {
112  if (right.no_item)
113  return left;
114  return right;
115  }
116 
118  util::atomic::fatal([] {
119  std::cout << "inverse operation not supported" << std::endl;
120  });
121  return identity();
122  }
123 
124 };
126 
127 /*---------------------------------------------------------------------*/
128 
130 template <class Item, class Size, class Key_swap>
131 class map_cache {
132 public:
133 
134  using size_type = Size;
135  using value_type = Item;
136  using key_type = typename value_type::first_type;
139  using measured_type = typename algebra_type::value_type; // = option_type
141 
142  static void swap(measured_type& x, measured_type& y) {
143  x.swap(y);
144  }
145 
146 };
148 
149 /*---------------------------------------------------------------------*/
151 template <class Item>
152 class std_swap {
153 public:
154 
155  static void swap(Item& x, Item& y) {
156  std::swap(x, y);
157  }
158 
159 };
161 
162 /*---------------------------------------------------------------------*/
164 template <class Key,
165  class Item,
166  class Compare = std::less<Key>,
167  class Key_swap = std_swap<Key>,
168  class Alloc = std::allocator<std::pair<const Key, Item> >,
169  int chunk_capacity = 8
170  >
171 class map {
172 public:
173 
174  using key_type = Key;
175  using mapped_type = Item;
176  using value_type = std::pair<key_type, mapped_type>;
177  using key_compare = Compare;
178  using allocator_type = Alloc;
180  using const_reference = const value_type&;
181  using pointer = value_type*;
182  using const_pointer = const value_type*;
183  using difference_type = ptrdiff_t;
184  using size_type = size_t;
185  using key_swap_type = Key_swap;
186 
187 private:
188 
191  using option_type = typename cache_type::measured_type;
192 
193 public:
194 
196 
197 private:
198 
199  // invariant: items in seq are sorted in ascending order by their key values
200  mutable container_type seq;
201  mutable iterator it;
202 
203  iterator upper(const key_type& k) const {
204  option_type target(k);
205  it.search_by([target] (const option_type& key) {
206  return target >= key;
207  });
208  return it;
209  }
210 
211 public:
212 
213  map() {
214  it = seq.begin();
215  }
216 
217  map(const map& other)
218  : seq(other.seq) {
219  it = seq.begin();
220  }
221 
222  size_type size() const {
223  return seq.size();
224  }
225 
226  bool empty() const {
227  return size() == 0;
228  }
229 
230  iterator find(const key_type& k) const {
231  iterator it = upper(k);
232  return ((*it).first == k) ? it : seq.end();
233  }
234 
235  mapped_type& operator[](const key_type& k) const {
236  it = upper(k);
237  if (it == seq.end()) {
238  // key k is larger than any key current in seq
239  value_type val;
240  val.first = k;
241  seq.push_back(val);
242  it = seq.end()-1;
243  } else if ((*it).first != k) {
244  // iterator it points to the first key that is less than k
245  value_type val;
246  val.first = k;
247  it = seq.insert(it, val);
248  }
249  return (*it).second;
250  }
251 
252  void erase(iterator it) {
253  if (it == seq.end())
254  return;
255  if (it == seq.end()-1) {
256  seq.pop_front();
257  return;
258  }
259  seq.erase(it, it+1);
260  }
261 
263  size_type nb = seq.size();
264  erase(find(k));
265  return nb - seq.size();
266  }
267 
268  std::ostream& stream(std::ostream& out) const {
269  out << "[";
270  size_type sz = size();
271  seq.for_each([&] (value_type v) {
272  out << "(" << v.first << "," << v.second << ")";
273  if (sz-- != 1)
274  out << ",";
275  });
276  return out << "]";
277  }
278 
279  iterator begin() const {
280  return seq.begin();
281  }
282 
283  iterator end() const {
284  return seq.end();
285  }
286 
287  void check() const {
288  seq.check();
289  }
290 
291 };
293 
294 /***********************************************************************/
295 
296 } // end namespace
297 } // end namespace
298 } // end namespace
299 
300 #endif
option(const option &other)
Definition: map.hpp:46
iterator erase(iterator first, iterator last)
Erases items.
Alloc allocator_type
Definition: map.hpp:178
iterator begin() const
Returns iterator to beginning.
[map_cache]
Definition: map.hpp:152
measured_type operator()(const value_type *lo, const value_type *hi) const
Definition: map.hpp:87
size_t size_type
Definition: map.hpp:184
ptrdiff_t difference_type
Definition: map.hpp:183
Compare key_compare
Definition: map.hpp:177
option(const Item &item)
Definition: map.hpp:43
void push_back(const value_type &x)
Adds item at the end.
bool operator>=(const self_type &y) const
Definition: map.hpp:64
size_type size() const
Returns size.
size_type size() const
Definition: map.hpp:222
const value_type & const_reference
Definition: map.hpp:180
void swap(option &other)
Definition: map.hpp:49
static value_type inverse(value_type x)
Definition: map.hpp:117
iterator begin() const
Definition: map.hpp:279
const value_type * const_pointer
Definition: map.hpp:182
static value_type combine(value_type left, value_type right)
Definition: map.hpp:111
value_type pop_front()
Deletes first item.
iterator find(const key_type &k) const
Definition: map.hpp:230
typename container_type::iterator iterator
Definition: map.hpp:195
bool empty() const
Definition: map.hpp:226
void erase(iterator it)
Definition: map.hpp:252
std::ostream & stream(std::ostream &out) const
Definition: map.hpp:268
void check() const
Definition: map.hpp:287
Definition: algebra.hpp:18
Iterator< self_type, config_type > iterator
map(const map &other)
Definition: map.hpp:217
iterator end() const
Returns iterator to end.
static value_type identity()
Definition: map.hpp:107
static void swap(measured_type &x, measured_type &y)
Definition: map.hpp:142
void for_each(const Body &f) const
Visits every item in the container.
mapped_type & operator[](const key_type &k) const
Definition: map.hpp:235
static constexpr bool has_inverse
Definition: map.hpp:105
value_type * pointer
Definition: map.hpp:181
std::pair< key_type, mapped_type > value_type
Definition: map.hpp:176
[take_right_if_nonempty]
Definition: map.hpp:131
value_type & reference
Definition: map.hpp:179
bool operator<(const self_type &y) const
Definition: map.hpp:54
iterator end() const
Definition: map.hpp:283
typename value_type::first_type key_type
Definition: map.hpp:79
size_type erase(const key_type &k)
Definition: map.hpp:262
iterator insert(iterator position, const value_type &val)
Inserts items.
typename value_type::first_type key_type
Definition: map.hpp:136
const int chunk_capacity
[weighted_split_example]
typename algebra_type::value_type measured_type
Definition: map.hpp:139
static void swap(Item &x, Item &y)
Definition: map.hpp:155
measured_type operator()(const value_type &v) const
Definition: map.hpp:82
Key_swap key_swap_type
Definition: map.hpp:185
bytes_8 Item
Definition: do_fifo.cpp:107
[get_key_of_last_item]
Definition: map.hpp:100
Chunked-sequence functor.