STX B+ Tree Template Classes 0.8.6

stx/btree_set.h

Go to the documentation of this file.
00001 // $Id: btree_set.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_SET_H_
00026 #define _STX_BTREE_SET_H_
00027 
00028 #include <stx/btree.h>
00029 
00030 namespace stx {
00031 
00050 template <typename _Key,
00051           typename _Compare = std::less<_Key>,
00052           typename _Traits = btree_default_set_traits<_Key>,
00053           typename _Alloc = std::allocator<_Key> >
00054 class btree_set
00055 {
00056 public:
00057     // *** Template Parameter Types
00058 
00061     typedef _Key                        key_type;
00062 
00064     typedef _Compare                    key_compare;
00065 
00068     typedef _Traits                     traits;
00069 
00071     typedef _Alloc                      allocator_type;
00072 
00076     BTREE_FRIENDS
00077 
00078 private:
00079     // *** The data_type
00080 
00082     struct empty_struct
00083     {
00084     };
00085 
00086 public:
00087     // *** Constructed Types
00088 
00090     typedef struct empty_struct         data_type;
00091 
00093     typedef key_type                    value_type;
00094 
00096     typedef btree_set<key_type, key_compare, traits, allocator_type> self;
00097 
00099     typedef stx::btree<key_type, data_type, value_type, key_compare, traits, false, allocator_type> btree_impl;
00100 
00102     typedef typename btree_impl::value_compare  value_compare;
00103 
00105     typedef typename btree_impl::size_type      size_type;
00106 
00108     typedef typename btree_impl::tree_stats     tree_stats;
00109 
00110 public:
00111     // *** Static Constant Options and Values of the B+ Tree
00112 
00114     static const unsigned short         leafslotmax =  btree_impl::leafslotmax;
00115 
00118     static const unsigned short         innerslotmax =  btree_impl::innerslotmax;
00119 
00123     static const unsigned short minleafslots = btree_impl::minleafslots;
00124 
00128     static const unsigned short mininnerslots = btree_impl::mininnerslots;
00129 
00132     static const bool                   selfverify = btree_impl::selfverify;
00133 
00137     static const bool                   debug = btree_impl::debug;
00138 
00140     static const bool                   allow_duplicates = btree_impl::allow_duplicates;
00141 
00142 public:
00143     // *** Iterators and Reverse Iterators
00144 
00147     typedef typename btree_impl::iterator               iterator;
00148 
00151     typedef typename btree_impl::const_iterator         const_iterator;
00152 
00154     typedef typename btree_impl::reverse_iterator       reverse_iterator;
00155 
00157     typedef typename btree_impl::const_reverse_iterator const_reverse_iterator;
00158 
00159 private:
00160     // *** Tree Implementation Object
00161 
00163     btree_impl  tree;
00164 
00165 public:
00166     // *** Constructors and Destructor
00167 
00170     explicit inline btree_set(const allocator_type &alloc = allocator_type())
00171         : tree(alloc)
00172     {
00173     }
00174 
00177     explicit inline btree_set(const key_compare &kcf,
00178                               const allocator_type &alloc = allocator_type())
00179         : tree(kcf, alloc)
00180     {
00181     }
00182 
00184     template <class InputIterator>
00185     inline btree_set(InputIterator first, InputIterator last,
00186                      const allocator_type &alloc = allocator_type())
00187         : tree(alloc)
00188     {
00189         insert(first, last);
00190     }
00191 
00194     template <class InputIterator>
00195     inline btree_set(InputIterator first, InputIterator last, const key_compare &kcf,
00196                      const allocator_type &alloc = allocator_type())
00197         : tree(kcf, alloc)
00198     {
00199         insert(first, last);
00200     }
00201 
00203     inline ~btree_set()
00204     {
00205     }
00206 
00208     void swap(self& from)
00209     {
00210         std::swap(tree, from.tree);
00211     }
00212 
00213 public:
00214     // *** Key and Value Comparison Function Objects
00215 
00217     inline key_compare key_comp() const
00218     {
00219         return tree.key_comp();
00220     }
00221 
00224     inline value_compare value_comp() const
00225     {
00226         return tree.value_comp();
00227     }
00228 
00229 public:
00230     // *** Allocators
00231 
00233     allocator_type get_allocator() const
00234     {
00235         return tree.get_allocator();
00236     }
00237 
00238 public:
00239     // *** Fast Destruction of the B+ Tree
00240 
00242     void clear()
00243     {
00244         tree.clear();
00245     }
00246 
00247 public:
00248     // *** STL Iterator Construction Functions
00249 
00252     inline iterator begin()
00253     {
00254         return tree.begin();
00255     }
00256 
00259     inline iterator end()
00260     {
00261         return tree.end();
00262     }
00263 
00266     inline const_iterator begin() const
00267     {
00268         return tree.begin();
00269     }
00270 
00273     inline const_iterator end() const
00274     {
00275         return tree.end();
00276     }
00277 
00280     inline reverse_iterator rbegin()
00281     {
00282         return tree.rbegin();
00283     }
00284 
00287     inline reverse_iterator rend()
00288     {
00289         return tree.rend();
00290     }
00291 
00294     inline const_reverse_iterator rbegin() const
00295     {
00296         return tree.rbegin();
00297     }
00298 
00301     inline const_reverse_iterator rend() const
00302     {
00303         return tree.rend();
00304     }
00305 
00306 public:
00307     // *** Access Functions to the Item Count
00308 
00310     inline size_type size() const
00311     {
00312         return tree.size();
00313     }
00314 
00316     inline bool empty() const
00317     {
00318         return tree.empty();
00319     }
00320 
00323     inline size_type max_size() const
00324     {
00325         return tree.max_size();
00326     }
00327 
00329     inline const tree_stats& get_stats() const
00330     {
00331         return tree.get_stats();
00332     }
00333 
00334 public:
00335     // *** Standard Access Functions Querying the Tree by Descending to a Leaf
00336 
00339     bool exists(const key_type &key) const
00340     {
00341         return tree.exists(key);
00342     }
00343 
00346     iterator find(const key_type &key)
00347     {
00348         return tree.find(key);
00349     }
00350 
00353     const_iterator find(const key_type &key) const
00354     {
00355         return tree.find(key);
00356     }
00357 
00361     size_type count(const key_type &key) const
00362     {
00363         return tree.count(key);
00364     }
00365 
00368     iterator lower_bound(const key_type& key)
00369     {
00370         return tree.lower_bound(key);
00371     }
00372 
00376     const_iterator lower_bound(const key_type& key) const
00377     {
00378         return tree.lower_bound(key);
00379     }
00380 
00383     iterator upper_bound(const key_type& key)
00384     {
00385         return tree.upper_bound(key);
00386     }
00387 
00391     const_iterator upper_bound(const key_type& key) const
00392     {
00393         return tree.upper_bound(key);
00394     }
00395 
00397     inline std::pair<iterator, iterator> equal_range(const key_type& key)
00398     {
00399         return tree.equal_range(key);
00400     }
00401 
00403     inline std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00404     {
00405         return tree.equal_range(key);
00406     }
00407 
00408 public:
00409     // *** B+ Tree Object Comparison Functions
00410 
00413     inline bool operator==(const self &other) const
00414     {
00415         return (tree == other.tree);
00416     }
00417 
00419     inline bool operator!=(const self &other) const
00420     {
00421         return (tree != other.tree);
00422     }
00423 
00426     inline bool operator<(const self &other) const
00427     {
00428         return (tree < other.tree);
00429     }
00430 
00432     inline bool operator>(const self &other) const
00433     {
00434         return (tree > other.tree);
00435     }
00436 
00438     inline bool operator<=(const self &other) const
00439     {
00440         return (tree <= other.tree);
00441     }
00442 
00444     inline bool operator>=(const self &other) const
00445     {
00446         return (tree >= other.tree);
00447     }
00448 
00449 public:
00451 
00453     inline self& operator= (const self &other)
00454     {
00455         if (this != &other)
00456         {
00457             tree = other.tree;
00458         }
00459         return *this;
00460     }
00461 
00464     inline btree_set(const self &other)
00465         : tree(other.tree)
00466     {
00467     }
00468 
00469 public:
00470     // *** Public Insertion Functions
00471 
00474     inline std::pair<iterator, bool> insert(const key_type& x)
00475     {
00476         return tree.insert2(x, data_type());
00477     }
00478 
00481     inline iterator insert(iterator hint, const key_type &x)
00482     {
00483         return tree.insert2(hint, x, data_type());
00484     }
00485 
00488     template <typename InputIterator>
00489     inline void insert(InputIterator first, InputIterator last)
00490     {
00491         InputIterator iter = first;
00492         while(iter != last)
00493         {
00494             insert(*iter);
00495             ++iter;
00496         }
00497     }
00498 
00499 public:
00500     // *** Public Erase Functions
00501 
00504     bool erase_one(const key_type &key)
00505     {
00506         return tree.erase_one(key);
00507     }
00508 
00510     size_type erase(const key_type &key)
00511     {
00512         return tree.erase(key);
00513     }
00514 
00516     void erase(iterator iter)
00517     {
00518         return tree.erase(iter);
00519     }
00520 
00521 #ifdef BTREE_TODO
00522 
00523 
00524     void erase(iterator /* first */, iterator /* last */)
00525     {
00526         abort();
00527     }
00528 #endif
00529 
00530 #ifdef BTREE_DEBUG
00531 public:
00532     // *** Debug Printing
00533 
00537     void print(std::ostream &os) const
00538     {
00539         tree.print(os);
00540     }
00541 
00543     void print_leaves(std::ostream &os) const
00544     {
00545         tree.print_leaves(os);
00546     }
00547 
00548 #endif
00549 
00550 public:
00551     // *** Verification of B+ Tree Invariants
00552 
00555     void verify() const
00556     {
00557         tree.verify();
00558     }
00559 
00560 public:
00561 
00566     void dump(std::ostream &os) const
00567     {
00568         tree.dump(os);
00569     }
00570 
00575     bool restore(std::istream &is)
00576     {
00577         return tree.restore(is);
00578     }
00579 };
00580 
00581 } // namespace stx
00582 
00583 #endif // _STX_BTREE_SET_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines