stx/btree_map.h

Go to the documentation of this file.
00001 // $Id: btree_map.h 35 2007-04-27 11:26:33Z tb $
00006 /*
00007  * STX B+ Tree Template Classes v0.7
00008  * Copyright (C) 2007 Timo Bingmann
00009  *
00010  * This library is free software; you can redistribute it and/or modify it
00011  * under the terms of the GNU Lesser General Public License as published by the
00012  * Free Software Foundation; either version 2.1 of the License, or (at your
00013  * option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful, but WITHOUT
00016  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00017  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
00018  * for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public License
00021  * along with this library; if not, write to the Free Software Foundation,
00022  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 #ifndef _STX_BTREE_MAP_H_
00026 #define _STX_BTREE_MAP_H_
00027 
00028 #include <stx/btree.h>
00029 
00030 namespace stx {
00031 
00045 template <typename _Key, typename _Data,
00046           typename _Compare = std::less<_Key>,
00047           typename _Traits = btree_default_map_traits<_Key, _Data> >
00048 class btree_map
00049 {
00050 public:
00051     // *** Template Parameter Types
00052 
00055     typedef _Key                        key_type;
00056 
00059     typedef _Data                       data_type;
00060 
00062     typedef _Compare                    key_compare;
00063 
00066     typedef _Traits                     traits;
00067 
00068 public:
00069     // *** Constructed Types
00070 
00072     typedef btree_map<key_type, data_type, key_compare, traits> self;
00073 
00076     typedef std::pair<key_type, data_type>      value_type;
00077 
00079     typedef stx::btree<key_type, data_type, value_type, key_compare, traits, false> btree_impl;
00080 
00082     typedef typename btree_impl::value_compare  value_compare;
00083 
00085     typedef typename btree_impl::size_type      size_type;
00086 
00088     typedef typename btree_impl::tree_stats     tree_stats;
00089 
00090 public:
00091     // *** Static Constant Options and Values of the B+ Tree
00092 
00094     static const unsigned short         leafslotmax =  btree_impl::leafslotmax;
00095 
00098     static const unsigned short         innerslotmax =  btree_impl::innerslotmax;
00099 
00103     static const unsigned short         minleafslots = btree_impl::minleafslots;
00104 
00108     static const unsigned short         mininnerslots = btree_impl::mininnerslots;
00109 
00112     static const bool                   selfverify = btree_impl::selfverify;
00113 
00117     static const bool                   debug = btree_impl::debug;
00118 
00120     static const bool                   allow_duplicates = btree_impl::allow_duplicates;
00121 
00122 public:
00123     // *** Iterators and Reverse Iterators
00124 
00127     typedef typename btree_impl::iterator               iterator;
00128 
00131     typedef typename btree_impl::const_iterator         const_iterator;
00132 
00134     typedef typename btree_impl::reverse_iterator       reverse_iterator;
00135 
00137     typedef typename btree_impl::const_reverse_iterator const_reverse_iterator;
00138 
00139 private:
00140     // *** Tree Implementation Object
00141 
00143     btree_impl  tree;
00144 
00145 public:
00146     // *** Constructors and Destructor
00147 
00150     inline btree_map()
00151         : tree()
00152     {
00153     }
00154 
00157     inline btree_map(const key_compare &kcf)
00158         : tree(kcf)
00159     {
00160     }
00161 
00163     template <class InputIterator>
00164     inline btree_map(InputIterator first, InputIterator last)
00165         : tree(first, last)
00166     {
00167     }
00168 
00171     template <class InputIterator>
00172     inline btree_map(InputIterator first, InputIterator last, const key_compare &kcf)
00173         : tree(first, last, kcf)
00174     {
00175     }
00176 
00178     inline ~btree_map()
00179     {
00180     }
00181 
00183     void swap(self& from)
00184     {
00185         std::swap(tree, from.tree);
00186     }
00187 
00188 public:
00189     // *** Key and Value Comparison Function Objects
00190 
00192     inline key_compare key_comp() const
00193     {
00194         return tree.key_comp();
00195     }
00196 
00199     inline value_compare value_comp() const
00200     {
00201         return tree.value_comp();
00202     }
00203 
00204 public:
00205     // *** Fast Destruction of the B+ Tree
00206 
00208     void clear()
00209     {
00210         tree.clear();
00211     }
00212 
00213 public:
00214     // *** STL Iterator Construction Functions
00215 
00218     inline iterator begin()
00219     {
00220         return tree.begin();
00221     }
00222 
00225     inline iterator end()
00226     {
00227         return tree.end();
00228     }
00229 
00232     inline const_iterator begin() const
00233     {
00234         return tree.begin();
00235     }
00236 
00239     inline const_iterator end() const
00240     {
00241         return tree.end();
00242     }
00243 
00246     inline reverse_iterator rbegin()
00247     {
00248         return tree.rbegin();
00249     }
00250 
00253     inline reverse_iterator rend()
00254     {
00255         return tree.rend();
00256     }
00257 
00260     inline const_reverse_iterator rbegin() const
00261     {
00262         return tree.rbegin();
00263     }
00264 
00267     inline const_reverse_iterator rend() const
00268     {
00269         return tree.rend();
00270     }
00271 
00272 public:
00273     // *** Access Functions to the Item Count
00274 
00276     inline size_type size() const
00277     {
00278         return tree.size();
00279     }
00280 
00282     inline bool empty() const
00283     {
00284         return tree.empty();
00285     }
00286     
00289     inline size_type max_size() const
00290     {
00291         return tree.max_size();
00292     }
00293 
00295     inline const tree_stats& get_stats() const
00296     {
00297         return tree.get_stats();
00298     }
00299 
00300 public:
00301     // *** Standard Access Functions Querying the Tree by Descending to a Leaf
00302 
00305     bool exists(const key_type &key) const
00306     {
00307         return tree.exists(key);
00308     }
00309 
00312     iterator find(const key_type &key)
00313     {
00314         return tree.find(key);
00315     }
00316 
00319     const_iterator find(const key_type &key) const
00320     {
00321         return tree.find(key);
00322     }
00323 
00327     size_type count(const key_type &key) const
00328     {
00329         return tree.count(key);
00330     }
00331 
00334     iterator lower_bound(const key_type& key)
00335     {
00336         return tree.lower_bound(key);
00337     }
00338 
00341     const_iterator lower_bound(const key_type& key) const
00342     {
00343         return tree.lower_bound(key);
00344     }
00345 
00348     iterator upper_bound(const key_type& key)
00349     {
00350         return tree.upper_bound(key);
00351     }
00352 
00355     const_iterator upper_bound(const key_type& key) const
00356     {
00357         return tree.upper_bound(key);
00358     }
00359 
00361     inline std::pair<iterator, iterator> equal_range(const key_type& key)
00362     {
00363         return tree.equal_range(key);
00364     }
00365 
00367     inline std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00368     {
00369         return tree.equal_range(key);
00370     }
00371 
00372 public:
00373     // *** B+ Tree Object Comparison Functions
00374 
00378     inline bool operator==(const self &other) const
00379     {
00380         return (tree == other.tree);
00381     }
00382 
00384     inline bool operator!=(const self &other) const
00385     {
00386         return (tree != other.tree);
00387     }
00388 
00391     inline bool operator<(const self &other) const
00392     {
00393         return (tree < other.tree);
00394     }
00395 
00397     inline bool operator>(const self &other) const
00398     {
00399         return (tree > other.tree);
00400     }
00401 
00403     inline bool operator<=(const self &other) const
00404     {
00405         return (tree <= other.tree);
00406     }
00407 
00409     inline bool operator>=(const self &other) const
00410     {
00411         return (tree >= other.tree);
00412     }
00413 
00414 public:
00416 
00418     inline self& operator= (const self &other)
00419     {
00420         if (this != &other)
00421         {
00422             tree = other.tree;
00423         }
00424         return *this;
00425     }
00426 
00429     inline btree_map(const self &other)
00430         : tree(other.tree)
00431     {
00432     }
00433     
00434 public:
00435     // *** Public Insertion Functions
00436 
00439     inline std::pair<iterator, bool> insert(const value_type& x)
00440     {
00441         return tree.insert2(x.first, x.second);
00442     }
00443     
00447     inline std::pair<iterator, bool> insert(const key_type& key, const data_type& data)
00448     {
00449         return tree.insert2(key, data);
00450     }
00451 
00456     inline std::pair<iterator, bool> insert2(const key_type& key, const data_type& data)
00457     {
00458         return tree.insert2(key, data);
00459     }
00460 
00463     inline iterator insert(iterator hint, const value_type &x)
00464     {
00465         return tree.insert2(hint, x.first, x.second);
00466     }
00467 
00470     inline iterator insert2(iterator hint, const key_type& key, const data_type& data)
00471     {
00472         return tree.insert2(hint, key, data);
00473     }
00474 
00478     inline data_type& operator[](const key_type& key)
00479     {
00480         iterator i = insert( value_type(key, data_type()) ).first;
00481         return i.data();
00482     }
00483 
00486     template <typename InputIterator>
00487     inline void insert(InputIterator first, InputIterator last)
00488     {
00489         return tree.insert(first, last);
00490     }
00491 
00492 public:
00493     // *** Public Erase Functions
00494 
00497     bool erase_one(const key_type &key)
00498     {
00499         return tree.erase_one(key);
00500     }
00501 
00504     size_type erase(const key_type &key)
00505     {
00506         return tree.erase(key);
00507     }
00508 
00509 #ifdef BTREE_TODO
00511     void erase(iterator iter)
00512     {
00513 
00514     }
00515 #endif
00516 
00517 #ifdef BTREE_TODO
00520     void erase(iterator /* first */, iterator /* last */)
00521     {
00522         abort();
00523     }
00524 #endif
00525 
00526 public:
00527     // *** Debug Printing
00528 
00532     void print() const
00533     {
00534         tree.print();
00535     }
00536 
00538     void print_leaves() const
00539     {
00540         tree.print_leaves();
00541     }
00542 
00543 public:
00544     // *** Verification of B+ Tree Invariants
00545 
00548     void verify() const
00549     {
00550         tree.verify();
00551     }
00552 
00553 public:
00554 
00559     void dump(std::ostream &os) const
00560     {
00561         tree.dump(os);
00562     }
00563 
00568     bool restore(std::istream &is)
00569     {
00570         return tree.restore(is);
00571     }
00572 };
00573 
00574 } // namespace stx
00575 
00576 #endif // _STX_BTREE_MAP_H_

Generated on Fri Apr 27 14:49:56 2007 for STX B+ Tree Template Classes by  doxygen 1.5.2