home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / libg++-2.5.3-bin.lha / lib / g++-include / gen / BSTSet.hP < prev    next >
Text File  |  1994-02-21  |  3KB  |  153 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 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, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>BSTSet_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>BSTSet_h 1
  25.  
  26. #include "<T>.Set.h"
  27.  
  28. #ifndef _<T>BSTNode
  29. #define _<T>BSTNode 1
  30.  
  31. struct <T>BSTNode
  32. {
  33.   <T>BSTNode*         lt;
  34.   <T>BSTNode*         rt;
  35.   <T>BSTNode*         par;
  36.   <T>                 item;
  37.                       <T>BSTNode(<T&> h, <T>BSTNode* l=0, <T>BSTNode* r=0,
  38.                                  <T>BSTNode* p = 0);
  39.                       ~<T>BSTNode();
  40. };
  41.  
  42. inline <T>BSTNode::<T>BSTNode(<T&> h, <T>BSTNode* l, <T>BSTNode* r,
  43.                               <T>BSTNode* p)
  44. :item(h), lt(l), rt(r), par(p) {}
  45.  
  46. inline <T>BSTNode::~<T>BSTNode() {}
  47.  
  48. typedef <T>BSTNode* <T>BSTNodePtr;
  49.  
  50. #endif
  51.  
  52. class <T>BSTSet : public <T>Set
  53. {
  54. protected:
  55.   <T>BSTNode*      root;
  56.  
  57.   <T>BSTNode*     leftmost();
  58.   <T>BSTNode*     rightmost();
  59.   <T>BSTNode*     pred(<T>BSTNode* t);
  60.   <T>BSTNode*     succ(<T>BSTNode* t);
  61.   void            _kill(<T>BSTNode* t);
  62.   <T>BSTNode*     _copy(<T>BSTNode* t);
  63.  
  64. public:
  65.                    <T>BSTSet();
  66.                    <T>BSTSet(<T>BSTSet& a);
  67.                    ~<T>BSTSet();
  68.  
  69.   Pix             add(<T&> item);
  70.   void            del(<T&> item);
  71.   int             contains(<T&> item);
  72.  
  73.   void            clear();
  74.  
  75.   Pix             first();
  76.   void            next(Pix& i);
  77.   <T>&            operator () (Pix i);
  78.   Pix             seek(<T&> item);
  79.  
  80.   Pix             last();
  81.   void            prev(Pix& i);
  82.  
  83.   int             operator == (<T>BSTSet& b);
  84.   int             operator != (<T>BSTSet& b);
  85.   int             operator <= (<T>BSTSet& b); 
  86.  
  87.   void            balance();
  88.   int             OK();
  89. };
  90.  
  91. inline <T>BSTSet::~<T>BSTSet()
  92. {
  93.   _kill(root);
  94. }
  95.  
  96. inline <T>BSTSet::<T>BSTSet()
  97. {
  98.   root = 0;
  99.   count = 0;
  100. }
  101.  
  102.  
  103. inline <T>BSTSet::<T>BSTSet(<T>BSTSet& a)
  104. {
  105.   count = a.count;
  106.   root = _copy(a.root);
  107. }
  108.  
  109. inline int <T>BSTSet::operator != (<T>BSTSet& b)
  110. {
  111.   return ! (*this == b);
  112. }
  113.  
  114. inline Pix <T>BSTSet::first()
  115. {
  116.   return Pix(leftmost());
  117. }
  118.  
  119. inline Pix <T>BSTSet::last()
  120. {
  121.   return Pix(rightmost());
  122. }
  123.  
  124. inline void <T>BSTSet::next(Pix& i)
  125. {
  126.   if (i != 0) i = Pix(succ((<T>BSTNode*)i));
  127. }
  128.  
  129. inline void <T>BSTSet::prev(Pix& i)
  130. {
  131.   if (i != 0) i = Pix(pred((<T>BSTNode*)i));
  132. }
  133.  
  134. inline <T>& <T>BSTSet::operator () (Pix i)
  135. {
  136.   if (i == 0) error("null Pix");
  137.   return ((<T>BSTNode*)i)->item;
  138. }
  139.  
  140. inline void <T>BSTSet::clear()
  141. {
  142.   _kill(root);
  143.   count = 0;
  144.   root = 0;
  145. }
  146.  
  147. inline int <T>BSTSet::contains(<T&> key)
  148. {
  149.   return seek(key) != 0;
  150. }
  151.  
  152. #endif
  153.