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