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