source: mainline/uspace/lib/cpp/include/impl/unordered_map.hpp@ 86b3ae98

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86b3ae98 was 170761c, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added partial definition of unordered_map - that is, all function declaration and implementation of some that consisted only of relying the arguments into the underlying hash table

  • Property mode set to 100644
File size: 17.9 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_UNORDERED_MAP
30#define LIBCPP_UNORDERED_MAP
31
32#include <initializer_list>
33#include <internal/hash_map.hpp>
34#include <functional>
35#include <memory>
36#include <utility>
37
38namespace std
39{
40 /**
41 * 23.5.4, class template unordered_map:
42 */
43
44 template<
45 class Key, class Value
46 class Hash = hash<Key>,
47 class Pred = equal_to<Key>,
48 class Alloc = allocator<pair<const Key, Value>>
49 >
50 class unordered_map
51 {
52 public:
53 using key_type = Key;
54 using mapped_type = Value;
55 using value_type = pair<const key_type, mapped_type>;
56 using hasher = Hash;
57 using key_equal = Pred;
58 using allocator_type = Alloc;
59 using pointer = typename allocator_traits<allocator_type>::pointer;
60 using const_pointer = typename allocator_traits<allocator_type>::const_pointer;
61 using reference = value_type&;
62 using const_reference = const value_type&;
63 using size_type = size_t;
64 using difference_type = ptrdiff_t;
65
66 using iterator = aux::hash_table_iterator<
67 value_type, reference, pointer, size_type
68 >;
69 using const_iterator = aux::hash_table_const_iterator<
70 value_type, const_reference, const_pointer, size_type
71 >;
72 using local_iterator = aux::hash_table_local_iterator<
73 value_type, reference, pointer
74 >;
75 using const_local_iterator = aux::hash_table_const_local_iterator<
76 value_type, const_reference, const_pointer
77 >;
78
79 unordered_map()
80 : unordered_map{default_bucket_count_}
81 { /* DUMMY BODY */ }
82
83 explicit unordered_map(size_type bucket_count = default_bucket_count_,
84 const hasher& hf = hasher{},
85 const key_equal& eql = key_equal{},
86 const allocator_type& alloc = allocator_type{})
87 : table_{bucket_count, hf, eql}, allocator_{alloc}
88 { /* DUMMY BODY */ }
89
90 template<class InputIterator>
91 unordered_map(InputIterator first, InputIterator last,
92 size_type bucket_count = default_bucket_count_,
93 const hasher& hf = hasher{},
94 const key_equal& eql = key_equal{},
95 const allocator_type& alloc = allocator_type{})
96 : unordered_map{bucket_count, hf, eql, alloc}
97 {
98 // TODO: insert the range
99 }
100
101 unordered_map(const unordered_map& other)
102 : unordered_map{other, other.allocator_}
103 { /* DUMMY BODY */ }
104
105 unordered_map(unordered_map&& other)
106 : table_{move(other.table_)}, allocator_{move(other.allocator_)}
107 { /* DUMMY BODY */ }
108
109 explicit unordered_map(const allocator_type& alloc)
110 : table_{default_bucket_count_}, allocator_{alloc}
111 { /* DUMMY BODY */ }
112
113 unordered_map(const unordered_map& other, const allocator_type& alloc)
114 : table_{other.table_}, allocator_{alloc}
115 { /* DUMMY BODY */ }
116
117 unordered_map(unordered_map&& other, const allocator_type& alloc)
118 : table_{move(other.table_)}, allocator_{alloc}
119 { /* DUMMY BODY */ }
120
121 unordered_map(initializer_list<value_type> init,
122 size_type bucket_count = default_bucket_count_,
123 const hasher& hf = hasher{},
124 const key_equal& eql = key_equal{},
125 const allocator_type& alloc = allocator_type{})
126 : unordered_map{bucket_count, hf, eql, alloc}
127 {
128 // TODO: insert the range
129 }
130
131 unordered_map(size_type bucket_count, const allocator_type& alloc)
132 : unordered_map{bucket_count, hasher{}, key_equal{}, alloc}
133 { /* DUMMY BODY */ }
134
135 unordered_map(size_type bucket_count, const hasher& hf, const allocator_type& alloc)
136 : unordered_map{bucket_count, hf, key_equal{}, alloc}
137 { /* DUMMY BODY */ }
138
139 template<class InputIterator>
140 unordered_map(InputIterator first, InputIterator last,
141 size_type bucket_count, const allocator_type& alloc)
142 : unordered_map{first, last, bucket_count, hasher{}, key_equal{}, alloc}
143 { /* DUMMY BODY */ }
144
145 template<class InputIterator>
146 unordered_map(InputIterator first, InputIterator last,
147 size_type bucket_count, const hasher& hf, const allocator_type& alloc)
148 : unordered_map{first, last, bucket_count, hf, key_equal{}, alloc}
149 { /* DUMMY BODY */ }
150
151 unordered_map(initializer_list<value_type> init, size_type bucket_count,
152 const allocator_type& alloc)
153 : unordered_map{init, bucket_count, hasher{}, key_equal{}, alloc}
154 { /* DUMMY BODY */ }
155
156 unordered_map(initializer_list<value_type> init, size_type bucket_count,
157 const hasher& hf, const allocator_type& alloc)
158 : unordered_map{init, bucket_count, hf, key_equal{}, alloc}
159 { /* DUMMY BODY */ }
160
161 ~unordered_map()
162 { /* DUMMY BODY */ }
163
164 unordered_map& operator=(const unordered_map& other)
165 {
166 // TODO: implement
167 return *this;
168 }
169
170 unordered_map& operator=(unordered_map&& other)
171 noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
172 is_nothrow_move_assignable<hasher>::value &&
173 is_nothrow_move_assignable<key_equal>::value)
174 {
175 // TODO: implement
176 return *this;
177 }
178
179 unordered_map& operator=(initializer_list<value_type>& init)
180 {
181 // TODO: implement
182 return *this;
183 }
184
185 allocator_type get_allocator() const noexcept
186 {
187 return allocator_;
188 }
189
190 bool empty() const noexcept
191 {
192 return table_.empty();
193 }
194
195 size_type size() const noexcept
196 {
197 return table_.size();
198 }
199
200 size_type max_size() const noexcept
201 {
202 return table_.max_size();
203 }
204
205 iterator begin() noexcept
206 {
207 return table_.begin();
208 }
209
210 const_iterator begin() const noexcept
211 {
212 return table_.begin();
213 }
214
215 iterator end() noexcept
216 {
217 return table_.end();
218 }
219
220 const_iterator end() const noexcept
221 {
222 return table_.end();
223 }
224
225 const_iterator cbegin() const noexcept
226 {
227 return table_.cbegin();
228 }
229
230 const_iterator cend() const noexcept
231 {
232 return table_.cend();
233 }
234
235 template<class... Args>
236 pair<iterator, bool> emplace(Args&&... args)
237 {
238 // TODO: implement
239 }
240
241 template<class... Args>
242 iterator emplace_hint(const_iterator position, Args&&... args)
243 {
244 // TODO: implement
245 }
246
247 pair<iterator, bool> insert(const value_type& val)
248 {
249 // TODO: implement
250 }
251
252 pair<iterator, bool> insert(value_type&& val)
253 {
254 // TODO: implement
255 }
256
257 template<class T>
258 pair<iterator, bool> insert(T&& val)
259 {
260 // TODO: implement
261 }
262
263 iterator insert(const_iterator hint, const value_type& val)
264 {
265 // TODO: implement
266 }
267
268 iterator insert(const_iterator hint, value_type&& val)
269 {
270 // TODO: implement
271 }
272
273 template<class T>
274 iterator insert(const_iterator hint, T&& val)
275 {
276 // TODO: implement
277 }
278
279 template<class InputIterator>
280 void insert(InputIterator first, InputIterator last)
281 {
282 // TODO: implement
283 }
284
285 void insert(initializer_list<value_type> init)
286 {
287 insert(init.begin(), init.end());
288 }
289
290 template<class... Args>
291 pair<iterator, bool> try_emplace(const key_type& key, Args&&... args)
292 {
293 // TODO: implement
294 }
295
296 template<class... Args>
297 pair<iterator, bool> try_emplace(key_type&& key, Args&&... args)
298 {
299 // TODO: implement
300 }
301
302 template<class... Args>
303 iterator try_emplace(const_iterator hint, const key_type& key, Args&&... args)
304 {
305 // TODO: implement
306 }
307
308 template<class... Args>
309 iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args)
310 {
311 // TODO: implement
312 }
313
314 template<class T>
315 pair<iterator, bool> insert_or_assign(const key_type& key, T&& val)
316 {
317 // TODO: implement
318 }
319
320 template<class T>
321 pair<iterator, bool> insert_or_assign(key_type&& key, T&& val)
322 {
323 // TODO: implement
324 }
325
326 template<class T>
327 iterator insert_or_assign(const_iterator hint, const key_type& key, T&& val)
328 {
329 // TODO: implement
330 }
331
332 template<class T>
333 iterator insert_or_assign(const_iterator hint, key_type&& key, T&& val)
334 {
335 // TODO: implement
336 }
337
338 iterator erase(const_iterator position)
339 {
340 // TODO: implement
341 }
342
343 size_type erase(const key_type& key)
344 {
345 // TODO: implement
346 }
347
348 iterator erase(const_iterator first, const_iterator last)
349 {
350 // TODO: implement
351 }
352
353 void clear() noexcept
354 {
355 table_.clear();
356 }
357
358 void swap(unordered_map& other)
359 noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
360 noexcept(std::swap(declval<hasher>(), declval<hasher>())) &&
361 noexcept(std::swap(declval<key_equal>(), declval<key_equal>())))
362 {
363 table_.swap(other.table_);
364 std::swap(allocator_, other.allocator_);
365 }
366
367 hasher hash_function() const
368 {
369 return table_.hash_function();
370 }
371
372 key_equal key_eq() const
373 {
374 return table_.key_eq();
375 }
376
377 iterator find(const key_type& key)
378 {
379 // TODO: implement
380 }
381
382 const_iterator find(const key_type& key) const
383 {
384 // TODO: implement
385 }
386
387 size_type count(const key_type& key) const
388 {
389 // TODO: implement
390 }
391
392 pair<iterator, iterator> equal_range(const key_type& key)
393 {
394 // TODO: implement
395 }
396
397 pair<const_iterator, const_iterator> equal_range(const key_type& key) const
398 {
399 // TODO: implement
400 }
401
402 mapped_type& operator[](const key_type& key)
403 {
404 // TODO: implement
405 }
406
407 mapped_type& operator[](key_type&& key)
408 {
409 // TODO: implement
410 }
411
412 mapped_type& at(const key_type& key)
413 {
414 // TODO: implement
415 }
416
417 const mapped_type& at(const key_type& key) const
418 {
419 // TODO: implement
420 }
421
422 size_type bucket_count() const noexcept
423 {
424 return table_.bucket_count();
425 }
426
427 size_type max_bucket_count() const noexcept
428 {
429 return table_.max_bucket_count();
430 }
431
432 size_type bucket_size(size_type idx) const
433 {
434 return table_.bucket_size(idx);
435 }
436
437 size_type bucket(const key_type& key) const
438 {
439 return table_.bucket(key);
440 }
441
442 local_iterator begin(size_type idx)
443 {
444 return table_.begin(idx);
445 }
446
447 const_local_iterator begin(size_type idx) const
448 {
449 return table_.begin(idx);
450 }
451
452 local_iterator end(size_type idx)
453 {
454 return table_.end(idx);
455 }
456
457 const_local_iterator end(size_type idx) const
458 {
459 return table_.end(idx);
460 }
461
462 const_local_iterator cbegin(size_type idx) const
463 {
464 return table_.cbegin(idx);
465 }
466
467 const_local_iterator cend(size_type idx) const
468 {
469 return table_.cend(idx);
470 }
471
472 float load_factor() const noexcept
473 {
474 return table_.load_factor();
475 }
476
477 float max_load_factor() const noexcept
478 {
479 return table_.max_load_factor();
480 }
481
482 void max_load_factor(float factor)
483 {
484 table_.max_load_factor(factor);
485 }
486
487 void rehash(size_type bucket_count)
488 {
489 table_.rehash(bucket_count);
490 }
491
492 void reserve(size_type count)
493 {
494 table_.reserve(count);
495 }
496
497 private:
498 aux::hash_map<
499 value_type, key_type, aux::key_value_key_extractor<key_type, mapped_type>,
500 hasher, key_equal, allocator_type, size_type,
501 iterator, const_iterator, local_iterator, const_local_iterator,
502 aux::hash_single_policy
503 > table_;
504
505 allocator_type allocator_;
506
507 static constexpr size_type default_bucket_count_{16};
508 };
509
510 /**
511 * 23.5.5, class template unordered_multimap:
512 */
513
514 template<
515 class Key, class Value
516 class Hash = hash<Key>,
517 class Pred = equal_to<Key>,
518 class Alloc = allocator<pair<const Key, Value>>
519 >
520 class unordered_multimap;
521
522 template<class Key, class Value, class Hash, class Pred, class Alloc>
523 void swap(unordered_map<Key, Value, Hash, Pred, Alloc>& lhs,
524 unordered_map<Key, Value, Hash, Pred, Alloc>& rhs)
525 noexcept(noexcept(lhs.swap(rhs)))
526 {
527 lhs.swap(rhs);
528 }
529
530 template<class Key, class Value, class Hash, class Pred, class Alloc>
531 void swap(unordered_multimap<Key, Value, Hash, Pred, Alloc>& lhs,
532 unordered_multimap<Key, Value, Hash, Pred, Alloc>& rhs)
533 noexcept(noexcept(lhs.swap(rhs)))
534 {
535 lhs.swap(rhs);
536 }
537
538 template<class Key, class Value, class Hash, class Pred, class Alloc>
539 bool operator==(unordered_map<Key, Value, Hash, Pred, Alloc>& lhs,
540 unordered_map<Key, Value, Hash, Pred, Alloc>& rhs)
541 {
542 // TODO: implement
543 return false;
544 }
545
546 template<class Key, class Value, class Hash, class Pred, class Alloc>
547 bool operator!=(unordered_map<Key, Value, Hash, Pred, Alloc>& lhs,
548 unordered_map<Key, Value, Hash, Pred, Alloc>& rhs)
549 {
550 return !(lhs == rhs);
551 }
552
553 template<class Key, class Value, class Hash, class Pred, class Alloc>
554 bool operator==(unordered_multimap<Key, Value, Hash, Pred, Alloc>& lhs,
555 unordered_multimap<Key, Value, Hash, Pred, Alloc>& rhs)
556 {
557 // TODO: implement
558 return false;
559 }
560
561 template<class Key, class Value, class Hash, class Pred, class Alloc>
562 bool operator!=(unordered_multimap<Key, Value, Hash, Pred, Alloc>& lhs,
563 unordered_multimap<Key, Value, Hash, Pred, Alloc>& rhs)
564 {
565 return !(lhs == rhs);
566 }
567}
568
569#endif
Note: See TracBrowser for help on using the repository browser.