home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CLASSINC.PAK / DICT.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  10.0 KB  |  367 lines

  1. //----------------------------------------------------------------------------
  2. // Borland BIDS Container Library
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.7  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined( CLASSLIB_DICT_H )
  9. #define CLASSLIB_DICT_H
  10.  
  11. #if !defined( CLASSLIB_DEFS_H )
  12. #include <classlib/defs.h>
  13. #endif
  14.  
  15. #if !defined( CLASSLIB_SHDDEL_H )
  16. #include <classlib/shddel.h>
  17. #endif
  18.  
  19. #if !defined( CLASSLIB_HASHIMP_H )
  20. #include <classlib/hashimp.h>
  21. #endif
  22.  
  23. #pragma option -Vo-
  24. #if defined( BI_CLASSLIB_NO_po )
  25. #pragma option -po-
  26. #endif
  27.  
  28. #if defined(BI_NAMESPACE)
  29. namespace ClassLib {
  30. #endif
  31.  
  32. /*------------------------------------------------------------------------*/
  33. /*                                                                        */
  34. /*  template <class T,class A> class TMDictionaryAsHashTable              */
  35. /*  template <class T,class A> class TMDictionaryAsHashTableIterator      */
  36. /*                                                                        */
  37. /*  Managed dictionary and iterator                                       */
  38. /*                                                                        */
  39. /*  Implements the dictionary container using a hash table.  Assumes      */
  40. /*  that T is of class TAssociation.                                      */
  41. /*                                                                        */
  42. /*------------------------------------------------------------------------*/
  43.  
  44. template <class T,class A> class TMDictionaryAsHashTableIterator;
  45.  
  46. template <class T,class A> class TMDictionaryAsHashTable :
  47.     public TShouldDelete
  48. {
  49. public:
  50.  
  51.     friend class TMDictionaryAsHashTableIterator<T,A>;
  52.  
  53.     TMDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
  54.         HashTable(size)
  55.         {
  56.         }
  57.  
  58.     int Add( const T& t )
  59.         {
  60.         return Find( t ) ? 0 : HashTable.Add( t );
  61.         }
  62.  
  63.     int Detach( const T& t, DeleteType dt = DefDelete );
  64.  
  65.     T *Find( const T& t )
  66.         {
  67.         return HashTable.Find( t );
  68.         }
  69.  
  70.     void Flush( DeleteType dt = DefDelete );
  71.  
  72.     void ForEach( void (*func)(T&, void *),
  73.                   void *args )
  74.         {
  75.         HashTable.ForEach( func, args );
  76.         }
  77.  
  78.     unsigned GetItemsInContainer() const
  79.         {
  80.         return HashTable.GetItemsInContainer();
  81.         }
  82.  
  83.     int IsEmpty() const
  84.         {
  85.         return HashTable.IsEmpty();
  86.         }
  87.  
  88. protected:
  89.  
  90.     TMHashTableImp<T,A> HashTable;
  91.  
  92. private:
  93.  
  94.     static void DeleteElement( T& t, void * );
  95.  
  96. };
  97.  
  98. template <class T, class A>
  99. int TMDictionaryAsHashTable<T,A>::Detach( const T& t,
  100.                                           DeleteType dt = DefDelete )
  101. {
  102.     if( DelObj(dt) )
  103.         {
  104.         T *assoc = Find(t);
  105.         if( assoc )
  106.             assoc->DeleteElements();
  107.         }
  108.     return HashTable.Detach( t );
  109. }
  110.  
  111. template <class T, class A>
  112. void TMDictionaryAsHashTable<T,A>::Flush(DeleteType dt)
  113. {
  114.     if( DelObj(dt) )
  115.         {
  116.         HashTable.ForEach( DeleteElement, 0 );
  117.         }
  118.     HashTable.Flush();
  119. }
  120.  
  121. template <class T, class A>
  122. void TMDictionaryAsHashTable<T,A>::DeleteElement( T& t, void * )
  123. {
  124.     t.DeleteElements();
  125. }
  126.  
  127. template <class T,class A>
  128. class TMDictionaryAsHashTableIterator :
  129.     public TMHashTableIteratorImp<T,A>
  130. {
  131. public:
  132.  
  133.     TMDictionaryAsHashTableIterator( const TMDictionaryAsHashTable<T,A>& t ) :
  134.         TMHashTableIteratorImp<T,A>( t.HashTable )
  135.         {
  136.         }
  137.  
  138. };
  139.  
  140. /*------------------------------------------------------------------------*/
  141. /*                                                                        */
  142. /*  template <class T> class TDictionaryAsHashTable                       */
  143. /*  template <class T> class TDictionaryAsHashTableIterator               */
  144. /*                                                                        */
  145. /*  Standard dictionary and iterator                                      */
  146. /*                                                                        */
  147. /*------------------------------------------------------------------------*/
  148.  
  149. template <class T> class TDictionaryAsHashTable :
  150.     public TMDictionaryAsHashTable<T,TStandardAllocator>
  151. {
  152.  
  153. public:
  154.  
  155.     TDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
  156.         TMDictionaryAsHashTable<T,TStandardAllocator>(size)
  157.         {
  158.         }
  159.  
  160. };
  161.  
  162. template <class T>
  163. class TDictionaryAsHashTableIterator :
  164.     public TMDictionaryAsHashTableIterator<T,TStandardAllocator>
  165. {
  166. public:
  167.  
  168.     TDictionaryAsHashTableIterator( const TDictionaryAsHashTable<T>& t ) :
  169.         TMDictionaryAsHashTableIterator<T,TStandardAllocator>( t )
  170.         {
  171.         }
  172.  
  173. };
  174.  
  175. /*------------------------------------------------------------------------*/
  176. /*                                                                        */
  177. /*  template <class T,class A> class TMIDictionaryAsHashTable             */
  178. /*  template <class T,class A> class TMIDictionaryAsHashTableIterator     */
  179. /*                                                                        */
  180. /*  Managed indirect dictionary and iterator                              */
  181. /*                                                                        */
  182. /*------------------------------------------------------------------------*/
  183.  
  184. template <class T,class A> class TMIDictionaryAsHashTableIterator;
  185.  
  186. template <class T,class A> class TMIDictionaryAsHashTable :
  187.     public TShouldDelete
  188. {
  189.  
  190. public:
  191.  
  192.     friend class TMIDictionaryAsHashTableIterator<T,A>;
  193.  
  194.     TMIDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
  195.         HashTable(size)
  196.         {
  197.         }
  198.  
  199.     int Add( T *t )
  200.         {
  201.         return Find( t ) ? 0 : HashTable.Add( t );
  202.         }
  203.  
  204.     int Detach( T *t, DeleteType dt = DefDelete );
  205.  
  206.     T *Find( T *t )
  207.         {
  208.         return HashTable.Find( t );
  209.         }
  210.  
  211.     void Flush( DeleteType dt = DefDelete );
  212.  
  213.     void ForEach( void (*func)(T&, void *),
  214.                   void *args )
  215.         {
  216.         HashTable.ForEach( func, args );
  217.         }
  218.  
  219.     unsigned GetItemsInContainer() const
  220.         {
  221.         return HashTable.GetItemsInContainer();
  222.         }
  223.  
  224.     int IsEmpty() const
  225.         {
  226.         return HashTable.IsEmpty();
  227.         }
  228.  
  229. protected:
  230.  
  231.     TMIHashTableImp<T,A> HashTable;
  232.  
  233. private:
  234.  
  235.     static void DeleteElement( T& t, void * );
  236.  
  237. };
  238.  
  239. template <class T, class A>
  240. int TMIDictionaryAsHashTable<T,A>::Detach( T *t, DeleteType dt = DefDelete )
  241. {
  242.     if( DelObj(dt) )
  243.         {
  244.         T *assoc = Find(t);
  245.         if( assoc )
  246.             assoc->DeleteElements();
  247.         }
  248.     return HashTable.Detach( t, DelObj(dt) );
  249. }
  250.  
  251. template <class T, class A>
  252. void TMIDictionaryAsHashTable<T,A>::Flush( DeleteType dt = DefDelete )
  253. {
  254.     if( DelObj(dt) )
  255.         {
  256.         HashTable.ForEach( DeleteElement, 0 );
  257.         }
  258.     HashTable.Flush(DelObj(dt));
  259. }
  260.  
  261. template <class T, class A>
  262. void TMIDictionaryAsHashTable<T,A>::DeleteElement( T& t, void * )
  263. {
  264.     t.DeleteElements();
  265. }
  266.  
  267. template <class T,class A>
  268. class TMIDictionaryAsHashTableIterator :
  269.     public TMIHashTableIteratorImp<T,A>
  270. {
  271. public:
  272.  
  273.     TMIDictionaryAsHashTableIterator( const TMIDictionaryAsHashTable<T,A>& t ) :
  274.         TMIHashTableIteratorImp<T,A>( t.HashTable )
  275.         {
  276.         }
  277.  
  278. };
  279.  
  280. /*------------------------------------------------------------------------*/
  281. /*                                                                        */
  282. /*  template <class T> class TIDictionaryAsHashTable                      */
  283. /*  template <class T> class TIDictionaryAsHashTableIterator              */
  284. /*                                                                        */
  285. /*  Standard indirect dictionary and iterator                             */
  286. /*                                                                        */
  287. /*------------------------------------------------------------------------*/
  288.  
  289. template <class T> class TIDictionaryAsHashTableIterator;
  290.  
  291. template <class T> class TIDictionaryAsHashTable :
  292.     public TMIDictionaryAsHashTable<T,TStandardAllocator>
  293. {
  294.  
  295. public:
  296.  
  297.     friend class TIDictionaryAsHashTableIterator<T>;
  298.  
  299.     TIDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
  300.         TMIDictionaryAsHashTable<T,TStandardAllocator>(size)
  301.         {
  302.         }
  303.  
  304. };
  305.  
  306. template <class T> class TIDictionaryAsHashTableIterator :
  307.     public TMIDictionaryAsHashTableIterator<T,TStandardAllocator>
  308. {
  309. public:
  310.  
  311.     TIDictionaryAsHashTableIterator( const TIDictionaryAsHashTable<T>& t ) :
  312.         TMIDictionaryAsHashTableIterator<T,TStandardAllocator>( t )
  313.         {
  314.         }
  315.  
  316. };
  317.  
  318. /*------------------------------------------------------------------------*/
  319. /*                                                                        */
  320. /*  template <class T> class TDictionary                                  */
  321. /*  template <class T> class TDictionaryIterator                          */
  322. /*                                                                        */
  323. /*  Easy names for TDictionaryAsHashTable and                             */
  324. /*  TDictionaryAsHashTableIterator                                        */
  325. /*                                                                        */
  326. /*------------------------------------------------------------------------*/
  327.  
  328. template <class T> class TDictionary :
  329.     public TDictionaryAsHashTable<T>
  330. {
  331.  
  332. public:
  333.  
  334.     TDictionary( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
  335.         TDictionaryAsHashTable<T>( size )
  336.         {
  337.         }
  338.  
  339. };
  340.  
  341. template <class T> class TDictionaryIterator :
  342.     public TDictionaryAsHashTableIterator<T>
  343. {
  344.  
  345. public:
  346.  
  347.  
  348.     TDictionaryIterator( const TDictionary<T>& a ) :
  349.         TDictionaryAsHashTableIterator<T>(a)
  350.         {
  351.         }
  352.  
  353. };
  354.  
  355. #if defined(BI_NAMESPACE)
  356. }   // namespace ClassLib
  357. #endif
  358.  
  359. #if defined( BI_CLASSLIB_NO_po )
  360. #pragma option -po.
  361. #endif
  362.  
  363. #pragma option -Vo.
  364.  
  365. #endif  // CLASSLIB_DICT_H
  366.  
  367.