home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games (Alt) / INFESPGAMES.iso / os2 / ribble / support / cassoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-09  |  2.2 KB  |  94 lines

  1. #include <CAssoc.h>
  2.  
  3.  
  4. template <class Xc, class Yc, class AssocItem>
  5. Assoc<Xc, Yc, AssocItem>::Assoc(void)
  6. : stackIter(stack)
  7. {
  8.   bucket = new ChainElement*[BucketSize];
  9.   CAssert(bucket);
  10.  
  11.   for (int i = BucketSize; --i >= 0; )
  12.     bucket[i] = 0;
  13. }
  14.  
  15. template <class Xc, class Yc, class AssocItem>
  16. Assoc<Xc, Yc, AssocItem>::~Assoc(void)
  17. {
  18.   delete [] bucket;
  19.  
  20.   stackIter.rewind();
  21.   while (stackIter)
  22.     delete stackIter++;
  23. }
  24.  
  25. template <class Xc, class Yc, class AssocItem>
  26. void
  27. Assoc<Xc, Yc, AssocItem>::Destroy(void)
  28. {
  29.   for (int i = BucketSize; --i >= 0; )
  30.     bucket[i] = 0;
  31.   stackIter.rewind();
  32. }
  33.  
  34. template <class Xc, class Yc, class AssocItem>
  35. void
  36. Assoc<Xc, Yc, AssocItem>::InsertCell(Xc _row, Yc _col, AssocItem& _info)
  37. {
  38.   // Hash up the _row and _col parameters to get an index into
  39.   // the bucket.
  40.  
  41.   unsigned int index = HashItems(_row, _col);
  42.  
  43.   ChainElement* element = bucket[index];
  44.   if (element == 0)
  45.     {
  46.       ChainElement* newElement = GetElement();
  47.       newElement->SetRow(_row);
  48.       newElement->SetCol(_col);
  49.       newElement->SetItem(&_info);
  50.       newElement->SetNext(0);
  51.       bucket[index] = newElement;
  52.     }
  53.   else
  54.     {
  55.       while (element)
  56.         {
  57.           if (element->QueryRow() == _row && element->QueryCol() == _col)
  58.             break;
  59.  
  60.           ChainElement* next = element->QueryNext();
  61.           if (next == 0)
  62.             {
  63.               ChainElement* newElement = GetElement();
  64.               newElement->SetRow(_row);
  65.               newElement->SetCol(_col);
  66.               newElement->SetItem(&_info);
  67.               newElement->SetNext(0);
  68.               element->SetNext(newElement);
  69.               break;
  70.             }
  71.           else
  72.             element = next;
  73.         }
  74.     }
  75. }
  76.  
  77. template <class Xc, class Yc, class AssocItem>
  78. AssocItem*
  79. Assoc<Xc, Yc, AssocItem>::QueryCell(Xc _row, Yc _col)
  80. {
  81.   unsigned int index = HashItems(_row, _col);
  82.  
  83.   ChainElement* element = bucket[index];
  84.   while (element)
  85.     if (element->QueryRow() == _row && element->QueryCol() == _col)
  86.       return element->QueryItem();
  87.     else
  88.       element = element->QueryNext();
  89.  
  90.   return 0;
  91. }
  92.  
  93.  
  94.