1 // Multimap implementation -*- C++ -*- 2 3 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 2, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // You should have received a copy of the GNU General Public License along 17 // with this library; see the file COPYING. If not, write to the Free 18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 19 // USA. 20 21 // As a special exception, you may use this file as part of a free software 22 // library without restriction. Specifically, if other files instantiate 23 // templates or use macros or inline functions from this file, or you compile 24 // this file and link it with other files to produce an executable, this 25 // file does not by itself cause the resulting executable to be covered by 26 // the GNU General Public License. This exception does not however 27 // invalidate any other reasons why the executable file might be covered by 28 // the GNU General Public License. 29 30 /* 31 * 32 * Copyright (c) 1994 33 * Hewlett-Packard Company 34 * 35 * Permission to use, copy, modify, distribute and sell this software 36 * and its documentation for any purpose is hereby granted without fee, 37 * provided that the above copyright notice appear in all copies and 38 * that both that copyright notice and this permission notice appear 39 * in supporting documentation. Hewlett-Packard Company makes no 40 * representations about the suitability of this software for any 41 * purpose. It is provided "as is" without express or implied warranty. 42 * 43 * 44 * Copyright (c) 1996,1997 45 * Silicon Graphics Computer Systems, Inc. 46 * 47 * Permission to use, copy, modify, distribute and sell this software 48 * and its documentation for any purpose is hereby granted without fee, 49 * provided that the above copyright notice appear in all copies and 50 * that both that copyright notice and this permission notice appear 51 * in supporting documentation. Silicon Graphics makes no 52 * representations about the suitability of this software for any 53 * purpose. It is provided "as is" without express or implied warranty. 54 */ 55 56 /** @file stl_multimap.h 57 * This is an internal header file, included by other library headers. 58 * You should not attempt to use it directly. 59 */ 60 61 #ifndef _MULTIMAP_H 62 #define _MULTIMAP_H 1 63 64 #include <bits/concept_check.h> 65 66 namespace _GLIBCXX_STD 67 { 68 // Forward declaration of operators < and ==, needed for friend declaration. 69 70 template <typename _Key, typename _Tp, 71 typename _Compare = less<_Key>, 72 typename _Alloc = allocator<pair<const _Key, _Tp> > > 73 class multimap; 74 75 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 76 inline bool 77 operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 78 const multimap<_Key,_Tp,_Compare,_Alloc>& __y); 79 80 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 81 inline bool 82 operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 83 const multimap<_Key,_Tp,_Compare,_Alloc>& __y); 84 85 /** 86 * @brief A standard container made up of (key,value) pairs, which can be 87 * retrieved based on a key, in logarithmic time. 88 * 89 * @ingroup Containers 90 * @ingroup Assoc_containers 91 * 92 * Meets the requirements of a <a href="tables.html#65">container</a>, a 93 * <a href="tables.html#66">reversible container</a>, and an 94 * <a href="tables.html#69">associative container</a> (using equivalent 95 * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type 96 * is T, and the value_type is std::pair<const Key,T>. 97 * 98 * Multimaps support bidirectional iterators. 99 * 100 * @if maint 101 * The private tree data is declared exactly the same way for map and 102 * multimap; the distinction is made entirely in how the tree functions are 103 * called (*_unique versus *_equal, same as the standard). 104 * @endif 105 */ 106 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 107 class multimap 108 { 109 // concept requirements 110 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 111 __glibcxx_class_requires4(_Compare, bool, _Key, _Key, 112 _BinaryFunctionConcept) 113 114 public: 115 typedef _Key key_type; 116 typedef _Tp mapped_type; 117 typedef pair<const _Key, _Tp> value_type; 118 typedef _Compare key_compare; 119 120 class value_compare 121 : public binary_function<value_type, value_type, bool> 122 { 123 friend class multimap<_Key,_Tp,_Compare,_Alloc>; 124 protected: 125 _Compare comp; 126 value_compare(_Compare __c)127 value_compare(_Compare __c) 128 : comp(__c) { } 129 130 public: operator()131 bool operator()(const value_type& __x, const value_type& __y) const 132 { return comp(__x.first, __y.first); } 133 }; 134 135 private: 136 /// @if maint This turns a red-black tree into a [multi]map. @endif 137 typedef _Rb_tree<key_type, value_type, 138 _Select1st<value_type>, key_compare, _Alloc> _Rep_type; 139 /// @if maint The actual tree structure. @endif 140 _Rep_type _M_t; 141 142 public: 143 // many of these are specified differently in ISO, but the following are 144 // "functionally equivalent" 145 typedef typename _Alloc::pointer pointer; 146 typedef typename _Alloc::const_pointer const_pointer; 147 typedef typename _Alloc::reference reference; 148 typedef typename _Alloc::const_reference const_reference; 149 typedef typename _Rep_type::allocator_type allocator_type; 150 typedef typename _Rep_type::iterator iterator; 151 typedef typename _Rep_type::const_iterator const_iterator; 152 typedef typename _Rep_type::size_type size_type; 153 typedef typename _Rep_type::difference_type difference_type; 154 typedef typename _Rep_type::reverse_iterator reverse_iterator; 155 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 156 157 // [23.3.2] construct/copy/destroy 158 // (get_allocator() is also listed in this section) 159 /** 160 * @brief Default constructor creates no elements. 161 */ multimap()162 multimap() 163 : _M_t(_Compare(), allocator_type()) { } 164 165 // for some reason this was made a separate function 166 /** 167 * @brief Default constructor creates no elements. 168 */ 169 explicit 170 multimap(const _Compare& __comp, 171 const allocator_type& __a = allocator_type()) _M_t(__comp,__a)172 : _M_t(__comp, __a) { } 173 174 /** 175 * @brief %Multimap copy constructor. 176 * @param x A %multimap of identical element and allocator types. 177 * 178 * The newly-created %multimap uses a copy of the allocation object used 179 * by @a x. 180 */ multimap(const multimap & __x)181 multimap(const multimap& __x) 182 : _M_t(__x._M_t) { } 183 184 /** 185 * @brief Builds a %multimap from a range. 186 * @param first An input iterator. 187 * @param last An input iterator. 188 * 189 * Create a %multimap consisting of copies of the elements from 190 * [first,last). This is linear in N if the range is already sorted, 191 * and NlogN otherwise (where N is distance(first,last)). 192 */ 193 template <typename _InputIterator> multimap(_InputIterator __first,_InputIterator __last)194 multimap(_InputIterator __first, _InputIterator __last) 195 : _M_t(_Compare(), allocator_type()) 196 { _M_t.insert_equal(__first, __last); } 197 198 /** 199 * @brief Builds a %multimap from a range. 200 * @param first An input iterator. 201 * @param last An input iterator. 202 * @param comp A comparison functor. 203 * @param a An allocator object. 204 * 205 * Create a %multimap consisting of copies of the elements from 206 * [first,last). This is linear in N if the range is already sorted, 207 * and NlogN otherwise (where N is distance(first,last)). 208 */ 209 template <typename _InputIterator> 210 multimap(_InputIterator __first, _InputIterator __last, 211 const _Compare& __comp, 212 const allocator_type& __a = allocator_type()) _M_t(__comp,__a)213 : _M_t(__comp, __a) 214 { _M_t.insert_equal(__first, __last); } 215 216 // FIXME There is no dtor declared, but we should have something generated 217 // by Doxygen. I don't know what tags to add to this paragraph to make 218 // that happen: 219 /** 220 * The dtor only erases the elements, and note that if the elements 221 * themselves are pointers, the pointed-to memory is not touched in any 222 * way. Managing the pointer is the user's responsibilty. 223 */ 224 225 /** 226 * @brief %Multimap assignment operator. 227 * @param x A %multimap of identical element and allocator types. 228 * 229 * All the elements of @a x are copied, but unlike the copy constructor, 230 * the allocator object is not copied. 231 */ 232 multimap& 233 operator=(const multimap& __x) 234 { 235 _M_t = __x._M_t; 236 return *this; 237 } 238 239 /// Get a copy of the memory allocation object. 240 allocator_type get_allocator()241 get_allocator() const 242 { return _M_t.get_allocator(); } 243 244 // iterators 245 /** 246 * Returns a read/write iterator that points to the first pair in the 247 * %multimap. Iteration is done in ascending order according to the 248 * keys. 249 */ 250 iterator begin()251 begin() 252 { return _M_t.begin(); } 253 254 /** 255 * Returns a read-only (constant) iterator that points to the first pair 256 * in the %multimap. Iteration is done in ascending order according to 257 * the keys. 258 */ 259 const_iterator begin()260 begin() const 261 { return _M_t.begin(); } 262 263 /** 264 * Returns a read/write iterator that points one past the last pair in 265 * the %multimap. Iteration is done in ascending order according to the 266 * keys. 267 */ 268 iterator end()269 end() 270 { return _M_t.end(); } 271 272 /** 273 * Returns a read-only (constant) iterator that points one past the last 274 * pair in the %multimap. Iteration is done in ascending order according 275 * to the keys. 276 */ 277 const_iterator end()278 end() const 279 { return _M_t.end(); } 280 281 /** 282 * Returns a read/write reverse iterator that points to the last pair in 283 * the %multimap. Iteration is done in descending order according to the 284 * keys. 285 */ 286 reverse_iterator rbegin()287 rbegin() 288 { return _M_t.rbegin(); } 289 290 /** 291 * Returns a read-only (constant) reverse iterator that points to the 292 * last pair in the %multimap. Iteration is done in descending order 293 * according to the keys. 294 */ 295 const_reverse_iterator rbegin()296 rbegin() const 297 { return _M_t.rbegin(); } 298 299 /** 300 * Returns a read/write reverse iterator that points to one before the 301 * first pair in the %multimap. Iteration is done in descending order 302 * according to the keys. 303 */ 304 reverse_iterator rend()305 rend() 306 { return _M_t.rend(); } 307 308 /** 309 * Returns a read-only (constant) reverse iterator that points to one 310 * before the first pair in the %multimap. Iteration is done in 311 * descending order according to the keys. 312 */ 313 const_reverse_iterator rend()314 rend() const 315 { return _M_t.rend(); } 316 317 // capacity 318 /** Returns true if the %multimap is empty. */ 319 bool empty()320 empty() const 321 { return _M_t.empty(); } 322 323 /** Returns the size of the %multimap. */ 324 size_type size()325 size() const 326 { return _M_t.size(); } 327 328 /** Returns the maximum size of the %multimap. */ 329 size_type max_size()330 max_size() const 331 { return _M_t.max_size(); } 332 333 // modifiers 334 /** 335 * @brief Inserts a std::pair into the %multimap. 336 * @param x Pair to be inserted (see std::make_pair for easy creation 337 * of pairs). 338 * @return An iterator that points to the inserted (key,value) pair. 339 * 340 * This function inserts a (key, value) pair into the %multimap. 341 * Contrary to a std::map the %multimap does not rely on unique keys and 342 * thus multiple pairs with the same key can be inserted. 343 * 344 * Insertion requires logarithmic time. 345 */ 346 iterator insert(const value_type & __x)347 insert(const value_type& __x) 348 { return _M_t.insert_equal(__x); } 349 350 /** 351 * @brief Inserts a std::pair into the %multimap. 352 * @param position An iterator that serves as a hint as to where the 353 * pair should be inserted. 354 * @param x Pair to be inserted (see std::make_pair for easy creation 355 * of pairs). 356 * @return An iterator that points to the inserted (key,value) pair. 357 * 358 * This function inserts a (key, value) pair into the %multimap. 359 * Contrary to a std::map the %multimap does not rely on unique keys and 360 * thus multiple pairs with the same key can be inserted. 361 * Note that the first parameter is only a hint and can potentially 362 * improve the performance of the insertion process. A bad hint would 363 * cause no gains in efficiency. 364 * 365 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4 366 * for more on "hinting". 367 * 368 * Insertion requires logarithmic time (if the hint is not taken). 369 */ 370 iterator insert(iterator __position,const value_type & __x)371 insert(iterator __position, const value_type& __x) 372 { return _M_t.insert_equal(__position, __x); } 373 374 /** 375 * @brief A template function that attemps to insert a range of elements. 376 * @param first Iterator pointing to the start of the range to be 377 * inserted. 378 * @param last Iterator pointing to the end of the range. 379 * 380 * Complexity similar to that of the range constructor. 381 */ 382 template <typename _InputIterator> 383 void insert(_InputIterator __first,_InputIterator __last)384 insert(_InputIterator __first, _InputIterator __last) 385 { _M_t.insert_equal(__first, __last); } 386 387 /** 388 * @brief Erases an element from a %multimap. 389 * @param position An iterator pointing to the element to be erased. 390 * 391 * This function erases an element, pointed to by the given iterator, 392 * from a %multimap. Note that this function only erases the element, 393 * and that if the element is itself a pointer, the pointed-to memory is 394 * not touched in any way. Managing the pointer is the user's 395 * responsibilty. 396 */ 397 void erase(iterator __position)398 erase(iterator __position) 399 { _M_t.erase(__position); } 400 401 /** 402 * @brief Erases elements according to the provided key. 403 * @param x Key of element to be erased. 404 * @return The number of elements erased. 405 * 406 * This function erases all elements located by the given key from a 407 * %multimap. 408 * Note that this function only erases the element, and that if 409 * the element is itself a pointer, the pointed-to memory is not touched 410 * in any way. Managing the pointer is the user's responsibilty. 411 */ 412 size_type erase(const key_type & __x)413 erase(const key_type& __x) 414 { return _M_t.erase(__x); } 415 416 /** 417 * @brief Erases a [first,last) range of elements from a %multimap. 418 * @param first Iterator pointing to the start of the range to be 419 * erased. 420 * @param last Iterator pointing to the end of the range to be erased. 421 * 422 * This function erases a sequence of elements from a %multimap. 423 * Note that this function only erases the elements, and that if 424 * the elements themselves are pointers, the pointed-to memory is not 425 * touched in any way. Managing the pointer is the user's responsibilty. 426 */ 427 void erase(iterator __first,iterator __last)428 erase(iterator __first, iterator __last) 429 { _M_t.erase(__first, __last); } 430 431 /** 432 * @brief Swaps data with another %multimap. 433 * @param x A %multimap of the same element and allocator types. 434 * 435 * This exchanges the elements between two multimaps in constant time. 436 * (It is only swapping a pointer, an integer, and an instance of 437 * the @c Compare type (which itself is often stateless and empty), so it 438 * should be quite fast.) 439 * Note that the global std::swap() function is specialized such that 440 * std::swap(m1,m2) will feed to this function. 441 */ 442 void swap(multimap & __x)443 swap(multimap& __x) 444 { _M_t.swap(__x._M_t); } 445 446 /** 447 * Erases all elements in a %multimap. Note that this function only 448 * erases the elements, and that if the elements themselves are pointers, 449 * the pointed-to memory is not touched in any way. Managing the pointer 450 * is the user's responsibilty. 451 */ 452 void clear()453 clear() 454 { _M_t.clear(); } 455 456 // observers 457 /** 458 * Returns the key comparison object out of which the %multimap 459 * was constructed. 460 */ 461 key_compare key_comp()462 key_comp() const 463 { return _M_t.key_comp(); } 464 465 /** 466 * Returns a value comparison object, built from the key comparison 467 * object out of which the %multimap was constructed. 468 */ 469 value_compare value_comp()470 value_comp() const 471 { return value_compare(_M_t.key_comp()); } 472 473 // multimap operations 474 /** 475 * @brief Tries to locate an element in a %multimap. 476 * @param x Key of (key, value) pair to be located. 477 * @return Iterator pointing to sought-after element, 478 * or end() if not found. 479 * 480 * This function takes a key and tries to locate the element with which 481 * the key matches. If successful the function returns an iterator 482 * pointing to the sought after %pair. If unsuccessful it returns the 483 * past-the-end ( @c end() ) iterator. 484 */ 485 iterator find(const key_type & __x)486 find(const key_type& __x) 487 { return _M_t.find(__x); } 488 489 /** 490 * @brief Tries to locate an element in a %multimap. 491 * @param x Key of (key, value) pair to be located. 492 * @return Read-only (constant) iterator pointing to sought-after 493 * element, or end() if not found. 494 * 495 * This function takes a key and tries to locate the element with which 496 * the key matches. If successful the function returns a constant 497 * iterator pointing to the sought after %pair. If unsuccessful it 498 * returns the past-the-end ( @c end() ) iterator. 499 */ 500 const_iterator find(const key_type & __x)501 find(const key_type& __x) const 502 { return _M_t.find(__x); } 503 504 /** 505 * @brief Finds the number of elements with given key. 506 * @param x Key of (key, value) pairs to be located. 507 * @return Number of elements with specified key. 508 */ 509 size_type count(const key_type & __x)510 count(const key_type& __x) const 511 { return _M_t.count(__x); } 512 513 /** 514 * @brief Finds the beginning of a subsequence matching given key. 515 * @param x Key of (key, value) pair to be located. 516 * @return Iterator pointing to first element equal to or greater 517 * than key, or end(). 518 * 519 * This function returns the first element of a subsequence of elements 520 * that matches the given key. If unsuccessful it returns an iterator 521 * pointing to the first element that has a greater value than given key 522 * or end() if no such element exists. 523 */ 524 iterator lower_bound(const key_type & __x)525 lower_bound(const key_type& __x) 526 { return _M_t.lower_bound(__x); } 527 528 /** 529 * @brief Finds the beginning of a subsequence matching given key. 530 * @param x Key of (key, value) pair to be located. 531 * @return Read-only (constant) iterator pointing to first element 532 * equal to or greater than key, or end(). 533 * 534 * This function returns the first element of a subsequence of elements 535 * that matches the given key. If unsuccessful the iterator will point 536 * to the next greatest element or, if no such greater element exists, to 537 * end(). 538 */ 539 const_iterator lower_bound(const key_type & __x)540 lower_bound(const key_type& __x) const 541 { return _M_t.lower_bound(__x); } 542 543 /** 544 * @brief Finds the end of a subsequence matching given key. 545 * @param x Key of (key, value) pair to be located. 546 * @return Iterator pointing to the first element 547 * greater than key, or end(). 548 */ 549 iterator upper_bound(const key_type & __x)550 upper_bound(const key_type& __x) 551 { return _M_t.upper_bound(__x); } 552 553 /** 554 * @brief Finds the end of a subsequence matching given key. 555 * @param x Key of (key, value) pair to be located. 556 * @return Read-only (constant) iterator pointing to first iterator 557 * greater than key, or end(). 558 */ 559 const_iterator upper_bound(const key_type & __x)560 upper_bound(const key_type& __x) const 561 { return _M_t.upper_bound(__x); } 562 563 /** 564 * @brief Finds a subsequence matching given key. 565 * @param x Key of (key, value) pairs to be located. 566 * @return Pair of iterators that possibly points to the subsequence 567 * matching given key. 568 * 569 * This function is equivalent to 570 * @code 571 * std::make_pair(c.lower_bound(val), 572 * c.upper_bound(val)) 573 * @endcode 574 * (but is faster than making the calls separately). 575 */ 576 pair<iterator,iterator> equal_range(const key_type & __x)577 equal_range(const key_type& __x) 578 { return _M_t.equal_range(__x); } 579 580 /** 581 * @brief Finds a subsequence matching given key. 582 * @param x Key of (key, value) pairs to be located. 583 * @return Pair of read-only (constant) iterators that possibly points 584 * to the subsequence matching given key. 585 * 586 * This function is equivalent to 587 * @code 588 * std::make_pair(c.lower_bound(val), 589 * c.upper_bound(val)) 590 * @endcode 591 * (but is faster than making the calls separately). 592 */ 593 pair<const_iterator,const_iterator> equal_range(const key_type & __x)594 equal_range(const key_type& __x) const 595 { return _M_t.equal_range(__x); } 596 597 template <typename _K1, typename _T1, typename _C1, typename _A1> 598 friend bool 599 operator== (const multimap<_K1,_T1,_C1,_A1>&, 600 const multimap<_K1,_T1,_C1,_A1>&); 601 602 template <typename _K1, typename _T1, typename _C1, typename _A1> 603 friend bool 604 operator< (const multimap<_K1,_T1,_C1,_A1>&, 605 const multimap<_K1,_T1,_C1,_A1>&); 606 }; 607 608 /** 609 * @brief Multimap equality comparison. 610 * @param x A %multimap. 611 * @param y A %multimap of the same type as @a x. 612 * @return True iff the size and elements of the maps are equal. 613 * 614 * This is an equivalence relation. It is linear in the size of the 615 * multimaps. Multimaps are considered equivalent if their sizes are equal, 616 * and if corresponding elements compare equal. 617 */ 618 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 619 inline bool 620 operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 621 const multimap<_Key,_Tp,_Compare,_Alloc>& __y) 622 { return __x._M_t == __y._M_t; } 623 624 /** 625 * @brief Multimap ordering relation. 626 * @param x A %multimap. 627 * @param y A %multimap of the same type as @a x. 628 * @return True iff @a x is lexicographically less than @a y. 629 * 630 * This is a total ordering relation. It is linear in the size of the 631 * multimaps. The elements must be comparable with @c <. 632 * 633 * See std::lexicographical_compare() for how the determination is made. 634 */ 635 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 636 inline bool 637 operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 638 const multimap<_Key,_Tp,_Compare,_Alloc>& __y) 639 { return __x._M_t < __y._M_t; } 640 641 /// Based on operator== 642 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 643 inline bool 644 operator!=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 645 const multimap<_Key,_Tp,_Compare,_Alloc>& __y) 646 { return !(__x == __y); } 647 648 /// Based on operator< 649 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 650 inline bool 651 operator>(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 652 const multimap<_Key,_Tp,_Compare,_Alloc>& __y) 653 { return __y < __x; } 654 655 /// Based on operator< 656 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 657 inline bool 658 operator<=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 659 const multimap<_Key,_Tp,_Compare,_Alloc>& __y) 660 { return !(__y < __x); } 661 662 /// Based on operator< 663 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 664 inline bool 665 operator>=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x, 666 const multimap<_Key,_Tp,_Compare,_Alloc>& __y) 667 { return !(__x < __y); } 668 669 /// See std::multimap::swap(). 670 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc> 671 inline void swap(multimap<_Key,_Tp,_Compare,_Alloc> & __x,multimap<_Key,_Tp,_Compare,_Alloc> & __y)672 swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x, 673 multimap<_Key,_Tp,_Compare,_Alloc>& __y) 674 { __x.swap(__y); } 675 } // namespace std 676 677 #endif /* _MULTIMAP_H */ 678