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

  1. //
  2. //  wchiter.h    Definitions for the WATCOM Container Hash Iterator Classes
  3. //
  4. //  Copyright by WATCOM International Corp. 1988-1996.  All rights reserved.
  5. //
  6. #ifndef _WCHITER_H_INCLUDED
  7. #define _WCHITER_H_INCLUDED
  8.  
  9. #ifndef __cplusplus
  10. #error wchiter.h is for use with C++
  11. #endif
  12.  
  13. #ifndef _WCDEFS_H_INCLUDED
  14.  #include <wcdefs.h>
  15. #endif
  16. #ifndef _WCEXCEPT_H_INCLUDED
  17.  #include <wcexcept.h>
  18. #endif
  19. #ifndef _WCHASH_H_INCLUDED
  20.  #include <wchash.h>
  21. #endif
  22.  
  23.  
  24. //
  25. // The WCHashIterBase is the base class for all hash iterator classes
  26. //
  27. // WCIterExcept is used as a base class to provide exception handling
  28. //
  29. //
  30.  
  31. class WCHashIterBase : public WCIterExcept {
  32. private:
  33.     typedef WCSLink BaseHashLink;
  34.  
  35. protected:
  36.     const WCHashBase *          curr_hash;
  37.     WCIsvSListIter<BaseHashLink> bucket_iter;
  38.     unsigned                    curr_bucket;
  39.     int                         at_end;
  40.  
  41.     _WPRTLINK WCbool base_advance();
  42.  
  43.     inline WCbool base_hash_valid() const {
  44.         return( ( curr_hash != 0 )&&( curr_hash->num_buckets > 0 ) );
  45.     };
  46.  
  47. public:
  48.     inline WCHashIterBase() : curr_hash( 0 ), bucket_iter()
  49.                                 , curr_bucket( 0 ), at_end( 0 ) {};
  50.  
  51.     inline WCHashIterBase( const WCHashBase& hash )
  52.                 : curr_hash( &hash ), bucket_iter() {
  53.         reset();
  54.     };
  55.  
  56.     inline virtual ~WCHashIterBase() = 0;
  57.  
  58.     _WPRTLINK void reset();
  59. };
  60.  
  61.  
  62. WCHashIterBase::~WCHashIterBase() {};
  63.  
  64.  
  65.  
  66. //
  67. // The WCValHashTableIter is the iterator for the WCValHashTable class
  68. //
  69. // WCIterExcept is used as a base class to provide exception handling
  70. //
  71.  
  72. template<class Type>
  73. class WCValHashTableIter : public WCHashIterBase {
  74. protected:
  75.     typedef WCHashLink<Type> HashLink;
  76.     
  77.     // get the current hashlink object, or 0 if current undefined
  78.     WCHashLink<Type> *base_curr_hashlink() const;
  79.  
  80. public:
  81.     inline WCValHashTableIter() {};
  82.  
  83.     inline WCValHashTableIter( const WCValHashTable<Type>& hash )
  84.                 : WCHashIterBase( hash ) {};
  85.  
  86.     inline virtual ~WCValHashTableIter() {};
  87.  
  88.     inline const WCValHashTable<Type> *container() const {
  89.         if( curr_hash == 0 ) {
  90.             base_throw_undef_iter();
  91.         }
  92.         return( (const WCValHashTable<Type> *)curr_hash );
  93.     };
  94.  
  95.     Type current() const;
  96.  
  97.     inline void reset() {
  98.         WCHashIterBase::reset();
  99.     };
  100.  
  101.     inline void reset( const WCValHashTable<Type> &hash ) {
  102.         curr_hash = &hash;
  103.         reset();
  104.     };
  105.  
  106.     inline WCbool operator++() {
  107.         return( base_advance() );
  108.     }
  109.  
  110.     inline WCbool operator()() {
  111.         return( base_advance() );
  112.     }
  113. };
  114.  
  115.  
  116. template <class Type>
  117. WCHashLink<Type> *WCValHashTableIter<Type>::base_curr_hashlink() const {
  118.     HashLink *link = 0;
  119.     if( base_hash_valid() ) {
  120.         link = (HashLink *)bucket_iter.current();
  121.     } 
  122.     return( link );
  123. };
  124.  
  125.  
  126. template <class Type>
  127. Type WCValHashTableIter<Type>::current() const {
  128.     HashLink *link = base_curr_hashlink();
  129.     if( link == 0 ) {
  130.         // bucket_iter is off end or no hash to iterate over
  131.         base_throw_undef_item();
  132.         Type temp;
  133.         return( temp );
  134.     }
  135.     return( link->data );
  136. };
  137.  
  138.  
  139.  
  140.  
  141. //
  142. // The WCPtrHashTableIter is the iterator for the WCPtrHashTable class.
  143. //
  144.  
  145. template<class Type>
  146. class WCPtrHashTableIter : public WCValHashTableIter<void *> {
  147. public:
  148.     inline WCPtrHashTableIter() {};
  149.  
  150.     inline WCPtrHashTableIter( const WCPtrHashTable<Type>& hash )
  151.                 : WCValHashTableIter( hash ) {};
  152.  
  153.     inline virtual ~WCPtrHashTableIter() {};
  154.  
  155.     inline const WCPtrHashTable<Type> *container() const {
  156.         return( (const WCPtrHashTable<Type> *)WCValHashTableIter::container() );
  157.     };
  158.  
  159.     inline Type *current() const {
  160.         return( (Type *)WCValHashTableIter::current() );
  161.     };
  162.  
  163.     inline void reset() {
  164.         WCValHashTableIter::reset();
  165.     };
  166.  
  167.     inline void reset( const WCPtrHashTable<Type> &hash ) {
  168.         WCValHashTableIter::reset( hash );
  169.     };
  170. };
  171.  
  172.  
  173.  
  174.  
  175. //
  176. // The WCValHashSetIter is the iterator for the WCValHashSet class.
  177. //
  178.  
  179. template<class Type>
  180. class WCValHashSetIter : public WCValHashTableIter<Type> {
  181. public:
  182.     inline WCValHashSetIter() {};
  183.  
  184.     inline WCValHashSetIter( const WCValHashSet<Type>& hash )
  185.                 : WCValHashTableIter( hash ) {};
  186.  
  187.     inline virtual ~WCValHashSetIter() {};
  188.  
  189.     inline const WCValHashSet<Type> *container() const {
  190.         return( (const WCValHashSet<Type> *)WCValHashTableIter::container() );
  191.     };
  192.  
  193.     inline void reset() {
  194.         WCValHashTableIter::reset();
  195.     };
  196.  
  197.     inline void reset( const WCValHashSet<Type> &hash ) {
  198.         WCValHashTableIter::reset( hash );
  199.     };
  200. };
  201.  
  202.  
  203.  
  204.  
  205. //
  206. // The WCPtrHashSetIter is the iterator for the WCPtrHashSet class.
  207. //
  208.  
  209. template<class Type>
  210. class WCPtrHashSetIter : public WCPtrHashTableIter<Type> {
  211. public:
  212.     inline WCPtrHashSetIter() {};
  213.  
  214.     inline WCPtrHashSetIter( const WCPtrHashSet<Type>& hash )
  215.                 : WCPtrHashTableIter( hash ) {};
  216.  
  217.     inline virtual ~WCPtrHashSetIter() {};
  218.  
  219.     inline const WCPtrHashSet<Type> *container() const {
  220.         return( (const WCPtrHashSet<Type> *)WCPtrHashTableIter::container() );
  221.     };
  222.  
  223.     inline void reset() {
  224.         WCPtrHashTableIter::reset();
  225.     };
  226.  
  227.     inline void reset( const WCPtrHashSet<Type> &hash ) {
  228.         WCPtrHashTableIter::reset( hash );
  229.     };
  230. };
  231.  
  232.  
  233.  
  234.  
  235. //
  236. // The WCValHashDictIter is the iterator for the WCValHashDict class.
  237. //
  238. // This class inherits privately to hide the current member function.
  239. //
  240.  
  241. template<class Key, class Value>
  242. class WCValHashDictIter
  243.         : private WCValHashTableIter<WCHashDictKeyVal<Key, Value > > {
  244. public:
  245.     inline WCValHashDictIter() {};
  246.  
  247.     inline WCValHashDictIter( const WCValHashDict<Key, Value>& hash )
  248.                 : WCValHashTableIter( hash ) {};
  249.  
  250.     inline virtual ~WCValHashDictIter() {};
  251.  
  252.     inline const WCValHashDict<Key, Value> *container() const {
  253.         return( (const WCValHashDict<Key, Value> *)WCValHashTableIter
  254.                                                         ::container() );
  255.     };
  256.  
  257.     inline wciter_state exceptions() const {
  258.         return( WCValHashTableIter::exceptions() );
  259.     };
  260.  
  261.     inline wciter_state exceptions( wciter_state const set_flags ) {
  262.         return( WCValHashTableIter::exceptions( set_flags ) );
  263.     };
  264.  
  265.     Key key() const;
  266.  
  267.     inline void reset() {
  268.         WCValHashTableIter::reset();
  269.     };
  270.  
  271.     inline void reset( const WCValHashDict<Key, Value> &hash ) {
  272.         WCValHashTableIter::reset( hash );
  273.     };
  274.  
  275.     Value value() const;
  276.  
  277.     inline WCbool operator++() {
  278.         return( WCValHashTableIter::operator++() );
  279.     };
  280.  
  281.     inline WCbool operator()() {
  282.         return( WCValHashTableIter::operator()() );
  283.     };
  284. };
  285.  
  286.  
  287. template <class Key, class Value>
  288. Key WCValHashDictIter<Key, Value>::key() const {
  289.     HashLink *link = base_curr_hashlink();
  290.     if( link == 0 ) {
  291.         // bucket_iter is off end or no hash to iterate over
  292.         base_throw_undef_item();
  293.         Key temp;
  294.         return( temp );
  295.     }
  296.     return( link->data.key );
  297. };
  298.  
  299.  
  300. template <class Key, class Value>
  301. Value WCValHashDictIter<Key, Value>::value() const {
  302.     HashLink *link = base_curr_hashlink();
  303.     if( link == 0 ) {
  304.         // bucket_iter is off end or no hash to iterate over
  305.         base_throw_undef_item();
  306.         Value temp;
  307.         return( temp );
  308.     }
  309.     return( link->data.value );
  310. };
  311.  
  312.  
  313.  
  314.  
  315. //
  316. // The WCPtrHashDictIter is the iterator for the WCPtrHashDict class.
  317. //
  318.  
  319. template<class Key, class Value>
  320. class WCPtrHashDictIter
  321.         : public WCValHashDictIter<void *, void *> {
  322. public:
  323.     inline WCPtrHashDictIter() {};
  324.  
  325.     inline WCPtrHashDictIter( const WCPtrHashDict<Key, Value>& hash )
  326.                 : WCValHashDictIter( hash ) {};
  327.  
  328.     inline virtual ~WCPtrHashDictIter() {};
  329.  
  330.     inline const WCPtrHashDict<Key, Value> *container() const {
  331.         return( (const WCPtrHashDict<Key, Value> *)WCValHashDictIter
  332.                                                         ::container() );
  333.     };
  334.  
  335.     inline Key * key() const {
  336.         return( (Key *)WCValHashDictIter::key() );
  337.     };
  338.  
  339.     inline void reset() {
  340.         WCValHashDictIter::reset();
  341.     };
  342.  
  343.     inline void reset( const WCPtrHashDict<Key, Value> &hash ) {
  344.         WCValHashDictIter::reset( hash );
  345.     };
  346.  
  347.     Value *value() const {
  348.         return( (Value *)WCValHashDictIter::value() );
  349.     };
  350. };
  351.  
  352. #endif
  353.