Click
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
HashTable< K, V > Class Template Reference

Hash table template. More...

#include <hashtable.hh>

Public Types

typedef K key_type
 Key type.
 
typedef const K & key_const_reference
 Const reference to key type.
 
typedef V mapped_type
 Value type.
 
typedef Pair< const K, V > value_type
 Pair of key type and value type.
 
typedef rep_type::size_type size_type
 Type of sizes.
 
typedef rep_type::bucket_count_type bucket_count_type
 Type of bucket sizes.
 
typedef HashTable_const_iterator< value_type > const_iterator
 
typedef HashTable_iterator< value_type > iterator
 

Public Member Functions

 HashTable ()
 Construct an empty hash table with normal default value.
 
 HashTable (const mapped_type &d)
 Construct an empty hash table with default value d.
 
 HashTable (const mapped_type &d, bucket_count_type n)
 Construct an empty hash table with at least n buckets.
 
 HashTable (const HashTable< K, V > &x)
 Construct a hash table as a copy of x.
 
 ~HashTable ()
 Destroy this hash table, freeing its memory.
 
size_type size () const
 Return the number of elements in the hash table.
 
bool empty () const
 Return true iff size() == 0.
 
bucket_count_type bucket_count () const
 Return the number of buckets in the hash table.
 
size_type bucket_size (bucket_count_type n) const
 Return the number of elements in bucket n.
 
const mapped_type & default_value () const
 Return the hash table's default value.
 
iterator begin ()
 Return an iterator for the first element in the table.
 
const_iterator begin () const
 
iterator end ()
 Return an iterator for the end of the table.
 
const_iterator end () const
 
size_type count (key_const_reference key) const
 Return 1 if an element with key key exists, 0 otherwise.
 
const_iterator find (key_const_reference key) const
 Return an iterator for the element with key key, if any.
 
iterator find (key_const_reference key)
 
iterator find_prefer (key_const_reference key)
 Return an iterator for the element with key key, if any.
 
const mapped_type & get (key_const_reference key) const
 Return the value for key.
 
mapped_type * get_pointer (key_const_reference key)
 Return a pointer to the value for key.
 
const mapped_type * get_pointer (key_const_reference key) const
 
const mapped_type & operator[] (key_const_reference key) const
 Return the value for key.
 
mapped_type & operator[] (key_const_reference key)
 Return a reference to the value for key.
 
iterator find_insert (key_const_reference key)
 Ensure an element with key key and return its iterator.
 
iterator find_insert (key_const_reference key, const mapped_type &value)
 Ensure an element for key key and return its iterator.
 
bool set (key_const_reference key, const mapped_type &value)
 Set the mapping for key to value.
 
bool replace (key_const_reference key, const mapped_type &value)
 Set the mapping for key to value.
 
iterator erase (const iterator &it)
 Remove the element indicated by it.
 
size_type erase (key_const_reference key)
 Remove any element with key.
 
void clear ()
 Remove all elements.
 
void swap (HashTable< K, V > &x)
 Swap the contents of this hash table and x.
 
void rehash (bucket_count_type nb)
 Rehash the table, ensuring it contains at least n buckets.
 
HashTable< K, V > & operator= (const HashTable< K, V > &x)
 Assign this hash table's contents to a copy of x.
 

Detailed Description

template<typename K, typename V>
class HashTable< K, V >

Hash table template.

The HashTable template implements a hash table or associative array suitable for use in the kernel or at user level. Its interface is similar to C++'s std::map and std::unordered_map, although those types have more methods.

Used with two template parameters, as HashTable<K, V>, the table maps keys K to values V. Used with one template parameter, as HashTable<T>, HashTable is a hash set. The type T must declare types named key_type and key_const_reference. It also must declare a hashkey() member function returning type key_const_reference. An object's hashkey() defines its key.

HashTable is a chained hash table. (Open coding is not appropriate in the kernel, where large contiguous memory allocations are essentially impossible.) When run through Google's sparse_hash_table tests (April 2008, sparsehash-1.1), HashTable appears to perform slightly better than g++'s hash_map, better than sparse_hash_map, and worse than dense_hash_map; it takes less memory than hash_map and dense_hash_map.

HashTable is faster than Click's prior HashMap class and has fewer potential race conditions in multithreaded use. HashMap remains for backward compatibility but should not be used in new code.

