blocxx
Map.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
39#ifndef BLOCXX_MAP_HPP_INCLUDE_GUARD_
40#define BLOCXX_MAP_HPP_INCLUDE_GUARD_
41#include "blocxx/BLOCXX_config.h"
43#include <map>
44#include <functional>
45
46// This class is a wrapper around std::map<> and adds COW capabilities.
47namespace BLOCXX_NAMESPACE
48{
49
50
51// forward declarations are necessary for template friends.
52template<class Key, class T, class Compare > class Map;
53
54template<class Key, class T, class Compare>
55inline bool operator==(const Map<Key, T, Compare>& x,
56 const Map<Key, T, Compare>& y);
57
58template<class Key, class T, class Compare>
59inline bool operator<(const Map<Key, T, Compare>& x,
60 const Map<Key, T, Compare>& y);
61
62
63template<class Key, class T, class Compare = std::less<Key> > class Map
64{
65 typedef std::map<Key, T, Compare > M;
67public:
68 typedef typename M::key_type key_type;
69 typedef typename M::mapped_type mapped_type;
70 typedef typename M::value_type value_type;
71 typedef typename M::key_compare key_compare;
72 typedef typename M::value_compare value_compare;
73 typedef typename M::pointer pointer;
74 typedef typename M::const_pointer const_pointer;
75 typedef typename M::reference reference;
76 typedef typename M::const_reference const_reference;
77 typedef typename M::iterator iterator;
78 typedef typename M::const_iterator const_iterator;
79 typedef typename M::reverse_iterator reverse_iterator;
80 typedef typename M::const_reverse_iterator const_reverse_iterator;
81 typedef typename M::size_type size_type;
82 typedef typename M::difference_type difference_type;
83 Map() : m_impl(new M) { }
84 explicit Map(M* toWrap) : m_impl(toWrap)
85 { }
86 explicit Map(const Compare& comp)
87 : m_impl(new M(comp)) { }
88 template <class InputIterator>
89 Map(InputIterator first, InputIterator last) :
90 m_impl(new M(first, last))
91 {
92 }
93 template <class InputIterator>
94 Map(InputIterator first, InputIterator last, const Compare& comp) :
95 m_impl(new M(first, last, comp))
96 {
97 }
99 {
100 return &*m_impl;
101 }
103 {
104 return m_impl->key_comp();
105 }
107 {
108 return m_impl->value_comp();
109 }
111 {
112 return m_impl->begin();
113 }
115 {
116 return m_impl->begin();
117 }
119 {
120 return m_impl->end();
121 }
123 {
124 return m_impl->end();
125 }
127 {
128 return m_impl->rbegin();
129 }
131 {
132 return m_impl->rbegin();
133 }
135 {
136 return m_impl->rend();
137 }
139 {
140 return m_impl->rend();
141 }
142 bool empty() const
143 {
144 return m_impl->empty();
145 }
147 {
148 return m_impl->size();
149 }
151 {
152 return m_impl->max_size();
153 }
154 T& operator[](const key_type& k)
155 {
156 return m_impl->operator[](k);
157 }
159 {
160 m_impl.swap(x.m_impl);
161 }
162 std::pair<iterator, bool> insert(const value_type& x)
163 {
164 return m_impl->insert(x);
165 }
166 iterator insert(iterator position, const value_type& x)
167 {
168 return m_impl->insert(position, x);
169 }
170 template <class InputIterator>
171 void insert(InputIterator first, InputIterator last)
172 {
173 m_impl->insert(first, last);
174 }
175 void erase(iterator position)
176 {
177 m_impl->erase(position);
178 }
180 {
181 return m_impl->erase(x);
182 }
183 void erase(iterator first, iterator last)
184 {
185 m_impl->erase(first, last);
186 }
187 void clear()
188 {
189 m_impl->clear();
190 }
192 {
193 return m_impl->find(x);
194 }
196 {
197 return m_impl->find(x);
198 }
199 size_type count(const key_type& x) const
200 {
201 return m_impl->count(x);
202 }
204 {
205 return m_impl->lower_bound(x);
206 }
208 {
209 return m_impl->lower_bound(x);
210 }
212 {
213 return m_impl->upper_bound(x);
214 }
216 {
217 return m_impl->upper_bound(x);
218 }
219 std::pair<iterator, iterator> equal_range(const key_type& x)
220 {
221 return m_impl->equal_range(x);
222 }
223 std::pair<const_iterator, const_iterator>
224 equal_range(const key_type& x) const
225 {
226 return m_impl->equal_range(x);
227 }
229 const Map<Key, T, Compare>& y);
230 friend bool operator< <>(const Map<Key, T, Compare>& x,
231 const Map<Key, T, Compare>& y);
232};
233template <class Key, class T, class Compare>
234std::map<Key, T, Compare>* COWReferenceClone(std::map<Key, T, Compare>* obj)
235{
236 return new std::map<Key, T, Compare>(*obj);
237}
238template<class Key, class T, class Compare>
239inline bool operator==(const Map<Key, T, Compare>& x,
240 const Map<Key, T, Compare>& y)
241{
242 return *x.m_impl == *y.m_impl;
243}
244template<class Key, class T, class Compare>
245inline bool operator<(const Map<Key, T, Compare>& x,
246 const Map<Key, T, Compare>& y)
247{
248 return *x.m_impl < *y.m_impl;
249}
250template <class Key, class T, class Compare>
253{
254 x.swap(y);
255}
256
257} // end namespace BLOCXX_NAMESPACE
258
259#endif
COWReference A smart pointer that uses non-intrusive reference counting.
void swap(COWReference< T > &arg)
M::const_reference const_reference
Definition Map.hpp:76
size_type count(const key_type &x) const
Definition Map.hpp:199
T & operator[](const key_type &k)
Definition Map.hpp:154
iterator insert(iterator position, const value_type &x)
Definition Map.hpp:166
M::key_compare key_compare
Definition Map.hpp:71
M::const_reverse_iterator const_reverse_iterator
Definition Map.hpp:80
const_iterator upper_bound(const key_type &x) const
Definition Map.hpp:215
const_reverse_iterator rbegin() const
Definition Map.hpp:130
M::iterator iterator
Definition Map.hpp:77
reverse_iterator rend()
Definition Map.hpp:134
const_reverse_iterator rend() const
Definition Map.hpp:138
iterator upper_bound(const key_type &x)
Definition Map.hpp:211
const_iterator lower_bound(const key_type &x) const
Definition Map.hpp:207
std::pair< iterator, iterator > equal_range(const key_type &x)
Definition Map.hpp:219
std::map< Key, T, Compare > M
Definition Map.hpp:65
M::key_type key_type
Definition Map.hpp:68
M::value_type value_type
Definition Map.hpp:70
M::value_compare value_compare
Definition Map.hpp:72
Map(const Compare &comp)
Definition Map.hpp:86
Map(InputIterator first, InputIterator last, const Compare &comp)
Definition Map.hpp:94
reverse_iterator rbegin()
Definition Map.hpp:126
iterator begin()
Definition Map.hpp:110
std::pair< const_iterator, const_iterator > equal_range(const key_type &x) const
Definition Map.hpp:224
M::size_type size_type
Definition Map.hpp:81
Map(InputIterator first, InputIterator last)
Definition Map.hpp:89
std::pair< iterator, bool > insert(const value_type &x)
Definition Map.hpp:162
M::const_iterator const_iterator
Definition Map.hpp:78
iterator lower_bound(const key_type &x)
Definition Map.hpp:203
Map(M *toWrap)
Definition Map.hpp:84
void erase(iterator position)
Definition Map.hpp:175
const_iterator find(const key_type &x) const
Definition Map.hpp:195
size_type max_size() const
Definition Map.hpp:150
M::mapped_type mapped_type
Definition Map.hpp:69
const_iterator end() const
Definition Map.hpp:122
M::pointer pointer
Definition Map.hpp:73
friend bool operator==(const Map< Key, T, Compare > &x, const Map< Key, T, Compare > &y)
Definition Map.hpp:239
key_compare key_comp() const
Definition Map.hpp:102
size_type size() const
Definition Map.hpp:146
void insert(InputIterator first, InputIterator last)
Definition Map.hpp:171
M::reverse_iterator reverse_iterator
Definition Map.hpp:79
void erase(iterator first, iterator last)
Definition Map.hpp:183
M::difference_type difference_type
Definition Map.hpp:82
value_compare value_comp() const
Definition Map.hpp:106
M::const_pointer const_pointer
Definition Map.hpp:74
M::reference reference
Definition Map.hpp:75
iterator find(const key_type &x)
Definition Map.hpp:191
size_type erase(const key_type &x)
Definition Map.hpp:179
COWReference< M > m_impl
Definition Map.hpp:66
bool empty() const
Definition Map.hpp:142
iterator end()
Definition Map.hpp:118
void swap(Map< Key, T, Compare > &x)
Definition Map.hpp:158
const_iterator begin() const
Definition Map.hpp:114
Taken from RFC 1321.
T * COWReferenceClone(T *obj)
bool operator<(const Array< T > &x, const Array< T > &y)
bool operator==(const Array< T > &x, const Array< T > &y)
void swap(Array< T > &x, Array< T > &y)