1#ifndef CLICK_HASHTABLE_HH
2#define CLICK_HASHTABLE_HH
19#include <click/pair.hh>
20#include <click/hashcontainer.hh>
21#include <click/hashallocator.hh>
28template <
typename K,
typename V =
void>
class HashTable;
107 typedef typename T::key_type key_type;
108 typedef typename T::key_const_reference key_const_reference;
109 key_const_reference hashkey()
const {
119 typedef typename T::key_type
key_type;
128 typedef typename rep_type::size_type
size_type;
153#if HAVE_CXX_RVALUE_REFERENCES
171 inline bool empty()
const {
177 return _rep.bucket_count();
183 return _rep.bucket_size(n);
193 inline iterator
begin();
195 inline const_iterator
begin()
const;
199 inline iterator
end();
201 inline const_iterator
end()
const;
273 iterator
erase(
const iterator &it);
301#if HAVE_CXX_RVALUE_REFERENCES
312 SizedHashAllocator<
sizeof(elt)> _alloc;
319 template <
typename K,
typename V>
friend class HashTable;
335 return &_rep.get()->v;
343 return &_rep.get()->v;
349 return _rep.get()->v;
355 return _rep.get()->hashkey();
365 inline operator unspecified_bool_type()
const {
449template <
typename K,
typename V>
457 const Pair<K, V> *
get()
const {
459 return &_rep.get()->v;
467 return &_rep.get()->v;
473 return _rep.get()->v;
479 const K &
key()
const {
486 const V &value()
const {
497 inline operator unspecified_bool_type()
const {
526template <
typename K,
typename V>
536 Pair<K, V> *
get()
const {
569template <
typename K,
typename V>
597 : _rep(), _default_value() {
602 : _rep(), _default_value(d) {
609 : _rep(n), _default_value(d) {
614 : _rep(x._rep), _default_value(x._default_value) {
617#if HAVE_CXX_RVALUE_REFERENCES
620 : _rep(), _default_value() {
642 return _rep.bucket_count();
648 return _rep.bucket_size(n);
656 return _default_value;
670 inline const_iterator
begin()
const {
680 inline const_iterator
end()
const {
687 return _rep.count(key);
694 return _rep.find(key);
698 return _rep.find(key);
706 return _rep.find_prefer(key);
715 if (const_iterator i =
find(key))
718 return _default_value;
726 if (iterator i =
find(key))
733 if (const_iterator i =
find(key))
748 if (const_iterator i =
find(key))
751 return _default_value;
762#if CLICK_HASHMAP_UPGRADE_WARNINGS
779 return _rep.find_insert(
value_type(key, _default_value));
793 return _rep.find_insert(
value_type(key, value));
822 iterator
erase(
const iterator &it) {
823 return _rep.erase(it);
830 return _rep.erase(key);
843 click_swap(x._default_value, _default_value);
859 _default_value = x._default_value;
863#if HAVE_CXX_RVALUE_REFERENCES
883 for (
typename rep_type::iterator it = _rep.begin(); it; ) {
884 elt *e = _rep.erase(it);
886 _alloc.deallocate(e);
893 return const_iterator(_rep.begin());
899 return iterator(_rep.begin());
905 return const_iterator(_rep.end());
911 return iterator(_rep.end());
917 return _rep.contains(key);
941 typename rep_type::iterator i = _rep.find(key);
943 if (elt *e =
reinterpret_cast<elt *
>(_alloc.allocate())) {
944 new(
reinterpret_cast<void *
>(&e->v)) T(key);
945 _rep.set(i, e,
true);
954 return *find_insert(key);
960 typename rep_type::iterator i = _rep.find(hashkey(v));
962 if (elt *e =
reinterpret_cast<elt *
>(_alloc.allocate())) {
963 new(
reinterpret_cast<void *
>(&e->v)) T(v);
964 _rep.set(i, e,
true);
972 typename rep_type::iterator i = _rep.find(hashkey(value));
975 else if (elt *e =
reinterpret_cast<elt *
>(_alloc.allocate())) {
976 new(
reinterpret_cast<void *
>(&e->v)) T(value);
977 _rep.set(i, e,
true);
983template <
typename K,
typename V>
986 typename rep_type::rep_type::iterator i = _rep._rep.find(key);
989 else if (
typename rep_type::elt *e =
reinterpret_cast<typename rep_type::elt *
>(_rep._alloc.allocate())) {
990 new(
reinterpret_cast<void *
>(&e->v))
value_type(key, value);
991 _rep._rep.set(i, e,
true);
1001 if (elt *e = _rep.erase(
reinterpret_cast<typename rep_type::iterator &
>(itx._rep))) {
1003 _alloc.deallocate(e);
1008template <
typename T>
1011 if (elt *e = _rep.erase(key)) {
1013 _alloc.deallocate(e);
1019template <
typename T>
1023 bucket_count_type b = (bucket_count_type) -1;
1024 typename rep_type::iterator j = _rep.
end();
1025 for (
typename rep_type::const_iterator i = o._rep.begin(); i; ++i) {
1026 if (b != i.bucket())
1027 j = _rep.begin((b = i.bucket()));
1028 if (elt *e =
reinterpret_cast<elt *
>(_alloc.allocate())) {
1029 new(
reinterpret_cast<void *
>(&e->v)) T(i->v);
1030 _rep.insert_at(j, e);
1035template <
typename T>
1038 for (
typename rep_type::const_iterator i = o._rep.begin(); i; ++i)
1042template <
typename T>
1047 if (_rep.bucket_count() < o._rep.bucket_count())
1048 _rep.rehash(o._rep.bucket_count());
1054template <
typename T>
1057 for (
typename rep_type::iterator it = _rep.begin(); it; ) {
1058 elt *e = _rep.erase(it);
1060 _alloc.deallocate(e);
1064template <
typename T>
1068 _alloc.swap(o._alloc);
1071template <
typename K,
typename V>
1074 return find_insert(key).value();
1077template <
typename K,
typename V>
1080 return set(key, value);
1085template <
typename T>
1088 return a.
get() == b.
get();
1092template <
typename T>
1095 return a.
get() != b.
get();
1099template <
typename K,
typename V>
1105template <
typename K,
typename V>
1111template <
typename K,
typename V>
Intrusive hash table template.
Definition hashcontainer.hh:85
The const_iterator type for HashTable.
Definition hashtable.hh:326
void operator++()
Advance this iterator to the next element.
Definition hashtable.hh:375
const T & operator*() const
Return a reference to the element.
Definition hashtable.hh:348
HashTable_const_iterator()
Construct an uninitialized iterator.
Definition hashtable.hh:329
const T * get() const
Return a pointer to the element, null if *this == end().
Definition hashtable.hh:333
HashTable< T >::key_const_reference key() const
Return this element's key.
Definition hashtable.hh:354
bool live() const
Return true iff *this != end().
Definition hashtable.hh:359
void operator++(int)
Advance this iterator to the next element.
Definition hashtable.hh:370
const T * operator->() const
Return a pointer to the element.
Definition hashtable.hh:342
The iterator type for HashTable.
Definition hashtable.hh:412
T & operator*() const
Return a reference to the element.
Definition hashtable.hh:433
HashTable_iterator()
Construct an uninitialized iterator.
Definition hashtable.hh:417
T * operator->() const
Return a pointer to the element.
Definition hashtable.hh:427
T * get() const
Return a pointer to the element, null if *this == end().
Definition hashtable.hh:421
Hash table template.
Definition hashtable.hh:570
const mapped_type & operator[](key_const_reference key) const
Return the value for key.
Definition hashtable.hh:747
Pair< const K, V > value_type
Pair of key type and value type.
Definition hashtable.hh:586
bool set(key_const_reference key, const mapped_type &value)
Set the mapping for key to value.
Definition hashtable.hh:984
void clear()
Remove all elements.
Definition hashtable.hh:835
void rehash(bucket_count_type nb)
Rehash the table, ensuring it contains at least n buckets.
Definition hashtable.hh:851
~HashTable()
Destroy this hash table, freeing its memory.
Definition hashtable.hh:626
size_type erase(key_const_reference key)
Remove any element with key.
Definition hashtable.hh:829
iterator erase(const iterator &it)
Remove the element indicated by it.
Definition hashtable.hh:822
HashTable< K, V > & operator=(const HashTable< K, V > &x)
Assign this hash table's contents to a copy of x.
Definition hashtable.hh:857
K key_type
Key type.
Definition hashtable.hh:577
HashTable(const HashTable< K, V > &x)
Construct a hash table as a copy of x.
Definition hashtable.hh:613
HashTable(const mapped_type &d)
Construct an empty hash table with default value d.
Definition hashtable.hh:601
size_type size() const
Return the number of elements in the hash table.
Definition hashtable.hh:631
bucket_count_type bucket_count() const
Return the number of buckets in the hash table.
Definition hashtable.hh:641
const mapped_type * get_pointer(key_const_reference key) const
Definition hashtable.hh:732
size_type count(key_const_reference key) const
Return 1 if an element with key key exists, 0 otherwise.
Definition hashtable.hh:686
mapped_type * get_pointer(key_const_reference key)
Return a pointer to the value for key.
Definition hashtable.hh:725
const mapped_type & get(key_const_reference key) const
Return the value for key.
Definition hashtable.hh:714
rep_type::bucket_count_type bucket_count_type
Type of bucket sizes.
Definition hashtable.hh:592
iterator find_insert(key_const_reference key)
Ensure an element with key key and return its iterator.
Definition hashtable.hh:778
iterator find_prefer(key_const_reference key)
Return an iterator for the element with key key, if any.
Definition hashtable.hh:705
HashTable(const mapped_type &d, bucket_count_type n)
Construct an empty hash table with at least n buckets.
Definition hashtable.hh:608
rep_type::size_type size_type
Type of sizes.
Definition hashtable.hh:589
bool empty() const
Return true iff size() == 0.
Definition hashtable.hh:636
const_iterator begin() const
Definition hashtable.hh:670
bool replace(key_const_reference key, const mapped_type &value)
Set the mapping for key to value.
Definition hashtable.hh:1078
iterator begin()
Return an iterator for the first element in the table.
Definition hashtable.hh:666
size_type bucket_size(bucket_count_type n) const
Return the number of elements in bucket n.
Definition hashtable.hh:647
const_iterator find(key_const_reference key) const
Return an iterator for the element with key key, if any.
Definition hashtable.hh:693
iterator end()
Return an iterator for the end of the table.
Definition hashtable.hh:676
const K & key_const_reference
Const reference to key type.
Definition hashtable.hh:580
iterator find(key_const_reference key)
Definition hashtable.hh:697
const_iterator end() const
Definition hashtable.hh:680
iterator find_insert(key_const_reference key, const mapped_type &value)
Ensure an element for key key and return its iterator.
Definition hashtable.hh:791
HashTable()
Construct an empty hash table with normal default value.
Definition hashtable.hh:596
void swap(HashTable< K, V > &x)
Swap the contents of this hash table and x.
Definition hashtable.hh:841
V mapped_type
Value type.
Definition hashtable.hh:583
const mapped_type & default_value() const
Return the hash table's default value.
Definition hashtable.hh:655
mapped_type & operator[](key_const_reference key)
Return a reference to the value for key.
Definition hashtable.hh:1072
bool operator==(const HashTable_const_iterator< T > &a, const HashTable_const_iterator< T > &b)
Compare two HashTable iterators for equality.
Definition hashtable.hh:1086
bool operator!=(const HashTable_const_iterator< T > &a, const HashTable_const_iterator< T > &b)
Compare two HashTable iterators for inequality.
Definition hashtable.hh:1093