home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / GNU_1OF3.ZIP / HEADERS.ZIP / g++-include / gen / SkipMap.hP < prev    next >
Encoding:
Text File  |  1992-01-14  |  4.0 KB  |  176 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, 675 Mass Ave, Cambridge, MA 02139, 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 <values.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.     <T><C>SkipMapNode(int size);
  44. };
  45.  
  46. class <T><C>RealSkipMapNode : public <T><C>SkipMapNode
  47. {
  48. friend class <T><C>SkipMap;
  49.   private:
  50.     <T>             item;
  51.     <C>             cont;
  52.     <T><C>RealSkipMapNode(<T&> h, <C&> i, int size);
  53. };
  54.  
  55. typedef <T><C>RealSkipMapNode* <T><C>SkipMapNodePtr;
  56.  
  57. inline <T><C>SkipMapNode::<T><C>SkipMapNode(int size)
  58. : forward(new <T><C>SkipMapNodePtr[size+1])
  59. {
  60. }
  61.  
  62. inline <T><C>RealSkipMapNode::<T><C>RealSkipMapNode(<T&> h, <C&> i, int size)
  63. : item(h), cont(i),
  64.   <T><C>SkipMapNode(size)
  65. {
  66. }
  67.  
  68. class <T><C>SkipMap : public <T><C>Map
  69. {
  70. friend class <T><C>SkipMapinit;
  71.   protected:
  72.     <T><C>SkipMapNode*   header;
  73.     int                  level;
  74.     int                  max_levels;
  75.     int                  randoms_left;
  76.     long                 random_bits;
  77.     
  78.     static MLCG*         gen;
  79.     int                  random_level(void);
  80.     
  81.     <T><C>SkipMapNodePtr leftmost();
  82.     <T><C>SkipMapNodePtr rightmost();
  83.     <T><C>SkipMapNodePtr pred(<T><C>SkipMapNodePtr t);
  84.     <T><C>SkipMapNodePtr succ(<T><C>SkipMapNodePtr t);
  85.     void                 _kill();
  86.   private:
  87.     enum { BITS_IN_RANDOM = LONGBITS-1 };
  88.     
  89.   public:
  90.     <T><C>SkipMap( <C&> dflt, long size=DEFAULT_INITIAL_CAPACITY);
  91.     <T><C>SkipMap(<T><C>SkipMap& a);
  92.     ~<T><C>SkipMap();
  93.     
  94.     <C>&          operator [] (<T&> key);
  95.     
  96.     void          del(<T&> key);
  97.     
  98.     Pix           first();
  99.     void          next(Pix& i);
  100.     <T>&          key(Pix i);
  101.     <C>&          contents(Pix i);
  102.     
  103.     Pix           seek(<T&> key);
  104.     int           contains(<T&> key);
  105.     void          clear(); 
  106.     
  107.     Pix           last();
  108.     void          prev(Pix& i);
  109.     
  110.     int           OK();
  111. };
  112.  
  113. inline <T><C>SkipMap::~<T><C>SkipMap()
  114. {
  115.     _kill();
  116.     delete header;
  117. }
  118.  
  119. inline <T><C>SkipMapNodePtr <T><C>SkipMap::leftmost()
  120. {
  121.     return header->forward[0]==header ? 0 : header->forward[0];
  122. }
  123.  
  124. inline Pix <T><C>SkipMap::first()
  125. {
  126.     return Pix(leftmost());
  127. }
  128.  
  129. inline Pix <T><C>SkipMap::last()
  130. {
  131.     return Pix(rightmost());
  132. }
  133.  
  134. inline <T><C>SkipMapNodePtr <T><C>SkipMap::succ(<T><C>SkipMapNodePtr t)
  135. {
  136.     return t->forward[0]==header ? 0 : t->forward[0];
  137. }
  138.  
  139. inline void <T><C>SkipMap::next(Pix& i)
  140. {
  141.     if (i != 0) i = Pix(succ((<T><C>SkipMapNodePtr)i));
  142. }
  143.  
  144. inline void <T><C>SkipMap::prev(Pix& i)
  145. {
  146.     if (i != 0) i = Pix(pred((<T><C>SkipMapNodePtr)i));
  147. }
  148.  
  149. inline <T>& <T><C>SkipMap::key (Pix i)
  150. {
  151.     if (i == 0) error("null Pix");
  152.     return ((<T><C>SkipMapNodePtr)i)->item;
  153. }
  154.  
  155. inline <C>& <T><C>SkipMap::contents (Pix i)
  156. {
  157.     if (i == 0) error("null Pix");
  158.     return ((<T><C>SkipMapNodePtr)i)->cont;
  159. }
  160.  
  161. inline int <T><C>SkipMap::contains(<T&> key)
  162. {
  163.     return seek(key) != 0;
  164. }
  165.  
  166. static class <T><C>SkipMapinit
  167. {
  168.   public:
  169.     <T><C>SkipMapinit();
  170.     ~<T><C>SkipMapinit();
  171.   private:
  172.     static int count;
  173. } <T><C>skipMapinit;
  174.  
  175. #endif
  176.