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 / AVLSet.hP < prev    next >
Text File  |  1996-10-12  |  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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>AVL_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>AVL_h 1
  25.  
  26. #include "<T>.Set.h"
  27.  
  28. struct <T>AVLNode
  29. {
  30.   <T>AVLNode*         lt;
  31.   <T>AVLNode*         rt;
  32.   <T>                 item;
  33.   char                stat;
  34.                       <T>AVLNode(<T&> h, <T>AVLNode* l=0, <T>AVLNode* r=0);
  35.                       ~<T>AVLNode();
  36. };
  37.  
  38. inline <T>AVLNode::<T>AVLNode(<T&> h, <T>AVLNode* l, <T>AVLNode* r)
  39. :lt(l), rt(r), item(h), stat(0) {}
  40.  
  41. inline <T>AVLNode::~<T>AVLNode() {}
  42.  
  43. typedef <T>AVLNode* <T>AVLNodePtr;
  44.  
  45.  
  46. class <T>AVLSet : public <T>Set
  47. {
  48. protected:
  49.   <T>AVLNode*   root;
  50.  
  51.                 <T>AVLSet(<T>AVLNode* p, int l);
  52.  
  53.   <T>AVLNode*   leftmost();
  54.   <T>AVLNode*   rightmost();
  55.   <T>AVLNode*   pred(<T>AVLNode* t);
  56.   <T>AVLNode*   succ(<T>AVLNode* t);
  57.   void          _kill(<T>AVLNode* t);
  58.   void          _add(<T>AVLNode*& t);
  59.   void          _del(<T>AVLNode* p, <T>AVLNode*& t);
  60.  
  61. public:
  62.                 <T>AVLSet();
  63.                 <T>AVLSet(<T>AVLSet& a);
  64.   inline               ~<T>AVLSet();
  65.  
  66.   Pix           add(<T&> item);
  67.   void          del(<T&> item);
  68.   inline int           contains(<T&> item);
  69.  
  70.   inline void          clear();
  71.  
  72.   inline Pix           first();
  73.   inline void          next(Pix& i);
  74.   inline <T>&          operator () (Pix i);
  75.   int           owns(Pix i);
  76.   Pix           seek(<T&> item);
  77.  
  78.   Pix           last();
  79.   void          prev(Pix& i);
  80.  
  81.   void          operator |= (<T>AVLSet& b);
  82.   void          operator -= (<T>AVLSet& b);
  83.   void          operator &= (<T>AVLSet& b);
  84.  
  85.   int           operator == (<T>AVLSet& b);
  86.   int           operator != (<T>AVLSet& b);
  87.   int           operator <= (<T>AVLSet& b); 
  88.  
  89.   int           OK();
  90. };
  91.  
  92. inline <T>AVLSet::~<T>AVLSet()
  93. {
  94.   _kill(root);
  95. }
  96.  
  97. inline <T>AVLSet::<T>AVLSet()
  98. {
  99.   root = 0;
  100.   count = 0;
  101. }
  102.  
  103. inline <T>AVLSet::<T>AVLSet(<T>AVLNode* p, int l)
  104. {
  105.   root = p;
  106.   count = l;
  107. }
  108.  
  109. inline int <T>AVLSet::operator != (<T>AVLSet& b)
  110. {
  111.   return ! ((*this) == b);
  112. }
  113.  
  114. inline Pix <T>AVLSet::first()
  115. {
  116.   return Pix(leftmost());
  117. }
  118.  
  119. inline Pix <T>AVLSet::last()
  120. {
  121.   return Pix(rightmost());
  122. }
  123.  
  124. inline void <T>AVLSet::next(Pix& i)
  125. {
  126.   if (i != 0) i = Pix(succ((<T>AVLNode*)i));
  127. }
  128.  
  129. inline void <T>AVLSet::prev(Pix& i)
  130. {
  131.   if (i != 0) i = Pix(pred((<T>AVLNode*)i));
  132. }
  133.  
  134. inline <T>& <T>AVLSet::operator () (Pix i)
  135. {
  136.   if (i == 0) error("null Pix");
  137.   return ((<T>AVLNode*)i)->item;
  138. }
  139.  
  140. inline void <T>AVLSet::clear()
  141. {
  142.   _kill(root);
  143.   count = 0;
  144.   root = 0;
  145. }
  146.  
  147. inline int <T>AVLSet::contains(<T&> key)
  148. {
  149.   return seek(key) != 0;
  150. }
  151.  
  152. #endif
  153.