A hash map usable by ATS core. More...
#include <Map.h>
Data Structures | |
struct | Bucket |
Hash bucket. More... | |
struct | iterator |
Standard iterator for walking the table. More... | |
struct | Location |
Information about locating a value in the hash table. More... | |
Public Types | |
enum | ExpansionPolicy { MANUAL, AVERAGE, MAXIMUM } |
When the hash table is expanded. More... | |
typedef TSHashTable | self |
Self reference type. | |
typedef H | Hasher |
Rename and promote. | |
typedef Hasher::ID | ID |
ID type. | |
typedef Hasher::Key | Key |
Key type. | |
typedef Hasher::Value | Value |
Stored value (element) type. | |
typedef Hasher::ListHead | ListHead |
Anchor for chain. | |
Public Member Functions | |
iterator | begin () |
First element. | |
iterator | end () |
Past last element. | |
TSHashTable (size_t nb=DEFAULT_BUCKET_COUNT) | |
Constructor (also default). | |
void | insert (Value *value) |
Insert a value in to the table. | |
Location | find (Key key) |
Find a value that matches key. | |
Location | find (Value *value) |
Get a Location for value. | |
bool | remove (Location const &location) |
Remove the value at location from the table. | |
bool | remove (Key key) |
Remove all values with key. | |
void | clear () |
Remove all values from the table. | |
size_t | count () const |
Get the number of elements in the table. | |
size_t | bucketCount () const |
Get the number of buckets in the table. | |
void | setExpansionPolicy (ExpansionPolicy p) |
Enable or disable expanding the table when chains are long. | |
void | expansionPolicy () const |
Get the current expansion policy. | |
void | setExpansionLimit (size_t n) |
Set the limit value for the expansion policy. | |
size_t | expansionLimit () const |
Set the limit value for the expansion policy. | |
void | expand () |
Expand the hash. | |
Static Public Attributes | |
static size_t const | DEFAULT_BUCKET_COUNT = 7 |
The default starting number of buckets. | |
static size_t const | DEFAULT_EXPANSION_LIMIT = 4 |
The default expansion policy limit. | |
Protected Types | |
typedef Vec< Bucket, DefaultAlloc, 0 > | Array |
Bucket array. | |
typedef DLL< Bucket, typename Bucket::Link_m_link > | BucketChain |
Make available to nested classes statically. | |
Protected Member Functions | |
void | findBucket (Key key, Location &location) |
Get the ID and bucket for key. | |
Protected Attributes | |
size_t | m_count |
# of elements stored in the table. | |
ExpansionPolicy | m_expansion_policy |
When to exand the table. | |
size_t | m_expansion_limit |
Limit value for expansion. | |
Array | m_array |
Bucket storage. | |
BucketChain | m_bucket_chain |
List of non-empty buckets. |
A hash map usable by ATS core.
This class depends on the DLL
class from List.h
. It assumes it can uses instances of that class to store chains of elements.
Values stored in this container are not destroyed when the container is destroyed. These must be released by the client.
Duplicate keys are allowed. Clients must walk the list for multiple entries.
Location::operator++()
By default the table automatically expands to limit the average chain length. This can be tuned. If set to MANUAL
then the table will expand only when explicitly requested to do so by the client.
All the parameters for the hash map are passed via the template argument H. This is a struct that contains both type definitions and static methods. It must have
ID is a typedef
for the hash type. This is the type of the value produced by the hash function. It must be a numeric type.
Key is a typedef
for the key type. This is passed to the hash function and used for equality checking of elements. It is presumed cheap to copy. If the underlying key is not a simple type then Key should be declared as a constant pointer or a constant reference. The hash table will never attempt to modify a key.
Value is a typedef
for the value type, the type of the element stored in the hash table.
ListHead is typedef
for the DLL
compatible class that can serve as the anchor for a chain of Value instances. This is use both as data to be stored in a bucket and for access to next and previous pointers from instances of Value.
Method hash
converts a Key
to a hash value. The key argument can be by value or by constant reference.
Method key
extracts the key from a Value
instance.
Method equal
checks for equality between a Key
and a Value
. The key argument can be a constant reference or by value. The arguments should be const
if not by value.
Example for HttpServerSession
keyed by the origin server IP address.
struct Hasher { typedef uint32_t ID; typedef sockaddr const* Key; typedef HttpServerSession Value; typedef DList(HttpServerSession, ip_hash_link) ListHead; static uint32_t hash(sockaddr const* key) { return ats_ip_hash(key); } static sockaddr const* key(HttpServerSession const* value) { return &value->ip.sa } static bool equal(sockaddr const* lhs, sockaddr const* rhs) { return ats_ip_eq(lhs, rhs); } // Alternatively // static ID hash(Key* key); // static Key key(Value* value); // static bool equal(Key lhs, Key rhs);
In HttpServerSession
is the definition
LINK(HttpServerSession, ip_hash_link);
which creates the internal links used by TSHashTable
.
Definition at line 1030 of file Map.h.
typedef Vec<Bucket, DefaultAlloc, 0> TSHashTable< H >::Array [protected] |
typedef DLL<Bucket, typename Bucket::Link_m_link> TSHashTable< H >::BucketChain [protected] |
typedef H TSHashTable< H >::Hasher |
typedef Hasher::ID TSHashTable< H >::ID |
typedef Hasher::Key TSHashTable< H >::Key |
typedef Hasher::ListHead TSHashTable< H >::ListHead |
typedef TSHashTable TSHashTable< H >::self |
typedef Hasher::Value TSHashTable< H >::Value |
enum TSHashTable::ExpansionPolicy |
TSHashTable< H >::TSHashTable | ( | size_t | nb = DEFAULT_BUCKET_COUNT |
) |
Constructor (also default).
Constructs an empty table with at least nb buckets.
Definition at line 1282 of file Map.h.
References Vec< C, A, S >::i, TSHashTable< H >::m_array, Vec< C, A, S >::n, prime2, and Vec< C, A, S >::set_expand().
TSHashTable< H >::iterator TSHashTable< H >::begin | ( | ) |
First element.
Definition at line 1255 of file Map.h.
References TSHashTable< H >::end(), DLL< C, L >::head, TSHashTable< H >::m_bucket_chain, and TSHashTable< H >::Bucket::m_chain.
Referenced by ServerSessionPool::purge().
size_t TSHashTable< H >::bucketCount | ( | ) | const [inline] |
void TSHashTable< H >::clear | ( | void | ) |
Remove all values from the table.
The values are not cleaned up. The values are not touched in this method, therefore it is safe to destroy them first and then clear
this table.
Definition at line 1385 of file Map.h.
References DLL< C, L >::clear(), TSHashTable< H >::m_array, TSHashTable< H >::m_bucket_chain, TSHashTable< H >::m_count, and Vec< C, A, S >::n.
Referenced by ServerSessionPool::purge().
size_t TSHashTable< H >::count | ( | ) | const [inline] |
TSHashTable< H >::iterator TSHashTable< H >::end | ( | ) |
Past last element.
Definition at line 1265 of file Map.h.
Referenced by TSHashTable< H >::begin(), and ServerSessionPool::purge().
void TSHashTable< H >::expand | ( | ) |
Expand the hash.
Useful primarily when the expansion policy is set to MANUAL
.
Definition at line 1397 of file Map.h.
References DLL< C, L >::clear(), DLL< C, L >::head, Vec< C, A, S >::i, TSHashTable< H >::insert(), TSHashTable< H >::m_array, TSHashTable< H >::m_bucket_chain, TSHashTable< H >::Bucket::m_chain, TSHashTable< H >::m_count, TSHashTable< H >::m_expansion_policy, Vec< C, A, S >::move(), Vec< C, A, S >::n, DLL< Bucket, typename Bucket::Link_m_link >::next(), and Vec< C, A, S >::set_expand().
Referenced by TSHashTable< H >::insert().
size_t TSHashTable< H >::expansionLimit | ( | ) | const [inline] |
void TSHashTable< H >::expansionPolicy | ( | ) | const [inline] |
TSHashTable< H >::Location TSHashTable< H >::find | ( | Key | key | ) |
Find a value that matches key.
Location
of the value. Use Location::isValid()
to check for success. Definition at line 1310 of file Map.h.
References TSHashTable< H >::findBucket(), TSHashTable< H >::Location::m_bucket, TSHashTable< H >::Bucket::m_chain, and TSHashTable< H >::Location::m_value.
Referenced by ServerSessionPool::acquireSession(), ServerSessionPool::eventHandler(), and TSHashTable< H >::remove().
TSHashTable< H >::Location TSHashTable< H >::find | ( | Value * | value | ) |
Get a Location
for value.
This is a bit obscure but needed in certain cases. It should only be used on a value that is already known to be in the table. It just does the bucket lookup and uses that and the value to construct a Location
that can be used with other methods. The m_distance value is not set in this case for performance reasons.
Definition at line 1324 of file Map.h.
References TSHashTable< H >::findBucket(), TSHashTable< H >::Location::m_bucket, TSHashTable< H >::Bucket::m_chain, and TSHashTable< H >::Location::m_value.
void TSHashTable< H >::findBucket | ( | Key | key, | |
Location & | location | |||
) | [protected] |
Get the ID and bucket for key.
Fills m_id and m_bucket in location from key.
Definition at line 1304 of file Map.h.
References TSHashTable< H >::m_array, TSHashTable< H >::Location::m_bucket, TSHashTable< H >::Location::m_id, and Vec< C, A, S >::n.
Referenced by TSHashTable< H >::find().
void TSHashTable< H >::insert | ( | Value * | value | ) |
Insert a value in to the table.
The value must NOT already be in a table of this type.
Definition at line 1333 of file Map.h.
References TSHashTable< H >::AVERAGE, TSHashTable< H >::expand(), ink_assert, TSHashTable< H >::m_array, TSHashTable< H >::m_bucket_chain, TSHashTable< H >::Bucket::m_chain, TSHashTable< H >::Bucket::m_count, TSHashTable< H >::m_count, TSHashTable< H >::m_expansion_limit, TSHashTable< H >::m_expansion_policy, TSHashTable< H >::Bucket::m_mixed_p, TSHashTable< H >::MAXIMUM, Vec< C, A, S >::n, and DLL< C, L >::push().
Referenced by TSHashTable< H >::expand(), and ServerSessionPool::releaseSession().
bool TSHashTable< H >::remove | ( | Key | key | ) |
Remove all values with key.
true
if any value was removed, false
otherwise. Definition at line 1373 of file Map.h.
References TSHashTable< H >::Location::advance(), TSHashTable< H >::find(), and TSHashTable< H >::Location::isValid().
bool TSHashTable< H >::remove | ( | Location const & | location | ) |
Remove the value at location from the table.
This method assumes a location is consistent. Be very careful if you modify a Location
.
true
if the value was removed, false
otherwise. Definition at line 1355 of file Map.h.
References ink_assert, TSHashTable< H >::Location::isValid(), TSHashTable< H >::Location::m_bucket, TSHashTable< H >::m_bucket_chain, TSHashTable< H >::Bucket::m_chain, TSHashTable< H >::m_count, TSHashTable< H >::Bucket::m_count, TSHashTable< H >::Bucket::m_mixed_p, TSHashTable< H >::Location::m_value, and DLL< C, L >::remove().
Referenced by ServerSessionPool::acquireSession(), and ServerSessionPool::eventHandler().
void TSHashTable< H >::setExpansionLimit | ( | size_t | n | ) | [inline] |
void TSHashTable< H >::setExpansionPolicy | ( | ExpansionPolicy | p | ) | [inline] |
Enable or disable expanding the table when chains are long.
Definition at line 1218 of file Map.h.
Referenced by ServerSessionPool::ServerSessionPool().
size_t const TSHashTable< H >::DEFAULT_BUCKET_COUNT = 7 [static] |
size_t const TSHashTable< H >::DEFAULT_EXPANSION_LIMIT = 4 [static] |
Array TSHashTable< H >::m_array [protected] |
Bucket storage.
Definition at line 1238 of file Map.h.
Referenced by TSHashTable< HostHashing >::bucketCount(), TSHashTable< H >::clear(), TSHashTable< H >::expand(), TSHashTable< H >::findBucket(), TSHashTable< H >::insert(), and TSHashTable< H >::TSHashTable().
BucketChain TSHashTable< H >::m_bucket_chain [protected] |
List of non-empty buckets.
Definition at line 1246 of file Map.h.
Referenced by TSHashTable< H >::begin(), TSHashTable< H >::clear(), TSHashTable< H >::expand(), TSHashTable< H >::insert(), and TSHashTable< H >::remove().
size_t TSHashTable< H >::m_count [protected] |
# of elements stored in the table.
Definition at line 1235 of file Map.h.
Referenced by TSHashTable< H >::clear(), TSHashTable< HostHashing >::count(), TSHashTable< H >::expand(), TSHashTable< H >::insert(), and TSHashTable< H >::remove().
size_t TSHashTable< H >::m_expansion_limit [protected] |
Limit value for expansion.
Definition at line 1237 of file Map.h.
Referenced by TSHashTable< HostHashing >::expansionLimit(), TSHashTable< H >::insert(), and TSHashTable< HostHashing >::setExpansionLimit().
ExpansionPolicy TSHashTable< H >::m_expansion_policy [protected] |
When to exand the table.
Definition at line 1236 of file Map.h.
Referenced by TSHashTable< H >::expand(), TSHashTable< HostHashing >::expansionPolicy(), TSHashTable< H >::insert(), and TSHashTable< HostHashing >::setExpansionPolicy().