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

  1. //----------------------------------------------------------------------------
  2. // Borland BIDS Container Library
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.9  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined( CLASSLIB_ASSOC_H )
  9. #define CLASSLIB_ASSOC_H
  10.  
  11. #if !defined( CLASSLIB_DEFS_H )
  12. # include <classlib/defs.h>
  13. #endif
  14. #if !defined( CLASSLIB_ALLOCTR_H )
  15. # include <classlib/alloctr.h>
  16. #endif
  17. #if !defined( CLASSLIB_VOIDP_H )
  18. # include <classlib/voidp.h>
  19. #endif
  20. #if !defined( CLASSLIB_HASHIMP_H )
  21. # include <classlib/hashimp.h>
  22. #endif
  23.  
  24. #pragma option -Vo-
  25. #if defined( BI_CLASSLIB_NO_po )
  26. # pragma option -po-
  27. #endif
  28.  
  29. #if defined(BI_NAMESPACE)
  30. namespace ClassLib {
  31. #endif
  32.  
  33. /*------------------------------------------------------------------------*/
  34. /*                                                                        */
  35. /*  template <class K,class V,class A> class TMDDAssociation              */
  36. /*                                                                        */
  37. /*  Managed association (direct key, direct value)                        */
  38. /*                                                                        */
  39. /*  Implements a binding of a key (K) and value (V).  Assumes that        */
  40. /*  K has a HashValue() member function or a global function with         */
  41. /*  the following prototype exists:                                       */
  42. /*                                                                        */
  43. /*      unsigned HashValue( K& );                                         */
  44. /*                                                                        */
  45. /*------------------------------------------------------------------------*/
  46.  
  47. template <class K,class V,class A> class TMDDAssociation
  48. {
  49. public:
  50.  
  51.     TMDDAssociation()
  52.         {
  53.         }
  54.  
  55.     TMDDAssociation( const K& k, const V& v ) :
  56.         KeyData(k),
  57.         ValueData(v)
  58.         {
  59.         }
  60.  
  61.     unsigned HashValue() const
  62.         {
  63.         return ::HashValue( KeyData );
  64.         }
  65.  
  66.     const K& Key() const
  67.         {
  68.         return KeyData;
  69.         }
  70.  
  71.     const V& Value() const
  72.         {
  73.         return ValueData;
  74.         }
  75.  
  76.     int operator == (const TMDDAssociation<K,V,A> & a) const
  77.         {
  78.         return KeyData == a.KeyData;
  79.         }
  80.  
  81.     void DeleteElements()
  82.         {
  83.         }
  84.  
  85. protected:
  86.  
  87.     K KeyData;
  88.     V ValueData;
  89. };
  90.  
  91. /*------------------------------------------------------------------------*/
  92. /*                                                                        */
  93. /*  template <class K,class V> class TDDAssociation                       */
  94. /*                                                                        */
  95. /*  Standard association (direct key, direct value)                       */
  96. /*                                                                        */
  97. /*------------------------------------------------------------------------*/
  98.  
  99. template <class K,class V> class TDDAssociation :
  100.     public TMDDAssociation<K,V,TStandardAllocator>
  101. {
  102. public:
  103.  
  104.     TDDAssociation() :
  105.         TMDDAssociation<K,V,TStandardAllocator>()
  106.         {
  107.         }
  108.  
  109.     TDDAssociation( const K& k, const V& v ) :
  110.         TMDDAssociation<K,V,TStandardAllocator>( k, v )
  111.         {
  112.         }
  113. };
  114.  
  115. /*------------------------------------------------------------------------*/
  116. /*                                                                        */
  117. /*  template <class K,class V,class A> class TMDIAssociation              */
  118. /*                                                                        */
  119. /*  Managed association (direct key, indirect value)                      */
  120. /*                                                                        */
  121. /*------------------------------------------------------------------------*/
  122.  
  123. template <class K,class V,class A> class TMDIAssociation :
  124.     private TMDDAssociation<K,TVoidPointer,A>
  125. {
  126.  
  127.     typedef TMDDAssociation<K,TVoidPointer,A> Parent;
  128.  
  129. public:
  130.  
  131.     TMDIAssociation() :
  132.         TMDDAssociation<K,TVoidPointer,A>()
  133.         {
  134.         }
  135.  
  136.     TMDIAssociation( const K& k, V *v ) :
  137.         TMDDAssociation<K,TVoidPointer,A>( k, v )
  138.         {
  139.         }
  140.  
  141.     const V *Value() const
  142.         {
  143.         return STATIC_CAST(V *,STATIC_CAST(void *,(TMDDAssociation<K,TVoidPointer,A>::Value())));
  144.         }
  145.  
  146.     int operator == (const TMDIAssociation<K,V,A> & a) const
  147.         {
  148.         return Parent::operator ==(a);
  149.         }
  150.  
  151.     Parent::HashValue;
  152.     Parent::Key;
  153.  
  154.     void DeleteElements()
  155.         {
  156.         delete CONST_CAST(V *,Value());
  157.         }
  158.  
  159. };
  160.  
  161. /*------------------------------------------------------------------------*/
  162. /*                                                                        */
  163. /*  template <class K,class V> class TDIAssociation                       */
  164. /*                                                                        */
  165. /*  Standard association (direct key, indirect value)                     */
  166. /*                                                                        */
  167. /*------------------------------------------------------------------------*/
  168.  
  169. template <class K,class V> class TDIAssociation :
  170.     public TMDIAssociation<K,V,TStandardAllocator>
  171. {
  172. public:
  173.  
  174.     TDIAssociation() :
  175.         TMDIAssociation<K,V,TStandardAllocator>()
  176.         {
  177.         }
  178.  
  179.     TDIAssociation( const K& k, V *v ) :
  180.         TMDIAssociation<K,V,TStandardAllocator>( k, v )
  181.         {
  182.         }
  183. };
  184.  
  185.  
  186. /*------------------------------------------------------------------------*/
  187. /*                                                                        */
  188. /*  template <class K,class V,class A> class TMIDAssociation              */
  189. /*                                                                        */
  190. /*  Managed association (indirect key, direct value)                      */
  191. /*                                                                        */
  192. /*------------------------------------------------------------------------*/
  193.  
  194. template <class K,class V,class A> class TMIDAssociation :
  195.     private TMDDAssociation<TVoidPointer,V,A>
  196. {
  197.  
  198.     typedef TMDDAssociation<TVoidPointer,V,A> Parent;
  199.  
  200. public:
  201.  
  202.     TMIDAssociation() :
  203.         TMDDAssociation<TVoidPointer,V,A>()
  204.         {
  205.         }
  206.  
  207.     TMIDAssociation( K *k, const V& v ) :
  208.         TMDDAssociation<TVoidPointer,V,A>( k, v )
  209.         {
  210.         }
  211.  
  212.     const K *Key() const
  213.         {
  214.         return STATIC_CAST(K *, STATIC_CAST(void *, Parent::Key()));
  215.         }
  216.  
  217.     int operator == (const TMIDAssociation<K,V,A>& a) const
  218.         {
  219.         return *Key() == *a.Key();
  220.         }
  221.  
  222.     unsigned HashValue() const
  223.         {
  224.         return ::HashValue( *Key() );
  225.         }
  226.  
  227.     Parent::Value;
  228.  
  229.     void DeleteElements()
  230.         {
  231.         delete CONST_CAST(K *,Key());
  232.         }
  233.  
  234. };
  235.  
  236. /*------------------------------------------------------------------------*/
  237. /*                                                                        */
  238. /*  template <class K,class V> class TIDAssociation                       */
  239. /*                                                                        */
  240. /*  Standard association (indirect key, direct value)                     */
  241. /*                                                                        */
  242. /*------------------------------------------------------------------------*/
  243.  
  244. template <class K,class V> class TIDAssociation :
  245.     public TMIDAssociation<K,V,TStandardAllocator>
  246. {
  247. public:
  248.  
  249.     TIDAssociation() :
  250.         TMIDAssociation<K,V,TStandardAllocator>()
  251.         {
  252.         }
  253.  
  254.     TIDAssociation( K *k, const V& v ) :
  255.         TMIDAssociation<K,V,TStandardAllocator>( k, v )
  256.         {
  257.         }
  258. };
  259.  
  260.  
  261. /*------------------------------------------------------------------------*/
  262. /*                                                                        */
  263. /*  template <class K,class V,class A> class TMIIAssociation              */
  264. /*                                                                        */
  265. /*  Managed association (indirect key, indirect value)                    */
  266. /*                                                                        */
  267. /*------------------------------------------------------------------------*/
  268.  
  269. template <class K,class V,class A> class TMIIAssociation :
  270.     private TMDDAssociation<TVoidPointer,TVoidPointer,A>
  271. {
  272.  
  273.     typedef TMDDAssociation<TVoidPointer,TVoidPointer,A> Parent;
  274.  
  275. public:
  276.  
  277.     TMIIAssociation() :
  278.         TMDDAssociation<TVoidPointer,TVoidPointer,A>()
  279.         {
  280.         }
  281.  
  282.     TMIIAssociation( K *k, V *v ) :
  283.         TMDDAssociation<TVoidPointer,TVoidPointer,A>
  284.             ( k, v )
  285.         {
  286.         }
  287.  
  288.     const K *Key() const
  289.         {
  290.         return STATIC_CAST(K *, STATIC_CAST(void *, Parent::Key()));
  291.         }
  292.  
  293.     const V *Value() const
  294.         {
  295.         return STATIC_CAST(V *, STATIC_CAST(void *, Parent::Value()));
  296.         }
  297.  
  298.     int operator == (const TMIIAssociation<K,V,A>& a) const
  299.         {
  300.         return *Key() == *a.Key();
  301.         }
  302.  
  303.     unsigned HashValue() const
  304.         {
  305.         return ::HashValue( *Key() );
  306.         }
  307.  
  308.     void DeleteElements()
  309.         {
  310.         delete CONST_CAST(K *,Key());
  311.         delete CONST_CAST(V *,Value());
  312.         }
  313.  
  314. };
  315.  
  316. /*------------------------------------------------------------------------*/
  317. /*                                                                        */
  318. /*  template <class K,class V> class TIIAssociation                       */
  319. /*                                                                        */
  320. /*  Standard association (indirect key, indirect value)                   */
  321. /*                                                                        */
  322. /*------------------------------------------------------------------------*/
  323.  
  324. template <class K,class V> class TIIAssociation :
  325.     public TMIIAssociation<K,V,TStandardAllocator>
  326. {
  327. public:
  328.  
  329.     TIIAssociation() :
  330.         TMIIAssociation<K,V,TStandardAllocator>()
  331.         {
  332.         }
  333.  
  334.     TIIAssociation( K *k, V *v ) :
  335.         TMIIAssociation<K,V,TStandardAllocator>( k, v )
  336.         {
  337.         }
  338. };
  339.  
  340. #if defined(BI_NAMESPACE)
  341. }   // namespace ClassLib
  342. #endif
  343.  
  344. #if defined( BI_CLASSLIB_NO_po )
  345. #pragma option -po.
  346. #endif
  347.  
  348. #pragma option -Vo.
  349.  
  350. #endif  // CLASSLIB_ASSOC_H
  351.