OPIC
Object Persistence In C
Related Functions
PascalHashTable Struct Reference

An opaque object that manages associations of key-value pairs. More...

Related Functions

(Note that these are not member functions.)

PascalHashTablePHNew (OPHeap *heap, uint64_t num_objects, double load, size_t key_inline_size, size_t valsize)
 Constructor for PascalHashTable. More...
 
void PHDestroy (PascalHashTable *rhh)
 Destructor for PascalHashTable. More...
 
bool PHInsertCustom (PascalHashTable *rhh, OPHash hasher, void *key, size_t keysize, void *val)
 Associates the specified key and value with custom hash function. More...
 
bool PHUpsertCustom (PascalHashTable *rhh, OPHash hasher, void *key, size_t keysize, void **val_ref, bool *is_duplicate)
 Update or insert depends on whether the key already exists in the hash table using custom hash function. More...
 
void * PHGetCustom (PascalHashTable *rhh, OPHash hasher, void *key, size_t keysize)
 Obtain the value associated with the key and hash function. Returns NULL if the key was not found. More...
 
void * PHDeleteCustom (PascalHashTable *rhh, OPHash hasher, void *key, size_t keysize)
 Deletes the key-value entry with specified hash function. More...
 
static bool PHInsert (PascalHashTable *rhh, void *key, size_t keysize, void *val)
 Associates the specified key and value using default hash function. More...
 
static bool PHUpsert (PascalHashTable *rhh, void *key, size_t keysize, void **val_ref, bool *is_duplicate)
 Update or insert depends on whether the key already exists in the hash table. More...
 
static void * PHGet (PascalHashTable *rhh, void *key, size_t keysize)
 Obtain the value associated with the key using default hash function. Returns NULL if the key was not found. More...
 
static void * PHDelete (PascalHashTable *rhh, void *key, size_t keysize)
 Deletes the key-value entry using default hash function. More...
 
uint64_t PHObjcnt (PascalHashTable *rhh)
 Obtain the number of objects stored in this hash table.
 
uint64_t PHCapacity (PascalHashTable *rhh)
 Obtain the number of objects can be stored in this hash table.
 
size_t PHKeyInlineSize (PascalHashTable *rhh)
 Obtain the size of the key that can be stored inline in the hash table. If the size of the key is larger than this size, the key would not be inline but allocated somewhere in the OPHeap.
 
size_t PHValSize (PascalHashTable *rhh)
 Obtain the size of the value configured for this hash table.
 
void PHIterate (PascalHashTable *rhh, OPHashIterator iterator, void *context)
 Iterates over all key-value pairs in this hash table with user specified context. More...
 
void PHPrintStat (PascalHashTable *rhh)
 Prints the accumulated count for each probing number.
 

Detailed Description

An opaque object that manages associations of key-value pairs.

This object is not thread safe.

Friends And Related Function Documentation

◆ PHNew()

PascalHashTable * PHNew ( OPHeap heap,
uint64_t  num_objects,
double  load,
size_t  key_inline_size,
size_t  valsize 
)
related

Constructor for PascalHashTable.

Parameters
heapOPHeap instance.
num_objectsnumber of object we decided to put in.
load(0.0-1.0) how full the table could be before expansion.
key_inline_sizeSize to store the key inline. If the key size is larger than this value, the key would be copied to OPHeap and get referenced by a pointer. If key_inline_size is set to 0, pointer is always used.
valsizelength of the value measured in bytes. This value can be zero for hashset.
Returns
PascalHashTable instance if allocation succeeded, else return NULL.

◆ PHDestroy()

void PHDestroy ( PascalHashTable rhh)
related

Destructor for PascalHashTable.

Parameters
rhhPascalHashTable instance to destroy.

All the keys managed by this instance will get dealloc as well. User need to manage the value deallocation prior calling this method.

◆ PHInsertCustom()

bool PHInsertCustom ( PascalHashTable rhh,
OPHash  hasher,
void *  key,
size_t  keysize,
void *  val 
)
related

Associates the specified key and value with custom hash function.

Parameters
rhhPascalHashTable instance.
hasherhash function.
keypointer the the key
keysizelength of key data, measured in bytes.
valpointer to the value.
Returns
true if the operation succeeded, false otherwise.

The content pointed by key and val will be copied into the hash table. If the value size were 0, only the key get copied. When there's a key collision, the coresponding value get replaced.

If the inserted key-value pairs exceeded the original size user configured, the hash table will resized with a larger capacity. If the resize failed, false is returned.

◆ PHUpsertCustom()

bool PHUpsertCustom ( PascalHashTable rhh,
OPHash  hasher,
void *  key,
size_t  keysize,
void **  val_ref,
bool *  is_duplicate 
)
related

Update or insert depends on whether the key already exists in the hash table using custom hash function.

