2015년 4월 29일 수요일

OCI Hash table

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 :
  • integers
  • strings
  • pointers
This hash table implementation :
  • handle collisions
  • allows multiple values per key
Internal conception
  • The hash table is composed of an array of slots.
  • Each slot can hold a linked list of entries (one per key)
  • Each entry can hold a linked list of values
Note:
  • The internal hash function computes the index in the array where the entry has to be inserted/looked up.
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;
}

댓글 없음:

댓글 쓰기