home *** CD-ROM | disk | FTP | other *** search
/ PC World 1997 November / PCWorld_1997-11_cd.bin / software / programy / komix / DATA.Z / PSetDict.hxx < prev    next >
Text File  |  1996-05-31  |  3KB  |  130 lines

  1. /*---------------------------------------------------------------------------
  2.  *
  3.  *      (c)     Westmount Technology    1993
  4.  *
  5.  *      File:        @(#)PSetDict.hxx    1.3
  6.  *      Author:        peku
  7.  *      Description:    dictionary of pointer sets
  8.  *            expects that class Value_PtrSet exists 
  9.  *---------------------------------------------------------------------------
  10.  SccsId = @(#)PSetDict.hxx    1.3   22 Feb 1994 Copyright 1993 Westmount Technology */
  11.  
  12. #ifndef PSETDICT_HXX
  13. #define PSETDICT_HXX
  14.  
  15. #ifndef PTRSET_HXX
  16. #include "PtrSet.hxx"
  17. #endif
  18.  
  19. #ifndef PTRDICT_HXX
  20. #include "PtrDict.hxx"
  21. #endif
  22.  
  23. #if HAS_TEMPLATES
  24.  
  25. template<class Key, class Value>
  26. class PSetDict : private GenPtrDict {
  27. public:                
  28.     void    add(const Key &key, Value *value)
  29.     {
  30.         PtrSet<Value> *vptr;
  31.         if (!(vptr = (PtrSet<Value> *)GenPtrDict::get(key))) {
  32.             vptr = new PtrSet<Value>;
  33.             GenPtrDict::set(new Key(key), (void *)vptr);
  34.         }
  35.         vptr->add(value);
  36.     }
  37.     PtrSet<Value> *get(const Key &key) const
  38.     {
  39.         return (PtrSet<Value> *) GenPtrDict::get(key);
  40.     }
  41.     void    remove(const Key &key, Value *value)
  42.     {
  43.         PtrSet<Value> *vptr;
  44.         if (vptr = (PtrSet<Value> *)GenPtrDict::get(key)) {
  45.             vptr->remove(value);
  46.             if (!vptr->first()) {
  47.                 GenPtrDict::remove(key);
  48.                 delete vptr;
  49.             }
  50.         }
  51.     }
  52.     int isPresent(const Key &key) const
  53.     {
  54.         return GenPtrDict::isPresent(key);
  55.     }
  56.     const Key *firstKey()
  57.     {
  58.         return (Key *) GenPtrDict::firstKey();
  59.     }
  60.     const Key *nextKey()
  61.     {
  62.         return (Key *) GenPtrDict::nextKey();
  63.     }
  64.     PtrSet<Value> *firstValue()
  65.     {
  66.         return (PtrSet<Value> *) GenPtrDict::firstValue();
  67.     }
  68.     PtrSet<Value> *nextValue()
  69.     {
  70.         return (PtrSet<Value> *) GenPtrDict::nextValue();
  71.     }
  72. };
  73.     
  74. #else
  75.  
  76. #define    PSetDict_(key,value)    name3(key,value,_PSetDict)
  77.  
  78. #define PSetDictdeclare2(Key,Value)                    \
  79. class PSetDict_(Key,Value) : private GenPtrDict {            \
  80. public:                                    \
  81.     void    add(const Key &key, Value *value)            \
  82.     {                                \
  83.         PtrSet_(Value) *vptr;                    \
  84.         if (!(vptr = (PtrSet_(Value) *)GenPtrDict::get(key))) { \
  85.             vptr = new PtrSet_(Value);            \
  86.             GenPtrDict::set(new Key(key), (void *)vptr);    \
  87.         }                            \
  88.         vptr->add(value);                    \
  89.     }                                \
  90.     PtrSet_(Value) *get(const Key &key) const            \
  91.     {                                \
  92.         return (PtrSet_(Value) *) GenPtrDict::get(key);        \
  93.     }                                \
  94.     void    remove(const Key &key, Value *value)            \
  95.     {                                \
  96.         PtrSet_(Value) *vptr;                    \
  97.         if (vptr = (PtrSet_(Value) *)GenPtrDict::get(key)) {    \
  98.             vptr->remove(value);                \
  99.             if (!vptr->first()) {                \
  100.                 GenPtrDict::remove(key);        \
  101.                 delete vptr;                 \
  102.             }                        \
  103.         }                            \
  104.     }                                \
  105.     int isPresent(const Key &key) const                \
  106.     {                                \
  107.         return GenPtrDict::isPresent(key);            \
  108.     }                                \
  109.     const Key *firstKey()                        \
  110.     {                                \
  111.         return (Key *) GenPtrDict::firstKey();            \
  112.     }                                \
  113.     const Key *nextKey()                        \
  114.     {                                \
  115.         return (Key *) GenPtrDict::nextKey();            \
  116.     }                                \
  117.     PtrSet_(Value) *firstValue()                    \
  118.     {                                \
  119.         return (PtrSet_(Value) *) GenPtrDict::firstValue();    \
  120.     }                                \
  121.     PtrSet_(Value) *nextValue()                    \
  122.     {                                \
  123.         return (PtrSet_(Value) *) GenPtrDict::nextValue();    \
  124.     }                                \
  125. }
  126.     
  127. #endif /* HAS_TEMPLATES */
  128.  
  129. #endif /* PSETDICT_HXX */
  130.