3 // Copyright (C) 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
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 3, or (at your option) any later
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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
37 * @file assoc_container.hpp
38 * Contains associative containers.
41 #ifndef PB_DS_ASSOC_CNTNR_HPP
42 #define PB_DS_ASSOC_CNTNR_HPP
44 #include <bits/c++config.h>
45 #include <ext/typelist.h>
46 #include <ext/pb_ds/tag_and_trait.hpp>
47 #include <ext/pb_ds/detail/standard_policies.hpp>
48 #include <ext/pb_ds/detail/container_base_dispatch.hpp>
49 #include <ext/pb_ds/detail/basic_tree_policy/traits.hpp>
53 /** @defgroup pbds Policy-Based Data Structures
56 * This is a library of policy-based elementary data structures:
57 * associative containers and priority queues. It is designed for
58 * high-performance, flexibility, semantic safety, and conformance
59 * to the corresponding containers in std (except for some points
60 * where it differs by design).
63 * http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/index.html
68 #define PB_DS_BASE_C_DEC \
69 detail::container_base_dispatch<Key, Mapped, Tag, Policy_Tl, Allocator>::type
71 /// An abstract basic associative container.
72 template<typename Key
,
77 class container_base
: public PB_DS_BASE_C_DEC
80 typedef typename PB_DS_BASE_C_DEC base_type
;
83 typedef Tag container_category
;
84 typedef Allocator allocator_type
;
85 typedef typename
allocator_type::size_type size_type
;
86 typedef typename
allocator_type::difference_type difference_type
;
89 typedef typename
allocator_type::template rebind
<Key
>::other::value_type key_type
;
90 typedef typename
allocator_type::template rebind
<key_type
>::other key_rebind
;
91 typedef typename
key_rebind::reference key_reference
;
92 typedef typename
key_rebind::const_reference const_key_reference
;
93 typedef typename
key_rebind::pointer key_pointer
;
94 typedef typename
key_rebind::const_pointer const_key_pointer
;
97 typedef Mapped mapped_type
;
98 typedef typename
allocator_type::template rebind
<mapped_type
>::other mapped_rebind
;
99 typedef typename
mapped_rebind::reference mapped_reference
;
100 typedef typename
mapped_rebind::const_reference const_mapped_reference
;
101 typedef typename
mapped_rebind::pointer mapped_pointer
;
102 typedef typename
mapped_rebind::const_pointer const_mapped_pointer
;
105 typedef typename
base_type::value_type value_type
;
106 typedef typename
allocator_type::template rebind
<value_type
>::other value_rebind
;
107 typedef typename
value_rebind::reference reference
;
108 typedef typename
value_rebind::const_reference const_reference
;
109 typedef typename
value_rebind::pointer pointer
;
110 typedef typename
value_rebind::const_pointer const_pointer
;
113 typedef typename
base_type::iterator iterator
;
114 typedef typename
base_type::const_iterator const_iterator
;
115 typedef typename
base_type::point_iterator point_iterator
;
116 typedef typename
base_type::const_point_iterator const_point_iterator
;
119 ~container_base() { }
122 #define PB_DS_CLASS_NAME container_base
123 #include <ext/pb_ds/detail/constructors_destructor_fn_imps.hpp>
124 #undef PB_DS_CLASS_NAME
127 #undef PB_DS_BASE_C_DEC
130 #define PB_DS_BASE_C_DEC \
131 container_base<Key, Mapped, Tag, typename __gnu_cxx::typelist::append< \
132 typename __gnu_cxx::typelist::create4<Hash_Fn, Eq_Fn, Resize_Policy, detail::integral_constant<int, Store_Hash> >::type, Policy_TL>::type, Allocator>
134 /// An abstract basic hash-based associative container.
135 template<typename Key
,
139 typename Resize_Policy
,
144 class basic_hash_table
: public PB_DS_BASE_C_DEC
147 typedef PB_DS_BASE_C_DEC base_type
;
151 ~basic_hash_table() { }
154 #define PB_DS_CLASS_NAME basic_hash_table
155 #include <ext/pb_ds/detail/constructors_destructor_fn_imps.hpp>
156 #undef PB_DS_CLASS_NAME
160 operator=(const base_type
&);
163 #undef PB_DS_BASE_C_DEC
166 #define PB_DS_BASE_C_DEC \
167 basic_hash_table<Key, Mapped, Hash_Fn, Eq_Fn, Resize_Policy, Store_Hash, \
169 typename __gnu_cxx::typelist::create1<Comb_Hash_Fn>::type, Allocator>
171 /// A concrete collision-chaining hash-based associative container.
172 template<typename Key
,
174 typename Hash_Fn
= typename
detail::default_hash_fn
<Key
>::type
,
175 typename Eq_Fn
= typename
detail::default_eq_fn
<Key
>::type
,
176 typename Comb_Hash_Fn
= detail::default_comb_hash_fn::type
,
177 typename Resize_Policy
= typename
detail::default_resize_policy
<Comb_Hash_Fn
>::type
,
178 bool Store_Hash
= detail::default_store_hash
,
179 typename Allocator
= std::allocator
<char> >
180 class cc_hash_table
: public PB_DS_BASE_C_DEC
183 typedef PB_DS_BASE_C_DEC base_type
;
186 typedef Hash_Fn hash_fn
;
188 typedef Resize_Policy resize_policy
;
189 typedef Comb_Hash_Fn comb_hash_fn
;
191 // Default constructor.
194 // Constructor taking some policy objects. r_hash_fn will be
195 // copied by the Hash_Fn object of the container object.
196 cc_hash_table(const hash_fn
& h
)
199 // Constructor taking some policy objects. r_hash_fn will be
200 // copied by the hash_fn object of the container object, and
201 // r_eq_fn will be copied by the eq_fn object of the container
203 cc_hash_table(const hash_fn
& h
, const eq_fn
& e
)
204 : base_type(h
, e
) { }
206 // Constructor taking some policy objects. r_hash_fn will be
207 // copied by the hash_fn object of the container object, r_eq_fn
208 // will be copied by the eq_fn object of the container object, and
209 // r_comb_hash_fn will be copied by the comb_hash_fn object of the
211 cc_hash_table(const hash_fn
& h
, const eq_fn
& e
, const comb_hash_fn
& ch
)
212 : base_type(h
, e
, ch
) { }
214 // Constructor taking some policy objects. r_hash_fn will be
215 // copied by the hash_fn object of the container object, r_eq_fn
216 // will be copied by the eq_fn object of the container object,
217 // r_comb_hash_fn will be copied by the comb_hash_fn object of the
218 // container object, and r_resize_policy will be copied by the
219 // resize_policy object of the container object.
220 cc_hash_table(const hash_fn
& h
, const eq_fn
& e
, const comb_hash_fn
& ch
,
221 const resize_policy
& rp
)
222 : base_type(h
, e
, ch
, rp
) { }
224 // Constructor taking __iterators to a range of value_types. The
225 // value_types between first_it and last_it will be inserted into
226 // the container object.
227 template<typename It
>
228 cc_hash_table(It first
, It last
)
229 { base_type::copy_from_range(first
, last
); }
231 // Constructor taking __iterators to a range of value_types and
232 // some policy objects. The value_types between first_it and
233 // last_it will be inserted into the container object.
234 template<typename It
>
235 cc_hash_table(It first
, It last
, const hash_fn
& h
)
237 { copy_from_range(first
, last
); }
239 // Constructor taking __iterators to a range of value_types and
240 // some policy objects The value_types between first_it and
241 // last_it will be inserted into the container object. r_hash_fn
242 // will be copied by the hash_fn object of the container object,
243 // and r_eq_fn will be copied by the eq_fn object of the container
245 template<typename It
>
246 cc_hash_table(It first
, It last
, const hash_fn
& h
, const eq_fn
& e
)
248 { copy_from_range(first
, last
); }
250 // Constructor taking __iterators to a range of value_types and
251 // some policy objects The value_types between first_it and
252 // last_it will be inserted into the container object. r_hash_fn
253 // will be copied by the hash_fn object of the container object,
254 // r_eq_fn will be copied by the eq_fn object of the container
255 // object, and r_comb_hash_fn will be copied by the comb_hash_fn
256 // object of the container object.
257 template<typename It
>
258 cc_hash_table(It first
, It last
, const hash_fn
& h
, const eq_fn
& e
,
259 const comb_hash_fn
& ch
)
260 : base_type(h
, e
, ch
)
261 { copy_from_range(first
, last
); }
263 // Constructor taking __iterators to a range of value_types and
264 // some policy objects The value_types between first_it and
265 // last_it will be inserted into the container object. r_hash_fn
266 // will be copied by the hash_fn object of the container object,
267 // r_eq_fn will be copied by the eq_fn object of the container
268 // object, r_comb_hash_fn will be copied by the comb_hash_fn
269 // object of the container object, and r_resize_policy will be
270 // copied by the resize_policy object of the container object.
271 template<typename It
>
272 cc_hash_table(It first
, It last
, const hash_fn
& h
, const eq_fn
& e
,
273 const comb_hash_fn
& ch
, const resize_policy
& rp
)
274 : base_type(h
, e
, ch
, rp
)
275 { copy_from_range(first
, last
); }
277 cc_hash_table(const cc_hash_table
& other
)
278 : base_type((const base_type
&)other
)
285 operator=(const cc_hash_table
& other
)
289 cc_hash_table
tmp(other
);
296 swap(cc_hash_table
& other
)
297 { base_type::swap(other
); }
300 #undef PB_DS_BASE_C_DEC
303 #define PB_DS_BASE_C_DEC \
304 basic_hash_table<Key, Mapped, Hash_Fn, Eq_Fn, Resize_Policy, Store_Hash, \
306 typename __gnu_cxx::typelist::create2<Comb_Probe_Fn, Probe_Fn>::type, Allocator>
308 /// A concrete general-probing hash-based associative container.
309 template<typename Key
,
311 typename Hash_Fn
= typename
detail::default_hash_fn
<Key
>::type
,
312 typename Eq_Fn
= typename
detail::default_eq_fn
<Key
>::type
,
313 typename Comb_Probe_Fn
= detail::default_comb_hash_fn::type
,
314 typename Probe_Fn
= typename
detail::default_probe_fn
<Comb_Probe_Fn
>::type
,
315 typename Resize_Policy
= typename
detail::default_resize_policy
<Comb_Probe_Fn
>::type
,
316 bool Store_Hash
= detail::default_store_hash
,
317 typename Allocator
= std::allocator
<char> >
318 class gp_hash_table
: public PB_DS_BASE_C_DEC
321 typedef PB_DS_BASE_C_DEC base_type
;
324 typedef Hash_Fn hash_fn
;
326 typedef Comb_Probe_Fn comb_probe_fn
;
327 typedef Probe_Fn probe_fn
;
328 typedef Resize_Policy resize_policy
;
330 // Default constructor.
333 // Constructor taking some policy objects. r_hash_fn will be
334 // copied by the hash_fn object of the container object.
335 gp_hash_table(const hash_fn
& h
)
338 // Constructor taking some policy objects. r_hash_fn will be
339 // copied by the hash_fn object of the container object, and
340 // r_eq_fn will be copied by the eq_fn object of the container
342 gp_hash_table(const hash_fn
& h
, const eq_fn
& e
)
343 : base_type(h
, e
) { }
345 // Constructor taking some policy objects. r_hash_fn will be
346 // copied by the hash_fn object of the container object, r_eq_fn
347 // will be copied by the eq_fn object of the container object, and
348 // r_comb_probe_fn will be copied by the comb_probe_fn object of
349 // the container object.
350 gp_hash_table(const hash_fn
& h
, const eq_fn
& e
, const comb_probe_fn
& cp
)
351 : base_type(h
, e
, cp
) { }
353 // Constructor taking some policy objects. r_hash_fn will be
354 // copied by the hash_fn object of the container object, r_eq_fn
355 // will be copied by the eq_fn object of the container object,
356 // r_comb_probe_fn will be copied by the comb_probe_fn object of
357 // the container object, and r_probe_fn will be copied by the
358 // probe_fn object of the container object.
359 gp_hash_table(const hash_fn
& h
, const eq_fn
& e
, const comb_probe_fn
& cp
,
361 : base_type(h
, e
, cp
, p
) { }
363 // Constructor taking some policy objects. r_hash_fn will be
364 // copied by the hash_fn object of the container object, r_eq_fn
365 // will be copied by the eq_fn object of the container object,
366 // r_comb_probe_fn will be copied by the comb_probe_fn object of
367 // the container object, r_probe_fn will be copied by the probe_fn
368 // object of the container object, and r_resize_policy will be
369 // copied by the Resize_Policy object of the container object.
370 gp_hash_table(const hash_fn
& h
, const eq_fn
& e
, const comb_probe_fn
& cp
,
371 const probe_fn
& p
, const resize_policy
& rp
)
372 : base_type(h
, e
, cp
, p
, rp
) { }
374 // Constructor taking __iterators to a range of value_types. The
375 // value_types between first_it and last_it will be inserted into
376 // the container object.
377 template<typename It
>
378 gp_hash_table(It first
, It last
)
379 { base_type::copy_from_range(first
, last
); }
381 // Constructor taking __iterators to a range of value_types and
382 // some policy objects. The value_types between first_it and
383 // last_it will be inserted into the container object. r_hash_fn
384 // will be copied by the hash_fn object of the container object.
385 template<typename It
>
386 gp_hash_table(It first
, It last
, const hash_fn
& h
)
388 { base_type::copy_from_range(first
, last
); }
390 // Constructor taking __iterators to a range of value_types and
391 // some policy objects. The value_types between first_it and
392 // last_it will be inserted into the container object. r_hash_fn
393 // will be copied by the hash_fn object of the container object,
394 // and r_eq_fn will be copied by the eq_fn object of the container
396 template<typename It
>
397 gp_hash_table(It first
, It last
, const hash_fn
& h
, const eq_fn
& e
)
399 { base_type::copy_from_range(first
, last
); }
401 // Constructor taking __iterators to a range of value_types and
402 // some policy objects. The value_types between first_it and
403 // last_it will be inserted into the container object. r_hash_fn
404 // will be copied by the hash_fn object of the container object,
405 // r_eq_fn will be copied by the eq_fn object of the container
406 // object, and r_comb_probe_fn will be copied by the comb_probe_fn
407 // object of the container object.
408 template<typename It
>
409 gp_hash_table(It first
, It last
, const hash_fn
& h
, const eq_fn
& e
,
410 const comb_probe_fn
& cp
)
411 : base_type(h
, e
, cp
)
412 { base_type::copy_from_range(first
, last
); }
414 // Constructor taking __iterators to a range of value_types and
415 // some policy objects. The value_types between first_it and
416 // last_it will be inserted into the container object. r_hash_fn
417 // will be copied by the hash_fn object of the container object,
418 // r_eq_fn will be copied by the eq_fn object of the container
419 // object, r_comb_probe_fn will be copied by the comb_probe_fn
420 // object of the container object, and r_probe_fn will be copied
421 // by the probe_fn object of the container object.
422 template<typename It
>
423 gp_hash_table(It first
, It last
, const hash_fn
& h
, const eq_fn
& e
,
424 const comb_probe_fn
& cp
, const probe_fn
& p
)
425 : base_type(h
, e
, cp
, p
)
426 { base_type::copy_from_range(first
, last
); }
428 // Constructor taking __iterators to a range of value_types and
429 // some policy objects. The value_types between first_it and
430 // last_it will be inserted into the container object. r_hash_fn
431 // will be copied by the hash_fn object of the container object,
432 // r_eq_fn will be copied by the eq_fn object of the container
433 // object, r_comb_probe_fn will be copied by the comb_probe_fn
434 // object of the container object, r_probe_fn will be copied by
435 // the probe_fn object of the container object, and
436 // r_resize_policy will be copied by the resize_policy object of
437 // the container object.
438 template<typename It
>
439 gp_hash_table(It first
, It last
, const hash_fn
& h
, const eq_fn
& e
,
440 const comb_probe_fn
& cp
, const probe_fn
& p
,
441 const resize_policy
& rp
)
442 : base_type(h
, e
, cp
, p
, rp
)
443 { base_type::copy_from_range(first
, last
); }
445 gp_hash_table(const gp_hash_table
& other
)
446 : base_type((const base_type
&)other
)
453 operator=(const gp_hash_table
& other
)
457 gp_hash_table
tmp(other
);
464 swap(gp_hash_table
& other
)
465 { base_type::swap(other
); }
468 #undef PB_DS_BASE_C_DEC
471 #define PB_DS_BASE_C_DEC \
472 container_base<Key, Mapped, Tag, Policy_Tl, Allocator>
474 /// An abstract basic tree-like (tree, trie) associative container.
475 template<typename Key
, typename Mapped
, typename Tag
,
476 typename Node_Update
, typename Policy_Tl
, typename Allocator
>
477 class basic_tree
: public PB_DS_BASE_C_DEC
480 typedef PB_DS_BASE_C_DEC base_type
;
483 typedef Node_Update node_update
;
489 #define PB_DS_CLASS_NAME basic_tree
490 #include <ext/pb_ds/detail/constructors_destructor_fn_imps.hpp>
491 #undef PB_DS_CLASS_NAME
494 #undef PB_DS_BASE_C_DEC
497 #define PB_DS_TREE_NODE_AND_IT_TRAITS_C_DEC \
498 detail::tree_traits<Key, Mapped,Cmp_Fn,Node_Update,Tag, Allocator>
500 #define PB_DS_BASE_C_DEC \
501 basic_tree<Key,Mapped,Tag,typename PB_DS_TREE_NODE_AND_IT_TRAITS_C_DEC::node_update, \
502 typename __gnu_cxx::typelist::create2<Cmp_Fn, PB_DS_TREE_NODE_AND_IT_TRAITS_C_DEC >::type, Allocator>
504 /// A concrete basic tree-based associative container.
505 template<typename Key
, typename Mapped
, typename Cmp_Fn
= std::less
<Key
>,
506 typename Tag
= rb_tree_tag
,
507 template<typename Const_Node_Iterator
, typename Node_Iterator
, typename Cmp_Fn_
, typename Allocator_
>
508 class Node_Update
= __gnu_pbds::null_tree_node_update
,
509 typename Allocator
= std::allocator
<char> >
510 class tree
: public PB_DS_BASE_C_DEC
513 typedef PB_DS_BASE_C_DEC base_type
;
516 // Comparison functor type.
517 typedef Cmp_Fn cmp_fn
;
521 // Constructor taking some policy objects. r_cmp_fn will be copied
522 // by the Cmp_Fn object of the container object.
523 tree(const cmp_fn
& c
)
526 // Constructor taking __iterators to a range of value_types. The
527 // value_types between first_it and last_it will be inserted into
528 // the container object.
529 template<typename It
>
530 tree(It first
, It last
)
531 { base_type::copy_from_range(first
, last
); }
533 // Constructor taking __iterators to a range of value_types and
534 // some policy objects The value_types between first_it and
535 // last_it will be inserted into the container object. r_cmp_fn
536 // will be copied by the cmp_fn object of the container object.
537 template<typename It
>
538 tree(It first
, It last
, const cmp_fn
& c
)
540 { base_type::copy_from_range(first
, last
); }
542 tree(const tree
& other
)
543 : base_type((const base_type
&)other
) { }
549 operator=(const tree
& other
)
561 { base_type::swap(other
); }
564 #undef PB_DS_BASE_C_DEC
565 #undef PB_DS_TREE_NODE_AND_IT_TRAITS_C_DEC
568 #define PB_DS_TRIE_NODE_AND_ITS_TRAITS \
569 detail::trie_traits<Key,Mapped,E_Access_Traits,Node_Update,Tag,Allocator>
571 #define PB_DS_BASE_C_DEC \
572 basic_tree<Key,Mapped,Tag, typename PB_DS_TRIE_NODE_AND_ITS_TRAITS::node_update, \
573 typename __gnu_cxx::typelist::create2<E_Access_Traits, PB_DS_TRIE_NODE_AND_ITS_TRAITS >::type, Allocator>
575 /// A concrete basic trie-based associative container.
576 template<typename Key
,
578 typename E_Access_Traits
= typename
detail::default_trie_e_access_traits
<Key
>::type
,
579 typename Tag
= pat_trie_tag
,
580 template<typename Const_Node_Iterator
,
581 typename Node_Iterator
,
582 typename E_Access_Traits_
,
584 class Node_Update
= null_trie_node_update
,
585 typename Allocator
= std::allocator
<char> >
586 class trie
: public PB_DS_BASE_C_DEC
589 typedef PB_DS_BASE_C_DEC base_type
;
592 // Element access traits type.
593 typedef E_Access_Traits e_access_traits
;
597 // Constructor taking some policy objects. r_e_access_traits will
598 // be copied by the E_Access_Traits object of the container
600 trie(const e_access_traits
& t
)
603 // Constructor taking __iterators to a range of value_types. The
604 // value_types between first_it and last_it will be inserted into
605 // the container object.
606 template<typename It
>
607 trie(It first
, It last
)
608 { base_type::copy_from_range(first
, last
); }
610 // Constructor taking __iterators to a range of value_types and
611 // some policy objects. The value_types between first_it and
612 // last_it will be inserted into the container object.
613 template<typename It
>
614 trie(It first
, It last
, const e_access_traits
& t
)
616 { base_type::copy_from_range(first
, last
); }
618 trie(const trie
& other
)
619 : base_type((const base_type
&)other
) { }
625 operator=(const trie
& other
)
637 { base_type::swap(other
); }
640 #undef PB_DS_BASE_C_DEC
641 #undef PB_DS_TRIE_NODE_AND_ITS_TRAITS
644 #define PB_DS_BASE_C_DEC \
645 container_base<Key, Mapped, list_update_tag, \
646 typename __gnu_cxx::typelist::create2<Eq_Fn, Update_Policy>::type, Allocator>
648 /// A list-update based associative container.
649 template<typename Key
,
651 class Eq_Fn
= typename
detail::default_eq_fn
<Key
>::type
,
652 class Update_Policy
= detail::default_update_policy::type
,
653 class Allocator
= std::allocator
<char> >
654 class list_update
: public PB_DS_BASE_C_DEC
657 typedef PB_DS_BASE_C_DEC base_type
;
661 typedef Update_Policy update_policy
;
662 typedef Allocator allocator
;
666 // Constructor taking __iterators to a range of value_types. The
667 // value_types between first_it and last_it will be inserted into
668 // the container object.
669 template<typename It
>
670 list_update(It first
, It last
)
671 { base_type::copy_from_range(first
, last
); }
673 list_update(const list_update
& other
)
674 : base_type((const base_type
&)other
) { }
680 operator=(const list_update
& other
)
684 list_update
tmp(other
);
691 swap(list_update
& other
)
692 { base_type::swap(other
); }
695 #undef PB_DS_BASE_C_DEC
698 } // namespace __gnu_pbds