Back to index

php5  5.3.10
Classes | Defines | Typedefs | Functions
hash.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Hash
struct  Hash::_ht
struct  HashElem

Defines

#define SQLITE_HASH_INT   1
#define SQLITE_HASH_STRING   3
#define SQLITE_HASH_BINARY   4
#define sqliteHashFirst(H)   ((H)->first)
#define sqliteHashNext(E)   ((E)->next)
#define sqliteHashData(E)   ((E)->data)
#define sqliteHashKey(E)   ((E)->pKey)
#define sqliteHashKeysize(E)   ((E)->nKey)
#define sqliteHashCount(H)   ((H)->count)

Typedefs

typedef struct Hash
typedef struct HashElem

Functions

void sqliteHashInit (Hash *, int keytype, int copyKey)
void * sqliteHashInsert (Hash *, const void *pKey, int nKey, void *pData)
void * sqliteHashFind (const Hash *, const void *pKey, int nKey)
void sqliteHashClear (Hash *)

Class Documentation

struct Hash

Definition at line 32 of file hash.h.

Collaboration diagram for Hash:
Class Members
char copyKey
int count
unsigned int count
HashElem * first
struct _ht * ht
int htsize
unsigned int htsize
char keyClass
struct Hash::_ht

Definition at line 38 of file hash.h.

Collaboration diagram for Hash::_ht:
Class Members
HashElem * chain
int count
struct HashElem

Definition at line 50 of file hash.h.

Collaboration diagram for HashElem:
Class Members
void * data
HashElem * next
int nKey
void * pKey
const char * pKey
HashElem * prev

Define Documentation

#define SQLITE_HASH_BINARY   4

Definition at line 76 of file hash.h.

#define SQLITE_HASH_INT   1

Definition at line 73 of file hash.h.

#define SQLITE_HASH_STRING   3

Definition at line 75 of file hash.h.

#define sqliteHashCount (   H)    ((H)->count)

Definition at line 107 of file hash.h.

#define sqliteHashData (   E)    ((E)->data)

Definition at line 100 of file hash.h.

#define sqliteHashFirst (   H)    ((H)->first)

Definition at line 98 of file hash.h.

#define sqliteHashKey (   E)    ((E)->pKey)

Definition at line 101 of file hash.h.

#define sqliteHashKeysize (   E)    ((E)->nKey)

Definition at line 102 of file hash.h.

#define sqliteHashNext (   E)    ((E)->next)

Definition at line 99 of file hash.h.


Typedef Documentation

typedef struct Hash

Definition at line 21 of file hash.h.

typedef struct HashElem

Definition at line 22 of file hash.h.


Function Documentation

void sqliteHashClear ( Hash )

Definition at line 48 of file hash.c.

                              {
  HashElem *elem;         /* For looping over all elements of the table */

  assert( pH!=0 );
  elem = pH->first;
  pH->first = 0;
  if( pH->ht ) sqliteFree(pH->ht);
  pH->ht = 0;
  pH->htsize = 0;
  while( elem ){
    HashElem *next_elem = elem->next;
    if( pH->copyKey && elem->pKey ){
      sqliteFree(elem->pKey);
    }
    sqliteFree(elem);
    elem = next_elem;
  }
  pH->count = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* sqliteHashFind ( const Hash ,
const void *  pKey,
int  nKey 
)

Definition at line 261 of file hash.c.

                                                                {
  int h;             /* A hash on key */
  HashElem *elem;    /* The element that matches key */
  int (*xHash)(const void*,int);  /* The hash function */

  if( pH==0 || pH->ht==0 ) return 0;
  xHash = hashFunction(pH->keyClass);
  assert( xHash!=0 );
  h = (*xHash)(pKey,nKey);
  assert( (pH->htsize & (pH->htsize-1))==0 );
  elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
  return elem ? elem->data : 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqliteHashInit ( Hash ,
int  keytype,
int  copyKey 
)

Definition at line 32 of file hash.c.

                                                         {
  assert( new!=0 );
  assert( keyClass>=SQLITE_HASH_INT && keyClass<=SQLITE_HASH_BINARY );
  new->keyClass = keyClass;
  new->copyKey = copyKey &&
                (keyClass==SQLITE_HASH_STRING || keyClass==SQLITE_HASH_BINARY);
  new->first = 0;
  new->count = 0;
  new->htsize = 0;
  new->ht = 0;
}

Here is the caller graph for this function:

void* sqliteHashInsert ( Hash ,
const void *  pKey,
int  nKey,
void *  pData 
)

Definition at line 290 of file hash.c.

                                                                        {
  int hraw;             /* Raw hash value of the key */
  int h;                /* the hash of the key modulo hash table size */
  HashElem *elem;       /* Used to loop thru the element list */
  HashElem *new_elem;   /* New element added to the pH */
  int (*xHash)(const void*,int);  /* The hash function */

  assert( pH!=0 );
  xHash = hashFunction(pH->keyClass);
  assert( xHash!=0 );
  hraw = (*xHash)(pKey, nKey);
  assert( (pH->htsize & (pH->htsize-1))==0 );
  h = hraw & (pH->htsize-1);
  elem = findElementGivenHash(pH,pKey,nKey,h);
  if( elem ){
    void *old_data = elem->data;
    if( data==0 ){
      removeElementGivenHash(pH,elem,h);
    }else{
      elem->data = data;
    }
    return old_data;
  }
  if( data==0 ) return 0;
  new_elem = (HashElem*)sqliteMalloc( sizeof(HashElem) );
  if( new_elem==0 ) return data;
  if( pH->copyKey && pKey!=0 ){
    new_elem->pKey = sqliteMallocRaw( nKey );
    if( new_elem->pKey==0 ){
      sqliteFree(new_elem);
      return data;
    }
    memcpy((void*)new_elem->pKey, pKey, nKey);
  }else{
    new_elem->pKey = (void*)pKey;
  }
  new_elem->nKey = nKey;
  pH->count++;
  if( pH->htsize==0 ) rehash(pH,8);
  if( pH->htsize==0 ){
    pH->count = 0;
    sqliteFree(new_elem);
    return data;
  }
  if( pH->count > pH->htsize ){
    rehash(pH,pH->htsize*2);
  }
  assert( (pH->htsize & (pH->htsize-1))==0 );
  h = hraw & (pH->htsize-1);
  elem = pH->ht[h].chain;
  if( elem ){
    new_elem->next = elem;
    new_elem->prev = elem->prev;
    if( elem->prev ){ elem->prev->next = new_elem; }
    else            { pH->first = new_elem; }
    elem->prev = new_elem;
  }else{
    new_elem->next = pH->first;
    new_elem->prev = 0;
    if( pH->first ){ pH->first->prev = new_elem; }
    pH->first = new_elem;
  }
  pH->ht[h].count++;
  pH->ht[h].chain = new_elem;
  new_elem->data = data;
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: