Mark as release
[official-gcc.git] / libstdc++-v3 / include / bits / stl_map.h
blob13e62bc220c9cea09f82f0c1a19b6754414b2984
1 // Map implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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.
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.
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.
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.
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
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.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
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.
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.
62 #ifndef _MAP_H
63 #define _MAP_H 1
65 #include <bits/functexcept.h>
66 #include <bits/concept_check.h>
68 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
70 /**
71 * @brief A standard container made up of (key,value) pairs, which can be
72 * retrieved based on a key, in logarithmic time.
74 * @ingroup Containers
75 * @ingroup Assoc_containers
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>.
83 * Maps support bidirectional iterators.
85 * @if maint
86 * The private tree data is declared exactly the same way for map and
87 * multimap; the distinction is made entirely in how the tree functions are
88 * called (*_unique versus *_equal, same as the standard).
89 * @endif
91 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
92 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
93 class map
95 public:
96 typedef _Key key_type;
97 typedef _Tp mapped_type;
98 typedef std::pair<const _Key, _Tp> value_type;
99 typedef _Compare key_compare;
100 typedef _Alloc allocator_type;
102 private:
103 // concept requirements
104 typedef typename _Alloc::value_type _Alloc_value_type;
105 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
106 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
107 _BinaryFunctionConcept)
108 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
110 public:
111 class value_compare
112 : public std::binary_function<value_type, value_type, bool>
114 friend class map<_Key, _Tp, _Compare, _Alloc>;
115 protected:
116 _Compare comp;
118 value_compare(_Compare __c)
119 : comp(__c) { }
121 public:
122 bool operator()(const value_type& __x, const value_type& __y) const
123 { return comp(__x.first, __y.first); }
126 private:
127 /// @if maint This turns a red-black tree into a [multi]map. @endif
128 typedef typename _Alloc::template rebind<value_type>::other
129 _Pair_alloc_type;
131 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
132 key_compare, _Pair_alloc_type> _Rep_type;
134 /// @if maint The actual tree structure. @endif
135 _Rep_type _M_t;
137 public:
138 // many of these are specified differently in ISO, but the following are
139 // "functionally equivalent"
140 typedef typename _Pair_alloc_type::pointer pointer;
141 typedef typename _Pair_alloc_type::const_pointer const_pointer;
142 typedef typename _Pair_alloc_type::reference reference;
143 typedef typename _Pair_alloc_type::const_reference const_reference;
144 typedef typename _Rep_type::iterator iterator;
145 typedef typename _Rep_type::const_iterator const_iterator;
146 typedef typename _Rep_type::size_type size_type;
147 typedef typename _Rep_type::difference_type difference_type;
148 typedef typename _Rep_type::reverse_iterator reverse_iterator;
149 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
151 // [23.3.1.1] construct/copy/destroy
152 // (get_allocator() is normally listed in this section, but seems to have
153 // been accidentally omitted in the printed standard)
155 * @brief Default constructor creates no elements.
157 map()
158 : _M_t(_Compare(), allocator_type()) { }
160 // for some reason this was made a separate function
162 * @brief Default constructor creates no elements.
164 explicit
165 map(const _Compare& __comp, const allocator_type& __a = allocator_type())
166 : _M_t(__comp, __a) { }
169 * @brief Map copy constructor.
170 * @param x A %map of identical element and allocator types.
172 * The newly-created %map uses a copy of the allocation object used
173 * by @a x.
175 map(const map& __x)
176 : _M_t(__x._M_t) { }
179 * @brief Builds a %map from a range.
180 * @param first An input iterator.
181 * @param last An input iterator.
183 * Create a %map consisting of copies of the elements from [first,last).
184 * This is linear in N if the range is already sorted, and NlogN
185 * otherwise (where N is distance(first,last)).
187 template <typename _InputIterator>
188 map(_InputIterator __first, _InputIterator __last)
189 : _M_t(_Compare(), allocator_type())
190 { _M_t._M_insert_unique(__first, __last); }
193 * @brief Builds a %map from a range.
194 * @param first An input iterator.
195 * @param last An input iterator.
196 * @param comp A comparison functor.
197 * @param a An allocator object.
199 * Create a %map consisting of copies of the elements from [first,last).
200 * This is linear in N if the range is already sorted, and NlogN
201 * otherwise (where N is distance(first,last)).
203 template <typename _InputIterator>
204 map(_InputIterator __first, _InputIterator __last,
205 const _Compare& __comp, const allocator_type& __a = allocator_type())
206 : _M_t(__comp, __a)
207 { _M_t._M_insert_unique(__first, __last); }
209 // FIXME There is no dtor declared, but we should have something
210 // generated by Doxygen. I don't know what tags to add to this
211 // paragraph to make that happen:
213 * The dtor only erases the elements, and note that if the elements
214 * themselves are pointers, the pointed-to memory is not touched in any
215 * way. Managing the pointer is the user's responsibilty.
219 * @brief Map assignment operator.
220 * @param x A %map of identical element and allocator types.
222 * All the elements of @a x are copied, but unlike the copy constructor,
223 * the allocator object is not copied.
225 map&
226 operator=(const map& __x)
228 _M_t = __x._M_t;
229 return *this;
232 /// Get a copy of the memory allocation object.
233 allocator_type
234 get_allocator() const
235 { return _M_t.get_allocator(); }
237 // iterators
239 * Returns a read/write iterator that points to the first pair in the
240 * %map.
241 * Iteration is done in ascending order according to the keys.
243 iterator
244 begin()
245 { return _M_t.begin(); }
248 * Returns a read-only (constant) iterator that points to the first pair
249 * in the %map. Iteration is done in ascending order according to the
250 * keys.
252 const_iterator
253 begin() const
254 { return _M_t.begin(); }
257 * Returns a read/write iterator that points one past the last
258 * pair in the %map. Iteration is done in ascending order
259 * according to the keys.
261 iterator
262 end()
263 { return _M_t.end(); }
266 * Returns a read-only (constant) iterator that points one past the last
267 * pair in the %map. Iteration is done in ascending order according to
268 * the keys.
270 const_iterator
271 end() const
272 { return _M_t.end(); }
275 * Returns a read/write reverse iterator that points to the last pair in
276 * the %map. Iteration is done in descending order according to the
277 * keys.
279 reverse_iterator
280 rbegin()
281 { return _M_t.rbegin(); }
284 * Returns a read-only (constant) reverse iterator that points to the
285 * last pair in the %map. Iteration is done in descending order
286 * according to the keys.
288 const_reverse_iterator
289 rbegin() const
290 { return _M_t.rbegin(); }
293 * Returns a read/write reverse iterator that points to one before the
294 * first pair in the %map. Iteration is done in descending order
295 * according to the keys.
297 reverse_iterator
298 rend()
299 { return _M_t.rend(); }
302 * Returns a read-only (constant) reverse iterator that points to one
303 * before the first pair in the %map. Iteration is done in descending
304 * order according to the keys.
306 const_reverse_iterator
307 rend() const
308 { return _M_t.rend(); }
310 // capacity
311 /** Returns true if the %map is empty. (Thus begin() would equal
312 * end().)
314 bool
315 empty() const
316 { return _M_t.empty(); }
318 /** Returns the size of the %map. */
319 size_type
320 size() const
321 { return _M_t.size(); }
323 /** Returns the maximum size of the %map. */
324 size_type
325 max_size() const
326 { return _M_t.max_size(); }
328 // [23.3.1.2] element access
330 * @brief Subscript ( @c [] ) access to %map data.
331 * @param k The key for which data should be retrieved.
332 * @return A reference to the data of the (key,data) %pair.
334 * Allows for easy lookup with the subscript ( @c [] )
335 * operator. Returns data associated with the key specified in
336 * subscript. If the key does not exist, a pair with that key
337 * is created using default values, which is then returned.
339 * Lookup requires logarithmic time.
341 mapped_type&
342 operator[](const key_type& __k)
344 // concept requirements
345 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
347 iterator __i = lower_bound(__k);
348 // __i->first is greater than or equivalent to __k.
349 if (__i == end() || key_comp()(__k, (*__i).first))
350 __i = insert(__i, value_type(__k, mapped_type()));
351 return (*__i).second;
354 // _GLIBCXX_RESOLVE_LIB_DEFECTS
355 // DR 464. Suggestion for new member functions in standard containers.
357 * @brief Access to %map data.
358 * @param k The key for which data should be retrieved.
359 * @return A reference to the data whose key is equivalent to @a k, if
360 * such a data is present in the %map.
361 * @throw std::out_of_range If no such data is present.
363 mapped_type&
364 at(const key_type& __k)
366 iterator __i = lower_bound(__k);
367 if (__i == end() || key_comp()(__k, (*__i).first))
368 __throw_out_of_range(__N("map::at"));
369 return (*__i).second;
372 const mapped_type&
373 at(const key_type& __k) const
375 const_iterator __i = lower_bound(__k);
376 if (__i == end() || key_comp()(__k, (*__i).first))
377 __throw_out_of_range(__N("map::at"));
378 return (*__i).second;
381 // modifiers
383 * @brief Attempts to insert a std::pair into the %map.
385 * @param x Pair to be inserted (see std::make_pair for easy creation
386 * of pairs).
388 * @return A pair, of which the first element is an iterator that
389 * points to the possibly inserted pair, and the second is
390 * a bool that is true if the pair was actually inserted.
392 * This function attempts to insert a (key, value) %pair into the %map.
393 * A %map relies on unique keys and thus a %pair is only inserted if its
394 * first element (the key) is not already present in the %map.
396 * Insertion requires logarithmic time.
398 std::pair<iterator, bool>
399 insert(const value_type& __x)
400 { return _M_t._M_insert_unique(__x); }
403 * @brief Attempts to insert a std::pair into the %map.
404 * @param position An iterator that serves as a hint as to where the
405 * pair should be inserted.
406 * @param x Pair to be inserted (see std::make_pair for easy creation
407 * of pairs).
408 * @return An iterator that points to the element with key of @a x (may
409 * or may not be the %pair passed in).
412 * This function is not concerned about whether the insertion
413 * took place, and thus does not return a boolean like the
414 * single-argument insert() does. Note that the first
415 * parameter is only a hint and can potentially improve the
416 * performance of the insertion process. A bad hint would
417 * cause no gains in efficiency.
419 * See
420 * http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
421 * for more on "hinting".
423 * Insertion requires logarithmic time (if the hint is not taken).
425 iterator
426 insert(iterator __position, const value_type& __x)
427 { return _M_t._M_insert_unique(__position, __x); }
430 * @brief Template function that attemps to insert a range of elements.
431 * @param first Iterator pointing to the start of the range to be
432 * inserted.
433 * @param last Iterator pointing to the end of the range.
435 * Complexity similar to that of the range constructor.
437 template <typename _InputIterator>
438 void
439 insert(_InputIterator __first, _InputIterator __last)
440 { _M_t._M_insert_unique(__first, __last); }
443 * @brief Erases an element from a %map.
444 * @param position An iterator pointing to the element to be erased.
446 * This function erases an element, pointed to by the given
447 * iterator, from a %map. Note that this function only erases
448 * the element, and that if the element is itself a pointer,
449 * the pointed-to memory is not touched in any way. Managing
450 * the pointer is the user's responsibilty.
452 void
453 erase(iterator __position)
454 { _M_t.erase(__position); }
457 * @brief Erases elements according to the provided key.
458 * @param x Key of element to be erased.
459 * @return The number of elements erased.
461 * This function erases all the elements located by the given key from
462 * a %map.
463 * Note that this function only erases the element, and that if
464 * the element is itself a pointer, the pointed-to memory is not touched
465 * in any way. Managing the pointer is the user's responsibilty.
467 size_type
468 erase(const key_type& __x)
469 { return _M_t.erase(__x); }
472 * @brief Erases a [first,last) range of elements from a %map.
473 * @param first Iterator pointing to the start of the range to be
474 * erased.
475 * @param last Iterator pointing to the end of the range to be erased.
477 * This function erases a sequence of elements from a %map.
478 * Note that this function only erases the element, and that if
479 * the element is itself a pointer, the pointed-to memory is not touched
480 * in any way. Managing the pointer is the user's responsibilty.
482 void
483 erase(iterator __first, iterator __last)
484 { _M_t.erase(__first, __last); }
487 * @brief Swaps data with another %map.
488 * @param x A %map of the same element and allocator types.
490 * This exchanges the elements between two maps in constant
491 * time. (It is only swapping a pointer, an integer, and an
492 * instance of the @c Compare type (which itself is often
493 * stateless and empty), so it should be quite fast.) Note
494 * that the global std::swap() function is specialized such
495 * that std::swap(m1,m2) will feed to this function.
497 void
498 swap(map& __x)
499 { _M_t.swap(__x._M_t); }
502 * Erases all elements in a %map. Note that this function only
503 * erases the elements, and that if the elements themselves are
504 * pointers, the pointed-to memory is not touched in any way.
505 * Managing the pointer is the user's responsibilty.
507 void
508 clear()
509 { _M_t.clear(); }
511 // observers
513 * Returns the key comparison object out of which the %map was
514 * constructed.
516 key_compare
517 key_comp() const
518 { return _M_t.key_comp(); }
521 * Returns a value comparison object, built from the key comparison
522 * object out of which the %map was constructed.
524 value_compare
525 value_comp() const
526 { return value_compare(_M_t.key_comp()); }
528 // [23.3.1.3] map operations
530 * @brief Tries to locate an element in a %map.
531 * @param x Key of (key, value) %pair to be located.
532 * @return Iterator pointing to sought-after element, or end() if not
533 * found.
535 * This function takes a key and tries to locate the element with which
536 * the key matches. If successful the function returns an iterator
537 * pointing to the sought after %pair. If unsuccessful it returns the
538 * past-the-end ( @c end() ) iterator.
540 iterator
541 find(const key_type& __x)
542 { return _M_t.find(__x); }
545 * @brief Tries to locate an element in a %map.
546 * @param x Key of (key, value) %pair to be located.
547 * @return Read-only (constant) iterator pointing to sought-after
548 * element, or end() if not found.
550 * This function takes a key and tries to locate the element with which
551 * the key matches. If successful the function returns a constant
552 * iterator pointing to the sought after %pair. If unsuccessful it
553 * returns the past-the-end ( @c end() ) iterator.
555 const_iterator
556 find(const key_type& __x) const
557 { return _M_t.find(__x); }
560 * @brief Finds the number of elements with given key.
561 * @param x Key of (key, value) pairs to be located.
562 * @return Number of elements with specified key.
564 * This function only makes sense for multimaps; for map the result will
565 * either be 0 (not present) or 1 (present).
567 size_type
568 count(const key_type& __x) const
569 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
572 * @brief Finds the beginning of a subsequence matching given key.
573 * @param x Key of (key, value) pair to be located.
574 * @return Iterator pointing to first element equal to or greater
575 * than key, or end().
577 * This function returns the first element of a subsequence of elements
578 * that matches the given key. If unsuccessful it returns an iterator
579 * pointing to the first element that has a greater value than given key
580 * or end() if no such element exists.
582 iterator
583 lower_bound(const key_type& __x)
584 { return _M_t.lower_bound(__x); }
587 * @brief Finds the beginning of a subsequence matching given key.
588 * @param x Key of (key, value) pair to be located.
589 * @return Read-only (constant) iterator pointing to first element
590 * equal to or greater than key, or end().
592 * This function returns the first element of a subsequence of elements
593 * that matches the given key. If unsuccessful it returns an iterator
594 * pointing to the first element that has a greater value than given key
595 * or end() if no such element exists.
597 const_iterator
598 lower_bound(const key_type& __x) const
599 { return _M_t.lower_bound(__x); }
602 * @brief Finds the end of a subsequence matching given key.
603 * @param x Key of (key, value) pair to be located.
604 * @return Iterator pointing to the first element
605 * greater than key, or end().
607 iterator
608 upper_bound(const key_type& __x)
609 { return _M_t.upper_bound(__x); }
612 * @brief Finds the end of a subsequence matching given key.
613 * @param x Key of (key, value) pair to be located.
614 * @return Read-only (constant) iterator pointing to first iterator
615 * greater than key, or end().
617 const_iterator
618 upper_bound(const key_type& __x) const
619 { return _M_t.upper_bound(__x); }
622 * @brief Finds a subsequence matching given key.
623 * @param x Key of (key, value) pairs to be located.
624 * @return Pair of iterators that possibly points to the subsequence
625 * matching given key.
627 * This function is equivalent to
628 * @code
629 * std::make_pair(c.lower_bound(val),
630 * c.upper_bound(val))
631 * @endcode
632 * (but is faster than making the calls separately).
634 * This function probably only makes sense for multimaps.
636 std::pair<iterator, iterator>
637 equal_range(const key_type& __x)
638 { return _M_t.equal_range(__x); }
641 * @brief Finds a subsequence matching given key.
642 * @param x Key of (key, value) pairs to be located.
643 * @return Pair of read-only (constant) iterators that possibly points
644 * to the subsequence matching given key.
646 * This function is equivalent to
647 * @code
648 * std::make_pair(c.lower_bound(val),
649 * c.upper_bound(val))
650 * @endcode
651 * (but is faster than making the calls separately).
653 * This function probably only makes sense for multimaps.
655 std::pair<const_iterator, const_iterator>
656 equal_range(const key_type& __x) const
657 { return _M_t.equal_range(__x); }
659 template <typename _K1, typename _T1, typename _C1, typename _A1>
660 friend bool
661 operator== (const map<_K1, _T1, _C1, _A1>&,
662 const map<_K1, _T1, _C1, _A1>&);
664 template <typename _K1, typename _T1, typename _C1, typename _A1>
665 friend bool
666 operator< (const map<_K1, _T1, _C1, _A1>&,
667 const map<_K1, _T1, _C1, _A1>&);
671 * @brief Map equality comparison.
672 * @param x A %map.
673 * @param y A %map of the same type as @a x.
674 * @return True iff the size and elements of the maps are equal.
676 * This is an equivalence relation. It is linear in the size of the
677 * maps. Maps are considered equivalent if their sizes are equal,
678 * and if corresponding elements compare equal.
680 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
681 inline bool
682 operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
683 const map<_Key, _Tp, _Compare, _Alloc>& __y)
684 { return __x._M_t == __y._M_t; }
687 * @brief Map ordering relation.
688 * @param x A %map.
689 * @param y A %map of the same type as @a x.
690 * @return True iff @a x is lexicographically less than @a y.
692 * This is a total ordering relation. It is linear in the size of the
693 * maps. The elements must be comparable with @c <.
695 * See std::lexicographical_compare() for how the determination is made.
697 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
698 inline bool
699 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
700 const map<_Key, _Tp, _Compare, _Alloc>& __y)
701 { return __x._M_t < __y._M_t; }
703 /// Based on operator==
704 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
705 inline bool
706 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
707 const map<_Key, _Tp, _Compare, _Alloc>& __y)
708 { return !(__x == __y); }
710 /// Based on operator<
711 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
712 inline bool
713 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
714 const map<_Key, _Tp, _Compare, _Alloc>& __y)
715 { return __y < __x; }
717 /// Based on operator<
718 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
719 inline bool
720 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
721 const map<_Key, _Tp, _Compare, _Alloc>& __y)
722 { return !(__y < __x); }
724 /// Based on operator<
725 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
726 inline bool
727 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
728 const map<_Key, _Tp, _Compare, _Alloc>& __y)
729 { return !(__x < __y); }
731 /// See std::map::swap().
732 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
733 inline void
734 swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
735 map<_Key, _Tp, _Compare, _Alloc>& __y)
736 { __x.swap(__y); }
738 _GLIBCXX_END_NESTED_NAMESPACE
740 #endif /* _MAP_H */