home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / ribble / support / assocary.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-09  |  2.2 KB  |  122 lines

  1. // -------------------------------
  2. //  Name:           AssocAry.h
  3. //  Author:         J.R.Shannon
  4. //  Language:       C++
  5. //  Date:           6/11/92
  6. //  Revison:        2.1
  7. //  Last revision:  9/7/93
  8. //  Licence:        Public Domain
  9. //  Purpose:        Associative array
  10. // -------------------------------
  11.  
  12. #include <CSupport.h>
  13. #include <CDList.h>
  14.  
  15. #ifndef _AssocAry_h
  16. #define _AssocAry_h
  17.  
  18. template <class T>
  19. class CSExport AssocAry
  20. {
  21.   enum Constants
  22.   {
  23.     AssocTableSize = 1024
  24.   };
  25.  
  26. public:
  27.  
  28.   struct AssocEl
  29.   {
  30.     T         a;
  31.     T         b;
  32.     AssocEl*  next;
  33.     int      used;
  34.   };
  35.  
  36. public:
  37.   AssocAry(void);
  38.   ~AssocAry();
  39.   void Destroy(void);
  40.   void insert(const T a, const T b);
  41.   int findB(const T a, T& b) const;
  42.   int findA(const T b, T& a) const;
  43.   int remove(const T a);
  44.  
  45. private:
  46.   AssocEl* GetElement(void)
  47.   {
  48.     if (!stackIter)
  49.       {
  50.         AssocEl* newElement = new AssocEl;
  51.         stack.addTail(newElement);
  52.         stackIter.fastforward();
  53.       }
  54.     return stackIter++;
  55.   }
  56.  
  57.  
  58.   typedef CDList<AssocEl*>         AssocElStack;
  59.   typedef CDListIterator<AssocEl*> AssocElStackIter;
  60.  
  61.   AssocEl**         bucket;
  62.   AssocElStack      stack;
  63.   AssocElStackIter  stackIter;
  64. };
  65.  
  66. template <class T, class U>
  67. class CSExport AssocAry2
  68. {
  69.   enum Constants
  70.   {
  71.     AssocTableSize = 1024
  72.   };
  73.  
  74. public:
  75.  
  76.   struct AssocEl
  77.   {
  78.     T         a;
  79.     U         b;
  80.     AssocEl*  next;
  81.     int      used;
  82.   };
  83.  
  84. public:
  85.   AssocAry2(void);
  86.   ~AssocAry2();
  87.   void Destroy(void);
  88.   void insert(const T a, const U b);
  89.   int findB(const T a, U& b) const;
  90.   int findA(const U b, T& a) const;
  91.   int remove(const T a);
  92.   int remove(const U b);
  93.  
  94. private:
  95.   AssocEl* GetElement(void)
  96.   {
  97.     if (!stackIter)
  98.       {
  99.         AssocEl* newElement = new AssocEl;
  100.         stack.addTail(newElement);
  101.         stackIter.fastforward();
  102.       }
  103.     return stackIter++;
  104.   }
  105.  
  106.  
  107.   typedef CDList<AssocEl*>         AssocElStack;
  108.   typedef CDListIterator<AssocEl*> AssocElStackIter;
  109.  
  110.   AssocEl**         bucket;
  111.   AssocElStack      stack;
  112.   AssocElStackIter  stackIter;
  113. };
  114.  
  115.  
  116. #ifndef _CSET2
  117. #include <AssocAry.c>
  118. #endif
  119.  
  120. #endif
  121.  
  122.