BTree.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_BTree_hxx
00024 #define __ccxx_BTree_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/StaticObjectPool.h++>
00028 #include <commonc++/Iterator.h++>
00029
00030
00031 #ifdef CCXX_OS_WINDOWS
00032 #include <search.h>
00033 #endif
00034
00035 #include <iostream>
00036 #include <list>
00037
00038 namespace ccxx {
00039
00050 template <typename K, typename V, int ORDER = 10> class BTree
00051 {
00052 private:
00053
00054 enum _Status { BTreeOK, BTreeOverflow, BTreeDuplicate, BTreeUnderflow,
00055 BTreeNotFound };
00056
00057 struct _Datum
00058 {
00059 K key;
00060 V value;
00061 _Datum *next;
00062 _Datum *prev;
00063 };
00064
00065 struct _Node
00066 {
00067 _Datum *keys[ORDER * 2];
00068 _Node *children[ORDER * 2 + 1];
00069 _Node *parent;
00070 short count;
00071
00072 _Node() : parent(NULL), count(0)
00073 {
00074 for(int i = 0; i <= ORDER * 2; i++)
00075 children[i] = NULL;
00076 }
00077 };
00078
00079 _Node *_root;
00080 int _nkeys;
00081 int _order;
00082 size_t _capacity;
00083 size_t _numItems;
00084
00085 StaticObjectPool<_Node> _nodePool;
00086 StaticObjectPool<_Datum> _datumPool;
00087
00088 _Datum *_head;
00089 _Datum *_tail;
00090
00091 uint_t bsearch(const K x, _Datum **a, uint_t len) const;
00092 _Status insertNode(_Datum *key, _Node *node, _Datum **rkey, _Node **rnode);
00093 _Status deleteNode(K key, _Node *node);
00094 void _dump(_Node *node, int depth, std::ostream& stream) const;
00095
00096 void unlink(_Datum *datum);
00097 void link(_Datum *datum);
00098
00099 protected:
00100
00106 virtual void itemDropped(V data) const;
00107
00108 public:
00109
00114 BTree(size_t capacity);
00115
00117 virtual ~BTree();
00118
00124 bool put(const K key, const V data);
00125
00130 bool remove(const K key);
00131
00137 V get(const K key);
00138
00145 bool contains(const K key) const;
00146
00151 void dump(std::ostream& stream) const;
00152
00157 void getKeys(std::list<K>& keys) const;
00158
00163 void getValues(std::list<V>& values) const;
00164
00166 void clear();
00167
00169 inline size_t getCapacity() const throw()
00170 { return(_capacity); }
00171
00176 class Iterator : public ccxx::Iterator<V>
00177 {
00178 private:
00179
00180 BTree<K, V, ORDER>& _tree;
00181 _Datum *_prev;
00182 _Datum *_current;
00183 _Datum *_next;
00184
00185 public:
00186
00188 Iterator(BTree<K, V, ORDER>& tree);
00189 virtual ~Iterator() {}
00190
00191 virtual void rewind();
00192 virtual V next() throw(OutOfBoundsException);
00193 virtual bool hasNext();
00194 virtual void remove();
00195 };
00196 };
00197
00198 #include <commonc++/BTreeImpl.h++>
00199
00200 };
00201
00202 #endif // __ccxx_BTree_hxx