home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classinc.pak / ASSOC.H < prev    next >
C/C++ Source or Header  |  1997-07-23  |  11KB  |  349 lines

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