ProteoWizard
shared_map.hpp
Go to the documentation of this file.
1 //
2 // $Id: shared_map.hpp 2759 2011-06-09 16:34:23Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2011 Vanderbilt University - Nashville, TN 37232
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 
23 #ifndef _SHARED_MAP_HPP_
24 #define _SHARED_MAP_HPP_
25 
26 
27 #include <map>
28 #include <boost/shared_ptr.hpp>
29 
30 
31 namespace pwiz {
32 namespace util {
33 
34 
35 /// copy semantics of shared_ptr<map<K,V> > with a std::map interface
36 template<class keyT,
37  class valueT,
38  class _Pr = std::less<keyT>,
39  class _Alloc = std::allocator<std::pair<const keyT, valueT> > >
41 {
42  public:
43 
44  typedef std::map<keyT, valueT, _Pr, _Alloc> BaseType;
45  typedef typename BaseType::allocator_type allocator_type;
46  typedef typename BaseType::key_type key_type;
47  typedef typename BaseType::value_type value_type;
48  typedef typename BaseType::key_compare key_compare;
49  typedef typename BaseType::value_compare value_compare;
50  typedef typename BaseType::size_type size_type;
51  typedef typename BaseType::mapped_type mapped_type;
52  typedef typename BaseType::difference_type difference_type;
53  typedef typename BaseType::pointer pointer;
54  typedef typename BaseType::const_pointer const_pointer;
55  typedef typename BaseType::reference reference;
56  typedef typename BaseType::const_reference const_reference;
57  typedef typename BaseType::iterator iterator;
58  typedef typename BaseType::const_iterator const_iterator;
59  typedef typename BaseType::reverse_iterator reverse_iterator;
60  typedef typename BaseType::const_reverse_iterator const_reverse_iterator;
61 
62  private:
63  boost::shared_ptr<BaseType> _base;
64 
65  public:
66 
67  /// Constructs an empty map that uses the predicate _Pred to order keys, if it is supplied. The map uses the allocator _Alloc for all storage management, if it is supplied.
68  explicit shared_map(const key_compare& predicate = key_compare(), const allocator_type& allocator = allocator_type())
69  : _base(new BaseType(predicate, allocator))
70  {
71  }
72 
73  /// Constructs a map containing values in the range [_First, _Last). Creation of the new map is only guaranteed to succeed if the iterators start and finish return values of type pair<class Key, class Value> and all values of Key in the range [_First, _Last) are unique. The map uses the predicate _Pred to order keys, and the allocator _Alloc for all storage management.
74  template<class _Iter>
75  shared_map(_Iter _First, _Iter _Last)
77  {
78  for (; _First != _Last; ++_First)
79  this->insert(*_First);
80  }
81 
82  /// Constructs a map containing values in the range [_First, _Last). Creation of the new map is only guaranteed to succeed if the iterators start and finish return values of type pair<class Key, class Value> and all values of Key in the range [_First, _Last) are unique. The map uses the predicate _Pred to order keys, and the allocator _Alloc for all storage management.
83  template<class _Iter>
84  shared_map(_Iter _First, _Iter _Last, const key_compare& _Pred)
85  : _base(new BaseType(_Pred, allocator_type()))
86  {
87  for (; _First != _Last; ++_First)
88  this->insert(*_First);
89  }
90 
91  /// Constructs a map containing values in the range [_First, _Last). Creation of the new map is only guaranteed to succeed if the iterators start and finish return values of type pair<class Key, class Value> and all values of Key in the range [_First, _Last) are unique. The map uses the predicate _Pred to order keys, and the allocator _Alloc for all storage management.
92  template<class _Iter>
93  shared_map(_Iter _First, _Iter _Last, const key_compare& _Pred, const allocator_type& _Al)
94  : _base(new BaseType(_Pred, _Al))
95  {
96  for (; _First != _Last; ++_First)
97  this->insert(*_First);
98  }
99 
101 
102  /// Returns a copy of the allocator used by self for storage management.
104  {return _base->get_allocator();}
105 
106  /// Returns an iterator pointing to the first element stored in the map. First is defined by the map's comparison operator, Compare.
108  {return _base->begin();}
109 
110  /// Returns a const_iterator pointing to the first element stored in the map.
112  {return _base->begin();}
113 
114  /// Returns an iterator pointing to the last element stored in the map; in other words, to the off-the-end value.
116  {return _base->end();}
117 
118  /// Returns a const_iterator pointing to the last element stored in the map.
120  {return _base->end();}
121 
122  /// Returns a reverse_iterator pointing to the first element stored in the map. First is defined by the map's comparison operator, Compare.
124  {return _base->rbegin();}
125 
126  /// Returns a const_reverse_iterator pointing to the first element stored in the map.
128  {return _base->rbegin();}
129 
130  /// Returns a reverse_iterator pointing to the last element stored in the map; in other words, to the off-the-end value).
132  {return _base->rend();}
133 
134  /// Returns a const_reverse_iterator pointing to the last element stored in the map.
136  {return _base->rend();}
137 
138  /// Replaces the contents of *this with a copy of the map x.
140  {_base = x._base; return *this;}
141 
142  /// If an element with the key x exists in the map, then a reference to its associated value is returned. Otherwise the pair x,T() is inserted into the map and a reference to the default object T() is returned.
144  {return (*_base)[x];}
145 
146  /// Erases all elements from the self.
147  void clear()
148  {_base->clear();}
149 
150  /// Returns a 1 if a value with the key x exists in the map. Otherwise returns a 0.
151  size_type count(const key_type& x) const
152  {return _base->count(x);}
153 
154  /// Returns true if the map is empty, false otherwise.
155  bool empty() const
156  {return _base->empty();}
157 
158  /// Returns the pair (lower_bound(x), upper_bound(x)).
159  std::pair<iterator, iterator> equal_range(const key_type& x)
160  {return _base->equal_range(x);}
161 
162  /// Returns the pair (lower_bound(x), upper_bound(x)).
163  std::pair<const_iterator,const_iterator> equal_range(const key_type& x) const
164  {return _base->equal_range(x);}
165 
166  /// Deletes the map element pointed to by the iterator position.
168  {_base->erase(position);}
169 
170  /// If the iterators start and finish point to the same map and last is reachable from first, all elements in the range [start, finish) are deleted from the map.
171  void erase(iterator start, iterator finish)
172  {_base->erase(start, finish);}
173 
174  /// Deletes the element with the key value x from the map, if one exists. Returns 1 if x existed in the map, 0 otherwise.
176  {return _base->erase(x);}
177 
178  /// Searches the map for a pair with the key value x and returns an iterator to that pair if it is found. If such a pair is not found the value end() is returned.
180  {return _base->find(x);}
181 
182  /// Same as find above but returns a const_iterator.
184  {return _base->find(x);}
185 
186  /// If a value_type with the same key as x is not present in the map, then x is inserted into the map. Otherwise, the pair is not inserted. A position may be supplied as a hint regarding where to do the insertion. If the insertion is done right after position, then it takes amortized constant time. Otherwise it takes O(log N) time.
187  std::pair<iterator, bool> insert(const value_type& x)
188  {return _base->insert(x);}
189 
190  /// If a value_type with the same key as x is not present in the map, then x is inserted into the map. Otherwise, the pair is not inserted. A position may be supplied as a hint regarding where to do the insertion. If the insertion is done right after position, then it takes amortized constant time. Otherwise it takes O(log N) time.
192  {return _base->insert(position, x);}
193 
194  /// Copies of each element in the range [start, finish) that possess a unique key (one not already in the map) are inserted into the map. The iterators start and finish must return values of type pair<T1,T2>. This operation takes approximately O(N*log(size()+N)) time.
195  template <class InputIterator>
196  void insert(InputIterator start, InputIterator finish)
197  {_base->insert(start, finish);}
198 
199  /// Returns a function object capable of comparing key values using the comparison operation, Compare, of the current map.
201  {return _base->key_comp();}
202 
203  /// Returns a reference to the first entry with a key greater than or equal to x.
205  {return _base->lower_bound(x);}
206 
207  /// Same as lower_bound above but returns a const_iterator.
209  {return _base->lower_bound(x);}
210 
211  /// Returns the maximum possible size of the map. This size is only constrained by the number of unique keys that can be represented by the type Key.
213  {return _base->max_size();}
214 
215  /// Returns the number of elements in the map.
216  size_type size() const
217  {return _base->size();}
218 
219  /// Swaps the contents of the map x with the current map, *this.
221  {_base->swap(x._base);}
222 
223  /// Returns an iterator for the first entry with a key greater than x.
225  {return _base->upper_bound(x);}
226 
227  /// Same as upper_bound above, but returns a const_iterator.
229  {return _base->upper_bound(x);}
230 
231  /// Returns a function object capable of comparing pair<const Key, T> values using the comparison operation, Compare, of the current map. This function is identical to key_comp for sets.
233  {return _base->value_comp();}
234 };
235 
236 
237 } // namespace util
238 } // namespace pwiz
239 
240 #endif // _SHARED_MAP_HPP_