STX B+ Tree Template Classes 0.8.6
|
00001 // $Id: btree_multimap.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_MULTIMAP_H_ 00026 #define _STX_BTREE_MULTIMAP_H_ 00027 00028 #include <stx/btree.h> 00029 00030 namespace stx { 00031 00047 template <typename _Key, typename _Data, 00048 typename _Compare = std::less<_Key>, 00049 typename _Traits = btree_default_map_traits<_Key, _Data>, 00050 typename _Alloc = std::allocator<std::pair<_Key, _Data> > > 00051 class btree_multimap 00052 { 00053 public: 00054 // *** Template Parameter Types 00055 00058 typedef _Key key_type; 00059 00062 typedef _Data data_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 public: 00080 // *** Constructed Types 00081 00083 typedef btree_multimap<key_type, data_type, key_compare, traits, allocator_type> self; 00084 00087 typedef std::pair<key_type, data_type> value_type; 00088 00090 typedef stx::btree<key_type, data_type, value_type, key_compare, traits, true, allocator_type> btree_impl; 00091 00093 typedef typename btree_impl::value_compare value_compare; 00094 00096 typedef typename btree_impl::size_type size_type; 00097 00099 typedef typename btree_impl::tree_stats tree_stats; 00100 00101 public: 00102 // *** Static Constant Options and Values of the B+ Tree 00103 00105 static const unsigned short leafslotmax = btree_impl::leafslotmax; 00106 00109 static const unsigned short innerslotmax = btree_impl::innerslotmax; 00110 00114 static const unsigned short minleafslots = btree_impl::minleafslots; 00115 00119 static const unsigned short mininnerslots = btree_impl::mininnerslots; 00120 00123 static const bool selfverify = btree_impl::selfverify; 00124 00128 static const bool debug = btree_impl::debug; 00129 00131 static const bool allow_duplicates = btree_impl::allow_duplicates; 00132 00133 public: 00134 // *** Iterators and Reverse Iterators 00135 00138 typedef typename btree_impl::iterator iterator; 00139 00142 typedef typename btree_impl::const_iterator const_iterator; 00143 00145 typedef typename btree_impl::reverse_iterator reverse_iterator; 00146 00148 typedef typename btree_impl::const_reverse_iterator const_reverse_iterator; 00149 00150 private: 00151 // *** Tree Implementation Object 00152 00154 btree_impl tree; 00155 00156 public: 00157 // *** Constructors and Destructor 00158 00161 explicit inline btree_multimap(const allocator_type &alloc = allocator_type()) 00162 : tree(alloc) 00163 { 00164 } 00165 00168 explicit inline btree_multimap(const key_compare &kcf, 00169 const allocator_type &alloc = allocator_type()) 00170 : tree(kcf, alloc) 00171 { 00172 } 00173 00175 template <class InputIterator> 00176 inline btree_multimap(InputIterator first, InputIterator last, 00177 const allocator_type &alloc = allocator_type()) 00178 : tree(first, last, alloc) 00179 { 00180 } 00181 00184 template <class InputIterator> 00185 inline btree_multimap(InputIterator first, InputIterator last, const key_compare &kcf, 00186 const allocator_type &alloc = allocator_type()) 00187 : tree(first, last, kcf, alloc) 00188 { 00189 } 00190 00192 inline ~btree_multimap() 00193 { 00194 } 00195 00197 void swap(self& from) 00198 { 00199 std::swap(tree, from.tree); 00200 } 00201 00202 public: 00203 // *** Key and Value Comparison Function Objects 00204 00206 inline key_compare key_comp() const 00207 { 00208 return tree.key_comp(); 00209 } 00210 00213 inline value_compare value_comp() const 00214 { 00215 return tree.value_comp(); 00216 } 00217 00218 public: 00219 // *** Allocators 00220 00222 allocator_type get_allocator() const 00223 { 00224 return tree.get_allocator(); 00225 } 00226 00227 public: 00228 // *** Fast Destruction of the B+ Tree 00229 00231 void clear() 00232 { 00233 tree.clear(); 00234 } 00235 00236 public: 00237 // *** STL Iterator Construction Functions 00238 00241 inline iterator begin() 00242 { 00243 return tree.begin(); 00244 } 00245 00248 inline iterator end() 00249 { 00250 return tree.end(); 00251 } 00252 00255 inline const_iterator begin() const 00256 { 00257 return tree.begin(); 00258 } 00259 00262 inline const_iterator end() const 00263 { 00264 return tree.end(); 00265 } 00266 00269 inline reverse_iterator rbegin() 00270 { 00271 return tree.rbegin(); 00272 } 00273 00276 inline reverse_iterator rend() 00277 { 00278 return tree.rend(); 00279 } 00280 00283 inline const_reverse_iterator rbegin() const 00284 { 00285 return tree.rbegin(); 00286 } 00287 00290 inline const_reverse_iterator rend() const 00291 { 00292 return tree.rend(); 00293 } 00294 00295 public: 00296 // *** Access Functions to the Item Count 00297 00299 inline size_type size() const 00300 { 00301 return tree.size(); 00302 } 00303 00305 inline bool empty() const 00306 { 00307 return tree.empty(); 00308 } 00309 00312 inline size_type max_size() const 00313 { 00314 return tree.max_size(); 00315 } 00316 00318 inline const tree_stats& get_stats() const 00319 { 00320 return tree.get_stats(); 00321 } 00322 00323 public: 00324 // *** Standard Access Functions Querying the Tree by Descending to a Leaf 00325 00328 bool exists(const key_type &key) const 00329 { 00330 return tree.exists(key); 00331 } 00332 00335 iterator find(const key_type &key) 00336 { 00337 return tree.find(key); 00338 } 00339 00342 const_iterator find(const key_type &key) const 00343 { 00344 return tree.find(key); 00345 } 00346 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_multimap(const self &other) 00454 : tree(other.tree) 00455 { 00456 } 00457 00458 public: 00459 // *** Public Insertion Functions 00460 00463 inline iterator insert(const value_type& x) 00464 { 00465 return tree.insert2(x.first, x.second).first; 00466 } 00467 00471 inline iterator insert(const key_type& key, const data_type& data) 00472 { 00473 return tree.insert2(key, data).first; 00474 } 00475 00480 inline iterator insert2(const key_type& key, const data_type& data) 00481 { 00482 return tree.insert2(key, data).first; 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 00501 template <typename InputIterator> 00502 inline void insert(InputIterator first, InputIterator last) 00503 { 00504 return tree.insert(first, last); 00505 } 00506 00507 public: 00508 // *** Public Erase Functions 00509 00512 bool erase_one(const key_type &key) 00513 { 00514 return tree.erase_one(key); 00515 } 00516 00519 size_type erase(const key_type &key) 00520 { 00521 return tree.erase(key); 00522 } 00523 00525 void erase(iterator iter) 00526 { 00527 return tree.erase(iter); 00528 } 00529 00530 #ifdef BTREE_TODO 00531 00532 00533 void erase(iterator /* first */, iterator /* last */) 00534 { 00535 abort(); 00536 } 00537 #endif 00538 00539 #ifdef BTREE_DEBUG 00540 public: 00541 // *** Debug Printing 00542 00546 void print(std::ostream &os) const 00547 { 00548 tree.print(os); 00549 } 00550 00552 void print_leaves(std::ostream &os) const 00553 { 00554 tree.print_leaves(os); 00555 } 00556 #endif 00557 00558 public: 00559 // *** Verification of B+ Tree Invariants 00560 00563 void verify() const 00564 { 00565 tree.verify(); 00566 } 00567 00568 public: 00569 00574 void dump(std::ostream &os) const 00575 { 00576 tree.dump(os); 00577 } 00578 00583 bool restore(std::istream &is) 00584 { 00585 return tree.restore(is); 00586 } 00587 }; 00588 00589 } // namespace stx 00590 00591 #endif // _STX_BTREE_MULTIMAP_H_