]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp
24a9f1bf3ba2260b2e4b9f118068b3244ca71535
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / gp_hash_table_map_ / gp_ht_map_.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
9 // version.
10
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
41
42 /**
43 * @file gp_ht_map_.hpp
44 * Contains an implementation class for gp_ht_map_.
45 */
46
47 #include <ext/pb_ds/tag_and_trait.hpp>
48 #include <ext/pb_ds/detail/hash_fn/ranged_probe_fn.hpp>
49 #include <ext/pb_ds/detail/types_traits.hpp>
50 #include <ext/pb_ds/exception.hpp>
51 #include <ext/pb_ds/detail/eq_fn/hash_eq_fn.hpp>
52 #include <utility>
53 #ifdef PB_DS_HT_MAP_TRACE_
54 #include <iostream>
55 #endif
56 #ifdef _GLIBCXX_DEBUG
57 #include <ext/pb_ds/detail/map_debug_base.hpp>
58 #endif
59 #include <debug/debug.h>
60
61 namespace pb_ds
62 {
63 namespace detail
64 {
65 #define PB_DS_CLASS_T_DEC \
66 template<typename Key, typename Mapped, typename Hash_Fn, typename Eq_Fn, \
67 typename Allocator, bool Store_Hash, typename Comb_Probe_Fn, \
68 typename Probe_Fn, typename Resize_Policy>
69
70 #ifdef PB_DS_DATA_TRUE_INDICATOR
71 #define PB_DS_CLASS_NAME gp_ht_map_data_
72 #endif
73
74 #ifdef PB_DS_DATA_FALSE_INDICATOR
75 #define PB_DS_CLASS_NAME gp_ht_map_no_data_
76 #endif
77
78 #define PB_DS_CLASS_C_DEC \
79 PB_DS_CLASS_NAME<Key, Mapped, Hash_Fn, Eq_Fn, Allocator, \
80 Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>
81
82 #define PB_DS_HASH_EQ_FN_C_DEC \
83 hash_eq_fn<Key, Eq_Fn, Allocator, Store_Hash>
84
85 #define PB_DS_RANGED_PROBE_FN_C_DEC \
86 ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, Store_Hash>
87
88 #define PB_DS_TYPES_TRAITS_C_DEC \
89 types_traits<Key, Mapped, Allocator, Store_Hash>
90
91 #ifdef _GLIBCXX_DEBUG
92 #define PB_DS_MAP_DEBUG_BASE_C_DEC \
93 map_debug_base<Key, Eq_Fn, typename Allocator::template rebind<Key>::other::const_reference>
94 #endif
95
96 #ifdef PB_DS_DATA_TRUE_INDICATOR
97 #define PB_DS_V2F(X) (X).first
98 #define PB_DS_V2S(X) (X).second
99 #endif
100
101 #ifdef PB_DS_DATA_FALSE_INDICATOR
102 #define PB_DS_V2F(X) (X)
103 #define PB_DS_V2S(X) Mapped()
104 #endif
105
106 template<typename Key,
107 typename Mapped,
108 typename Hash_Fn,
109 typename Eq_Fn,
110 typename Allocator,
111 bool Store_Hash,
112 typename Comb_Probe_Fn,
113 typename Probe_Fn,
114 typename Resize_Policy>
115 class PB_DS_CLASS_NAME :
116 #ifdef _GLIBCXX_DEBUG
117 protected PB_DS_MAP_DEBUG_BASE_C_DEC,
118 #endif
119 public PB_DS_HASH_EQ_FN_C_DEC,
120 public Resize_Policy,
121 public PB_DS_RANGED_PROBE_FN_C_DEC,
122 public PB_DS_TYPES_TRAITS_C_DEC
123 {
124 private:
125 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
126 typedef typename traits_base::value_type value_type_;
127 typedef typename traits_base::pointer pointer_;
128 typedef typename traits_base::const_pointer const_pointer_;
129 typedef typename traits_base::reference reference_;
130 typedef typename traits_base::const_reference const_reference_;
131 typedef typename traits_base::comp_hash comp_hash;
132
133 enum entry_status
134 {
135 empty_entry_status,
136 valid_entry_status,
137 erased_entry_status
138 } __attribute__ ((packed));
139
140 struct entry : public traits_base::stored_value_type
141 {
142 entry_status m_stat;
143 };
144
145 typedef typename Allocator::template rebind<entry>::other entry_allocator;
146 typedef typename entry_allocator::pointer entry_pointer;
147 typedef typename entry_allocator::const_pointer const_entry_pointer;
148 typedef typename entry_allocator::reference entry_reference;
149 typedef typename entry_allocator::const_reference const_entry_reference;
150 typedef typename entry_allocator::pointer entry_array;
151
152 typedef PB_DS_RANGED_PROBE_FN_C_DEC ranged_probe_fn_base;
153
154 #ifdef _GLIBCXX_DEBUG
155 typedef PB_DS_MAP_DEBUG_BASE_C_DEC map_debug_base;
156 #endif
157
158 typedef PB_DS_HASH_EQ_FN_C_DEC hash_eq_fn_base;
159 typedef Resize_Policy resize_base;
160
161 #define PB_DS_GEN_POS typename Allocator::size_type
162
163 #include <ext/pb_ds/detail/unordered_iterator/const_point_iterator.hpp>
164 #include <ext/pb_ds/detail/unordered_iterator/point_iterator.hpp>
165 #include <ext/pb_ds/detail/unordered_iterator/const_iterator.hpp>
166 #include <ext/pb_ds/detail/unordered_iterator/iterator.hpp>
167
168 #undef PB_DS_GEN_POS
169
170 public:
171 typedef Allocator allocator;
172 typedef typename Allocator::size_type size_type;
173 typedef typename Allocator::difference_type difference_type;
174 typedef Hash_Fn hash_fn;
175 typedef Eq_Fn eq_fn;
176 typedef Probe_Fn probe_fn;
177 typedef Comb_Probe_Fn comb_probe_fn;
178 typedef Resize_Policy resize_policy;
179
180 enum
181 {
182 store_hash = Store_Hash
183 };
184
185 typedef typename traits_base::key_type key_type;
186 typedef typename traits_base::key_pointer key_pointer;
187 typedef typename traits_base::const_key_pointer const_key_pointer;
188 typedef typename traits_base::key_reference key_reference;
189 typedef typename traits_base::const_key_reference const_key_reference;
190 typedef typename traits_base::mapped_type mapped_type;
191 typedef typename traits_base::mapped_pointer mapped_pointer;
192 typedef typename traits_base::const_mapped_pointer const_mapped_pointer;
193 typedef typename traits_base::mapped_reference mapped_reference;
194 typedef typename traits_base::const_mapped_reference const_mapped_reference;
195 typedef typename traits_base::value_type value_type;
196 typedef typename traits_base::pointer pointer;
197 typedef typename traits_base::const_pointer const_pointer;
198 typedef typename traits_base::reference reference;
199 typedef typename traits_base::const_reference const_reference;
200
201 #ifdef PB_DS_DATA_TRUE_INDICATOR
202 typedef point_iterator_ point_iterator;
203 #endif
204
205 #ifdef PB_DS_DATA_FALSE_INDICATOR
206 typedef const_point_iterator_ point_iterator;
207 #endif
208
209 typedef const_point_iterator_ const_point_iterator;
210
211 #ifdef PB_DS_DATA_TRUE_INDICATOR
212 typedef iterator_ iterator;
213 #endif
214
215 #ifdef PB_DS_DATA_FALSE_INDICATOR
216 typedef const_iterator_ iterator;
217 #endif
218
219 typedef const_iterator_ const_iterator;
220
221 PB_DS_CLASS_NAME();
222
223 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
224
225 PB_DS_CLASS_NAME(const Hash_Fn&);
226
227 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&);
228
229 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&);
230
231 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
232 const Probe_Fn&);
233
234 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
235 const Probe_Fn&, const Resize_Policy&);
236
237 template<typename It>
238 void
239 copy_from_range(It first_it, It last_it);
240
241 virtual
242 ~PB_DS_CLASS_NAME();
243
244 void
245 swap(PB_DS_CLASS_C_DEC& other);
246
247 inline size_type
248 size() const;
249
250 inline size_type
251 max_size() const;
252
253 inline bool
254 empty() const;
255
256 Hash_Fn&
257 get_hash_fn();
258
259 const Hash_Fn&
260 get_hash_fn() const;
261
262 Eq_Fn&
263 get_eq_fn();
264
265 const Eq_Fn&
266 get_eq_fn() const;
267
268 Probe_Fn&
269 get_probe_fn();
270
271 const Probe_Fn&
272 get_probe_fn() const;
273
274 Comb_Probe_Fn&
275 get_comb_probe_fn();
276
277 const Comb_Probe_Fn&
278 get_comb_probe_fn() const;
279
280 Resize_Policy&
281 get_resize_policy();
282
283 const Resize_Policy&
284 get_resize_policy() const;
285
286 inline std::pair<point_iterator, bool>
287 insert(const_reference r_val)
288 {
289 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
290 return insert_imp(r_val, traits_base::m_store_extra_indicator);
291 }
292
293 inline mapped_reference
294 operator[](const_key_reference r_key)
295 {
296 #ifdef PB_DS_DATA_TRUE_INDICATOR
297 return subscript_imp(r_key, traits_base::m_store_extra_indicator);
298 #else
299 insert(r_key);
300 return traits_base::s_null_mapped;
301 #endif
302 }
303
304 inline point_iterator
305 find(const_key_reference r_key);
306
307 inline const_point_iterator
308 find(const_key_reference r_key) const;
309
310 inline point_iterator
311 find_end();
312
313 inline const_point_iterator
314 find_end() const;
315
316 inline bool
317 erase(const_key_reference r_key);
318
319 template<typename Pred>
320 inline size_type
321 erase_if(Pred prd);
322
323 void
324 clear();
325
326 inline iterator
327 begin();
328
329 inline const_iterator
330 begin() const;
331
332 inline iterator
333 end();
334
335 inline const_iterator
336 end() const;
337
338 #ifdef _GLIBCXX_DEBUG
339 void
340 assert_valid() const;
341 #endif
342
343 #ifdef PB_DS_HT_MAP_TRACE_
344 void
345 trace() const;
346 #endif
347
348 private:
349 #ifdef PB_DS_DATA_TRUE_INDICATOR
350 friend class iterator_;
351 #endif
352
353 friend class const_iterator_;
354
355 void
356 deallocate_all();
357
358 void
359 initialize();
360
361 void
362 erase_all_valid_entries(entry_array, size_type);
363
364 inline bool
365 do_resize_if_needed();
366
367 inline void
368 do_resize_if_needed_no_throw();
369
370 void
371 resize_imp(size_type);
372
373 virtual void
374 do_resize(size_type);
375
376 void
377 resize_imp(entry_array, size_type);
378
379 inline void
380 resize_imp_reassign(entry_pointer, entry_array, false_type);
381
382 inline void
383 resize_imp_reassign(entry_pointer, entry_array, true_type);
384
385 inline size_type
386 find_ins_pos(const_key_reference, false_type);
387
388 inline comp_hash
389 find_ins_pos(const_key_reference, true_type);
390
391 inline std::pair<point_iterator, bool>
392 insert_imp(const_reference, false_type);
393
394 inline std::pair<point_iterator, bool>
395 insert_imp(const_reference, true_type);
396
397 inline pointer
398 insert_new_imp(const_reference r_val, size_type pos)
399 {
400 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
401
402 if (do_resize_if_needed())
403 pos = find_ins_pos(PB_DS_V2F(r_val),
404 traits_base::m_store_extra_indicator);
405
406 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
407
408 entry* const p_e = m_entries + pos;
409 new (&p_e->m_value) value_type(r_val);
410 p_e->m_stat = valid_entry_status;
411 resize_base::notify_inserted(++m_num_used_e);
412
413 _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
414
415 _GLIBCXX_DEBUG_ONLY(assert_valid();)
416 return &p_e->m_value;
417 }
418
419 inline pointer
420 insert_new_imp(const_reference r_val, comp_hash& r_pos_hash_pair)
421 {
422 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
423 valid_entry_status);
424
425 if (do_resize_if_needed())
426 r_pos_hash_pair = find_ins_pos(PB_DS_V2F(r_val),
427 traits_base::m_store_extra_indicator);
428
429 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
430 valid_entry_status);
431
432 entry* const p_e = m_entries + r_pos_hash_pair.first;
433 new (&p_e->m_value) value_type(r_val);
434 p_e->m_hash = r_pos_hash_pair.second;
435 p_e->m_stat = valid_entry_status;
436
437 resize_base::notify_inserted(++m_num_used_e);
438
439 _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
440
441 _GLIBCXX_DEBUG_ONLY(assert_valid();)
442 return &p_e->m_value;
443 }
444
445 #ifdef PB_DS_DATA_TRUE_INDICATOR
446 inline mapped_reference
447 subscript_imp(const_key_reference key, false_type)
448 {
449 _GLIBCXX_DEBUG_ONLY(assert_valid();)
450
451 const size_type pos = find_ins_pos(key,
452 traits_base::m_store_extra_indicator);
453
454 entry_pointer p_e = &m_entries[pos];
455 if (p_e->m_stat != valid_entry_status)
456 return insert_new_imp(value_type(key, mapped_type()), pos)->second;
457
458 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key);)
459 return p_e->m_value.second;
460 }
461
462 inline mapped_reference
463 subscript_imp(const_key_reference key, true_type)
464 {
465 _GLIBCXX_DEBUG_ONLY(assert_valid();)
466
467 comp_hash pos_hash_pair =
468 find_ins_pos(key, traits_base::m_store_extra_indicator);
469
470 if (m_entries[pos_hash_pair.first].m_stat != valid_entry_status)
471 return insert_new_imp(value_type(key, mapped_type()),
472 pos_hash_pair)->second;
473
474 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key));
475 return (m_entries + pos_hash_pair.first)->m_value.second;
476 }
477 #endif
478
479 inline pointer
480 find_key_pointer(const_key_reference key, false_type)
481 {
482 const size_type hash = ranged_probe_fn_base::operator()(key);
483 size_type i;
484 resize_base::notify_find_search_start();
485
486 // Loop until entry is found or until all possible entries accessed.
487 for (i = 0; i < m_num_e; ++i)
488 {
489 const size_type pos = ranged_probe_fn_base::operator()(key, hash, i);
490
491 entry* const p_e = m_entries + pos;
492 switch (p_e->m_stat)
493 {
494 case empty_entry_status:
495 {
496 resize_base::notify_find_search_end();
497 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
498
499 return NULL;
500 }
501 break;
502 case valid_entry_status:
503 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value), key))
504 {
505 resize_base::notify_find_search_end();
506 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key);)
507
508 return pointer(&p_e->m_value);
509 }
510 break;
511 case erased_entry_status:
512 break;
513 default:
514 _GLIBCXX_DEBUG_ASSERT(0);
515 };
516
517 resize_base::notify_find_search_collision();
518 }
519
520 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
521 resize_base::notify_find_search_end();
522 return NULL;
523 }
524
525 inline pointer
526 find_key_pointer(const_key_reference key, true_type)
527 {
528 comp_hash pos_hash_pair = ranged_probe_fn_base::operator()(key);
529 size_type i;
530 resize_base::notify_find_search_start();
531
532 // Loop until entry is found or until all possible entries accessed.
533 for (i = 0; i < m_num_e; ++i)
534 {
535 const size_type pos =
536 ranged_probe_fn_base::operator()(key, pos_hash_pair.second, i);
537
538 entry* const p_e = m_entries + pos;
539
540 switch(p_e->m_stat)
541 {
542 case empty_entry_status:
543 {
544 resize_base::notify_find_search_end();
545 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
546
547 return NULL;
548 }
549 break;
550 case valid_entry_status:
551 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value),
552 p_e->m_hash,
553 key, pos_hash_pair.second))
554 {
555 resize_base::notify_find_search_end();
556 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key);)
557 return pointer(&p_e->m_value);
558 }
559 break;
560 case erased_entry_status:
561 break;
562 default:
563 _GLIBCXX_DEBUG_ASSERT(0);
564 };
565
566 resize_base::notify_find_search_collision();
567 }
568
569 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
570 resize_base::notify_find_search_end();
571 return NULL;
572 }
573
574 inline bool
575 erase_imp(const_key_reference, true_type);
576
577 inline bool
578 erase_imp(const_key_reference, false_type);
579
580 inline void
581 erase_entry(entry_pointer p_e);
582
583 #ifdef PB_DS_DATA_TRUE_INDICATOR
584 void
585 inc_it_state(pointer& r_p_value, size_type& r_pos) const
586 { inc_it_state((const_mapped_pointer& )r_p_value, r_pos); }
587 #endif
588
589 void
590 inc_it_state(const_pointer& r_p_value, size_type& r_pos) const
591 {
592 _GLIBCXX_DEBUG_ASSERT(r_p_value != NULL);
593 for (++r_pos; r_pos < m_num_e; ++r_pos)
594 {
595 const_entry_pointer p_e =& m_entries[r_pos];
596 if (p_e->m_stat == valid_entry_status)
597 {
598 r_p_value =& p_e->m_value;
599 return;
600 }
601 }
602 r_p_value = NULL;
603 }
604
605 void
606 get_start_it_state(const_pointer& r_p_value, size_type& r_pos) const
607 {
608 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
609 {
610 const_entry_pointer p_e = &m_entries[r_pos];
611 if (p_e->m_stat == valid_entry_status)
612 {
613 r_p_value = &p_e->m_value;
614 return;
615 }
616 }
617 r_p_value = NULL;
618 }
619
620 void
621 get_start_it_state(pointer& r_p_value, size_type& r_pos)
622 {
623 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
624 {
625 entry_pointer p_e = &m_entries[r_pos];
626 if (p_e->m_stat == valid_entry_status)
627 {
628 r_p_value = &p_e->m_value;
629 return;
630 }
631 }
632 r_p_value = NULL;
633 }
634
635 #ifdef _GLIBCXX_DEBUG
636 void
637 assert_entry_array_valid(const entry_array, false_type) const;
638
639 void
640 assert_entry_array_valid(const entry_array, true_type) const;
641 #endif
642
643 static entry_allocator s_entry_allocator;
644 static iterator s_end_it;
645 static const_iterator s_const_end_it;
646
647 size_type m_num_e;
648 size_type m_num_used_e;
649 entry_pointer m_entries;
650
651 enum
652 {
653 store_hash_ok = !Store_Hash
654 || !is_same<Hash_Fn, pb_ds::null_hash_fn>::value
655 };
656
657 PB_DS_STATIC_ASSERT(sth, store_hash_ok);
658 };
659
660 #include <ext/pb_ds/detail/gp_hash_table_map_/constructor_destructor_fn_imps.hpp>
661 #include <ext/pb_ds/detail/gp_hash_table_map_/find_fn_imps.hpp>
662 #include <ext/pb_ds/detail/gp_hash_table_map_/resize_fn_imps.hpp>
663 #include <ext/pb_ds/detail/gp_hash_table_map_/debug_fn_imps.hpp>
664 #include <ext/pb_ds/detail/gp_hash_table_map_/info_fn_imps.hpp>
665 #include <ext/pb_ds/detail/gp_hash_table_map_/policy_access_fn_imps.hpp>
666 #include <ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp>
667 #include <ext/pb_ds/detail/gp_hash_table_map_/iterator_fn_imps.hpp>
668 #include <ext/pb_ds/detail/gp_hash_table_map_/insert_fn_imps.hpp>
669 #include <ext/pb_ds/detail/gp_hash_table_map_/trace_fn_imps.hpp>
670
671 #undef PB_DS_CLASS_T_DEC
672 #undef PB_DS_CLASS_C_DEC
673 #undef PB_DS_HASH_EQ_FN_C_DEC
674 #undef PB_DS_RANGED_PROBE_FN_C_DEC
675 #undef PB_DS_TYPES_TRAITS_C_DEC
676 #undef PB_DS_MAP_DEBUG_BASE_C_DEC
677 #undef PB_DS_CLASS_NAME
678 #undef PB_DS_V2F
679 #undef PB_DS_V2S
680
681 } // namespace detail
682 } // namespace pb_ds
683