Warning
HashMap users should beware of HashTable's operator[]. HashMap's operator[] returned a non-assignable const reference; it would never add an element to the hash table. In contrast, the HashTable operator[] may add an element to the table. (If added, the element will initially have the table's default value.) For instance, consider:
HashMap<String, int> h(0);
h.insert("A", 1);
if (!h["B"]) // Nota bene
printf("B wasn't in table, and still isn't\n");
for (HashMap<String, int>::iterator it = h.begin(); it; ++it)
printf("%s -> %d\n", it.key().c_str(), it.value());
// Prints B wasn't in table, and still isn't
// A -> 1
Here the h["B"] reference does not add an element to the hash table, as you can see from the iteration. Similar HashTable code has a different result:
h["A"] = 1;
if (!h["B"]) // Nota bene
printf("B wasn't in table, but it is now\n");
for (HashMap<String, int>::iterator it = h.begin(); it; ++it)
printf("%s -> %d\n", it.key().c_str(), it.value());
// Prints B wasn't in table, but it is now
// A -> 1
// B -> 0
Hash table template.
Definition hashtable.hh:570
If you don't want operator[] to add an element, either access operator[] through a const hash table, or use get():
if (!h.get("B"))
printf("B wasn't in table, and still isn't\n");
const HashTable<String, int> &const_h = h;
if (!const_h["B"])
printf("B wasn't in table, and still isn't\n");

Member Typedef Documentation

◆ key_type

template<typename K , typename V >
typedef K HashTable< K, V >::key_type

Key type.

◆ key_const_reference

template<typename K , typename V >
typedef const K& HashTable< K, V >::key_const_reference

Const reference to key type.

◆ mapped_type

template<typename K , typename V >
typedef V HashTable< K, V >::mapped_type

Value type.

◆ value_type

template<typename K , typename V >
typedef Pair<const K, V> HashTable< K, V >::value_type

Pair of key type and value type.

◆ size_type

template<typename K , typename V >
typedef rep_type::size_type HashTable< K, V >::size_type

Type of sizes.

◆ bucket_count_type

template<typename K , typename V >
typedef rep_type::bucket_count_type HashTable< K, V >::bucket_count_type

Type of bucket sizes.

Constructor & Destructor Documentation

◆ HashTable() [1/4]

template<typename K , typename V >
HashTable< K, V >::HashTable ( )
inline

Construct an empty hash table with normal default value.

◆ HashTable() [2/4]

template<typename K , typename V >
HashTable< K, V >::HashTable ( const mapped_type &  d)
inlineexplicit

Construct an empty hash table with default value d.

◆ HashTable() [3/4]

template<typename K , typename V >
HashTable< K, V >::HashTable ( const mapped_type &  d,
bucket_count_type  n 
)
inline

Construct an empty hash table with at least n buckets.

Parameters
ddefault value
nminimum number of buckets

◆ HashTable() [4/4]

template<typename K , typename V >
HashTable< K, V >::HashTable ( const HashTable< K, V > &  x)
inline

Construct a hash table as a copy of x.

◆ ~HashTable()

template<typename K , typename V >
HashTable< K, V >::~HashTable ( )
inline

Destroy this hash table, freeing its memory.

Member Function Documentation

◆ size()

template<typename K , typename V >
size_type HashTable< K, V >::size ( ) const
inline

Return the number of elements in the hash table.

◆ empty()

template<typename K , typename V >
bool HashTable< K, V >::empty ( ) const
inline

Return true iff size() == 0.

◆ bucket_count()

template<typename K , typename V >
bucket_count_type HashTable< K, V >::bucket_count ( ) const
inline

Return the number of buckets in the hash table.

◆ bucket_size()

template<typename K , typename V >
size_type HashTable< K, V >::bucket_size ( bucket_count_type  n) const
inline

Return the number of elements in bucket n.

Parameters
nbucket number, >= 0 and < bucket_count()

◆ default_value()

template<typename K , typename V >
const mapped_type & HashTable< K, V >::default_value ( ) const
inline

Return the hash table's default value.

The default value is returned by operator[]() when a key does not exist.

◆ begin() [1/2]

template<typename K , typename V >
iterator HashTable< K, V >::begin ( )
inline

Return an iterator for the first element in the table.

Note
HashTable iterators return elements in undefined order.

◆ begin() [2/2]

template<typename K , typename V >
const_iterator HashTable< K, V >::begin ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ end() [1/2]

template<typename K , typename V >
iterator HashTable< K, V >::end ( )
inline

Return an iterator for the end of the table.

Invariant
end().live() == false

◆ end() [2/2]

template<typename K , typename V >
const_iterator HashTable< K, V >::end ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ count()

template<typename K , typename V >
size_type HashTable< K, V >::count ( key_const_reference  key) const
inline

Return 1 if an element with key key exists, 0 otherwise.

◆ find() [1/2]

template<typename K , typename V >
const_iterator HashTable< K, V >::find ( key_const_reference  key) const
inline

Return an iterator for the element with key key, if any.

Returns end() if no such element exists.

◆ find() [2/2]

template<typename K , typename V >
iterator HashTable< K, V >::find ( key_const_reference  key)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ find_prefer()

template<typename K , typename V >
iterator HashTable< K, V >::find_prefer ( key_const_reference  key)
inline

Return an iterator for the element with key key, if any.

Like find(), but additionally moves the found element to the head of its bucket, possibly speeding up future lookups.

◆ get()

template<typename K , typename V >
const mapped_type & HashTable< K, V >::get ( key_const_reference  key) const
inline

Return the value for key.

If no element for key currently exists (find(key) == end()), returns default_value().

◆ get_pointer() [1/2]

template<typename K , typename V >
mapped_type * HashTable< K, V >::get_pointer ( key_const_reference  key)
inline

Return a pointer to the value for key.

If no element for key currently exists (find(key) == end()), returns null.

◆ get_pointer() [2/2]

template<typename K , typename V >
const mapped_type * HashTable< K, V >::get_pointer ( key_const_reference  key) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ operator[]() [1/2]

template<typename K , typename V >
const mapped_type & HashTable< K, V >::operator[] ( key_const_reference  key) const
inline

Return the value for key.

If no element for key currently exists (find(key) == end()), returns default_value().

Warning
The overloaded operator[] on non-const hash tables may add an element to the table. If you don't want to add an element, either access operator[] through a const hash table, or use get().

◆ operator[]() [2/2]

template<typename K , typename V >
HashTable< K, V >::mapped_type & HashTable< K, V >::operator[] ( key_const_reference  key)
inline

Return a reference to the value for key.

The caller can assign the reference to change the value. If no element for key currently exists (find(key) == end()), adds a new element with default_value() and returns a reference to that value.

Note
Inserting an element into a HashTable invalidates all existing iterators.

◆ find_insert() [1/2]

template<typename K , typename V >
iterator HashTable< K, V >::find_insert ( key_const_reference  key)
inline

Ensure an element with key key and return its iterator.

If an element with key already exists in the table, then find(key) and find_insert(key) are equivalent. Otherwise, find_insert adds a new element with key key and value default_value() to the table and returns its iterator.

Note
Inserting an element into a HashTable invalidates all existing iterators.

◆ find_insert() [2/2]

template<typename K , typename V >
iterator HashTable< K, V >::find_insert ( key_const_reference  key,
const mapped_type &  value 
)
inline

Ensure an element for key key and return its iterator.

If an element with key already exists in the table, then find(key) and find_insert(value) are equivalent. Otherwise, find_insert(key, value) adds a new element with key key and value value to the table and returns its iterator.

Note
Inserting an element into a HashTable invalidates all existing iterators.

◆ set()

template<typename K , typename V >
bool HashTable< K, V >::set ( key_const_reference  key,
const mapped_type &  value 
)

Set the mapping for key to value.

If an element for key already exists in the table, then its value is assigned to value and the function returns false. Otherwise, a new element mapping key to value is added and the function returns true.

The behavior is basically the same as "(*this)[@a key] = @a value". (The difference is that if (*this)[key] is not already in the hash table, the new value is constructed rather than assigned.)

Note
Inserting an element into a HashTable invalidates all existing iterators.

◆ replace()

template<typename K , typename V >
bool HashTable< K, V >::replace ( key_const_reference  key,
const mapped_type &  value 
)

Set the mapping for key to value.

This is a deprecated synonym for set().

Deprecated:
Use set().

◆ erase() [1/2]

template<typename K , typename V >
iterator HashTable< K, V >::erase ( const iterator &  it)
inline

Remove the element indicated by it.

Returns
A valid iterator pointing at the next element remaining, or end() if no such element exists.

◆ erase() [2/2]

template<typename K , typename V >
size_type HashTable< K, V >::erase ( key_const_reference  key)
inline

Remove any element with key.

Returns the number of elements removed, which is always 0 or 1.

◆ clear()

template<typename K , typename V >
void HashTable< K, V >::clear ( )
inline

Remove all elements.

Postcondition
size() == 0

◆ swap()

template<typename K , typename V >
void HashTable< K, V >::swap ( HashTable< K, V > &  x)
inline

Swap the contents of this hash table and x.

◆ rehash()

template<typename K , typename V >
void HashTable< K, V >::rehash ( bucket_count_type  nb)
inline

Rehash the table, ensuring it contains at least n buckets.

All existing iterators are invalidated. If n < bucket_count(), this function may make the hash table slower.

◆ operator=()

template<typename K , typename V >
HashTable< K, V > & HashTable< K, V >::operator= ( const HashTable< K, V > &  x)
inline

Assign this hash table's contents to a copy of x.


The documentation for this class was generated from the following file: