home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / gen / SkipMap.ccP < prev    next >
Text File  |  1993-06-29  |  7KB  |  308 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. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21.  
  22. #include <stream.h>
  23. #include <time.h>
  24. #include "<T>.<C>.SkipMap.h"
  25.  
  26. /*
  27.  * Bags implemented via William Pugh SkipList algorithms.
  28.  * CACM, June 1990, p 668-676.
  29.  *
  30.  */
  31.  
  32. MLCG* <T><C>SkipMap::gen = 0;
  33. int <T><C>SkipMapinit::count = 0;
  34.  
  35. static int countbits(long bits)
  36. {
  37.   int n = 0;
  38.   while(bits>>=1) n++;
  39.   return n;
  40. }
  41.  
  42. <T><C>SkipMap::<T><C>SkipMap(<C&> dflt, long size)
  43. : <T><C>Map(dflt),
  44.   level(0),
  45.   header(new <T><C>SkipMapNode (countbits(size)+1)),
  46.   max_levels (countbits(size)+1),
  47.   random_bits(gen->asLong()),
  48.   randoms_left(BITS_IN_RANDOM / 2)
  49. {
  50.   <T><C>SkipMapNodePtr *buffer_start = header->forward;
  51.   <T><C>SkipMapNodePtr *trav = &header->forward[max_levels];
  52.   
  53.   count = 0;
  54.   while (trav > buffer_start)
  55.     *--trav = (<T><C>SkipMapNodePtr) header;
  56. }
  57.  
  58. <T><C>SkipMap::<T><C>SkipMap(<T><C>SkipMap& b)
  59. : <T><C>Map(b.def),
  60.   level (0),
  61.   header (new <T><C>SkipMapNode (b.max_levels)),
  62.   max_levels (b.max_levels),
  63.   random_bits (gen->asLong()),
  64.   randoms_left (BITS_IN_RANDOM / 2)
  65. {
  66.   <T><C>SkipMapNodePtr *buffer_start = header->forward;
  67.   <T><C>SkipMapNodePtr *trav = &header->forward[max_levels];
  68.   
  69.   count = 0;
  70.   
  71.    while (trav > buffer_start)
  72.      *--trav = (<T><C>SkipMapNodePtr)header;
  73.   
  74.   for (<T><C>SkipMapNodePtr t = b.leftmost(); t; t = b.succ(t))
  75.     (*this)[t->item] = t->cont;
  76. }
  77.  
  78. <C>& <T><C>SkipMap::operator [] (<T&> item)
  79. {
  80.   <T><C>SkipMapNodePtr *update = new <T><C>SkipMapNodePtr[max_levels+1];
  81.   <T><C>SkipMapNodePtr curr =
  82.     (<T><C>SkipMapNodePtr) this->header;
  83.   int   l = level;
  84.   <T><C>SkipMapNodePtr temp;
  85.   
  86.   do
  87.   {
  88.     while ((temp = curr->forward[l])!=header &&
  89.        <T>CMP(temp->item, item) < 0)
  90.       curr = temp;
  91.     update[l] = curr;
  92.   } 
  93.   while (--l >= 0);
  94.   
  95.   if (temp != header && <T>CMP(temp->item, item) == 0)
  96.   {
  97.       delete update;
  98.       return temp->cont;
  99.   }
  100.  
  101.   if ((l = random_level ()) > level)
  102.   {
  103.     l = ++level;
  104.     update[l] = (<T><C>SkipMapNodePtr)header;
  105.   };
  106.  
  107.   temp = new <T><C>RealSkipMapNode (item, def, l);
  108.   <T><C>SkipMapNodePtr *temp_forward = temp->forward;
  109.   
  110.   do
  111.   {
  112.     <T><C>SkipMapNodePtr *curr_forward = update[l]->forward;
  113.     
  114.     temp_forward[l] = curr_forward[l];
  115.     curr_forward[l] = temp;
  116.   } 
  117.   while (--l >= 0);
  118.  
  119.   count++;
  120.   delete update;
  121.   return temp->cont;
  122. }
  123.  
  124. void <T><C>SkipMap::del(<T&> key)
  125. {
  126.   
  127.   int   l = level;
  128.   int   curr_level = level;
  129.   <T><C>SkipMapNodePtr *update = new <T><C>SkipMapNodePtr[max_levels+1];
  130.   <T><C>SkipMapNodePtr curr = (<T><C>SkipMapNodePtr)header;
  131.   <T><C>SkipMapNodePtr temp;
  132.   
  133.   do
  134.   {
  135.     while ((temp = curr->forward[l])!=header
  136.        && <T>CMP(temp->item,key) < 0)
  137.       curr = temp;
  138.     update[l] = curr;
  139.   } 
  140.   while (--l >= 0);
  141.   
  142.   if (<T>CMP(temp->item,key)==0)
  143.   {
  144.     <T><C>SkipMapNodePtr *temp_forward = temp->forward;
  145.     
  146.     for (l = 0;
  147.      l <= curr_level && (curr = update[l])->forward[l] == temp;
  148.      l++)
  149.       curr->forward[l] = temp_forward[l];
  150.     
  151.     delete temp;
  152.     
  153.     <T><C>SkipMapNodePtr *forward = header->forward;
  154.     
  155.     while (forward[curr_level]==header && curr_level > 0)
  156.       curr_level--;
  157.     
  158.     level = curr_level;
  159.     count--;
  160.     delete update;
  161.     return;
  162.   }
  163. }
  164.  
  165. <T><C>SkipMapNodePtr <T><C>SkipMap::rightmost()
  166. {
  167.   <T><C>SkipMapNodePtr temp;
  168.   <T><C>SkipMapNode*   curr = header;
  169.   int l = level;
  170.   
  171.   do
  172.     while ((temp = curr->forward[l])!=header)
  173.       curr = temp;
  174.   while (--l >= 0);
  175.   
  176.   return temp==header ? 0 : temp;
  177. }
  178.  
  179. <T><C>SkipMapNodePtr <T><C>SkipMap::pred(<T><C>SkipMapNodePtr t)
  180. {
  181.   <T><C>SkipMapNodePtr temp, curr = (<T><C>SkipMapNodePtr) header;
  182.   int l = level;
  183.   
  184.   do
  185.     while ((temp = curr->forward[l])!=t)
  186.       curr = temp;
  187.   while (--l >= 0);
  188.   
  189.   return curr == header ? 0 : curr;
  190. }
  191.  
  192. void <T><C>SkipMap::_kill()
  193. {
  194.   <T><C>SkipMapNode *p = this->header->forward[0];
  195.   
  196.   while (p != header)
  197.   {
  198.     <T><C>SkipMapNodePtr q = p->forward[0];
  199.     delete p;
  200.     p = q;
  201.   }
  202. }
  203.  
  204. void <T><C>SkipMap::clear()
  205. {
  206.   <T><C>SkipMapNodePtr *buffer_start = header->forward;
  207.   <T><C>SkipMapNodePtr *trav = &header->forward[level+1];
  208.   _kill();
  209.   count = 0;
  210.     
  211.   while (trav > buffer_start)
  212.     *--trav = (<T><C>SkipMapNodePtr)header;
  213. }
  214.  
  215. Pix <T><C>SkipMap::seek(<T&> key)
  216. {
  217.   <T><C>SkipMapNodePtr temp;
  218.   <T><C>SkipMapNode *curr  = header;
  219.   int   l = level;
  220.     
  221.   do
  222.   {
  223.     while ((temp = curr->forward[l])!=header &&
  224.        <T>CMP(temp->item, key) < 0)
  225.       curr = temp;
  226.   }
  227.   while (--l >= 0);
  228.   
  229.   if (<T>CMP(temp->item, key) != 0)
  230.     return 0;
  231.   else
  232.   {
  233.     return Pix(temp);
  234.   }
  235. }
  236.  
  237. /*
  238.  * random function for probabilistic balancing
  239.  *
  240.  * Hardwired for p = .25.  Not too flexible,
  241.  * but fast.  Changing this would require a constructor
  242.  * that would accept a different value for p, etc.
  243.  * Perhaps someone else would like to implement this?
  244.  *
  245.  */
  246. int <T><C>SkipMap::random_level (void)
  247. {
  248.   int rlevel = 0;
  249.   int b;
  250.   
  251.   do
  252.   {
  253.     b = random_bits & 3L;
  254.     if (!b)
  255.       rlevel++;
  256.     random_bits >>= 2;
  257.     if (--randoms_left == 0)
  258.     {
  259.       random_bits = gen->asLong();
  260.       randoms_left = BITS_IN_RANDOM / 2;
  261.     };
  262.   } 
  263.   while (!b);
  264.   
  265.   return rlevel > max_levels ? max_levels : rlevel;
  266. }
  267.  
  268. int <T><C>SkipMap::OK()
  269. {
  270.   int v = 1;
  271.   if (header == 0) 
  272.     v = 0;
  273.   else
  274.   {
  275.     int n = 0;
  276.     <T><C>SkipMapNodePtr trail = leftmost();
  277.     <T><C>SkipMapNodePtr t = 0;
  278.     if (trail) t = succ(trail);
  279.     if (t) n++;
  280.     while (t != 0)
  281.     {
  282.       ++n;
  283.       v &= <T>CMP(trail->item, t->item) < 0;
  284.       trail = t;
  285.       t = succ(t);
  286.     }
  287.     v &= n == count;
  288.   }
  289.   if (!v) error("invariant failure");
  290.   return v;
  291. }
  292.  
  293. <T><C>SkipMapinit::<T><C>SkipMapinit()
  294. {
  295.     if (!count)
  296.     <T><C>SkipMap::gen = new MLCG(time(0));
  297.     count++;
  298. }
  299.  
  300. <T><C>SkipMapinit::~<T><C>SkipMapinit()
  301. {
  302.     count--;
  303.     if (!count)
  304.     delete <T><C>SkipMap::gen;
  305. }
  306.  
  307.         
  308.