home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / h.z / WCHBASE.H < prev    next >
C/C++ Source or Header  |  1996-07-24  |  5KB  |  175 lines

  1. //
  2. //  wchbase.h    Definitions for the base classes used by
  3. //               the WATCOM Container Hash Classes
  4. //
  5. //  Copyright by WATCOM International Corp. 1988-1996.  All rights reserved.
  6. //
  7. #ifndef _WCHBASE_H_INCLUDED
  8. #define _WCHBASE_H_INCLUDED
  9.  
  10. #ifndef __cplusplus
  11. #error wchbase.h is for use with C++
  12. #endif
  13.  
  14. #ifndef _WCDEFS_H_INCLUDED
  15.  #include <wcdefs.h>
  16. #endif
  17.  
  18. #if defined( new ) && defined( _WNEW_OPERATOR )
  19. #  undef new
  20. #endif
  21. #if defined( delete ) && defined( _WDELETE_OPERATOR )
  22. #  undef delete
  23. #endif
  24.  
  25.  
  26. //
  27. // The default number of buckets in a hash table.  If a second parameter is
  28. // passed to the hash table constructor, this value will not be used
  29. //
  30.  
  31. const unsigned WC_DEFAULT_HASH_SIZE = 101;
  32.     
  33.  
  34.  
  35.  
  36. //
  37. // Hash implementation object:
  38. // A singly linked list element for storing the values in the hash table
  39. //
  40.  
  41. template <class Type>
  42. class WCHashLink : public WCSLink {
  43. public:
  44.     Type data;
  45.  
  46.     // used by WCHashNew to be able to call a constructor on user allocator
  47.     // allocated memory
  48.     inline void * operator new( size_t, void * ptr ){
  49.     return( ptr );
  50.     }
  51.     inline WCHashLink( const Type & datum ) : data( datum ) {};
  52.     inline ~WCHashLink() {};
  53. };
  54.  
  55.  
  56.  
  57. //
  58. // Hash Dictionary implementation object:
  59. // Combines the Key and Value into one object 
  60. //
  61.  
  62. template <class Key, class Value>
  63. class WCHashDictKeyVal{
  64. public:
  65.     Key key;
  66.     Value value;
  67.  
  68.     inline WCHashDictKeyVal( const WCHashDictKeyVal &orig )
  69.         : key( orig.key ), value( orig.value ) {};
  70.     inline WCHashDictKeyVal( const Key &new_key, const Value &new_value ) 
  71.         : key( new_key ), value( new_value ) {};
  72.     inline WCHashDictKeyVal( const Key &new_key ) : key( new_key ) {};
  73.     inline WCHashDictKeyVal() {};
  74.     inline ~WCHashDictKeyVal() {};
  75.  
  76.     // this operator is NEVER used, but necessary for compilation
  77.     // (needed by WCValHashSet, inherited by WCValHashDict)
  78.     inline int operator==( const WCHashDictKeyVal & ) const {
  79.     return( 0 );
  80.     };
  81. };
  82.     
  83.  
  84.  
  85.  
  86. //
  87. // Provide base functionality for WATCOM container hash tables, sets and
  88. // dictionaries.
  89. //
  90. // WCExcept provides exception handling.
  91. //
  92. // This class is an abstract class so that objects of this type cannot be
  93. // created.
  94. //
  95.  
  96.  
  97. class WCHashBase : public WCExcept {
  98. protected:
  99.     WCIsvSList<WCSLink> *hash_array;
  100.     unsigned num_buckets;
  101.     unsigned num_entries;
  102.  
  103.     // link base non-templated class
  104.     typedef WCSLink BaseHashLink;
  105.     // pointer to element of templated type
  106.     typedef const void *TTypePtr;
  107.  
  108.     enum find_type {        // enumerations for base_find
  109.     FIND_FIRST,        // find first matching element
  110.     NEXT_MULT_FIND };    // find next matching element in bucket
  111.  
  112.     // copy constructor base
  113.     _WPRTLINK void base_construct( const WCHashBase * orig );
  114.  
  115.     // for copy constructor
  116.     virtual BaseHashLink *base_copy_link( const BaseHashLink *orig ) = 0;
  117.  
  118.     virtual int base_equivalent( BaseHashLink *elem1
  119.                        , TTypePtr elem2 ) const = 0;
  120.  
  121.     virtual unsigned base_get_bucket( TTypePtr elem ) const = 0;
  122.  
  123.     // for the resize member function
  124.     virtual unsigned base_get_bucket_for_link( BaseHashLink *link ) const = 0;
  125.  
  126.     _WPRTLINK BaseHashLink *base_find( TTypePtr elem, unsigned *bucket
  127.                    , unsigned *index, find_type type ) const;
  128.  
  129.     // common initialization code for the constructors
  130.     _WPRTLINK void base_init( unsigned buckets );
  131.  
  132.     _WPRTLINK BaseHashLink *base_remove_but_not_delete( TTypePtr elem );
  133.  
  134.     // the insert function for WCValHashSet and WCPtrHashSet
  135.     _WPRTLINK BaseHashLink *base_set_insert( TTypePtr elem );
  136.  
  137.     virtual BaseHashLink * WCHashNew( TTypePtr elem ) = 0;
  138.  
  139.     virtual void WCHashDelete( BaseHashLink *old_link ) = 0;
  140.  
  141.  
  142.     inline WCHashBase() : hash_array( 0 ), num_buckets( 0 )
  143.                 , num_entries( 0 ) {};
  144.  
  145.     inline WCHashBase( unsigned buckets ) {
  146.     base_init( buckets );
  147.     };
  148.  
  149.     virtual ~WCHashBase() = 0;
  150.  
  151.     _WPRTLINK void clear ();
  152.  
  153.     _WPRTLINK WCbool insert( TTypePtr elem );
  154.  
  155.     _WPRTLINK unsigned occurrencesOf( TTypePtr elem ) const;
  156.  
  157.     _WPRTLINK unsigned removeAll( TTypePtr elem );
  158.  
  159.     _WPRTLINK void resize( unsigned buckets );
  160.  
  161. public:
  162.     _WPRTLINK static unsigned bitHash( const void * data, size_t size );
  163.  
  164.     friend class WCHashIterBase;
  165. };
  166.  
  167. #if defined( _WNEW_OPERATOR )
  168. #  define new _WNEW_OPERATOR
  169. #endif
  170. #if defined( _WDELETE_OPERATOR )
  171. #  define delete _WDELETE_OPERATOR
  172. #endif
  173.     
  174. #endif
  175.