home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / gen / SkipMap.hP < prev    next >
Text File  |  1996-10-12  |  4KB  |  177 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1991 Free Software Foundation
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17.  
  18. /*
  19.  * Bags implemented via William Pugh SkipList algorithms.
  20.  * CACM, June 1990, p 668-676.
  21.  *
  22.  */
  23.  
  24. #ifndef _<T><C>SkipMap_h
  25. #ifdef __GNUG__
  26. #pragma interface
  27. #endif
  28. #define _<T><C>SkipMap_h 1
  29.  
  30. #include "<T>.<C>.Map.h"
  31.  
  32. #include <limits.h>
  33. #include <MLCG.h>
  34.  
  35. class <T><C>SkipMap;
  36. class <T><C>RealSkipMapNode;
  37.  
  38. class <T><C>SkipMapNode
  39. {
  40. friend class <T><C>SkipMap;
  41.   private:
  42.     <T><C>RealSkipMapNode * * forward;
  43.   protected:
  44.     <T><C>SkipMapNode(int size);
  45. };
  46.  
  47. class <T><C>RealSkipMapNode : public <T><C>SkipMapNode
  48. {
  49. friend class <T><C>SkipMap;
  50.   private:
  51.     <T>             item;
  52.     <C>             cont;
  53.     <T><C>RealSkipMapNode(<T&> h, <C&> i, int size);
  54. };
  55.  
  56. typedef <T><C>RealSkipMapNode* <T><C>SkipMapNodePtr;
  57.  
  58. inline <T><C>SkipMapNode::<T><C>SkipMapNode(int size)
  59. : forward(new <T><C>SkipMapNodePtr[size+1])
  60. {
  61. }
  62.  
  63. inline <T><C>RealSkipMapNode::<T><C>RealSkipMapNode(<T&> h, <C&> i, int size)
  64. : item(h), cont(i),
  65.   <T><C>SkipMapNode(size)
  66. {
  67. }
  68.  
  69. class <T><C>SkipMap : public <T><C>Map
  70. {
  71. friend class <T><C>SkipMapinit;
  72.   protected:
  73.     <T><C>SkipMapNode*   header;
  74.     int                  level;
  75.     int                  max_levels;
  76.     int                  randoms_left;
  77.     long                 random_bits;
  78.     
  79.     static MLCG*         gen;
  80.     int                  random_level(void);
  81.     
  82.     <T><C>SkipMapNodePtr leftmost();
  83.     <T><C>SkipMapNodePtr rightmost();
  84.     <T><C>SkipMapNodePtr pred(<T><C>SkipMapNodePtr t);
  85.     <T><C>SkipMapNodePtr succ(<T><C>SkipMapNodePtr t);
  86.     void                 _kill();
  87.   private:
  88.     enum { BITS_IN_RANDOM = LONGBITS-1 };
  89.     
  90.   public:
  91.     <T><C>SkipMap( <C&> dflt, long size=DEFAULT_INITIAL_CAPACITY);
  92.     <T><C>SkipMap(<T><C>SkipMap& a);
  93.     ~<T><C>SkipMap();
  94.     
  95.     <C>&          operator [] (<T&> key);
  96.     
  97.     void          del(<T&> key);
  98.     
  99.     Pix           first();
  100.     void          next(Pix& i);
  101.     <T>&          key(Pix i);
  102.     <C>&          contents(Pix i);
  103.     
  104.     Pix           seek(<T&> key);
  105.     int           contains(<T&> key);
  106.     void          clear(); 
  107.     
  108.     Pix           last();
  109.     void          prev(Pix& i);
  110.     
  111.     int           OK();
  112. };
  113.  
  114. inline <T><C>SkipMap::~<T><C>SkipMap()
  115. {
  116.     _kill();
  117.     delete header;
  118. }
  119.  
  120. inline <T><C>SkipMapNodePtr <T><C>SkipMap::leftmost()
  121. {
  122.     return header->forward[0]==header ? 0 : header->forward[0];
  123. }
  124.  
  125. inline Pix <T><C>SkipMap::first()
  126. {
  127.     return Pix(leftmost());
  128. }
  129.  
  130. inline Pix <T><C>SkipMap::last()
  131. {
  132.     return Pix(rightmost());
  133. }
  134.  
  135. inline <T><C>SkipMapNodePtr <T><C>SkipMap::succ(<T><C>SkipMapNodePtr t)
  136. {
  137.     return t->forward[0]==header ? 0 : t->forward[0];
  138. }
  139.  
  140. inline void <T><C>SkipMap::next(Pix& i)
  141. {
  142.     if (i != 0) i = Pix(succ((<T><C>SkipMapNodePtr)i));
  143. }
  144.  
  145. inline void <T><C>SkipMap::prev(Pix& i)
  146. {
  147.     if (i != 0) i = Pix(pred((<T><C>SkipMapNodePtr)i));
  148. }
  149.  
  150. inline <T>& <T><C>SkipMap::key (Pix i)
  151. {
  152.     if (i == 0) error("null Pix");
  153.     return ((<T><C>SkipMapNodePtr)i)->item;
  154. }
  155.  
  156. inline <C>& <T><C>SkipMap::contents (Pix i)
  157. {
  158.     if (i == 0) error("null Pix");
  159.     return ((<T><C>SkipMapNodePtr)i)->cont;
  160. }
  161.  
  162. inline int <T><C>SkipMap::contains(<T&> key)
  163. {
  164.     return seek(key) != 0;
  165. }
  166.  
  167. static class <T><C>SkipMapinit
  168. {
  169.   public:
  170.     <T><C>SkipMapinit();
  171.     ~<T><C>SkipMapinit();
  172.   private:
  173.     static int count;
  174. } <T><C>skipMapinit;
  175.  
  176. #endif
  177.