OSDN Git Service

2001-06-21 Phil Edwards <pme@sources.redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_map.h
1 // Map implementation -*- C++ -*-
2
3 // Copyright (C) 2001 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 /* NOTE: This is an internal header file, included by other STL headers.
57  *   You should not attempt to use it directly.
58  */
59
60 #ifndef _CPP_BITS_STL_MAP_H
61 #define _CPP_BITS_STL_MAP_H 1
62
63 #include <bits/concept_check.h>
64
65 namespace std
66 {
67
68 template <class _Key, class _Tp, class _Compare = less<_Key>,
69           class _Alloc = allocator<pair<const _Key, _Tp> > >
70 class map
71 {
72   // concept requirements
73   __glibcpp_class_requires(_Tp, _SGIAssignableConcept);
74   __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept);
75
76 public:
77   // typedefs:
78   typedef _Key                 key_type;
79   typedef _Tp                   data_type;
80   typedef _Tp                   mapped_type;
81   typedef pair<const _Key, _Tp> value_type;
82   typedef _Compare             key_compare;
83     
84   class value_compare
85     : public binary_function<value_type, value_type, bool> {
86   friend class map<_Key,_Tp,_Compare,_Alloc>;
87   protected :
88     _Compare comp;
89     value_compare(_Compare __c) : comp(__c) {}
90   public:
91     bool operator()(const value_type& __x, const value_type& __y) const {
92       return comp(__x.first, __y.first);
93     }
94   };
95
96 private:
97   typedef _Rb_tree<key_type, value_type, 
98                    _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
99   _Rep_type _M_t;  // red-black tree representing map
100 public:
101   typedef typename _Rep_type::pointer pointer;
102   typedef typename _Rep_type::const_pointer const_pointer;
103   typedef typename _Rep_type::reference reference;
104   typedef typename _Rep_type::const_reference const_reference;
105   typedef typename _Rep_type::iterator iterator;
106   typedef typename _Rep_type::const_iterator const_iterator;
107   typedef typename _Rep_type::reverse_iterator reverse_iterator;
108   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
109   typedef typename _Rep_type::size_type size_type;
110   typedef typename _Rep_type::difference_type difference_type;
111   typedef typename _Rep_type::allocator_type allocator_type;
112
113   // allocation/deallocation
114
115   map() : _M_t(_Compare(), allocator_type()) {}
116   explicit map(const _Compare& __comp,
117                const allocator_type& __a = allocator_type())
118     : _M_t(__comp, __a) {}
119
120   template <class _InputIterator>
121   map(_InputIterator __first, _InputIterator __last)
122     : _M_t(_Compare(), allocator_type())
123     { _M_t.insert_unique(__first, __last); }
124
125   template <class _InputIterator>
126   map(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
127       const allocator_type& __a = allocator_type())
128     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
129   map(const map<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
130
131   map<_Key,_Tp,_Compare,_Alloc>&
132   operator=(const map<_Key, _Tp, _Compare, _Alloc>& __x)
133   {
134     _M_t = __x._M_t;
135     return *this; 
136   }
137
138   // accessors:
139
140   key_compare key_comp() const { return _M_t.key_comp(); }
141   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
142   allocator_type get_allocator() const { return _M_t.get_allocator(); }
143
144   iterator begin() { return _M_t.begin(); }
145   const_iterator begin() const { return _M_t.begin(); }
146   iterator end() { return _M_t.end(); }
147   const_iterator end() const { return _M_t.end(); }
148   reverse_iterator rbegin() { return _M_t.rbegin(); }
149   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
150   reverse_iterator rend() { return _M_t.rend(); }
151   const_reverse_iterator rend() const { return _M_t.rend(); }
152   bool empty() const { return _M_t.empty(); }
153   size_type size() const { return _M_t.size(); }
154   size_type max_size() const { return _M_t.max_size(); }
155   _Tp& operator[](const key_type& __k) {
156     iterator __i = lower_bound(__k);
157     // __i->first is greater than or equivalent to __k.
158     if (__i == end() || key_comp()(__k, (*__i).first))
159       __i = insert(__i, value_type(__k, _Tp()));
160     return (*__i).second;
161   }
162   void swap(map<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
163
164   // insert/erase
165
166   pair<iterator,bool> insert(const value_type& __x) 
167     { return _M_t.insert_unique(__x); }
168   iterator insert(iterator position, const value_type& __x)
169     { return _M_t.insert_unique(position, __x); }
170   template <class _InputIterator>
171   void insert(_InputIterator __first, _InputIterator __last) {
172     _M_t.insert_unique(__first, __last);
173   }
174
175   void erase(iterator __position) { _M_t.erase(__position); }
176   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
177   void erase(iterator __first, iterator __last)
178     { _M_t.erase(__first, __last); }
179   void clear() { _M_t.clear(); }
180
181   // map operations:
182
183   iterator find(const key_type& __x) { return _M_t.find(__x); }
184   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
185   size_type count(const key_type& __x) const {
186     return _M_t.find(__x) == _M_t.end() ? 0 : 1; 
187   }
188   iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
189   const_iterator lower_bound(const key_type& __x) const {
190     return _M_t.lower_bound(__x); 
191   }
192   iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
193   const_iterator upper_bound(const key_type& __x) const {
194     return _M_t.upper_bound(__x); 
195   }
196   
197   pair<iterator,iterator> equal_range(const key_type& __x) {
198     return _M_t.equal_range(__x);
199   }
200   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
201     return _M_t.equal_range(__x);
202   }
203
204   template <class _K1, class _T1, class _C1, class _A1>
205   friend bool operator== (const map<_K1, _T1, _C1, _A1>&,
206                           const map<_K1, _T1, _C1, _A1>&);
207   template <class _K1, class _T1, class _C1, class _A1>
208   friend bool operator< (const map<_K1, _T1, _C1, _A1>&,
209                          const map<_K1, _T1, _C1, _A1>&);
210 };
211
212 template <class _Key, class _Tp, class _Compare, class _Alloc>
213 inline bool operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
214                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
215   return __x._M_t == __y._M_t;
216 }
217
218 template <class _Key, class _Tp, class _Compare, class _Alloc>
219 inline bool operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
220                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
221   return __x._M_t < __y._M_t;
222 }
223
224 template <class _Key, class _Tp, class _Compare, class _Alloc>
225 inline bool operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
226                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
227   return !(__x == __y);
228 }
229
230 template <class _Key, class _Tp, class _Compare, class _Alloc>
231 inline bool operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
232                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
233   return __y < __x;
234 }
235
236 template <class _Key, class _Tp, class _Compare, class _Alloc>
237 inline bool operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
238                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
239   return !(__y < __x);
240 }
241
242 template <class _Key, class _Tp, class _Compare, class _Alloc>
243 inline bool operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
244                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
245   return !(__x < __y);
246 }
247
248 template <class _Key, class _Tp, class _Compare, class _Alloc>
249 inline void swap(map<_Key,_Tp,_Compare,_Alloc>& __x, 
250                  map<_Key,_Tp,_Compare,_Alloc>& __y) {
251   __x.swap(__y);
252 }
253
254 } // namespace std
255
256 #endif /* _CPP_BITS_STL_MAP_H */
257
258 // Local Variables:
259 // mode:C++
260 // End: