STX B+ Tree Template Classes 0.8.6

stx/btree_map.h

Go to the documentation of this file.
00001 // $Id: btree_map.h 128 2011-05-18 07:23:35Z tb $
00006 /*
00007  * STX B+ Tree Template Classes v0.8.6
00008  * Copyright (C) 2008-2011 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 
00046 template <typename _Key, typename _Data,
00047           typename _Compare = std::less<_Key>,
00048           typename _Traits = btree_default_map_traits<_Key, _Data>,
00049           typename _Alloc = std::allocator<std::pair<_Key, _Data> > >
00050 class btree_map
00051 {
00052 public:
00053     // *** Template Parameter Types
00054 
00057     typedef _Key                        key_type;
00058 
00061     typedef _Data                       data_type;
00062 
00064     typedef _Compare                    key_compare;
00065 
00068     typedef _Traits                     traits;
00069 
00071     typedef _Alloc                      allocator_type;
00072 
00073     // The macro BTREE_FRIENDS can be used by outside class to access the B+
00074     // tree internals. This was added for wxBTreeDemo to be able to draw the
00075     // tree.
00076     BTREE_FRIENDS
00077 
00078 public:
00079     // *** Constructed Types
00080 
00082     typedef btree_map<key_type, data_type, key_compare, traits, allocator_type> self;
00083 
00086     typedef std::pair<key_type, data_type>      value_type;
00087 
00089     typedef stx::btree<key_type, data_type, value_type, key_compare, traits, false, allocator_type> btree_impl;
00090 
00092     typedef typename btree_impl::value_compare  value_compare;
00093 
00095     typedef typename btree_impl::size_type      size_type;
00096 
00098     typedef typename btree_impl::tree_stats     tree_stats;
00099 
00100 public:
00101     // *** Static Constant Options and Values of the B+ Tree
00102 
00104     static const unsigned short         leafslotmax =  btree_impl::leafslotmax;
00105 
00108     static const unsigned short         innerslotmax =  btree_impl::innerslotmax;
00109 
00113     static const unsigned short         minleafslots = btree_impl::minleafslots;
00114 
00118     static const unsigned short         mininnerslots = btree_impl::mininnerslots;
00119 
00122     static const bool                   selfverify = btree_impl::selfverify;
00123 
00127     static const bool                   debug = btree_impl::debug;
00128 
00130     static const bool                   allow_duplicates = btree_impl::allow_duplicates;
00131 
00132 public:
00133     // *** Iterators and Reverse Iterators
00134 
00137     typedef typename btree_impl::iterator               iterator;
00138 
00141     typedef typename btree_impl::const_iterator         const_iterator;
00142 
00144     typedef typename btree_impl::reverse_iterator       reverse_iterator;
00145 
00147     typedef typename btree_impl::const_reverse_iterator const_reverse_iterator;
00148 
00149 private:
00150     // *** Tree Implementation Object
00151 
00153     btree_impl  tree;
00154 
00155 public:
00156     // *** Constructors and Destructor
00157 
00160     explicit inline btree_map(const allocator_type &alloc = allocator_type())
00161         : tree(alloc)
00162     {
00163     }
00164 
00167     explicit inline btree_map(const key_compare &kcf,
00168                               const allocator_type &alloc = allocator_type())
00169         : tree(kcf, alloc)
00170     {
00171     }
00172 
00174     template <class InputIterator>
00175     inline btree_map(InputIterator first, InputIterator last,
00176                      const allocator_type &alloc = allocator_type())
00177         : tree(first, last, alloc)
00178     {
00179     }
00180 
00183     template <class InputIterator>
00184     inline btree_map(InputIterator first, InputIterator last, const key_compare &kcf,
00185                      const allocator_type &alloc = allocator_type())
00186         : tree(first, last, kcf, alloc)
00187     {
00188     }
00189 
00191     inline ~btree_map()
00192     {
00193     }
00194 
00196     void swap(self& from)
00197     {
00198         std::swap(tree, from.tree);
00199     }
00200 
00201 public:
00202     // *** Key and Value Comparison Function Objects
00203 
00205     inline key_compare key_comp() const
00206     {
00207         return tree.key_comp();
00208     }
00209 
00212     inline value_compare value_comp() const
00213     {
00214         return tree.value_comp();
00215     }
00216 
00217 public:
00218     // *** Allocators
00219 
00221     allocator_type get_allocator() const
00222     {
00223         return tree.get_allocator();
00224     }
00225 
00226 public:
00227     // *** Fast Destruction of the B+ Tree
00228 
00230     void clear()
00231     {
00232         tree.clear();
00233     }
00234 
00235 public:
00236     // *** STL Iterator Construction Functions
00237 
00240     inline iterator begin()
00241     {
00242         return tree.begin();
00243     }
00244 
00247     inline iterator end()
00248     {
00249         return tree.end();
00250     }
00251 
00254     inline const_iterator begin() const
00255     {
00256         return tree.begin();
00257     }
00258 
00261     inline const_iterator end() const
00262     {
00263         return tree.end();
00264     }
00265 
00268     inline reverse_iterator rbegin()
00269     {
00270         return tree.rbegin();
00271     }
00272 
00275     inline reverse_iterator rend()
00276     {
00277         return tree.rend();
00278     }
00279 
00282     inline const_reverse_iterator rbegin() const
00283     {
00284         return tree.rbegin();
00285     }
00286 
00289     inline const_reverse_iterator rend() const
00290     {
00291         return tree.rend();
00292     }
00293 
00294 public:
00295     // *** Access Functions to the Item Count
00296 
00298     inline size_type size() const
00299     {
00300         return tree.size();
00301     }
00302 
00304     inline bool empty() const
00305     {
00306         return tree.empty();
00307     }
00308 
00311     inline size_type max_size() const
00312     {
00313         return tree.max_size();
00314     }
00315 
00317     inline const tree_stats& get_stats() const
00318     {
00319         return tree.get_stats();
00320     }
00321 
00322 public:
00323     // *** Standard Access Functions Querying the Tree by Descending to a Leaf
00324 
00327     bool exists(const key_type &key) const
00328     {
00329         return tree.exists(key);
00330     }
00331 
00334     iterator find(const key_type &key)
00335     {
00336         return tree.find(key);
00337     }
00338 
00341     const_iterator find(const key_type &key) const
00342     {
00343         return tree.find(key);
00344     }
00345 
00349     size_type count(const key_type &key) const
00350     {
00351         return tree.count(key);
00352     }
00353 
00356     iterator lower_bound(const key_type& key)
00357     {
00358         return tree.lower_bound(key);
00359     }
00360 
00364     const_iterator lower_bound(const key_type& key) const
00365     {
00366         return tree.lower_bound(key);
00367     }
00368 
00371     iterator upper_bound(const key_type& key)
00372     {
00373         return tree.upper_bound(key);
00374     }
00375 
00379     const_iterator upper_bound(const key_type& key) const
00380     {
00381         return tree.upper_bound(key);
00382     }
00383 
00385     inline std::pair<iterator, iterator> equal_range(const key_type& key)
00386     {
00387         return tree.equal_range(key);
00388     }
00389 
00391     inline std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00392     {
00393         return tree.equal_range(key);
00394     }
00395 
00396 public:
00397     // *** B+ Tree Object Comparison Functions
00398 
00402     inline bool operator==(const self &other) const
00403     {
00404         return (tree == other.tree);
00405     }
00406 
00408     inline bool operator!=(const self &other) const
00409     {
00410         return (tree != other.tree);
00411     }
00412 
00415     inline bool operator<(const self &other) const
00416     {
00417         return (tree < other.tree);
00418     }
00419 
00421     inline bool operator>(const self &other) const
00422     {
00423         return (tree > other.tree);
00424     }
00425 
00427     inline bool operator<=(const self &other) const
00428     {
00429         return (tree <= other.tree);
00430     }
00431 
00433     inline bool operator>=(const self &other) const
00434     {
00435         return (tree >= other.tree);
00436     }
00437 
00438 public:
00440 
00442     inline self& operator= (const self &other)
00443     {
00444         if (this != &other)
00445         {
00446             tree = other.tree;
00447         }
00448         return *this;
00449     }
00450 
00453     inline btree_map(const self &other)
00454         : tree(other.tree)
00455     {
00456     }
00457 
00458 public:
00459     // *** Public Insertion Functions
00460 
00463     inline std::pair<iterator, bool> insert(const value_type& x)
00464     {
00465         return tree.insert2(x.first, x.second);
00466     }
00467 
00471     inline std::pair<iterator, bool> insert(const key_type& key, const data_type& data)
00472     {
00473         return tree.insert2(key, data);
00474     }
00475 
00480     inline std::pair<iterator, bool> insert2(const key_type& key, const data_type& data)
00481     {
00482         return tree.insert2(key, data);
00483     }
00484 
00487     inline iterator insert(iterator hint, const value_type &x)
00488     {
00489         return tree.insert2(hint, x.first, x.second);
00490     }
00491 
00494     inline iterator insert2(iterator hint, const key_type& key, const data_type& data)
00495     {
00496         return tree.insert2(hint, key, data);
00497     }
00498 
00502     inline data_type& operator[](const key_type& key)
00503     {
00504         iterator i = insert( value_type(key, data_type()) ).first;
00505         return i.data();
00506     }
00507 
00510     template <typename InputIterator>
00511     inline void insert(InputIterator first, InputIterator last)
00512     {
00513         return tree.insert(first, last);
00514     }
00515 
00516 public:
00517     // *** Public Erase Functions
00518 
00521     bool erase_one(const key_type &key)
00522     {
00523         return tree.erase_one(key);
00524     }
00525 
00528     size_type erase(const key_type &key)
00529     {
00530         return tree.erase(key);
00531     }
00532 
00534     void erase(iterator iter)
00535     {
00536         return tree.erase(iter);
00537     }
00538 
00539 #ifdef BTREE_TODO
00540 
00541 
00542     void erase(iterator /* first */, iterator /* last */)
00543     {
00544         abort();
00545     }
00546 #endif
00547 
00548 #ifdef BTREE_DEBUG
00549 public:
00550     // *** Debug Printing
00551 
00555     void print(std::ostream &os) const
00556     {
00557         tree.print(os);
00558     }
00559 
00561     void print_leaves(std::ostream &os) const
00562     {
00563         tree.print_leaves(os);
00564     }
00565 #endif
00566 
00567 public:
00568     // *** Verification of B+ Tree Invariants
00569 
00572     void verify() const
00573     {
00574         tree.verify();
00575     }
00576 
00577 public:
00578 
00583     void dump(std::ostream &os) const
00584     {
00585         tree.dump(os);
00586     }
00587 
00592     bool restore(std::istream &is)
00593     {
00594         return tree.restore(is);
00595     }
00596 };
00597 
00598 } // namespace stx
00599 
00600 #endif // _STX_BTREE_MAP_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines