Merge with trank @ 137446
[official-gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / gp_hash_table_map_ / gp_ht_map_.hpp
blob2fcb25c8310d4e18de155335f5ffd6336461eded
1 // -*- C++ -*-
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.
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.
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.
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.
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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.
42 /**
43 * @file gp_ht_map_.hpp
44 * Contains an implementation class for gp_ht_map_.
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/debug_map_base.hpp>
58 #endif
59 #include <debug/debug.h>
61 namespace __gnu_pbds
63 namespace detail
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>
70 #ifdef PB_DS_DATA_TRUE_INDICATOR
71 #define PB_DS_CLASS_NAME gp_ht_map_data_
72 #endif
74 #ifdef PB_DS_DATA_FALSE_INDICATOR
75 #define PB_DS_CLASS_NAME gp_ht_map_no_data_
76 #endif
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>
82 #define PB_DS_HASH_EQ_FN_C_DEC \
83 hash_eq_fn<Key, Eq_Fn, Allocator, Store_Hash>
85 #define PB_DS_RANGED_PROBE_FN_C_DEC \
86 ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, Store_Hash>
88 #define PB_DS_TYPES_TRAITS_C_DEC \
89 types_traits<Key, Mapped, Allocator, Store_Hash>
91 #ifdef _GLIBCXX_DEBUG
92 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
93 debug_map_base<Key, Eq_Fn, typename Allocator::template rebind<Key>::other::const_reference>
94 #endif
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
101 #ifdef PB_DS_DATA_FALSE_INDICATOR
102 #define PB_DS_V2F(X) (X)
103 #define PB_DS_V2S(X) Mapped()
104 #endif
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_DEBUG_MAP_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
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;
133 enum entry_status
135 empty_entry_status,
136 valid_entry_status,
137 erased_entry_status
138 } __attribute__ ((packed));
140 struct entry : public traits_base::stored_value_type
142 entry_status m_stat;
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;
152 typedef PB_DS_RANGED_PROBE_FN_C_DEC ranged_probe_fn_base;
154 #ifdef _GLIBCXX_DEBUG
155 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
156 #endif
158 typedef PB_DS_HASH_EQ_FN_C_DEC hash_eq_fn_base;
159 typedef Resize_Policy resize_base;
161 #define PB_DS_GEN_POS typename Allocator::size_type
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>
168 #undef PB_DS_GEN_POS
170 public:
171 typedef Allocator allocator_type;
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;
180 enum
182 store_hash = Store_Hash
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;
201 #ifdef PB_DS_DATA_TRUE_INDICATOR
202 typedef point_iterator_ point_iterator;
203 #endif
205 #ifdef PB_DS_DATA_FALSE_INDICATOR
206 typedef const_point_iterator_ point_iterator;
207 #endif
209 typedef const_point_iterator_ const_point_iterator;
211 #ifdef PB_DS_DATA_TRUE_INDICATOR
212 typedef iterator_ iterator;
213 #endif
215 #ifdef PB_DS_DATA_FALSE_INDICATOR
216 typedef const_iterator_ iterator;
217 #endif
219 typedef const_iterator_ const_iterator;
221 PB_DS_CLASS_NAME();
223 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
225 PB_DS_CLASS_NAME(const Hash_Fn&);
227 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&);
229 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&);
231 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
232 const Probe_Fn&);
234 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
235 const Probe_Fn&, const Resize_Policy&);
237 template<typename It>
238 void
239 copy_from_range(It first_it, It last_it);
241 virtual
242 ~PB_DS_CLASS_NAME();
244 void
245 swap(PB_DS_CLASS_C_DEC& other);
247 inline size_type
248 size() const;
250 inline size_type
251 max_size() const;
253 inline bool
254 empty() const;
256 Hash_Fn&
257 get_hash_fn();
259 const Hash_Fn&
260 get_hash_fn() const;
262 Eq_Fn&
263 get_eq_fn();
265 const Eq_Fn&
266 get_eq_fn() const;
268 Probe_Fn&
269 get_probe_fn();
271 const Probe_Fn&
272 get_probe_fn() const;
274 Comb_Probe_Fn&
275 get_comb_probe_fn();
277 const Comb_Probe_Fn&
278 get_comb_probe_fn() const;
280 Resize_Policy&
281 get_resize_policy();
283 const Resize_Policy&
284 get_resize_policy() const;
286 inline std::pair<point_iterator, bool>
287 insert(const_reference r_val)
289 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
290 return insert_imp(r_val, traits_base::m_store_extra_indicator);
293 inline mapped_reference
294 operator[](const_key_reference r_key)
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
304 inline point_iterator
305 find(const_key_reference r_key);
307 inline const_point_iterator
308 find(const_key_reference r_key) const;
310 inline point_iterator
311 find_end();
313 inline const_point_iterator
314 find_end() const;
316 inline bool
317 erase(const_key_reference r_key);
319 template<typename Pred>
320 inline size_type
321 erase_if(Pred prd);
323 void
324 clear();
326 inline iterator
327 begin();
329 inline const_iterator
330 begin() const;
332 inline iterator
333 end();
335 inline const_iterator
336 end() const;
338 #ifdef _GLIBCXX_DEBUG
339 void
340 assert_valid() const;
341 #endif
343 #ifdef PB_DS_HT_MAP_TRACE_
344 void
345 trace() const;
346 #endif
348 private:
349 #ifdef PB_DS_DATA_TRUE_INDICATOR
350 friend class iterator_;
351 #endif
353 friend class const_iterator_;
355 void
356 deallocate_all();
358 void
359 initialize();
361 void
362 erase_all_valid_entries(entry_array, size_type);
364 inline bool
365 do_resize_if_needed();
367 inline void
368 do_resize_if_needed_no_throw();
370 void
371 resize_imp(size_type);
373 virtual void
374 do_resize(size_type);
376 void
377 resize_imp(entry_array, size_type);
379 inline void
380 resize_imp_reassign(entry_pointer, entry_array, false_type);
382 inline void
383 resize_imp_reassign(entry_pointer, entry_array, true_type);
385 inline size_type
386 find_ins_pos(const_key_reference, false_type);
388 inline comp_hash
389 find_ins_pos(const_key_reference, true_type);
391 inline std::pair<point_iterator, bool>
392 insert_imp(const_reference, false_type);
394 inline std::pair<point_iterator, bool>
395 insert_imp(const_reference, true_type);
397 inline pointer
398 insert_new_imp(const_reference r_val, size_type pos)
400 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
402 if (do_resize_if_needed())
403 pos = find_ins_pos(PB_DS_V2F(r_val),
404 traits_base::m_store_extra_indicator);
406 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
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);
413 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
415 _GLIBCXX_DEBUG_ONLY(assert_valid();)
416 return &p_e->m_value;
419 inline pointer
420 insert_new_imp(const_reference r_val, comp_hash& r_pos_hash_pair)
422 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
423 valid_entry_status);
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);
429 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
430 valid_entry_status);
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;
437 resize_base::notify_inserted(++m_num_used_e);
439 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
441 _GLIBCXX_DEBUG_ONLY(assert_valid();)
442 return &p_e->m_value;
445 #ifdef PB_DS_DATA_TRUE_INDICATOR
446 inline mapped_reference
447 subscript_imp(const_key_reference key, false_type)
449 _GLIBCXX_DEBUG_ONLY(assert_valid();)
451 const size_type pos = find_ins_pos(key,
452 traits_base::m_store_extra_indicator);
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;
458 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key);)
459 return p_e->m_value.second;
462 inline mapped_reference
463 subscript_imp(const_key_reference key, true_type)
465 _GLIBCXX_DEBUG_ONLY(assert_valid();)
467 comp_hash pos_hash_pair =
468 find_ins_pos(key, traits_base::m_store_extra_indicator);
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;
474 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key));
475 return (m_entries + pos_hash_pair.first)->m_value.second;
477 #endif
479 inline pointer
480 find_key_pointer(const_key_reference key, false_type)
482 const size_type hash = ranged_probe_fn_base::operator()(key);
483 size_type i;
484 resize_base::notify_find_search_start();
486 // Loop until entry is found or until all possible entries accessed.
487 for (i = 0; i < m_num_e; ++i)
489 const size_type pos = ranged_probe_fn_base::operator()(key, hash, i);
491 entry* const p_e = m_entries + pos;
492 switch (p_e->m_stat)
494 case empty_entry_status:
496 resize_base::notify_find_search_end();
497 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
499 return NULL;
501 break;
502 case valid_entry_status:
503 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value), key))
505 resize_base::notify_find_search_end();
506 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key);)
508 return pointer(&p_e->m_value);
510 break;
511 case erased_entry_status:
512 break;
513 default:
514 _GLIBCXX_DEBUG_ASSERT(0);
517 resize_base::notify_find_search_collision();
520 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
521 resize_base::notify_find_search_end();
522 return NULL;
525 inline pointer
526 find_key_pointer(const_key_reference key, true_type)
528 comp_hash pos_hash_pair = ranged_probe_fn_base::operator()(key);
529 size_type i;
530 resize_base::notify_find_search_start();
532 // Loop until entry is found or until all possible entries accessed.
533 for (i = 0; i < m_num_e; ++i)
535 const size_type pos =
536 ranged_probe_fn_base::operator()(key, pos_hash_pair.second, i);
538 entry* const p_e = m_entries + pos;
540 switch(p_e->m_stat)
542 case empty_entry_status:
544 resize_base::notify_find_search_end();
545 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
547 return NULL;
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))
555 resize_base::notify_find_search_end();
556 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key);)
557 return pointer(&p_e->m_value);
559 break;
560 case erased_entry_status:
561 break;
562 default:
563 _GLIBCXX_DEBUG_ASSERT(0);
566 resize_base::notify_find_search_collision();
569 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
570 resize_base::notify_find_search_end();
571 return NULL;
574 inline bool
575 erase_imp(const_key_reference, true_type);
577 inline bool
578 erase_imp(const_key_reference, false_type);
580 inline void
581 erase_entry(entry_pointer p_e);
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
589 void
590 inc_it_state(const_pointer& r_p_value, size_type& r_pos) const
592 _GLIBCXX_DEBUG_ASSERT(r_p_value != NULL);
593 for (++r_pos; r_pos < m_num_e; ++r_pos)
595 const_entry_pointer p_e =& m_entries[r_pos];
596 if (p_e->m_stat == valid_entry_status)
598 r_p_value =& p_e->m_value;
599 return;
602 r_p_value = NULL;
605 void
606 get_start_it_state(const_pointer& r_p_value, size_type& r_pos) const
608 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
610 const_entry_pointer p_e = &m_entries[r_pos];
611 if (p_e->m_stat == valid_entry_status)
613 r_p_value = &p_e->m_value;
614 return;
617 r_p_value = NULL;
620 void
621 get_start_it_state(pointer& r_p_value, size_type& r_pos)
623 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
625 entry_pointer p_e = &m_entries[r_pos];
626 if (p_e->m_stat == valid_entry_status)
628 r_p_value = &p_e->m_value;
629 return;
632 r_p_value = NULL;
635 #ifdef _GLIBCXX_DEBUG
636 void
637 assert_entry_array_valid(const entry_array, false_type) const;
639 void
640 assert_entry_array_valid(const entry_array, true_type) const;
641 #endif
643 static entry_allocator s_entry_allocator;
644 static iterator s_end_it;
645 static const_iterator s_const_end_it;
647 size_type m_num_e;
648 size_type m_num_used_e;
649 entry_pointer m_entries;
651 enum
653 store_hash_ok = !Store_Hash
654 || !is_same<Hash_Fn, __gnu_pbds::null_hash_fn>::value
657 PB_DS_STATIC_ASSERT(sth, store_hash_ok);
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>
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_DEBUG_MAP_BASE_C_DEC
677 #undef PB_DS_CLASS_NAME
678 #undef PB_DS_V2F
679 #undef PB_DS_V2S
681 } // namespace detail
682 } // namespace __gnu_pbds