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"
42 #include "blocxx/COWReference.hpp"
43 #include <map>
44 #include <functional>
45 
46 // This class is a wrapper around std::map<> and adds COW capabilities.
47 namespace BLOCXX_NAMESPACE
48 {
49 
50 
51 // forward declarations are necessary for template friends.
52 template<class Key, class T, class Compare > class Map;
53 
54 template<class Key, class T, class Compare>
55 inline bool operator==(const Map<Key, T, Compare>& x,
56  const Map<Key, T, Compare>& y);
57 
58 template<class Key, class T, class Compare>
59 inline bool operator<(const Map<Key, T, Compare>& x,
60  const Map<Key, T, Compare>& y);
61 
62 
63 template<class Key, class T, class Compare = std::less<Key> > class Map
64 {
65  typedef std::map<Key, T, Compare > M;
67 public:
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  }
146  size_type size() const
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  }
195  const_iterator find(const key_type& x) const
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  }
228  friend bool operator== <>(const Map<Key, T, Compare>& x,
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 };
233 template <class Key, class T, class Compare>
234 std::map<Key, T, Compare>* COWReferenceClone(std::map<Key, T, Compare>* obj)
235 {
236  return new std::map<Key, T, Compare>(*obj);
237 }
238 template<class Key, class T, class Compare>
239 inline 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 }
244 template<class Key, class T, class Compare>
245 inline 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 }
250 template <class Key, class T, class Compare>
251 inline void swap(Map<Key, T, Compare>& x,
253 {
254  x.swap(y);
255 }
256 
257 } // end namespace BLOCXX_NAMESPACE
258 
259 #endif