home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-1.ZIP / CLASSSRC.ZIP / HASHTBL.CPP < prev    next >
C/C++ Source or Header  |  1992-02-18  |  4KB  |  153 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  HASHTBL.CPP                                                           */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991                                  */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __HASHTBL_H )
  11. #include <HashTbl.h>
  12. #endif  // __HASHTBL_H
  13.  
  14. #ifndef __IOSTREAM_H
  15. #include <iostream.h>
  16. #endif
  17.  
  18. HashTable::HashTable( sizeType aPrime ) :
  19.     size( aPrime ),
  20.     table( aPrime ),
  21.     itemsInContainer(0)
  22. {
  23. }
  24.  
  25. void HashTable::add( Object& objectToAdd )
  26. {
  27.     hashValueType index = getHashValue( objectToAdd );
  28.     if( table[ index ] == 0 )
  29.         table[index] = new List;
  30.     ((List *)table[ index ])->add( objectToAdd );
  31.     itemsInContainer++;
  32. }
  33.  
  34. void HashTable::detach( Object& objectToDetach, DeleteType dt )
  35. {
  36.     hashValueType index = getHashValue( objectToDetach );
  37.     if( table[ index ] != 0 )
  38.         {
  39.         unsigned listSize = ((List *)table[ index ])->getItemsInContainer();
  40.         ((List *)table[ index ])->detach( objectToDetach, delItem(dt) );
  41.         if( ((List *)table[ index ])->getItemsInContainer() != listSize )
  42.             itemsInContainer--;
  43.         }
  44. }
  45.  
  46. static void setOwner( Object& list, void *owns )
  47. {
  48.     ((List&)list).ownsElements( *(TShouldDelete::DeleteType *)owns );
  49. }
  50.  
  51. void HashTable::flush( DeleteType dt )
  52. {
  53.     int shouldDel = delObj( dt );
  54.     table.forEach( setOwner, &shouldDel );
  55.     table.flush( 1 );
  56.     itemsInContainer = 0;
  57. }
  58.  
  59. Object& HashTable::findMember( Object& testObject ) const
  60. {
  61.     hashValueType index = getHashValue( testObject );
  62.     if( index >= table.limit() || table[ index ] == 0 )
  63.         {
  64.         return NOOBJECT;
  65.         }
  66.     return ((List *)table[ index ])->findMember( testObject );
  67. }
  68.  
  69. ContainerIterator& HashTable::initIterator() const
  70. {
  71.     return *( (ContainerIterator *)new HashTableIterator( *this ) );
  72. }
  73.  
  74. HashTableIterator::HashTableIterator( const HashTable& toIterate ) :
  75.                                 beingIterated( toIterate ),
  76.                                 listIterator(0)
  77. {
  78.     arrayIterator = new BI_IVectorIteratorImp<Object>( toIterate.table );
  79.     restart();
  80. }
  81.  
  82. HashTableIterator::~HashTableIterator()
  83. {
  84.     delete arrayIterator;
  85.     delete listIterator;
  86. }
  87.  
  88. Object& HashTableIterator::operator ++ ( int )
  89. {
  90.     Object& res = (listIterator == 0) ? NOOBJECT : listIterator->current();
  91.     scan();
  92.     return res;
  93. }
  94.  
  95. Object& HashTableIterator::operator ++ ()
  96. {
  97.     scan();
  98.     return (listIterator == 0) ? NOOBJECT : listIterator->current();
  99. }
  100.  
  101. HashTableIterator::operator int()
  102. {
  103.     return int(*arrayIterator);
  104. }
  105.  
  106. Object& HashTableIterator::current()
  107. {
  108.     return (listIterator == 0) ? NOOBJECT : listIterator->current();
  109. }
  110.  
  111. void HashTableIterator::restart()
  112. {
  113.     delete listIterator;
  114.  
  115.     arrayIterator->restart();
  116.     while( *arrayIterator != 0 && arrayIterator->current() == 0 )
  117.         (*arrayIterator)++;
  118.  
  119.     if( *arrayIterator != 0 )
  120.         {
  121.         Object *curList = arrayIterator->current();
  122.         listIterator = &(((List *)curList)->initIterator());
  123.         if( listIterator->current() == NOOBJECT )
  124.             scan();
  125.         }
  126.     else
  127.         listIterator = 0;
  128. }
  129.  
  130. void HashTableIterator::scan()
  131. {
  132.     if( listIterator == 0 )
  133.         return;
  134.  
  135.     (*listIterator)++;
  136.     while( listIterator != 0 && listIterator->current() == NOOBJECT )
  137.         {
  138.         delete listIterator;
  139.  
  140.         (*arrayIterator)++;
  141.         while( *arrayIterator != 0 && arrayIterator->current() == 0 )
  142.             (*arrayIterator)++;
  143.  
  144.         if( arrayIterator->current() != 0 )
  145.             {
  146.             Object *cur = arrayIterator->current();
  147.             listIterator = &(((Container *)cur)->initIterator());
  148.             }
  149.         else
  150.             listIterator = 0;
  151.         }
  152. }
  153.