libcommonc++  0.7
BTree.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_BTree_hxx
24 #define __ccxx_BTree_hxx
25 
26 #include <commonc++/Common.h++>
28 #include <commonc++/Iterator.h++>
29 
30 
31 #ifdef CCXX_OS_WINDOWS
32 #include <search.h>
33 #endif
34 
35 #include <iostream>
36 #include <list>
37 
38 namespace ccxx {
39 
50 template <typename K, typename V, int ORDER = 10> class BTree
51 {
52  private:
53 
55  enum _Status { BTreeOK, BTreeOverflow, BTreeDuplicate, BTreeUnderflow,
56  BTreeNotFound };
57 
58  struct _Datum
59  {
60  K key;
61  V value;
62  _Datum* next;
63  _Datum* prev;
64  };
65 
66  struct _Node
67  {
68  _Datum* keys[ORDER * 2];
69  _Node* children[ORDER * 2 + 1];
70  _Node* parent;
71  short count;
72 
73  _Node() : parent(NULL), count(0)
74  {
75  for(int i = 0; i <= ORDER * 2; i++)
76  children[i] = NULL;
77  }
78  };
81  _Node* _root;
82  int _nkeys;
83  int _order;
84  uint_t _capacity;
85  uint_t _numItems;
86 
87  StaticObjectPool<_Node> _nodePool;
88  StaticObjectPool<_Datum> _datumPool;
89 
90  _Datum* _head;
91  _Datum* _tail;
92 
93  uint_t bsearch(const K x, _Datum** a, uint_t len) const;
94  _Status insertNode(_Datum* key, _Node* node, _Datum** rkey, _Node** rnode);
95  _Status deleteNode(K key, _Node* node);
96  void _dump(_Node* node, int depth, std::ostream& stream) const;
97 
98  void unlink(_Datum* datum);
99  void link(_Datum* datum);
100 
101  protected:
102 
109  virtual void itemDropped(V data) const;
110 
111  public:
112 
118  BTree(uint_t capacity);
119 
121  virtual ~BTree();
122 
129  bool put(const K key, const V data);
130 
136  bool remove(const K key);
137 
144  V get(const K key);
145 
153  bool contains(const K key) const;
154 
160  void dump(std::ostream& stream) const;
161 
167  void getKeys(std::list<K>& keys) const;
168 
174  void getValues(std::list<V>& values) const;
175 
177  void clear();
178 
180  inline uint_t getCapacity() const
181  { return(_capacity); }
182 
188  class Iterator : public ccxx::Iterator<V>
189  {
190  private:
191 
192  BTree<K, V, ORDER>& _tree;
193  _Datum* _prev;
194  _Datum* _current;
195  _Datum* _next;
196 
197  public:
198 
201  virtual ~Iterator() {}
202 
203  virtual void rewind();
204  virtual V next();
205  virtual bool hasNext();
206  virtual void remove();
207  };
208 };
209 
210 #include <commonc++/BTreeImpl.h++>
211 
212 } // namespace ccxx
213 
214 #endif // __ccxx_BTree_hxx
virtual void itemDropped(V data) const
Called whenever an item is removed from the BTree to make room for another.
void getKeys(std::list< K > &keys) const
Get a list of all of the keys in the tree, in access-order.
virtual ~Iterator()
Destructor.
Definition: BTree.h++:201
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
void clear()
Remove all items from the tree.
void dump(std::ostream &stream) const
Dump a representation of the tree (keys only) to a stream.
void getValues(std::list< V > &values) const
Get a list of all of the values in the tree, in access-order.
A balanced, n-ary tree data structure for storing key/value relationships.
Definition: BTree.h++:50
uint_t getCapacity() const
Get the tree capacity.
Definition: BTree.h++:180
A BTree iterator.
Definition: BTree.h++:188
bool put(const K key, const V data)
Put a new item in the tree.
A Java-style iterator.
Definition: Iterator.h++:35
BTree(uint_t capacity)
Construct a new BTree with the given maximum capacity.
bool contains(const K key) const
Determine if an item with the given key exists in the tree.
virtual ~BTree()
Destructor.
Definition: AllocationMap.c++:25