|
Click
|
Intrusive hash table template. More...
#include <hashcontainer.hh>
Public Types | |
| enum | { max_bucket_count = 4194303 , initial_bucket_count = 63 } |
| typedef A::key_type | key_type |
| Key type. | |
| typedef T | value_type |
| Value type. | |
| typedef size_t | size_type |
| Type of sizes. | |
| typedef uint32_t | bucket_count_type |
| Type of bucket counts. | |
| typedef HashContainer_const_iterator< T, A > | const_iterator |
| typedef HashContainer_iterator< T, A > | iterator |
Public Member Functions | |
| HashContainer () | |
| Construct an empty HashContainer. | |
| HashContainer (bucket_count_type n) | |
| Construct an empty HashContainer with at least n buckets. | |
| ~HashContainer () | |
| Destroy the HashContainer. | |
| size_type | size () const |
| Return the number of elements stored. | |
| bool | empty () const |
| Return true iff size() == 0. | |
| bucket_count_type | bucket_count () const |
| Return the number of buckets. | |
| size_type | bucket_size (bucket_count_type n) const |
| Return the number of elements in bucket n. | |
| bucket_count_type | bucket (const key_type &key) const |
| Return the bucket number containing elements with key. | |
| bool | unbalanced () const |
| Return true if this HashContainer should be rebalanced. | |
| iterator | begin () |
| Return an iterator for the first element in the container. | |
| const_iterator | begin () const |
| iterator | end () |
| Return an iterator for the end of the container. | |
| const_iterator | end () const |
| iterator | begin (bucket_count_type n) |
| Return an iterator for the first element in bucket n. | |
| const_iterator | begin (bucket_count_type n) const |
| bool | contains (const key_type &key) const |
| Test if an element with key key exists in the table. | |
| size_type | count (const key_type &key) const |
| Return the number of elements with key key in the table. | |
| iterator | find (const key_type &key) |
| Return an iterator for an element with key, if any. | |
| const_iterator | find (const key_type &key) const |
| iterator | find_prefer (const key_type &key) |
| Return an iterator for an element with key key, if any. | |
| T * | get (const key_type &key) const |
| Return an element for key, if any. | |
| void | insert_at (iterator &it, T *element) |
| Insert an element at position it. | |
| T * | set (iterator &it, T *element, bool balance=false) |
| Replace the element at position it with element. | |
| T * | set (T *element) |
| Replace the element with element->hashkey() with element. | |
| T * | erase (iterator &it) |
| Remove the element at position it. | |
| T * | erase (const key_type &key) |
| Remove an element with hashkey key. | |
| void | clear () |
| Removes all elements from the container. | |
| void | swap (HashContainer< T, A > &x) |
| Swaps the contents of *this and x. | |
| void | rehash (bucket_count_type n) |
| Rehash the table, ensuring it contains at least n buckets. | |
| void | balance () |
| Rehash the table if it is unbalanced. | |
Intrusive hash table template.
The HashContainer template implements a hash table or associative array suitable for use in the kernel or at user level.
HashContainer is intrusive. This means it does not manage its contents' memory. While non-intrusive containers are more common in the STL, intrusive containers make it simple to store objects in more than one container at a time.
Unlike many hash tables HashContainer does not automatically grow itself to maintain good lookup performance. Its users are expected to call rehash() when appropriate. See unbalanced().
With the default adapter type (A), the template type T must:
These requirements can be changed by supplying a different A, or adapter, type.
HashContainer can store multiple elements with the same key, although this is not the normal use. An element stored in a HashContainer should not modify its key.
HashContainer is used to implement Click's HashTable template.
| typedef A::key_type HashContainer< T, A >::key_type |
Key type.
| typedef T HashContainer< T, A >::value_type |
Value type.
Must meet the HashContainer requirements defined by type A.
| typedef size_t HashContainer< T, A >::size_type |
Type of sizes.
| typedef uint32_t HashContainer< T, A >::bucket_count_type |
Type of bucket counts.
| HashContainer< T, A >::HashContainer | ( | ) |
Construct an empty HashContainer.
|
explicit |
Construct an empty HashContainer with at least n buckets.
| HashContainer< T, A >::~HashContainer | ( | ) |
Destroy the HashContainer.
|
inline |
Return the number of elements stored.
|
inline |
Return true iff size() == 0.
|
inline |
Return the number of buckets.
|
inline |
Return the number of elements in bucket n.
|
inline |
Return the bucket number containing elements with key.
|
inline |
Return true if this HashContainer should be rebalanced.
|
inline |
Return an iterator for the first element in the container.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Return an iterator for the end of the container.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Return an iterator for the first element in bucket n.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Test if an element with key key exists in the table.
|
inline |
Return the number of elements with key key in the table.
|
inline |
Return an iterator for an element with key, if any.
If no element with key exists in the table, find() returns an iterator that compares equal to end(). However, this iterator is special, and can also be used to efficiently insert an element with key key. In particular, the return value of find() always has can_insert(), and can thus be passed to insert_at() or set(). (It will insert elements at the head of the relevant bucket.)
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Return an iterator for an element with key key, if any.
Like find(), but additionally moves any found element to the head of its bucket, possibly speeding up future lookups.
|
inline |
Return an element for key, if any.
Returns null if no element for key currently exists. Equivalent to find(key).get().
|
inline |
Insert an element at position it.
| it | iterator |
| element | element |
Inserts element at the position in the hash table indicated by it. For instance, if it == begin(n) for some bucket number n, then element becomes the first element in bucket n. Other elements in the bucket, if any, are chained along.
On return, it is updated to point immediately after element. If it was not live before, then it will not be live after.
| T * HashContainer< T, A >::set | ( | iterator & | it, |
| T * | element, | ||
| bool | balance = false |
||
| ) |
Replace the element at position it with element.
| it | iterator |
| element | element (can be null) |
| balance | whether to balance the hash table |
Replaces the element pointed to by it with element, and returns the former element. If element is null the former element is removed. If there is no former element then element is inserted. When inserting an element with balance true, set() may rebalance the hash table.
As a side effect, it is advanced to point at the newly inserted element. If element is null, then it is advanced to point at the next element as by ++it.
|
inline |
Replace the element with element->hashkey() with element.
| element | element |
Finds an element with the same hashkey as element, removes it from the HashContainer, and replaces it with element. If there is no former element then element is inserted.
|
inline |
Remove the element at position it.
| it | iterator |
As a side effect, it is advanced to the next element as by ++it.
|
inline |
Remove an element with hashkey key.
Roughly equivalent to erase(find(key)).
|
inline |
Removes all elements from the container.
|
inline |
Swaps the contents of *this and x.
| void HashContainer< T, A >::rehash | ( | bucket_count_type | n | ) |
Rehash the table, ensuring it contains at least n buckets.
If n < bucket_count(), this function may make the hash table slower.
|
inline |
Rehash the table if it is unbalanced.