home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / SIHshTbl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  6.4 KB  |  231 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        SIHshTbl.cpp
  3.  
  4.     Contains:    Implementation of SIHashTable class
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     
  11. */
  12.  
  13. #ifndef _SIHSHTBL_
  14. #include "SIHshTbl.h"
  15. #endif
  16.  
  17. #ifndef _HshTbl_
  18. #include "HshTbl.h"
  19. #endif
  20.  
  21. #ifndef _EXCEPT_
  22. #include "Except.h"
  23. #endif
  24.  
  25. #ifndef _ODTYPES_
  26. #include "ODTypes.h"
  27. #endif
  28.  
  29. #ifndef _UTILERRS_
  30. #include "UtilErrs.h"
  31. #endif
  32.  
  33. #ifndef __AEOBJECTS__
  34. #include <AEObjects.h>
  35. #endif
  36.  
  37. #ifndef _ODDEBUG_
  38. #include "ODDebug.h"
  39. #endif
  40.  
  41. #pragma segment SIHashTable
  42.  
  43. //==============================================================================
  44. // Implementation comments
  45. //==============================================================================
  46.  
  47. /*
  48. The error codes returned from Hash Manager functions are incompletely
  49. documented.
  50. Therefore, I make some pessimistic assumptions. If creating a new hash table or
  51. any other routine that may need to allocate memory fails, I assume that it's
  52. out of memory and throw kODErrOutOfMemory. Otherwise, I don't check error codes
  53. explicitly.
  54. */
  55.  
  56. #define kMaxValueSize 10
  57. //==============================================================================
  58. // SIHashTable
  59. //==============================================================================
  60.  
  61. //------------------------------------------------------------------------------
  62. // SIHashTable::SIHashTable
  63. //------------------------------------------------------------------------------
  64.  
  65. SIHashTable::SIHashTable()
  66. {
  67.     fSIHashTable = kODNULL;
  68.     fValueSize = 0 ;
  69. }
  70.  
  71. //------------------------------------------------------------------------------
  72. // SIHashTable::Initialize
  73. //------------------------------------------------------------------------------
  74.  
  75. void SIHashTable::Initialize(ODULong numEntries, ODUShort keySize,
  76.                                 ODUShort valueSize, ODBoolean inSysHeap)
  77. {
  78.     if ( valueSize > kMaxValueSize )
  79.         THROW( kODErrHashValueSizeTooBig );
  80.     OSErr error = NewHashTable(numEntries, keySize, valueSize,
  81.             (MemProcBlock*)NULL, inSysHeap, &fSIHashTable);
  82.     if (error || !fSIHashTable)
  83.         THROW(kODErrOutOfMemory);
  84.     fValueSize = valueSize ;
  85. }
  86.  
  87. //------------------------------------------------------------------------------
  88. // SIHashTable::~SIHashTable
  89. //------------------------------------------------------------------------------
  90.  
  91. SIHashTable::~SIHashTable()
  92. {
  93.     if (fSIHashTable)
  94.         DisposeHashTable(&fSIHashTable, NULL);
  95. }
  96.  
  97. //------------------------------------------------------------------------------
  98. // SIHashTable::ReplaceEntry
  99. //------------------------------------------------------------------------------
  100.  
  101. void SIHashTable::ReplaceEntry(ODKeyPtr key, ODEntryPtr value)
  102. {
  103.     OSErr error = ::ReplaceEntry(fSIHashTable, NULL, (KeyPtr)key, (HEntryPtr)value);
  104.     if (error)
  105.         THROW(kODErrOutOfMemory);
  106. }
  107.  
  108. //------------------------------------------------------------------------------
  109. // SIHashTable::RemoveEntry
  110. //------------------------------------------------------------------------------
  111.  
  112. void SIHashTable::RemoveEntry(ODKeyPtr key)
  113. {
  114.     // As far as I could tell, RemoveKeyEntry never returns an error.
  115.  
  116.     RemoveKeyEntry(fSIHashTable, NULL, (KeyPtr)key);
  117. }
  118.  
  119. //------------------------------------------------------------------------------
  120. // SIHashTable::GetValue
  121. //------------------------------------------------------------------------------
  122.  
  123. ODBoolean SIHashTable::GetValue(ODKeyPtr key, ODEntryPtr value)
  124. {
  125.     if (GetKeyValue(fSIHashTable, NULL, (KeyPtr)key, (HEntryPtr)value) != noErr)
  126.         return kODFalse;
  127.     else
  128.         return kODTrue;
  129. }
  130.  
  131. //------------------------------------------------------------------------------
  132. // SIHashTable::Exists
  133. //------------------------------------------------------------------------------
  134.  
  135. ODBoolean SIHashTable::Exists(ODKeyPtr key)
  136. {
  137.     char someScratchValueSpace[kMaxValueSize] ;
  138.     WASSERT(fValueSize <= kMaxValueSize);
  139. //    if ( fValueSize > kMaxValueSize )
  140. //        THROW( -1000 ) ;                // <eeh> need a real # here
  141.  
  142.     OSErr err = GetKeyValue( fSIHashTable, kODNULL, (KeyPtr)key,
  143.         (HEntryPtr)someScratchValueSpace ) ;
  144.     return err == noErr ;
  145. }
  146.  
  147. //------------------------------------------------------------------------------
  148. // SIHashTable::GetSIHashTable
  149. //------------------------------------------------------------------------------
  150.  
  151. HashTable SIHashTable::GetSIHashTable()
  152. {
  153.     return fSIHashTable;
  154. }
  155.  
  156. //==============================================================================
  157. // SIHashTableIterator
  158. //==============================================================================
  159.  
  160. //------------------------------------------------------------------------------
  161. // SIHashTableIterator::SIHashTableIterator
  162. //------------------------------------------------------------------------------
  163.  
  164. SIHashTableIterator::SIHashTableIterator(SIHashTable* table)
  165. {
  166.     fTable = table;
  167.     fIndex = 0;
  168.     fDone = kODFalse;
  169. }
  170.  
  171. //------------------------------------------------------------------------------
  172. // SIHashTableIterator::~SIHashTableIterator
  173. //------------------------------------------------------------------------------
  174.  
  175. SIHashTableIterator::~SIHashTableIterator()
  176. {
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. // SIHashTableIterator::First
  181. //------------------------------------------------------------------------------
  182.  
  183. void SIHashTableIterator::First(ODKeyPtr key, ODEntryPtr value)
  184. {
  185.     if (!this->GetNext(key, value))
  186.         fDone = kODTrue;
  187. }
  188.  
  189. //------------------------------------------------------------------------------
  190. // SIHashTableIterator::Next
  191. //------------------------------------------------------------------------------
  192.  
  193. void SIHashTableIterator::Next(ODKeyPtr key, ODEntryPtr value)
  194. {
  195.     if (!this->GetNext(key, value))
  196.         fDone = kODTrue;
  197. }
  198.  
  199. //------------------------------------------------------------------------------
  200. // SIHashTableIterator::IsNotComplete
  201. //------------------------------------------------------------------------------
  202.  
  203. ODBoolean SIHashTableIterator::IsNotComplete()
  204. {
  205.     return !fDone;
  206. }
  207.  
  208. //------------------------------------------------------------------------------
  209. // SIHashTableIterator::GetNext
  210. //
  211. //    Returns true if another entry can be found, false otherwise.
  212. //------------------------------------------------------------------------------
  213.  
  214. ODBoolean SIHashTableIterator::GetNext(ODKeyPtr key, ODEntryPtr value)
  215. {
  216.     OSErr    result = noErr;
  217.     
  218.     do
  219.     {
  220.         result = GetIndexedEntry(fTable->GetSIHashTable(),
  221.                             kODNULL, fIndex, (KeyPtr)key, (HEntryPtr)value);
  222.         ++fIndex;
  223.         if (result == noErr)
  224.             return kODTrue;
  225.     }
  226.     while (result != kAEErrEndOfTable);
  227.  
  228.     return kODFalse;
  229. }
  230.  
  231.