Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
HashMgr Class Reference

#include <hashmgr.hxx>

Collaboration diagram for HashMgr:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 HashMgr (const char *tpath)
 ~HashMgr ()
struct hentrylookup (const char *) const
int hash (const char *) const
struct hentrywalk_hashtable (int &col, struct hentry *hp) const

Private Member Functions

 HashMgr (const HashMgr &)
HashMgroperator= (const HashMgr &)
int load_tables (const char *tpath)
int add_word (const char *word, int wl, const char *ap, int al)

Private Attributes

int tablesize
struct hentrytableptr

Detailed Description

Definition at line 6 of file hashmgr.hxx.


Constructor & Destructor Documentation

HashMgr::HashMgr ( const char *  tpath)

Definition at line 18 of file hashmgr.cpp.

{
  tablesize = 0;
  tableptr = NULL;
  int ec = load_tables(tpath);
  if (ec) {
    /* error condition - what should we do here */
    fprintf(stderr,"Hash Manager Error : %d\n",ec);
    fflush(stderr);
    if (tableptr) {
      free(tableptr);
    }
    tablesize = 0;
  }
}

Here is the call graph for this function:

Definition at line 35 of file hashmgr.cpp.

{
  if (tableptr) {
    // now pass through hash table freeing up everything
    // go through column by column of the table
    for (int i=0; i < tablesize; i++) {
      struct hentry * pt = &tableptr[i];
      struct hentry * nt = NULL;
      if (pt) {
       if (pt->word) free(pt->word);
        if (pt->astr) free(pt->astr);
        pt = pt->next;
      }
      while(pt) {
        nt = pt->next;
       if (pt->word) free(pt->word);
        if (pt->astr) free(pt->astr);
        free(pt);
       pt = nt;
      }
    }
    free(tableptr);
  }
  tablesize = 0;
}
HashMgr::HashMgr ( const HashMgr ) [private]

Member Function Documentation

int HashMgr::add_word ( const char *  word,
int  wl,
const char *  ap,
int  al 
) [private]

Definition at line 82 of file hashmgr.cpp.

{
    int i = hash(word);
    struct hentry * dp = &tableptr[i];
    struct hentry* hp;
    if (dp->word == NULL) {
       dp->wlen = wl;
       dp->alen = al;
       dp->word = mystrdup(word);
       dp->astr = mystrdup(aff);
       dp->next = NULL;
       if ((wl) && (dp->word == NULL)) return 1;
       if ((al) && (dp->astr == NULL)) return 1;
    } else {
       hp = (struct hentry *) malloc (sizeof(struct hentry));
       if (hp == NULL) return 1;
       hp->wlen = wl;
       hp->alen = al;
       hp->word = mystrdup(word);
       hp->astr = mystrdup(aff);
       hp->next = NULL;      
       while (dp->next != NULL) dp=dp->next; 
       dp->next = hp;
       if ((wl) && (hp->word == NULL)) return 1;
       if ((al) && (hp->astr == NULL)) return 1;
    }
    return 0;
}     

Here is the call graph for this function:

int HashMgr::hash ( const char *  word) const

Definition at line 197 of file hashmgr.cpp.

{
    long  hv = 0;
    for (int i=0; i < 4  &&  *word != 0; i++)
       hv = (hv << 8) | (*word++);
    while (*word != 0) {
      ROTATE(hv,ROTATE_LEN);
      hv ^= (*word++);
    }
    return (unsigned long) hv % tablesize;
}
int HashMgr::load_tables ( const char *  tpath) [private]

Definition at line 143 of file hashmgr.cpp.

{
  int wl, al;
  char * ap;

  // raw dictionary - munched file
  FILE * rawdict = fopen(tpath, "r");
  if (rawdict == NULL) return 1;

  // first read the first line of file to get hash table size */
  char ts[MAXDELEN];
  if (! fgets(ts, MAXDELEN-1,rawdict)) return 2;
  mychomp(ts);
  tablesize = atoi(ts);
  if (!tablesize) return 4; 
  tablesize = tablesize + 5;
  if ((tablesize %2) == 0) tablesize++;

  // allocate the hash table
  tableptr = (struct hentry *) calloc(tablesize, sizeof(struct hentry));
  if (! tableptr) return 3;

  // loop through all words on much list and add to hash
  // table and create word and affix strings

  while (fgets(ts,MAXDELEN-1,rawdict)) {
    mychomp(ts);
    // split each line into word and affix char strings
    ap = strchr(ts,'/');
    if (ap) {
      *ap = '\0';
      ap++;
      al = strlen(ap);
    } else {
      al = 0;
      ap = NULL;
    }

    wl = strlen(ts);

    // add the word and its index
    if (add_word(ts,wl,ap,al)) 
      return 5;;

  }

  fclose(rawdict);
  return 0;
}

Here is the call graph for this function:

struct hentry * HashMgr::lookup ( const char *  word) const [read]

Definition at line 65 of file hashmgr.cpp.

{
    struct hentry * dp;
    if (tableptr) {
       dp = &tableptr[hash(word)];
       if (dp->word == NULL) return NULL;
       for (  ;  dp != NULL;  dp = dp->next) {
          if (strcmp(word,dp->word) == 0) return dp;
       }
    }
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

HashMgr& HashMgr::operator= ( const HashMgr ) [private]
struct hentry * HashMgr::walk_hashtable ( int col,
struct hentry hp 
) const [read]

Definition at line 114 of file hashmgr.cpp.

{
  //reset to start
  if ((col < 0) || (hp == NULL)) {
    col = -1;
    hp = NULL;
  }

  if (hp && hp->next != NULL) {
    hp = hp->next;
  } else {
    col++;
    hp = (col < tablesize) ? &tableptr[col] : NULL;
    // search for next non-blank column entry
    while (hp && (hp->word == NULL)) {
        col ++;
        hp = (col < tablesize) ? &tableptr[col] : NULL;
    }
    if (col < tablesize) return hp;
    hp = NULL;
    col = -1;
  }
  return hp;
}

Here is the caller graph for this function:


Member Data Documentation

struct hentry* HashMgr::tableptr [private]

Definition at line 9 of file hashmgr.hxx.

Definition at line 8 of file hashmgr.hxx.


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