BTree.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 }; // namespace ccxx
00201 
00202 #endif // __ccxx_BTree_hxx
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3