home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / splaymap.hp < prev    next >
Text File  |  1993-07-23  |  4KB  |  161 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24.  
  25. #ifndef _<T><C>SplayMap_h
  26. #pragma once
  27. #define _<T><C>SplayMap_h 1
  28.  
  29. #include "<T>.<C>.Map.h"
  30.  
  31. #ifndef _<T><C>SplayNode
  32. #define _<T><C>SplayNode 1
  33.  
  34. struct <T><C>SplayNode
  35. {
  36.   <T><C>SplayNode*   lt;
  37.   <T><C>SplayNode*   rt;
  38.   <T><C>SplayNode*   par;
  39.   <T>                item;
  40.   <C>                cont;
  41.                      <T><C>SplayNode(<T&> h, <C&> c, 
  42.                                      <T><C>SplayNode* l=0, 
  43.                                      <T><C>SplayNode* r=0);
  44.                   ~<T><C>SplayNode();
  45. };
  46.  
  47.  
  48. inline <T><C>SplayNode::<T><C>SplayNode(<T&> h, <C&> c, 
  49.                                         <T><C>SplayNode* l=0, 
  50.                                         <T><C>SplayNode* r=0)
  51.      :item(h), cont(c), lt(l), rt(r)
  52. {
  53.   par = 0;
  54. }
  55.  
  56. inline <T><C>SplayNode::~<T><C>SplayNode() {}
  57.  
  58. typedef <T><C>SplayNode* <T><C>SplayNodePtr;
  59.  
  60. #endif
  61.  
  62. class <T><C>SplayMap : public <T><C>Map
  63. {
  64. protected:
  65.   <T><C>SplayNode*   root;
  66.  
  67.   <T><C>SplayNode*   leftmost();
  68.   <T><C>SplayNode*   rightmost();
  69.   <T><C>SplayNode*   pred(<T><C>SplayNode* t);
  70.   <T><C>SplayNode*   succ(<T><C>SplayNode* t);
  71.   void               _kill(<T><C>SplayNode* t);
  72.   <T><C>SplayNode*   _copy(<T><C>SplayNode* t);
  73.  
  74. public:
  75.                <T><C>SplayMap(<C&> dflt);
  76.                <T><C>SplayMap(<T><C>SplayMap& a);
  77.                ~<T><C>SplayMap();
  78.  
  79.   <C>&          operator [] (<T&> key);
  80.  
  81.   void          del(<T&> key);
  82.  
  83.   Pix           first();
  84.   void          next(Pix& i);
  85.   <T>&          key(Pix i);
  86.   <C>&          contents(Pix i);
  87.  
  88.   Pix           seek(<T&> key);
  89.   int           contains(<T&> key);
  90.  
  91.   void          clear(); 
  92.  
  93.   Pix           last();
  94.   void          prev(Pix& i);
  95.  
  96.   int           OK();
  97. };
  98.  
  99. inline <T><C>SplayMap::~<T><C>SplayMap()
  100. {
  101.   _kill(root);
  102. }
  103.  
  104. inline <T><C>SplayMap::<T><C>SplayMap(<C&> dflt) :(dflt)
  105. {
  106.   root = 0;
  107. }
  108.  
  109. inline <T><C>SplayMap::<T><C>SplayMap(<T><C>SplayMap& b) :(b.def)
  110. {
  111.   count = b.count;
  112.   root = _copy(b.root);
  113. }
  114.  
  115. inline Pix <T><C>SplayMap::first()
  116. {
  117.   return Pix(leftmost());
  118. }
  119.  
  120. inline Pix <T><C>SplayMap::last()
  121. {
  122.   return Pix(rightmost());
  123. }
  124.  
  125. inline void <T><C>SplayMap::next(Pix& i)
  126. {
  127.   if (i != 0) i = Pix(succ((<T><C>SplayNode*)i));
  128. }
  129.  
  130. inline void <T><C>SplayMap::prev(Pix& i)
  131. {
  132.   if (i != 0) i = Pix(pred((<T><C>SplayNode*)i));
  133. }
  134.  
  135. inline <T>& <T><C>SplayMap::key (Pix i)
  136. {
  137.   if (i == 0) error("null Pix");
  138.   return ((<T><C>SplayNode*)i)->item;
  139. }
  140.  
  141. inline <C>& <T><C>SplayMap::contents (Pix i)
  142. {
  143.   if (i == 0) error("null Pix");
  144.   return ((<T><C>SplayNode*)i)->cont;
  145. }
  146.  
  147. inline void <T><C>SplayMap::clear()
  148. {
  149.   _kill(root);
  150.   count = 0;
  151.   root = 0;
  152. }
  153.  
  154. inline int <T><C>SplayMap::contains(<T&> key)
  155. {
  156.   return seek(key) != 0;
  157. }
  158.  
  159.  
  160. #endif
  161.