1 // Map implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
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)
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.
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,
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.
33 * Hewlett-Packard Company
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.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
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.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef __GLIBCPP_INTERNAL_MAP_H
62 #define __GLIBCPP_INTERNAL_MAP_H
64 #include <bits/concept_check.h>
66 // Since this entire file is within namespace std, there's no reason to
67 // waste two spaces along the left column. Thus the leading indentation is
68 // slightly violated from here on.
73 * @brief A standard container made up of (key,value) pairs, which can be
74 * retrieved based on a key, in logarithmic time.
77 * @ingroup Assoc_containers
79 * Meets the requirements of a <a href="tables.html#65">container</a>, a
80 * <a href="tables.html#66">reversible container</a>, and an
81 * <a href="tables.html#69">associative container</a> (using unique keys).
82 * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
83 * value_type is std::pair<const Key,T>.
85 * Maps support bidirectional iterators.
88 * The private tree data is declared exactly the same way for map and
89 * multimap; the distinction is made entirely in how the tree functions are
90 * called (*_unique versus *_equal, same as the standard).
93 template <typename _Key, typename _Tp, typename _Compare = less<_Key>,
94 typename _Alloc = allocator<pair<const _Key, _Tp> > >
97 // concept requirements
98 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
99 __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept)
102 typedef _Key key_type;
103 typedef _Tp mapped_type;
104 typedef pair<const _Key, _Tp> value_type;
105 typedef _Compare key_compare;
108 : public binary_function<value_type, value_type, bool>
110 friend class map<_Key,_Tp,_Compare,_Alloc>;
113 value_compare(_Compare __c) : comp(__c) {}
115 bool operator()(const value_type& __x, const value_type& __y) const
116 { return comp(__x.first, __y.first); }
120 /// @if maint This turns a red-black tree into a [multi]map. @endif
121 typedef _Rb_tree<key_type, value_type,
122 _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
123 /// @if maint The actual tree structure. @endif
127 // many of these are specified differently in ISO, but the following are
128 // "functionally equivalent"
129 typedef typename _Rep_type::allocator_type allocator_type;
130 typedef typename _Rep_type::reference reference;
131 typedef typename _Rep_type::const_reference const_reference;
132 typedef typename _Rep_type::iterator iterator;
133 typedef typename _Rep_type::const_iterator const_iterator;
134 typedef typename _Rep_type::size_type size_type;
135 typedef typename _Rep_type::difference_type difference_type;
136 typedef typename _Rep_type::pointer pointer;
137 typedef typename _Rep_type::const_pointer const_pointer;
138 typedef typename _Rep_type::reverse_iterator reverse_iterator;
139 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
142 // [23.3.1.1] construct/copy/destroy
143 // (get_allocator() is normally listed in this section, but seems to have
144 // been accidentally omitted in the printed standard)
146 * @brief Default constructor creates no elements.
148 map() : _M_t(_Compare(), allocator_type()) { }
150 // for some reason this was made a separate function
152 * @brief Default constructor creates no elements.
155 map(const _Compare& __comp, const allocator_type& __a = allocator_type())
156 : _M_t(__comp, __a) { }
159 * @brief Map copy constructor.
160 * @param x A %map of identical element and allocator types.
162 * The newly-created %map uses a copy of the allocation object used
169 * @brief Builds a %map from a range.
170 * @param first An input iterator.
171 * @param last An input iterator.
173 * Creats a %map consisting of copies of the elements from [first,last).
174 * This is linear in N if the range is already sorted, and NlogN
175 * otherwise (where N is distance(first,last)).
177 template <typename _InputIterator>
178 map(_InputIterator __first, _InputIterator __last)
179 : _M_t(_Compare(), allocator_type())
180 { _M_t.insert_unique(__first, __last); }
183 * @brief Builds a %map from a range.
184 * @param first An input iterator.
185 * @param last An input iterator.
186 * @param comp A comparison functor.
187 * @param a An allocator object.
189 * Creats a %map consisting of copies of the elements from [first,last).
190 * This is linear in N if the range is already sorted, and NlogN
191 * otherwise (where N is distance(first,last)).
193 template <typename _InputIterator>
194 map(_InputIterator __first, _InputIterator __last,
195 const _Compare& __comp, const allocator_type& __a = allocator_type())
197 { _M_t.insert_unique(__first, __last); }
199 // FIXME There is no dtor declared, but we should have something generated
200 // by Doxygen. I don't know what tags to add to this paragraph to make
203 * The dtor only erases the elements, and note that if the elements
204 * themselves are pointers, the pointed-to memory is not touched in any
205 * way. Managing the pointer is the user's responsibilty.
209 * @brief Map assignment operator.
210 * @param x A %map of identical element and allocator types.
212 * All the elements of @a x are copied, but unlike the copy constructor, the
213 * allocator object is not copied.
216 operator=(const map& __x)
222 /// Get a copy of the memory allocation object.
224 get_allocator() const { return _M_t.get_allocator(); }
228 * Returns a read/write iterator that points to the first pair in the %map.
229 * Iteration is done in ascending order according to the keys.
232 begin() { return _M_t.begin(); }
235 * Returns a read-only (constant) iterator that points to the first pair
236 * in the %map. Iteration is done in ascending order according to the keys.
239 begin() const { return _M_t.begin(); }
242 * Returns a read/write iterator that points one past the last pair in the
243 * %map. Iteration is done in ascending order according to the keys.
246 end() { return _M_t.end(); }
249 * Returns a read-only (constant) iterator that points one past the last
250 * pair in the %map. Iteration is done in ascending order according to the
254 end() const { return _M_t.end(); }
257 * Returns a read/write reverse iterator that points to the last pair in
258 * the %map. Iteration is done in descending order according to the keys.
261 rbegin() { return _M_t.rbegin(); }
264 * Returns a read-only (constant) reverse iterator that points to the last
265 * pair in the %map. Iteration is done in descending order according to
268 const_reverse_iterator
269 rbegin() const { return _M_t.rbegin(); }
272 * Returns a read/write reverse iterator that points to one before the
273 * first pair in the %map. Iteration is done in descending order according
277 rend() { return _M_t.rend(); }
280 * Returns a read-only (constant) reverse iterator that points to one
281 * before the first pair in the %map. Iteration is done in descending order
282 * according to the keys.
284 const_reverse_iterator
285 rend() const { return _M_t.rend(); }
288 /** Returns true if the %map is empty. (Thus begin() would equal end().) */
290 empty() const { return _M_t.empty(); }
292 /** Returns the size of the %map. */
294 size() const { return _M_t.size(); }
296 /** Returns the maximum size of the %map. */
298 max_size() const { return _M_t.max_size(); }
300 // [23.3.1.2] element access
302 * @brief Subscript ( @c [] ) access to %map data.
303 * @param k The key for which data should be retrieved.
304 * @return A reference to the data of the (key,data) %pair.
306 * Allows for easy lookup with the subscript ( @c [] ) operator. Returns
307 * data associated with the key specified in subscript. If the key does
308 * not exist, a pair with that key is created using default values, which
311 * Lookup requires logarithmic time.
314 operator[](const key_type& __k)
316 // concept requirements
317 __glibcpp_function_requires(_DefaultConstructibleConcept<mapped_type>)
319 iterator __i = lower_bound(__k);
320 // __i->first is greater than or equivalent to __k.
321 if (__i == end() || key_comp()(__k, (*__i).first))
322 __i = insert(__i, value_type(__k, mapped_type()));
323 return (*__i).second;
328 * @brief Attempts to insert a std::pair into the %map.
329 * @param x Pair to be inserted (see std::make_pair for easy creation of
331 * @return A pair, of which the first element is an iterator that points
332 * to the possibly inserted pair, and the second is a bool that
333 * is true if the pair was actually inserted.
335 * This function attempts to insert a (key, value) %pair into the %map. A
336 * %map relies on unique keys and thus a %pair is only inserted if its first
337 * element (the key) is not already present in the %map.
339 * Insertion requires logarithmic time.
342 insert(const value_type& __x)
343 { return _M_t.insert_unique(__x); }
346 * @brief Attempts to insert a std::pair into the %map.
347 * @param position An iterator that serves as a hint as to where the
348 * pair should be inserted.
349 * @param x Pair to be inserted (see std::make_pair for easy creation of
351 * @return An iterator that points to the element with key of @a x (may
352 * or may not be the %pair passed in).
354 * This function is not concerned about whether the insertion took place,
355 * and thus does not return a boolean like the single-argument
356 * insert() does. Note that the first parameter is only a hint and can
357 * potentially improve the performance of the insertion process. A bad
358 * hint would cause no gains in efficiency.
360 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
361 * for more on "hinting".
363 * Insertion requires logarithmic time (if the hint is not taken).
366 insert(iterator position, const value_type& __x)
367 { return _M_t.insert_unique(position, __x); }
370 * @brief A template function that attemps to insert a range of elements.
371 * @param first Iterator pointing to the start of the range to be inserted.
372 * @param last Iterator pointing to the end of the range.
374 * Complexity similar to that of the range constructor.
376 template <typename _InputIterator>
378 insert(_InputIterator __first, _InputIterator __last)
379 { _M_t.insert_unique(__first, __last); }
382 * @brief Erases an element from a %map.
383 * @param position An iterator pointing to the element to be erased.
385 * This function erases an element, pointed to by the given iterator, from
386 * a %map. Note that this function only erases the element, and that if
387 * the element is itself a pointer, the pointed-to memory is not touched
388 * in any way. Managing the pointer is the user's responsibilty.
391 erase(iterator __position) { _M_t.erase(__position); }
394 * @brief Erases elements according to the provided key.
395 * @param x Key of element to be erased.
396 * @return The number of elements erased.
398 * This function erases all the elements located by the given key from
400 * Note that this function only erases the element, and that if
401 * the element is itself a pointer, the pointed-to memory is not touched
402 * in any way. Managing the pointer is the user's responsibilty.
405 erase(const key_type& __x) { return _M_t.erase(__x); }
408 * @brief Erases a [first,last) range of elements from a %map.
409 * @param first Iterator pointing to the start of the range to be erased.
410 * @param last Iterator pointing to the end of the range to be erased.
412 * This function erases a sequence of elements from a %map.
413 * Note that this function only erases the element, and that if
414 * the element is itself a pointer, the pointed-to memory is not touched
415 * in any way. Managing the pointer is the user's responsibilty.
418 erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
421 * @brief Swaps data with another %map.
422 * @param x A %map of the same element and allocator types.
424 * This exchanges the elements between two maps in constant time.
425 * (It is only swapping a pointer, an integer, and an instance of
426 * the @c Compare type (which itself is often stateless and empty), so it
427 * should be quite fast.)
428 * Note that the global std::swap() function is specialized such that
429 * std::swap(m1,m2) will feed to this function.
432 swap(map& __x) { _M_t.swap(__x._M_t); }
435 * Erases all elements in a %map. Note that this function only erases
436 * the elements, and that if the elements themselves are pointers, the
437 * pointed-to memory is not touched in any way. Managing the pointer is
438 * the user's responsibilty.
441 clear() { _M_t.clear(); }
445 * Returns the key comparison object out of which the %map was constructed.
448 key_comp() const { return _M_t.key_comp(); }
451 * Returns a value comparison object, built from the key comparison
452 * object out of which the %map was constructed.
455 value_comp() const { return value_compare(_M_t.key_comp()); }
457 // [23.3.1.3] map operations
459 * @brief Tries to locate an element in a %map.
460 * @param x Key of (key, value) %pair to be located.
461 * @return Iterator pointing to sought-after element, or end() if not
464 * This function takes a key and tries to locate the element with which
465 * the key matches. If successful the function returns an iterator
466 * pointing to the sought after %pair. If unsuccessful it returns the
467 * past-the-end ( @c end() ) iterator.
470 find(const key_type& __x) { return _M_t.find(__x); }
473 * @brief Tries to locate an element in a %map.
474 * @param x Key of (key, value) %pair to be located.
475 * @return Read-only (constant) iterator pointing to sought-after
476 * element, or end() if not found.
478 * This function takes a key and tries to locate the element with which
479 * the key matches. If successful the function returns a constant iterator
480 * pointing to the sought after %pair. If unsuccessful it returns the
481 * past-the-end ( @c end() ) iterator.
484 find(const key_type& __x) const { return _M_t.find(__x); }
487 * @brief Finds the number of elements with given key.
488 * @param x Key of (key, value) pairs to be located.
489 * @return Number of elements with specified key.
491 * This function only makes sense for multimaps; for map the result will
492 * either be 0 (not present) or 1 (present).
495 count(const key_type& __x) const
496 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
499 * @brief Finds the beginning of a subsequence matching given key.
500 * @param x Key of (key, value) pair to be located.
501 * @return Iterator pointing to first element matching given key, or
502 * end() if not found.
504 * This function is useful only with multimaps. It returns the first
505 * element of a subsequence of elements that matches the given key. If
506 * unsuccessful it returns an iterator pointing to the first element that
507 * has a greater value than given key or end() if no such element exists.
510 lower_bound(const key_type& __x) { return _M_t.lower_bound(__x); }
513 * @brief Finds the beginning of a subsequence matching given key.
514 * @param x Key of (key, value) pair to be located.
515 * @return Read-only (constant) iterator pointing to first element
516 * matching given key, or end() if not found.
518 * This function is useful only with multimaps. It returns the first
519 * element of a subsequence of elements that matches the given key. If
520 * unsuccessful the iterator will point to the next greatest element or,
521 * if no such greater element exists, to end().
524 lower_bound(const key_type& __x) const { return _M_t.lower_bound(__x); }
527 * @brief Finds the end of a subsequence matching given key.
528 * @param x Key of (key, value) pair to be located.
529 * @return Iterator pointing to last element matching given key.
531 * This function only makes sense with multimaps.
534 upper_bound(const key_type& __x) { return _M_t.upper_bound(__x); }
537 * @brief Finds the end of a subsequence matching given key.
538 * @param x Key of (key, value) pair to be located.
539 * @return Read-only (constant) iterator pointing to last element matching
542 * This function only makes sense with multimaps.
545 upper_bound(const key_type& __x) const
546 { return _M_t.upper_bound(__x); }
549 * @brief Finds a subsequence matching given key.
550 * @param x Key of (key, value) pairs to be located.
551 * @return Pair of iterators that possibly points to the subsequence
552 * matching given key.
554 * This function returns a pair of which the first
555 * element possibly points to the first element matching the given key
556 * and the second element possibly points to the last element matching the
557 * given key. If unsuccessful the first element of the returned pair will
558 * contain an iterator pointing to the next greatest element or, if no such
559 * greater element exists, to end().
561 * This function only makes sense for multimaps.
563 pair<iterator,iterator>
564 equal_range(const key_type& __x)
565 { return _M_t.equal_range(__x); }
568 * @brief Finds a subsequence matching given key.
569 * @param x Key of (key, value) pairs to be located.
570 * @return Pair of read-only (constant) iterators that possibly points to
571 * the subsequence matching given key.
573 * This function returns a pair of which the first
574 * element possibly points to the first element matching the given key
575 * and the second element possibly points to the last element matching the
576 * given key. If unsuccessful the first element of the returned pair will
577 * contain an iterator pointing to the next greatest element or, if no such
578 * a greater element exists, to end().
580 * This function only makes sense for multimaps.
582 pair<const_iterator,const_iterator>
583 equal_range(const key_type& __x) const
584 { return _M_t.equal_range(__x); }
586 template <typename _K1, typename _T1, typename _C1, typename _A1>
587 friend bool operator== (const map<_K1,_T1,_C1,_A1>&,
588 const map<_K1,_T1,_C1,_A1>&);
589 template <typename _K1, typename _T1, typename _C1, typename _A1>
590 friend bool operator< (const map<_K1,_T1,_C1,_A1>&,
591 const map<_K1,_T1,_C1,_A1>&);
596 * @brief Map equality comparison.
598 * @param y A %map of the same type as @a x.
599 * @return True iff the size and elements of the maps are equal.
601 * This is an equivalence relation. It is linear in the size of the
602 * maps. Maps are considered equivalent if their sizes are equal,
603 * and if corresponding elements compare equal.
605 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
607 operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x,
608 const map<_Key,_Tp,_Compare,_Alloc>& __y)
609 { return __x._M_t == __y._M_t; }
612 * @brief Map ordering relation.
614 * @param y A %map of the same type as @a x.
615 * @return True iff @a x is lexographically less than @a y.
617 * This is a total ordering relation. It is linear in the size of the
618 * maps. The elements must be comparable with @c <.
620 * See std::lexographical_compare() for how the determination is made.
622 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
624 operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x,
625 const map<_Key,_Tp,_Compare,_Alloc>& __y)
626 { return __x._M_t < __y._M_t; }
628 /// Based on operator==
629 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
631 operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
632 const map<_Key,_Tp,_Compare,_Alloc>& __y)
633 { return !(__x == __y); }
635 /// Based on operator<
636 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
638 operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x,
639 const map<_Key,_Tp,_Compare,_Alloc>& __y)
640 { return __y < __x; }
642 /// Based on operator<
643 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
645 operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
646 const map<_Key,_Tp,_Compare,_Alloc>& __y)
647 { return !(__y < __x); }
649 /// Based on operator<
650 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
652 operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
653 const map<_Key,_Tp,_Compare,_Alloc>& __y)
654 { return !(__x < __y); }
656 /// See std::map::swap().
657 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
659 swap(map<_Key,_Tp,_Compare,_Alloc>& __x, map<_Key,_Tp,_Compare,_Alloc>& __y)
664 #endif /* __GLIBCPP_INTERNAL_MAP_H */