Parameters
rhhRobinHoodHash instance.
hasherhash function.
keypointer to the key.
keysizelength of the key data, measured in bytes.
val_refreference of value pointer.
is_duplicatereference of duplication boolean variable.
Returns
true if the operation succeeded, false otherwise.

This method does not insert the value automatically, instead it provides the pointer to the address where value can be inserted or overriden.

If the inserted key-value pairs exceeded the original size user configured, the hash table will resized with a larger capacity. If the resize failed, false is returned.

◆ PHGetCustom()

void * PHGetCustom ( PascalHashTable rhh,
OPHash  hasher,
void *  key,
size_t  keysize 
)
related

Obtain the value associated with the key and hash function. Returns NULL if the key was not found.

Parameters
rhhPascalHashTable instance.
hasherhash function.
keypointer to the key.
keysizelength of the key data. measured in bytes.
Returns
pointer to the value if found, NULL otherwise.

If the value size were set to 0, PHGetCustom would still return a pointer to where it would store the value. User can still use the returned value to exam if the key were present in the hash table.

◆ PHDeleteCustom()

void * PHDeleteCustom ( PascalHashTable rhh,
OPHash  hasher,
void *  key,
size_t  keysize 
)
related

Deletes the key-value entry with specified hash function.

Parameters
rhhPascalHashTable instance.
hasherhash function.
keypointer to the key.
keysizelength of the key data, measured in bytes.
Returns
pointer to the value if found, NULL otherwise.

The key would get deallocated after deletion. Table will resize when many entries are deleted.

◆ PHInsert()

static bool PHInsert ( PascalHashTable rhh,
void *  key,
size_t  keysize,
void *  val 
)
related

Associates the specified key and value using default hash function.

Parameters
rhhPascalHashTable instance.
keypointer the the key
keysizelength of key data, measured in bytes.
valpointer to the value.
Returns
true if the operation succeeded, false otherwise.

The content pointed by key and val will be copied into the hash table. If the value size were 0, only the key get copied. When there's a key collision, the coresponding value get replaced.

If the inserted key-value pairs exceeded the original size user configured, the hash table will resized with a larger capacity. If the resize failed, false is returned.

Definition at line 177 of file pascal_hash_table.h.

◆ PHUpsert()

static bool PHUpsert ( PascalHashTable rhh,
void *  key,
size_t  keysize,
void **  val_ref,
bool *  is_duplicate 
)
related

Update or insert depends on whether the key already exists in the hash table.

Parameters
rhhRobinHoodHash instance.
keypointer to the key.
keysizelength of the key data, measured in bytes.
val_refreference of value pointer.
is_duplicatereference of duplication boolean variable.
Returns
true if the operation succeeded, false otherwise.

This method does not insert the value automatically, instead it provides the pointer to the address where value can be inserted or overriden.

If the inserted key-value pairs exceeded the original size user configured, the hash table will resized with a larger capacity. If the resize failed, false is returned.

Definition at line 202 of file pascal_hash_table.h.

◆ PHGet()

static void * PHGet ( PascalHashTable rhh,
void *  key,
size_t  keysize 
)
related

Obtain the value associated with the key using default hash function. Returns NULL if the key was not found.

Parameters
rhhPascalHashTable instance.
keypointer to the key.
keysizelength of the key data. measured in bytes.
Returns
pointer to the value if found, NULL otherwise.

If the value size were set to 0, PHGetCustom would still return a pointer to where it would store the value. User can still use the returned value to exam if the key were present in the hash table.

Definition at line 224 of file pascal_hash_table.h.

◆ PHDelete()

static void * PHDelete ( PascalHashTable rhh,
void *  key,
size_t  keysize 
)
related

Deletes the key-value entry using default hash function.

Parameters
rhhPascalHashTable instance.
keypointer to the key.
keysizelength of the key data, measured in bytes.
Returns
pointer to the value if found, NULL otherwise.

The key would get deallocated after deletion. Table will resize when many entries are deleted.

Definition at line 242 of file pascal_hash_table.h.

◆ PHIterate()

void PHIterate ( PascalHashTable rhh,
OPHashIterator  iterator,
void *  context 
)
related

Iterates over all key-value pairs in this hash table with user specified context.

Parameters
rhhPascalHashTable instance.
iteratorfunction pointer to user defined iterator function.
contextuser defined context.
// Function interface matches OPHashIterator
void my_iterator(void* key, void* value,
size_t keysize, size_t valsize,
void* context)
{
// Obtain the object we passed in.
struct MyStruct* my_s = (struct MyStruct*) context;
// assumes both key and value were null terminated string
printf("key: %s, value: %s\n", key, value);
}
// User defined context
struct MyStruct my_s;
// PHIterate takes in a PascalHashTable object, a fuction pointer
// OPHashIterator and a user defined context for iteration.
PHIterate(rhh, &my_iterator, &my_s);

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