home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / ribble / support / cassoc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  2.0 KB  |  83 lines

  1. #include <CSupport.h>
  2. #include <CAssert.h>
  3.  
  4. #ifndef _CAssoc_h
  5. #define _CAssoc_h
  6.  
  7. template <class Xc, class Yc, class AssocItem>
  8. class CSExport Assoc
  9. {
  10. private:
  11.  
  12.   enum Constants
  13.   {
  14.     BucketSize = 1024
  15.   };
  16.  
  17.   class ChainElement
  18.   {
  19.   public:
  20.     void          SetNext(ChainElement* _next)  { next = _next;  }
  21.     ChainElement* QueryNext(void)               { return next;   }
  22.     void          SetItem(AssocItem* _item)     { item = *_item; }
  23.     AssocItem*    QueryItem(void)               { return &item;  }
  24.     void          SetRow(Xc _row)               { row = _row;    }
  25.     Xc            QueryRow(void)                { return row;    }
  26.     void          SetCol(Yc _col)               { col = _col;    }
  27.     Yc            QueryCol(void)                { return col;    }
  28.  
  29.   private:
  30.     Xc                 row;
  31.     Yc                 col;
  32.     ChainElement*      next;
  33.     AssocItem          item;
  34.   };
  35.  
  36.   typedef CDList<ChainElement*> ElementStack;
  37.   typedef CDListIterator<ChainElement*> ElementStackIter;
  38.  
  39. public:
  40.   Assoc(void);
  41.   ~Assoc();
  42.   void Destroy(void);
  43.   void InsertCell(Xc _row, Yc _col, AssocItem& _info);
  44.   AssocItem* QueryCell(Xc _row, Yc _col);
  45.   ChainElement* GetElement(void)
  46.     {
  47.       if (!stackIter)
  48.         {
  49.           ChainElement* newElement = new ChainElement;
  50.           stack.addTail(newElement);
  51.           stackIter.fastforward();
  52.         }
  53.       return stackIter++;
  54.     }
  55.  
  56. private:
  57.  
  58.     // This is the one to tweak.
  59.  
  60.     unsigned int
  61.     HashItems(Xc _row, Yc _col)
  62.     {
  63.       unsigned int a = (unsigned int)_row;
  64.       unsigned int b = (unsigned int)_col;
  65.       unsigned int c = (unsigned int)_rotl((unsigned short)a,
  66.                        (int)(b & 0xf) + 1);
  67.       unsigned int d = a + b;
  68.       unsigned int e = c ^ d;
  69.       return e % BucketSize;
  70.     }
  71.  
  72.   ChainElement**    bucket;
  73.   ElementStack      stack;
  74.   ElementStackIter  stackIter;
  75. };
  76.  
  77. #ifndef _CSET2
  78. #include <CAssoc.c>
  79. #endif
  80.  
  81. #endif
  82.  
  83.