Back to index

tetex-bin  3.0
Public Member Functions | Private Member Functions | Private Attributes
GHash Class Reference

#include <GHash.h>

Collaboration diagram for GHash:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 GHash (GBool deleteKeysA=gFalse)
 ~GHash ()
void add (GString *key, void *val)
void add (GString *key, int val)
voidlookup (GString *key)
int lookupInt (GString *key)
voidlookup (char *key)
int lookupInt (char *key)
voidremove (GString *key)
int removeInt (GString *key)
voidremove (char *key)
int removeInt (char *key)
int getLength ()
void startIter (GHashIter **iter)
GBool getNext (GHashIter **iter, GString **key, void **val)
GBool getNext (GHashIter **iter, GString **key, int *val)
void killIter (GHashIter **iter)

Private Member Functions

void expand ()
GHashBucketfind (GString *key, int *h)
GHashBucketfind (char *key, int *h)
int hash (GString *key)
int hash (char *key)

Private Attributes

GBool deleteKeys
int size
int len
GHashBucket ** tab

Detailed Description

Definition at line 26 of file GHash.h.


Constructor & Destructor Documentation

GHash::GHash ( GBool  deleteKeysA = gFalse)

Definition at line 37 of file GHash.cc.

                              {
  int h;

  deleteKeys = deleteKeysA;
  size = 7;
  tab = (GHashBucket **)gmalloc(size * sizeof(GHashBucket *));
  for (h = 0; h < size; ++h) {
    tab[h] = NULL;
  }
  len = 0;
}

Here is the call graph for this function:

Definition at line 49 of file GHash.cc.

              {
  GHashBucket *p;
  int h;

  for (h = 0; h < size; ++h) {
    while (tab[h]) {
      p = tab[h];
      tab[h] = p->next;
      if (deleteKeys) {
       delete p->key;
      }
      delete p;
    }
  }
  gfree(tab);
}

Here is the call graph for this function:


Member Function Documentation

void GHash::add ( GString key,
void val 
)

Definition at line 66 of file GHash.cc.

                                       {
  GHashBucket *p;
  int h;

  // expand the table if necessary
  if (len >= size) {
    expand();
  }

  // add the new symbol
  p = new GHashBucket;
  p->key = key;
  p->val.p = val;
  h = hash(key);
  p->next = tab[h];
  tab[h] = p;
  ++len;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void GHash::add ( GString key,
int  val 
)

Definition at line 85 of file GHash.cc.

                                     {
  GHashBucket *p;
  int h;

  // expand the table if necessary
  if (len >= size) {
    expand();
  }

  // add the new symbol
  p = new GHashBucket;
  p->key = key;
  p->val.i = val;
  h = hash(key);
  p->next = tab[h];
  tab[h] = p;
  ++len;
}

Here is the call graph for this function:

void GHash::expand ( ) [private]

Definition at line 287 of file GHash.cc.

                   {
  GHashBucket **oldTab;
  GHashBucket *p;
  int oldSize, h, i;

  oldSize = size;
  oldTab = tab;
  size = 2*size + 1;
  tab = (GHashBucket **)gmalloc(size * sizeof(GHashBucket *));
  for (h = 0; h < size; ++h) {
    tab[h] = NULL;
  }
  for (i = 0; i < oldSize; ++i) {
    while (oldTab[i]) {
      p = oldTab[i];
      oldTab[i] = oldTab[i]->next;
      h = hash(p->key);
      p->next = tab[h];
      tab[h] = p;
    }
  }
  gfree(oldTab);
}

Here is the call graph for this function:

Here is the caller graph for this function:

GHashBucket * GHash::find ( GString key,
int h 
) [private]

Definition at line 311 of file GHash.cc.

                                             {
  GHashBucket *p;

  *h = hash(key);
  for (p = tab[*h]; p; p = p->next) {
    if (!p->key->cmp(key)) {
      return p;
    }
  }
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GHashBucket * GHash::find ( char *  key,
int h 
) [private]

Definition at line 323 of file GHash.cc.

                                          {
  GHashBucket *p;

  *h = hash(key);
  for (p = tab[*h]; p; p = p->next) {
    if (!p->key->cmp(key)) {
      return p;
    }
  }
  return NULL;
}

Here is the call graph for this function:

int GHash::getLength ( ) [inline]

Definition at line 41 of file GHash.h.

{ return len; }
GBool GHash::getNext ( GHashIter **  iter,
GString **  key,
void **  val 
)

Definition at line 242 of file GHash.cc.

                                                                {
  if (!*iter) {
    return gFalse;
  }
  if ((*iter)->p) {
    (*iter)->p = (*iter)->p->next;
  }
  while (!(*iter)->p) {
    if (++(*iter)->h == size) {
      delete *iter;
      *iter = NULL;
      return gFalse;
    }
    (*iter)->p = tab[(*iter)->h];
  }
  *key = (*iter)->p->key;
  *val = (*iter)->p->val.p;
  return gTrue;
}

Here is the caller graph for this function:

GBool GHash::getNext ( GHashIter **  iter,
GString **  key,
int val 
)

Definition at line 262 of file GHash.cc.

                                                              {
  if (!*iter) {
    return gFalse;
  }
  if ((*iter)->p) {
    (*iter)->p = (*iter)->p->next;
  }
  while (!(*iter)->p) {
    if (++(*iter)->h == size) {
      delete *iter;
      *iter = NULL;
      return gFalse;
    }
    (*iter)->p = tab[(*iter)->h];
  }
  *key = (*iter)->p->key;
  *val = (*iter)->p->val.i;
  return gTrue;
}
int GHash::hash ( GString key) [private]

Definition at line 335 of file GHash.cc.

                            {
  char *p;
  unsigned int h;
  int i;

  h = 0;
  for (p = key->getCString(), i = 0; i < key->getLength(); ++p, ++i) {
    h = 17 * h + (int)(*p & 0xff);
  }
  return (int)(h % size);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int GHash::hash ( char *  key) [private]

Definition at line 347 of file GHash.cc.

                         {
  char *p;
  unsigned int h;

  h = 0;
  for (p = key; *p; ++p) {
    h = 17 * h + (int)(*p & 0xff);
  }
  return (int)(h % size);
}

Definition at line 282 of file GHash.cc.

                                     {
  delete *iter;
  *iter = NULL;
}

Here is the caller graph for this function:

void * GHash::lookup ( GString key)

Definition at line 104 of file GHash.cc.

                                {
  GHashBucket *p;
  int h;

  if (!(p = find(key, &h))) {
    return NULL;
  }
  return p->val.p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void * GHash::lookup ( char *  key)

Definition at line 124 of file GHash.cc.

                             {
  GHashBucket *p;
  int h;

  if (!(p = find(key, &h))) {
    return NULL;
  }
  return p->val.p;
}

Here is the call graph for this function:

Definition at line 114 of file GHash.cc.

                                 {
  GHashBucket *p;
  int h;

  if (!(p = find(key, &h))) {
    return 0;
  }
  return p->val.i;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int GHash::lookupInt ( char *  key)

Definition at line 134 of file GHash.cc.

                              {
  GHashBucket *p;
  int h;

  if (!(p = find(key, &h))) {
    return 0;
  }
  return p->val.i;
}

Here is the call graph for this function:

void * GHash::remove ( GString key)

Definition at line 144 of file GHash.cc.

                                {
  GHashBucket *p;
  GHashBucket **q;
  void *val;
  int h;

  if (!(p = find(key, &h))) {
    return NULL;
  }
  q = &tab[h];
  while (*q != p) {
    q = &((*q)->next);
  }
  *q = p->next;
  if (deleteKeys) {
    delete p->key;
  }
  val = p->val.p;
  delete p;
  --len;
  return val;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void * GHash::remove ( char *  key)

Definition at line 190 of file GHash.cc.

                             {
  GHashBucket *p;
  GHashBucket **q;
  void *val;
  int h;

  if (!(p = find(key, &h))) {
    return NULL;
  }
  q = &tab[h];
  while (*q != p) {
    q = &((*q)->next);
  }
  *q = p->next;
  if (deleteKeys) {
    delete p->key;
  }
  val = p->val.p;
  delete p;
  --len;
  return val;
}

Here is the call graph for this function:

Definition at line 167 of file GHash.cc.

                                 {
  GHashBucket *p;
  GHashBucket **q;
  int val;
  int h;

  if (!(p = find(key, &h))) {
    return 0;
  }
  q = &tab[h];
  while (*q != p) {
    q = &((*q)->next);
  }
  *q = p->next;
  if (deleteKeys) {
    delete p->key;
  }
  val = p->val.i;
  delete p;
  --len;
  return val;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int GHash::removeInt ( char *  key)

Definition at line 213 of file GHash.cc.

                              {
  GHashBucket *p;
  GHashBucket **q;
  int val;
  int h;

  if (!(p = find(key, &h))) {
    return 0;
  }
  q = &tab[h];
  while (*q != p) {
    q = &((*q)->next);
  }
  *q = p->next;
  if (deleteKeys) {
    delete p->key;
  }
  val = p->val.i;
  delete p;
  --len;
  return val;
}

Here is the call graph for this function:

Definition at line 236 of file GHash.cc.

                                      {
  *iter = new GHashIter;
  (*iter)->h = -1;
  (*iter)->p = NULL;
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 55 of file GHash.h.

int GHash::len [private]

Definition at line 57 of file GHash.h.

int GHash::size [private]

Definition at line 56 of file GHash.h.

GHashBucket** GHash::tab [private]

Definition at line 58 of file GHash.h.


The documentation for this class was generated from the following files: