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 / SkipBag.ccP < prev    next >
Text File  |  1996-10-12  |  7KB  |  323 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /* 
  4. Copyright (C) 1991 Free Software Foundation
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. /*
  20.  * Bags implemented via William Pugh SkipList algorithms.
  21.  * CACM, June 1990, p 668-676.
  22.  *
  23.  */
  24.  
  25. #include <stream.h>
  26. #include <time.h>
  27. #include "<T>.SkipBag.h"
  28.  
  29. MLCG* <T>SkipBag::gen = 0;
  30. int <T>SkipBaginit::count = 0;
  31.  
  32. static int countbits(long bits)
  33. {
  34.    int n = 0;
  35.    while(bits>>=1L) n++;
  36.    return n;
  37. }
  38.  
  39. <T>SkipBag::<T>SkipBag(long size)
  40. : level(0),
  41.   header(new <T>SkipBagNode (countbits(size)+1)),
  42.   max_levels (countbits(size)+1),
  43.   random_bits(gen->asLong()),
  44.   randoms_left(BITS_IN_RANDOM / 2)
  45. {
  46.   <T>SkipBagNodePtr *buffer_start = header->forward;
  47.   <T>SkipBagNodePtr *trav = &header->forward[max_levels];
  48.   
  49.   count = 0;
  50.   while (trav > buffer_start)
  51.     *--trav = (<T>SkipBagNodePtr) header;
  52. }
  53.  
  54. <T>SkipBag::<T>SkipBag(<T>SkipBag& b)
  55. : level (0),
  56.   header (new <T>SkipBagNode (b.max_levels)),
  57.   max_levels (b.max_levels),
  58.   random_bits (gen->asLong()),
  59.   randoms_left (BITS_IN_RANDOM / 2)
  60. {
  61.   <T>SkipBagNodePtr *buffer_start = header->forward;
  62.   <T>SkipBagNodePtr *trav = &header->forward[max_levels];
  63.   
  64.   count = 0;
  65.   
  66.    while (trav > buffer_start)
  67.      *--trav = (<T>SkipBagNodePtr)header;
  68.   
  69.   for (<T>SkipBagNodePtr t = b.leftmost(); t; t = b.succ(t))
  70.       add(t->item);
  71. }
  72.  
  73. Pix <T>SkipBag::add (<T&> item)
  74. {
  75.   <T>SkipBagNodePtr *update = new <T>SkipBagNodePtr[max_levels+1];
  76.   <T>SkipBagNodePtr curr = (<T>SkipBagNodePtr) this->header;
  77.   int   l = level;
  78.   <T>SkipBagNodePtr temp;
  79.   
  80.   do
  81.   {
  82.     while ((temp = curr->forward[l])!=header &&
  83.        <T>CMP(temp->item, item) < 0)
  84.       curr = temp;
  85.     update[l] = curr;
  86.   } 
  87.   while (--l >= 0);
  88.   
  89.   if ((l = random_level ()) > level)
  90.   {
  91.     l = ++level;
  92.     update[l] = (<T>SkipBagNodePtr)header;
  93.   };
  94.  
  95.   temp = new <T>RealSkipBagNode (item, l);
  96.   <T>SkipBagNodePtr *temp_forward = temp->forward;
  97.   
  98.   do
  99.   {
  100.     <T>SkipBagNodePtr *curr_forward = update[l]->forward;
  101.     
  102.     temp_forward[l] = curr_forward[l];
  103.     curr_forward[l] = temp;
  104.   } 
  105.   while (--l >= 0);
  106.  
  107.   count++;
  108.   delete update;
  109.   return Pix(temp);
  110. }
  111.  
  112. void <T>SkipBag::del(<T&> key)
  113. {
  114.   
  115.   int   l = level;
  116.   int   curr_level = level;
  117.   <T>SkipBagNodePtr *update = new <T>SkipBagNodePtr[max_levels+1];
  118.   <T>SkipBagNodePtr curr = (<T>SkipBagNodePtr)header;
  119.   <T>SkipBagNodePtr temp;
  120.   
  121.   do
  122.   {
  123.     while ((temp = curr->forward[l])!=header
  124.        && <T>CMP(temp->item,key) < 0)
  125.       curr = temp;
  126.     update[l] = curr;
  127.   } 
  128.   while (--l >= 0);
  129.   
  130.   if (<T>CMP(temp->item,key)==0)
  131.   {
  132.     <T>SkipBagNodePtr *temp_forward = temp->forward;
  133.     
  134.     for (l = 0;
  135.      l <= curr_level && (curr = update[l])->forward[l] == temp;
  136.      l++)
  137.       curr->forward[l] = temp_forward[l];
  138.     
  139.     delete temp;
  140.     
  141.     <T>SkipBagNodePtr *forward = header->forward;
  142.     
  143.     while (forward[curr_level]==header && curr_level > 0)
  144.       curr_level--;
  145.  
  146.     level = curr_level;
  147.     count--;
  148.     delete update;
  149.     return;
  150.   }
  151. }
  152.  
  153. <T>SkipBagNodePtr <T>SkipBag::rightmost()
  154. {
  155.   <T>SkipBagNodePtr temp;
  156.   <T>SkipBagNode*   curr = header;
  157.   int l = level;
  158.   
  159.   do
  160.     while ((temp = curr->forward[l])!=header)
  161.       curr = temp;
  162.   while (--l >= 0);
  163.   
  164.   return temp==header ? 0 : temp;
  165. }
  166.  
  167. <T>SkipBagNodePtr <T>SkipBag::pred(<T>SkipBagNodePtr t)
  168. {
  169.   <T>SkipBagNodePtr temp, curr = (<T>SkipBagNodePtr) header;
  170.   int l = level;
  171.   
  172.   do
  173.     while ((temp = curr->forward[l])!=t)
  174.       curr = temp;
  175.   while (--l >= 0);
  176.   
  177.   return curr == header ? 0 : curr;
  178. }
  179.  
  180. void <T>SkipBag::_kill()
  181. {
  182.   <T>SkipBagNode *p = this->header->forward[0];
  183.   
  184.   while (p != header)
  185.   {
  186.     <T>SkipBagNodePtr q = p->forward[0];
  187.     delete p;
  188.     p = q;
  189.   }
  190. }
  191.  
  192. void <T>SkipBag::clear()
  193. {
  194.   <T>SkipBagNodePtr *buffer_start = header->forward;
  195.   <T>SkipBagNodePtr *trav = &header->forward[level+1];
  196.   _kill();
  197.   count = 0;
  198.     
  199.   while (trav > buffer_start)
  200.     *--trav = (<T>SkipBagNodePtr)header;
  201. }
  202.  
  203. Pix <T>SkipBag::seek(<T&> key, Pix i)
  204. {
  205.   <T>SkipBagNodePtr temp;
  206.   <T>SkipBagNode *curr  = header;
  207.   int   l = level;
  208.   if (i)
  209.       curr = (<T>SkipBagNode *)i;
  210.   
  211.   do
  212.   {
  213.     while ((temp = curr->forward[l])!=header &&
  214.        <T>CMP(temp->item, key) < 0)
  215.       curr = temp;
  216.   }
  217.   while (--l >= 0);
  218.   
  219.   if (<T>CMP(temp->item, key) != 0)
  220.     return 0;
  221.   else
  222.   {
  223.     return Pix(temp);
  224.   }
  225. }
  226.  
  227.  
  228. int <T>SkipBag::nof(<T&> item)
  229. {
  230.     int n = 0;
  231.     <T>SkipBagNodePtr t = (<T>SkipBagNodePtr)(seek(item));
  232.     if (t != 0)
  233.     {
  234.     do
  235.     {
  236.         ++n;
  237.         t = succ(t);
  238.     } while (t != 0 && <T>EQ(item, t->item));
  239.     }
  240.     return n;
  241. }
  242.  
  243. void <T>SkipBag::remove(<T&> key)
  244. {
  245.     Pix t = seek(key);
  246.     while (t != 0)
  247.     {
  248.     del(key);
  249.     t = seek(key);
  250.     }
  251. }
  252.  
  253.  
  254. /*
  255.  * random function for probabilistic balancing
  256.  *
  257.  * Hardwired for p = .25.  Not too flexible,
  258.  * but fast.  Changing this would require a constructor
  259.  * that would accept a different value for p, etc.
  260.  * Perhaps someone else would like to implement this?
  261.  *
  262.  */
  263. int <T>SkipBag::random_level (void)
  264. {
  265.   int rlevel = 0;
  266.   int b;
  267.   
  268.   do
  269.   {
  270.     b = random_bits & 3L;
  271.     if (!b)
  272.       rlevel++;
  273.     random_bits >>= 2;
  274.     if (--randoms_left == 0)
  275.     {
  276.       random_bits = gen->asLong();
  277.       randoms_left = BITS_IN_RANDOM / 2;
  278.     };
  279.   } 
  280.   while (!b);
  281.   
  282.   return rlevel > max_levels ? max_levels : rlevel;
  283. }
  284.  
  285. int <T>SkipBag::OK()
  286. {
  287.   int v = 1;
  288.   if (header == 0) 
  289.     v = 0;
  290.   else
  291.   {
  292.     int n = 0;
  293.     <T>SkipBagNodePtr trail = leftmost();
  294.     <T>SkipBagNodePtr t = 0;
  295.     if (trail) t = succ(trail);
  296.     if (t) n++;
  297.     while (t != 0)
  298.     {
  299.       ++n;
  300.       v &= <T>CMP(trail->item, t->item) < 0;
  301.       trail = t;
  302.       t = succ(t);
  303.     }
  304.     v &= n == count;
  305.   }
  306.   if (!v) error("invariant failure");
  307.   return v;
  308. }
  309.  
  310. <T>SkipBaginit::<T>SkipBaginit()
  311. {
  312.     if (!count)
  313.     <T>SkipBag::gen = new MLCG(time(0));
  314.     count++;
  315. }
  316.  
  317. <T>SkipBaginit::~<T>SkipBaginit()
  318. {
  319.     count--;
  320.     if (!count)
  321.     delete <T>SkipBag::gen;
  322. }
  323.