STX B+ Tree Template Classes 0.8.6

stx/btree_multiset.h

Go to the documentation of this file.
00001 // $Id: btree_multiset.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_MULTISET_H_
00026 #define _STX_BTREE_MULTISET_H_
00027 
00028 #include <stx/btree.h>
00029 
00030 namespace stx {
00031 
00051 template <typename _Key,
00052           typename _Compare = std::less<_Key>,
00053           typename _Traits = btree_default_set_traits<_Key>,
00054           typename _Alloc = std::allocator<_Key> >
00055 class btree_multiset
00056 {
00057 public:
00058     // *** Template Parameter Types
00059 
00062     typedef _Key                        key_type;
00063 
00065     typedef _Compare                    key_compare;
00066 
00069     typedef _Traits                     traits;
00070 
00072     typedef _Alloc                      allocator_type;
00073 
00074     // The macro BTREE_FRIENDS can be used by outside class to access the B+
00075     // tree internals. This was added for wxBTreeDemo to be able to draw the
00076     // tree.
00077     BTREE_FRIENDS
00078 
00079 private:
00080     // *** The Data_Type
00081 
00083     struct empty_struct
00084     {
00085     };
00086 
00087 public:
00088     // *** Constructed Types
00089 
00091     typedef struct empty_struct         data_type;
00092 
00094     typedef key_type                    value_type;
00095 
00097     typedef btree_multiset<key_type, key_compare, traits, allocator_type> self;
00098 
00100     typedef stx::btree<key_type, data_type, value_type, key_compare, traits, true, allocator_type> btree_impl;
00101 
00103     typedef typename btree_impl::value_compare  value_compare;
00104 
00106     typedef typename btree_impl::size_type      size_type;
00107 
00109     typedef typename btree_impl::tree_stats     tree_stats;
00110 
00111 public:
00112     // *** Static Constant Options and Values of the B+ Tree
00113 
00115     static const unsigned short         leafslotmax =  btree_impl::leafslotmax;
00116 
00119     static const unsigned short         innerslotmax =  btree_impl::innerslotmax;
00120 
00124     static const unsigned short         minleafslots = btree_impl::minleafslots;
00125 
00129     static const unsigned short         mininnerslots = btree_impl::mininnerslots;
00130 
00133     static const bool                   selfverify = btree_impl::selfverify;
00134 
00138     static const bool                   debug = btree_impl::debug;
00139 
00141     static const bool                   allow_duplicates = btree_impl::allow_duplicates;
00142 
00143 public:
00144     // *** Iterators and Reverse Iterators
00145 
00148     typedef typename btree_impl::iterator               iterator;
00149 
00152     typedef typename btree_impl::const_iterator         const_iterator;
00153 
00155     typedef typename btree_impl::reverse_iterator       reverse_iterator;
00156 
00158     typedef typename btree_impl::const_reverse_iterator const_reverse_iterator;
00159 
00160 private:
00161     // *** Tree Implementation Object
00162 
00164     btree_impl  tree;
00165 
00166 public:
00167     // *** Constructors and Destructor
00168 
00171     explicit inline btree_multiset(const allocator_type &alloc = allocator_type())
00172         : tree(alloc)
00173     {
00174     }
00175 
00178     explicit inline btree_multiset(const key_compare &kcf,
00179                                    const allocator_type &alloc = allocator_type())
00180         : tree(kcf, alloc)
00181     {
00182     }
00183 
00185     template <class InputIterator>
00186     inline btree_multiset(InputIterator first, InputIterator last,
00187                           const allocator_type &alloc = allocator_type())
00188         : tree(alloc)
00189     {
00190         insert(first, last);
00191     }
00192 
00195     template <class InputIterator>
00196     inline btree_multiset(InputIterator first, InputIterator last, const key_compare &kcf,
00197                           const allocator_type &alloc = allocator_type())
00198         : tree(kcf, alloc)
00199     {
00200         insert(first, last);
00201     }
00202 
00204     inline ~btree_multiset()
00205     {
00206     }
00207 
00209     void swap(self& from)
00210     {
00211         std::swap(tree, from.tree);
00212     }
00213 
00214 public:
00215     // *** Key and Value Comparison Function Objects
00216 
00218     inline key_compare key_comp() const
00219     {
00220         return tree.key_comp();
00221     }
00222 
00225     inline value_compare value_comp() const
00226     {
00227         return tree.value_comp();
00228     }
00229 
00230 public:
00231     // *** Allocators
00232 
00234     allocator_type get_allocator() const
00235     {
00236         return tree.get_allocator();
00237     }
00238 
00239 public:
00240     // *** Fast Destruction of the B+ Tree
00241 
00243     void clear()
00244     {
00245         tree.clear();
00246     }
00247 
00248 public:
00249     // *** STL Iterator Construction Functions
00250 
00253     inline iterator begin()
00254     {
00255         return tree.begin();
00256     }
00257 
00260     inline iterator end()
00261     {
00262         return tree.end();
00263     }
00264 
00267     inline const_iterator begin() const
00268     {
00269         return tree.begin();
00270     }
00271 
00274     inline const_iterator end() const
00275     {
00276         return tree.end();
00277     }
00278 
00281     inline reverse_iterator rbegin()
00282     {
00283         return tree.rbegin();
00284     }
00285 
00288     inline reverse_iterator rend()
00289     {
00290         return tree.rend();
00291     }
00292 
00295     inline const_reverse_iterator rbegin() const
00296     {
00297         return tree.rbegin();
00298     }
00299 
00302     inline const_reverse_iterator rend() const
00303     {
00304         return tree.rend();
00305     }
00306 
00307 public:
00308     // *** Access Functions to the Item Count
00309 
00311     inline size_type size() const
00312     {
00313         return tree.size();
00314     }
00315 
00317     inline bool empty() const
00318     {
00319         return tree.empty();
00320     }
00321 
00324     inline size_type max_size() const
00325     {
00326         return tree.max_size();
00327     }
00328 
00330     inline const tree_stats& get_stats() const
00331     {
00332         return tree.get_stats();
00333     }
00334 
00335 public:
00336     // *** Standard Access Functions Querying the Tree by Descending to a Leaf
00337 
00340     bool exists(const key_type &key) const
00341     {
00342         return tree.exists(key);
00343     }
00344 
00347     iterator find(const key_type &key)
00348     {
00349         return tree.find(key);
00350     }
00351 
00354     const_iterator find(const key_type &key) const
00355     {
00356         return tree.find(key);
00357     }
00358 
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_multiset(const self &other)
00465         : tree(other.tree)
00466     {
00467     }
00468 
00469 public:
00470     // *** Public Insertion Functions
00471 
00474     inline iterator insert(const key_type& x)
00475     {
00476         return tree.insert2(x, data_type()).first;
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 
00503     bool erase_one(const key_type &key)
00504     {
00505         return tree.erase_one(key);
00506     }
00507 
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 #endif
00548 
00549 public:
00550     // *** Verification of B+ Tree Invariants
00551 
00554     void verify() const
00555     {
00556         tree.verify();
00557     }
00558 
00559 public:
00560 
00565     void dump(std::ostream &os) const
00566     {
00567         tree.dump(os);
00568     }
00569 
00574     bool restore(std::istream &is)
00575     {
00576         return tree.restore(is);
00577     }
00578 };
00579 
00580 } // namespace stx
00581 
00582 #endif // _STX_BTREE_MULTISET_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines