home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / grafik / cgazv5n3 / dynhash.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-28  |  3.3 KB  |  67 lines

  1. /********* Listing 3 *************************** DYNHASH.HPP ******
  2. *  Copyright (C) 1990 by Tom Provenzano
  3. *  Implements a dynamic hashing scheme called linear hashing and
  4. *  is based on the article "Dynamic Hash Tables" by Per-Ake Larson,
  5. *  Communications of the ACM, v31 n4, April 1988, pp. 446-457.
  6. *******************************************************************/
  7.  
  8. #include <generic.hpp>  // Zortech's equivalent of generic.h
  9. #include <slist.hpp>    // use's Zortech Tools singly-linked lists & iterators
  10. #include "dynarray.hpp" // will inherit dynamic array class
  11.  
  12. // status parameters
  13.  
  14. const int OK             =  0;    // successful operation
  15. const int NOT_FOUND      = -100;  // item not found
  16. const int NO_MEMORY      = -101;  // memory allocation failure
  17. const int NOT_INSERTED   = -102;  // item not put in hashtable
  18. const int NOT_REMOVED    = -103;  // item not removed from hashtable
  19. const int EXPAND_ERROR   = -104;  // error during hashtable expansion
  20. const int CONTRACT_ERROR = -105;  // error during hashtable contraction
  21.  
  22. #define gdynhash(type) name2(type,gdynhash)
  23. #define gdynhashdeclare(type)                                           \
  24. struct gdynhash(type) : dynhash                                         \
  25. {                                                                       \
  26.   gdynhash(type)() : (sizeof(type)) {}                                  \
  27.   int   insert(type* p)   { return dynhash::insert(p); }                \
  28.   int   remove(type* p)   { return dynhash::remove(p); }                \
  29.   type* lookup(type* p)   { return (type* )dynhash::lookup(p);  }       \
  30.   type* get_next( void )  { return (type* )dynhash::get_next(); }   \
  31. };
  32.  
  33. typedef zSList* pzSList;  // because 'declare' won't accept pointer syntax
  34. declare(gdynarray,pzSList);
  35.  
  36. class dynhash
  37. {
  38. private:
  39.   int N;                        // minimum size of hash table
  40.   int splitp;                   // pointer to next bucket to split
  41.   int maxp;                     // upper bound on p during this expansion
  42.   int CurrentSize;              // current size (num records in table)
  43.   int MinLoadFactor;            // lower bound on load factor
  44.   int MaxLoadFactor;            // upper bound on load factor
  45.   int Threshold;                // load factor threshold reached (Y/N)
  46.   int recsize;                  // size of a table record (bytes)
  47.   int err;                      // class's error flag
  48.  
  49.   int hash( char * );           // compute hash function
  50.   int ExpandTable();            // expand hash table
  51.   int ContractTable();          // contract hash table
  52.   int ConvertKey( char * );     // convert key to integer value
  53.   int insert_rec( void * );     // insert record in hash table
  54.   int remove_rec( void * );     // remove record from hash table
  55.   gdynarray(pzSList) hashtable; // declare hash table dynarray of pzSList*'s
  56.  
  57. public:
  58.   dynhash( int );               // constructor
  59.   ~dynhash();                   // destructor
  60.   int insert( void * );         // insert entry in hash table
  61.   int remove( void * );         // remove entry from hash table
  62.   void *get_next( void );       // get next entry in hash table
  63.   void *lookup( void * );       // look up entry in hash table
  64.   int size() { return CurrentSize; } // report number of records in table
  65.   int error() { int x = err; err = 0; return x; } // error handler
  66. };