Hash tables


Detailed Description

OCILIB uses hash tables internally for index/name columns mapping.

OCILIB makes public its hash table’s implementation public for general purpose uses.

OCI_HashTable objects manage string keys / values that can be :

This hash table implementation :

Internal conception

Note:

Collisions are handled by chaining method.

#include "ocilib.h"

int main(void)
{
    int i, n;
    OCI_Connection *cn;
    OCI_Statement  *st;
    OCI_Resultset  *rs;
    OCI_HashTable *table;
    OCI_HashEntry *e;
    OCI_HashValue *v;

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
        return EXIT_FAILURE;

    cn    = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    st    = OCI_StatementCreate(cn);
    table = OCI_HashCreate(256, OCI_HASH_INTEGER);

    /* fill the hash table with data from DB */

    OCI_ExecuteStmt(st, "select code, name from products");
                  
    rs = OCI_GetResultset(st);

    while (OCI_FetchNext(rs))
        OCI_HashAddInt(table, OCI_GetString2(rs, "name"), OCI_GetInt2(rs, "code"));

    printf("%d row(s) fetched\n", OCI_GetRowCount(rs));

    /* lookup an entry */
   
    printf("code for %s is : %d\n", "Cars", OCI_HashGetInt(table, "Cars"));

    /* browse the hash table */

    n = OCI_HashGetSize(table);

    for (i = 0; i < n; i++)
    {
        e = OCI_HashGetEntry(table, i);

        while (e != NULL)
        {
            printf (">key: '%s'\n", e->key);

            v = e->values;

            while (v != NULL)
            {
                printf ("..... value : '%i'\n", v->value.num);
                v = v->next;
            }

            e = e->next;
        }
    }

    /* destroy table */

    OCI_HashFree(table);
    
    OCI_Cleanup();
 
    return EXIT_SUCCESS;
}


Functions

OCI_EXPORT OCI_HashTable *OCI_API OCI_HashCreate (unsigned int size, unsigned int type)
 Create a hash table.
OCI_EXPORT boolean OCI_API OCI_HashFree (OCI_HashTable *table)
 Destroy a hash table.
OCI_EXPORT unsigned int OCI_API OCI_HashGetSize (OCI_HashTable *table)
 Return the size of the hash table.
OCI_EXPORT unsigned int OCI_API OCI_HashGetType (OCI_HashTable *table)
 Return the type of the hash table.
OCI_EXPORT boolean OCI_API OCI_HashAddString (OCI_HashTable *table, const mtext *key, const mtext *value)
 Add a pair string key / string value to the hash table.
OCI_EXPORT const mtext *OCI_API OCI_HashGetString (OCI_HashTable *table, const mtext *key)
 Return the string value associated to the given key.
OCI_EXPORT boolean OCI_API OCI_HashAddInt (OCI_HashTable *table, const mtext *key, int value)
 Adds a pair string key / integer value to the hash table.
OCI_EXPORT int OCI_API OCI_HashGetInt (OCI_HashTable *table, const mtext *key)
 Return the integer value associated to the given key.
OCI_EXPORT boolean OCI_API OCI_HashAddPointer (OCI_HashTable *table, const mtext *key, void *value)
 Adds a pair string key / pointer value to the hash table.
OCI_EXPORT void *OCI_API OCI_HashGetPointer (OCI_HashTable *table, const mtext *key)
 Return a pointer associated with the given key.
OCI_EXPORT OCI_HashEntry *OCI_API OCI_HashLookup (OCI_HashTable *table, const mtext *key, boolean create)
 Lookup for an entry matching the key in the table.
OCI_EXPORT OCI_HashValue *OCI_API OCI_HashGetValue (OCI_HashTable *table, const mtext *key)
 Return the first hash slot that matches the key.
OCI_EXPORT OCI_HashEntry *OCI_API OCI_HashGetEntry (OCI_HashTable *table, unsigned int index)
 Return the entry slot of the hash table internal list at the given position.


Function Documentation

OCI_EXPORT boolean OCI_API OCI_HashAddInt ( OCI_HashTable table,
const mtext *  key,
int  value 
)

Adds a pair string key / integer value to the hash table.

Parameters:
table - Table handle
key - String key
value - Integer value
Returns:
TRUE on success otherwise FALSE

Definition at line 369 of file hash.c.

References OCI_Variant::num.

OCI_EXPORT boolean OCI_API OCI_HashAddPointer ( OCI_HashTable table,
const mtext *  key,
void *  value 
)

Adds a pair string key / pointer value to the hash table.

Parameters:
table - Table handle
key - String key
value - Pointer value
Returns:
TRUE on success otherwise FALSE

Definition at line 390 of file hash.c.

References OCI_Variant::p_void.

Referenced by OCI_ThreadKeyCreate().

OCI_EXPORT boolean OCI_API OCI_HashAddString ( OCI_HashTable table,
const mtext *  key,
const mtext *  value 
)

Add a pair string key / string value to the hash table.

Parameters:
table - Table handle
key - String key
value - string value
Returns:
TRUE on success otherwise FALSE

Definition at line 348 of file hash.c.

References OCI_Variant::p_mtext.

OCI_EXPORT OCI_HashTable* OCI_API OCI_HashCreate ( unsigned int  size,
unsigned int  type 
)

Create a hash table.

Parameters:
size - size of the hash table
type - type of the hash table
Note:
Parameter can be one of the following values :

Returns:
Hash handle on success or NULL on failure

Definition at line 72 of file hash.c.

References OCI_HashTable::count, OCI_HashTable::items, OCI_HashFree(), OCI_HashTable::size, and OCI_HashTable::type.

Referenced by OCI_ThreadKeyCreate().

OCI_EXPORT boolean OCI_API OCI_HashFree ( OCI_HashTable table  ) 

Destroy a hash table.

Parameters:
table - Table handle
Returns:
TRUE on success otherwise FALSE

Definition at line 109 of file hash.c.

References OCI_HashTable::items, OCI_HashEntry::key, OCI_HashValue::next, OCI_HashEntry::next, OCI_Variant::p_mtext, OCI_HashTable::size, OCI_HashTable::type, OCI_HashValue::value, and OCI_HashEntry::values.

Referenced by OCI_HashCreate().

OCI_EXPORT OCI_HashEntry* OCI_API OCI_HashGetEntry ( OCI_HashTable table,
unsigned int  index 
)

Return the entry slot of the hash table internal list at the given position.

Parameters:
table - Table handle
index - index
Returns:
Slot handle otherwise NULL

Definition at line 211 of file hash.c.

References OCI_HashTable::items, and OCI_HashTable::size.

OCI_EXPORT int OCI_API OCI_HashGetInt ( OCI_HashTable table,
const mtext *  key 
)

Return the integer value associated to the given key.

Parameters:
table - Table handle
key - String key
Returns:
Stored integer associated with the key otherwise 0

Definition at line 249 of file hash.c.

References OCI_Variant::num, OCI_HashGetValue(), OCI_HashTable::type, and OCI_HashValue::value.

OCI_EXPORT void* OCI_API OCI_HashGetPointer ( OCI_HashTable table,
const mtext *  key 
)

Return a pointer associated with the given key.

Parameters:
table - Table handle
key - String key
Returns:
Stored pointer associated with the key otherwise NULL

Definition at line 273 of file hash.c.

References OCI_HashGetValue(), OCI_Variant::p_void, OCI_HashTable::type, and OCI_HashValue::value.

Referenced by OCI_ThreadKeyGetValue(), and OCI_ThreadKeySetValue().

OCI_EXPORT unsigned int OCI_API OCI_HashGetSize ( OCI_HashTable table  ) 

Return the size of the hash table.

Parameters:
table - Table handle

Definition at line 164 of file hash.c.

References OCI_HashTable::size.

OCI_EXPORT const mtext* OCI_API OCI_HashGetString ( OCI_HashTable table,
const mtext *  key 
)

Return the string value associated to the given key.

Parameters:
table - Table handle
key - String key
Returns:
Stored string associated with the key otherwise NULL

Definition at line 225 of file hash.c.

References OCI_HashGetValue(), OCI_Variant::p_mtext, OCI_HashTable::type, and OCI_HashValue::value.

OCI_EXPORT unsigned int OCI_API OCI_HashGetType ( OCI_HashTable table  ) 

Return the type of the hash table.

Parameters:
table - Table handle
Note:
the return value can be one of the following values :

Returns:
Hashtable datatype or OCI_UNKNOWN the input handle is NULL

Definition at line 177 of file hash.c.

References OCI_HashTable::type.

OCI_EXPORT OCI_HashValue* OCI_API OCI_HashGetValue ( OCI_HashTable table,
const mtext *  key 
)

Return the first hash slot that matches the key.

Parameters:
table - Table handle
key - String key
Returns:
Slot handle if key found otherwise NULL

Definition at line 190 of file hash.c.

References OCI_HashLookup(), and OCI_HashEntry::values.

Referenced by OCI_HashGetInt(), OCI_HashGetPointer(), and OCI_HashGetString().

OCI_EXPORT OCI_HashEntry* OCI_API OCI_HashLookup ( OCI_HashTable table,
const mtext *  key,
boolean  create 
)

Lookup for an entry matching the key in the table.

Parameters:
table - Table handle
key - String key
create - Do create the entry if not exists
Returns:
Entry handle if key found/added otherwise NULL

Definition at line 411 of file hash.c.

References OCI_HashTable::items, OCI_HashEntry::key, and OCI_HashEntry::next.

Referenced by OCI_HashGetValue().


Generated on Thu Jul 30 17:41:55 2009 for OCILIB (C Driver for Oracle) by  doxygen 1.5.4