MFEM
v4.6.0
Finite element discretization library
|
#include <hash.hpp>
Classes | |
class | const_iterator |
class | iterator |
Public Member Functions | |
HashTable (int block_size=16 *1024, int init_hash_size=32 *1024) | |
Main constructor of the HashTable class. More... | |
HashTable (const HashTable &other) | |
Deep copy. More... | |
HashTable & | operator= (const HashTable &)=delete |
Copy assignment not supported. More... | |
~HashTable () | |
T * | Get (int p1, int p2) |
Item accessor with key (or parents) the pair 'p1', 'p2'. Default construct an item of type T if no value correspond to the requested key. More... | |
T * | Get (int p1, int p2, int p3, int p4=-1) |
Item accessor with key (or parents) the quadruplet 'p1', 'p2', 'p3', 'p4'. The key 'p4' is optional. Default construct an item of type T if no value corresponds to the requested key. More... | |
int | GetId (int p1, int p2) |
Get id of item whose parents are p1, p2... Create it if it doesn't exist. More... | |
int | GetId (int p1, int p2, int p3, int p4=-1) |
Get the "id" of an item, this "id" corresponding to the index of the item in the underlying BlockArray<T> object. Default construct an item and id if no value correspond to the requested key. More... | |
T * | Find (int p1, int p2) |
Find item whose parents are p1, p2... Return NULL if it doesn't exist. More... | |
T * | Find (int p1, int p2, int p3, int p4=-1) |
Item accessor with key (or parents) the quadruplet 'p1', 'p2', 'p3', 'p4'. The key 'p4' is optional. Return nullptr if no value correspond to the requested key. More... | |
const T * | Find (int p1, int p2) const |
Item const accessor with key (or parents) the pair 'p1', 'p2'. Return nullptr if no value correspond to the requested key. More... | |
const T * | Find (int p1, int p2, int p3, int p4=-1) const |
Item const accessor with key (or parents) the quadruplet 'p1', 'p2', 'p3', 'p4'. The key 'p4' is optional. Return nullptr if no value correspond to the requested key. More... | |
int | FindId (int p1, int p2) const |
Find id of item whose parents are p1, p2... Return -1 if it doesn't exist. More... | |
int | FindId (int p1, int p2, int p3, int p4=-1) const |
Find the "id" of an item, this "id" corresponding to the index of the item in the underlying BlockArray<T> object. Default construct an item and id if no value correspond to the requested key. More... | |
int | Size () const |
Return the number of elements currently stored in the HashTable. More... | |
int | NumIds () const |
Return the total number of ids (used and unused) in the HashTable. More... | |
int | NumFreeIds () const |
Return the number of free/unused ids in the HashTable. More... | |
bool | IdExists (int id) const |
Return true if item 'id' exists in (is used by) the container. More... | |
void | Delete (int id) |
Remove an item from the hash table. More... | |
void | DeleteAll () |
Remove all items. More... | |
void | Alloc (int id, int p1, int p2) |
Allocate an item at 'id'. Enlarge the underlying BlockArray if necessary. More... | |
void | UpdateUnused () |
Reinitialize the internal list of unallocated items. More... | |
void | Reparent (int id, int new_p1, int new_p2) |
Change the key associated with an item. More... | |
void | Reparent (int id, int new_p1, int new_p2, int new_p3, int new_p4=-1) |
Change the key associated with an item. More... | |
std::size_t | MemoryUsage () const |
Return total size of allocated memory (tables plus items), in bytes. More... | |
void | PrintMemoryDetail () const |
Write details of the memory usage to the mfem output stream. More... | |
void | PrintStats () const |
Print a histogram of bin sizes for debugging purposes. More... | |
iterator | begin () |
iterator | end () |
const_iterator | cbegin () const |
const_iterator | cend () const |
Public Member Functions inherited from mfem::BlockArray< T > | |
BlockArray (int block_size=16 *1024) | |
BlockArray (const BlockArray< T > &other) | |
BlockArray & | operator= (const BlockArray &)=delete |
~BlockArray () | |
int | Append () |
Allocate and construct a new item in the array, return its index. More... | |
int | Append (const T &item) |
Allocate and copy-construct a new item in the array, return its index. More... | |
T & | At (int index) |
Access item of the array. More... | |
const T & | At (int index) const |
T & | operator[] (int index) |
Access item of the array. More... | |
const T & | operator[] (int index) const |
int | Size () const |
Return the number of items actually stored. More... | |
int | Capacity () const |
Return the current capacity of the BlockArray. More... | |
void | DeleteAll () |
Destroy all items, set size to zero. More... | |
void | Swap (BlockArray< T > &other) |
std::size_t | MemoryUsage () const |
iterator | begin () |
iterator | end () |
const_iterator | cbegin () const |
const_iterator | cend () const |
Protected Types | |
typedef BlockArray< T > | Base |
Protected Member Functions | |
int | Hash (size_t p1, size_t p2) const |
hash function for Hashed2 items. More... | |
int | Hash (size_t p1, size_t p2, size_t p3) const |
hash function for Hashed4 items. More... | |
int | Hash (const Hashed2 &item) const |
Hash function for items of type T that inherit from Hashed2. More... | |
int | Hash (const Hashed4 &item) const |
Hash function for items of type T that inherit from Hashed4. More... | |
int | SearchList (int id, int p1, int p2) const |
Search the index of the item associated to the key (p1,p2) starting from the item with index id. More... | |
int | SearchList (int id, int p1, int p2, int p3) const |
Search the index of the item associated to the key (p1,p2,p3,(p4)) starting from the item with index id. More... | |
void | Insert (int idx, int id, T &item) |
Insert the item 'id' into bin 'idx'. More... | |
void | Unlink (int idx, int id) |
Unlink an item id from the linked list of bin idx. More... | |
void | CheckRehash () |
Check table fill factor and resize if necessary. More... | |
void | DoRehash () |
Double the size of the hash table (i.e., double the number of bins) and reinsert all items into the new bins. More... | |
int | BinSize (int idx) const |
Return the size of the bin "idx". More... | |
Protected Member Functions inherited from mfem::BlockArray< T > | |
int | Alloc () |
void | CheckIndex (int index) const |
void | Destroy () |
Protected Attributes | |
int * | table |
int | mask |
Array< int > | unused |
Protected Attributes inherited from mfem::BlockArray< T > | |
Array< T * > | blocks |
int | size |
int | shift |
int | mask |
HashTable is a container for items that require associative access through pairs (or quadruples) of indices:
(p1, p2) -> item (p1, p2, p3, p4) -> item
An example of this are edges and faces in a mesh. Each edge is uniquely identified by two parent vertices and so can be easily accessed from different elements using this class. Similarly for faces.
The order of the p1, p2, ... indices is not relevant as they are sorted each time this class is invoked.
There are two main methods this class provides. The Get(...) methods always return an item given the two or four indices. If the item didn't previously exist, the methods creates a new one. The Find(...) methods, on the other hand, just return NULL or -1 if the item doesn't exist.
Each new item is automatically assigned a unique ID - the index of the item inside the BlockArray. The IDs may (but need not) be used as p1, p2, ... of other items.
The item type (T) needs to follow either the Hashed2 or the Hashed4 concept. It is easiest to just inherit from these structs.
All items in the container can also be accessed sequentially using the provided iterator.
Notes: The data structure and implementation is based on a BlockArray<T> which provides an efficient item storage that avoids heap fragmentation, and index-based item access. The hash table implemented on top of the BlockArray provides fast associative (key -> value) access by grouping items into bins (buckets) of O(1) size.
Hash
.
|
protected |
mfem::HashTable< T >::HashTable | ( | int | block_size = 16*1024 , |
int | init_hash_size = 32*1024 |
||
) |
mfem::HashTable< T >::HashTable | ( | const HashTable< T > & | other | ) |
mfem::HashTable< T >::~HashTable | ( | ) |
void mfem::HashTable< T >::Alloc | ( | int | id, |
int | p1, | ||
int | p2 | ||
) |
Allocate an item at 'id'. Enlarge the underlying BlockArray if necessary.
[in] | id | Index of the item in the underlying BlockArray<T>. |
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
|
inline |
|
protected |
|
inline |
|
inline |
|
inlineprotected |
Check table fill factor and resize if necessary.
The method checks the average size of the bins (i.e., the fill factor). If the fill factor is > 2, the table is enlarged (see DoRehash()).
void mfem::HashTable< T >::Delete | ( | int | id | ) |
void mfem::HashTable< T >::DeleteAll | ( | ) |
|
protected |
Double the size of the hash table (i.e., double the number of bins) and reinsert all items into the new bins.
NOTE: Rehashing is computationally expensive (O(N) in the number of items), but since it is only done rarely (when the number of items doubles), the amortized complexity of inserting an item is still O(1).
|
inline |
|
inline |
Find item whose parents are p1, p2... Return NULL if it doesn't exist.
Item accessor with key (or parents) the pair 'p1', 'p2'. Return nullptr if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
|
inline |
Item accessor with key (or parents) the quadruplet 'p1', 'p2', 'p3', 'p4'. The key 'p4' is optional. Return nullptr if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
[in] | p3 | Third part of the key. |
[in] | p4 | Fourth part of the key (optional). |
|
inline |
Item const accessor with key (or parents) the pair 'p1', 'p2'. Return nullptr if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
|
inline |
Item const accessor with key (or parents) the quadruplet 'p1', 'p2', 'p3', 'p4'. The key 'p4' is optional. Return nullptr if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
[in] | p3 | Third part of the key. |
[in] | p4 | Fourth part of the key (optional). |
int mfem::HashTable< T >::FindId | ( | int | p1, |
int | p2 | ||
) | const |
Find id of item whose parents are p1, p2... Return -1 if it doesn't exist.
Find the "id" of an item, this "id" corresponding to the index of the item in the underlying BlockArray<T> object. Default construct an item and id if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
int mfem::HashTable< T >::FindId | ( | int | p1, |
int | p2, | ||
int | p3, | ||
int | p4 = -1 |
||
) | const |
Find the "id" of an item, this "id" corresponding to the index of the item in the underlying BlockArray<T> object. Default construct an item and id if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
[in] | p3 | Third part of the key. |
[in] | p4 | Fourth part of the key (optional). |
|
inline |
Item accessor with key (or parents) the pair 'p1', 'p2'. Default construct an item of type T if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
|
inline |
Item accessor with key (or parents) the quadruplet 'p1', 'p2', 'p3', 'p4'. The key 'p4' is optional. Default construct an item of type T if no value corresponds to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
[in] | p3 | Third part of the key. |
[in] | p4 | Fourth part of the key (optional). |
int mfem::HashTable< T >::GetId | ( | int | p1, |
int | p2 | ||
) |
Get id of item whose parents are p1, p2... Create it if it doesn't exist.
Get the "id" of an item, this "id" corresponding to the index of the item in the underlying BlockArray<T> object. Default construct an item and id if no value corresponds to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
int mfem::HashTable< T >::GetId | ( | int | p1, |
int | p2, | ||
int | p3, | ||
int | p4 = -1 |
||
) |
Get the "id" of an item, this "id" corresponding to the index of the item in the underlying BlockArray<T> object. Default construct an item and id if no value correspond to the requested key.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
[in] | p3 | Third part of the key. |
[in] | p4 | Fourth part of the key (optional). |
|
inlineprotected |
|
inlineprotected |
hash function for Hashed4 items.
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
[in] | p3 | Third part of the key. |
NOTE: The constants are arbitrary. NOTE: p4 is not hashed nor stored as p1, p2, p3 identify a face uniquely.
|
inlineprotected |
|
inlineprotected |
|
inline |
|
inlineprotected |
Insert the item 'id' into bin 'idx'.
[in] | idx | The bin/bucket index. |
[in] | id | The index of the item in the BlockArray<T>. |
[in] | item | The item to insert at the beginning of the linked list. |
std::size_t mfem::HashTable< T >::MemoryUsage | ( | ) | const |
|
inline |
|
inline |
|
delete |
Copy assignment not supported.
void mfem::HashTable< T >::PrintMemoryDetail | ( | ) | const |
void mfem::HashTable< T >::PrintStats | ( | ) | const |
void mfem::HashTable< T >::Reparent | ( | int | id, |
int | new_p1, | ||
int | new_p2 | ||
) |
Change the key associated with an item.
In other words, makes an item hashed under different parent IDs.
[in] | id | Index of the item in the underlying BlockArray<T>. |
[in] | new_p1 | First part of the new key. |
[in] | new_p2 | Second part of the new key. |
void mfem::HashTable< T >::Reparent | ( | int | id, |
int | new_p1, | ||
int | new_p2, | ||
int | new_p3, | ||
int | new_p4 = -1 |
||
) |
Change the key associated with an item.
In other words, makes an item hashed under different parent IDs.
[in] | id | Index of the item in the underlying BlockArray<T>. |
[in] | new_p1 | First part of the new key. |
[in] | new_p2 | Second part of the new key. |
[in] | new_p3 | Third part of the new key. |
[in] | new_p4 | Fourth part of the new key (optional). |
|
protected |
Search the index of the item associated to the key (p1,p2) starting from the item with index id.
[in] | id | Index of the item in the underlying BlockArray<T>. |
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
|
protected |
Search the index of the item associated to the key (p1,p2,p3,(p4)) starting from the item with index id.
[in] | id | Index of the item in the underlying BlockArray<T>. |
[in] | p1 | First part of the key. |
[in] | p2 | Second part of the key. |
[in] | p3 | Third part of the key. |
|
inline |
|
protected |
void mfem::HashTable< T >::UpdateUnused | ( | ) |
|
protected |
|
protected |
|
protected |