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)
76  : _base(new BaseType(key_compare(), allocator_type()))
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.
103  allocator_type get_allocator() const
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.
107  iterator begin()
108  {return _base->begin();}
109 
110  /// Returns a const_iterator pointing to the first element stored in the map.
111  const_iterator begin() const
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.
115  iterator end()
116  {return _base->end();}
117 
118  /// Returns a const_iterator pointing to the last element stored in the map.
119  const_iterator end() const
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.
123  reverse_iterator rbegin()
124  {return _base->rbegin();}
125 
126  /// Returns a const_reverse_iterator pointing to the first element stored in the map.
127  const_reverse_iterator rbegin() const
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).
131  reverse_iterator rend()
132  {return _base->rend();}
133 
134  /// Returns a const_reverse_iterator pointing to the last element stored in the map.
135  const_reverse_iterator rend() const
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.
143  mapped_type& operator[](const key_type& x)
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.
167  void erase(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.
175  size_type erase(const key_type& x)
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.
179  iterator find(const key_type& x)
180  {return _base->find(x);}
181 
182  /// Same as find above but returns a const_iterator.
183  const_iterator find(const key_type& x) const
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.
191  iterator insert(iterator position, const value_type& x)
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.
200  key_compare key_comp() const
201  {return _base->key_comp();}
202 
203  /// Returns a reference to the first entry with a key greater than or equal to x.
204  iterator lower_bound(const key_type& x)
205  {return _base->lower_bound(x);}
206 
207  /// Same as lower_bound above but returns a const_iterator.
208  const_iterator lower_bound(const key_type& x) const
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.
212  size_type max_size() const
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.
224  iterator upper_bound(const key_type& x)
225  {return _base->upper_bound(x);}
226 
227  /// Same as upper_bound above, but returns a const_iterator.
228  const_iterator upper_bound(const key_type& x) const
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.
232  value_compare value_comp() const
233  {return _base->value_comp();}
234 };
235 
236 
237 } // namespace util
238 } // namespace pwiz
239 
240 #endif // _SHARED_MAP_HPP_
iterator insert(iterator position, const value_type &x)
If a value_type with the same key as x is not present in the map, then x is inserted into the map...
Definition: shared_map.hpp:191
void swap(shared_map< keyT, valueT, key_compare, allocator_type > &x)
Swaps the contents of the map x with the current map, *this.
Definition: shared_map.hpp:220
std::pair< iterator, iterator > equal_range(const key_type &x)
Returns the pair (lower_bound(x), upper_bound(x)).
Definition: shared_map.hpp:159
shared_map(_Iter _First, _Iter _Last, const key_compare &_Pred)
Constructs a map containing values in the range [_First, _Last). Creation of the new map is only guar...
Definition: shared_map.hpp:84
size_type erase(const key_type &x)
Deletes the element with the key value x from the map, if one exists. Returns 1 if x existed in the m...
Definition: shared_map.hpp:175
std::pair< const_iterator, const_iterator > equal_range(const key_type &x) const
Returns the pair (lower_bound(x), upper_bound(x)).
Definition: shared_map.hpp:163
const_reverse_iterator rbegin() const
Returns a const_reverse_iterator pointing to the first element stored in the map. ...
Definition: shared_map.hpp:127
BaseType::const_reverse_iterator const_reverse_iterator
Definition: shared_map.hpp:60
BaseType::reverse_iterator reverse_iterator
Definition: shared_map.hpp:59
BaseType::const_pointer const_pointer
Definition: shared_map.hpp:54
reverse_iterator rbegin()
Returns a reverse_iterator pointing to the first element stored in the map. First is defined by the m...
Definition: shared_map.hpp:123
std::map< keyT, valueT, _Pr, _Alloc > BaseType
Definition: shared_map.hpp:44
BaseType::pointer pointer
Definition: shared_map.hpp:53
shared_map< keyT, valueT, key_compare, allocator_type > & operator=(const shared_map< keyT, valueT, key_compare, allocator_type > &x)
Replaces the contents of *this with a copy of the map x.
Definition: shared_map.hpp:139
const_iterator begin() const
Returns a const_iterator pointing to the first element stored in the map.
Definition: shared_map.hpp:111
bool empty() const
Returns true if the map is empty, false otherwise.
Definition: shared_map.hpp:155
value_compare value_comp() const
Returns a function object capable of comparing pair<const Key, T> values using the comparison operati...
Definition: shared_map.hpp:232
BaseType::value_compare value_compare
Definition: shared_map.hpp:49
shared_map(_Iter _First, _Iter _Last, const key_compare &_Pred, const allocator_type &_Al)
Constructs a map containing values in the range [_First, _Last). Creation of the new map is only guar...
Definition: shared_map.hpp:93
iterator find(const key_type &x)
Searches the map for a pair with the key value x and returns an iterator to that pair if it is found...
Definition: shared_map.hpp:179
BaseType::key_type key_type
Definition: shared_map.hpp:46
allocator_type get_allocator() const
Returns a copy of the allocator used by self for storage management.
Definition: shared_map.hpp:103
BaseType::value_type value_type
Definition: shared_map.hpp:47
size_type size() const
Returns the number of elements in the map.
Definition: shared_map.hpp:216
const_iterator lower_bound(const key_type &x) const
Same as lower_bound above but returns a const_iterator.
Definition: shared_map.hpp:208
size_type count(const key_type &x) const
Returns a 1 if a value with the key x exists in the map. Otherwise returns a 0.
Definition: shared_map.hpp:151
reverse_iterator rend()
Returns a reverse_iterator pointing to the last element stored in the map; in other words...
Definition: shared_map.hpp:131
key_compare key_comp() const
Returns a function object capable of comparing key values using the comparison operation, Compare, of the current map.
Definition: shared_map.hpp:200
shared_map(_Iter _First, _Iter _Last)
Constructs a map containing values in the range [_First, _Last). Creation of the new map is only guar...
Definition: shared_map.hpp:75
iterator lower_bound(const key_type &x)
Returns a reference to the first entry with a key greater than or equal to x.
Definition: shared_map.hpp:204
void erase(iterator position)
Deletes the map element pointed to by the iterator position.
Definition: shared_map.hpp:167
void insert(InputIterator start, InputIterator finish)
Copies of each element in the range [start, finish) that possess a unique key (one not already in the...
Definition: shared_map.hpp:196
mapped_type & operator[](const key_type &x)
If an element with the key x exists in the map, then a reference to its associated value is returned...
Definition: shared_map.hpp:143
size_type max_size() const
Returns the maximum possible size of the map. This size is only constrained by the number of unique k...
Definition: shared_map.hpp:212
const_iterator end() const
Returns a const_iterator pointing to the last element stored in the map.
Definition: shared_map.hpp:119
const_reverse_iterator rend() const
Returns a const_reverse_iterator pointing to the last element stored in the map.
Definition: shared_map.hpp:135
BaseType::const_reference const_reference
Definition: shared_map.hpp:56
BaseType::size_type size_type
Definition: shared_map.hpp:50
BaseType::key_compare key_compare
Definition: shared_map.hpp:48
BaseType::iterator iterator
Definition: shared_map.hpp:57
BaseType::reference reference
Definition: shared_map.hpp:55
const_iterator upper_bound(const key_type &x) const
Same as upper_bound above, but returns a const_iterator.
Definition: shared_map.hpp:228
iterator begin()
Returns an iterator pointing to the first element stored in the map. First is defined by the map&#39;s co...
Definition: shared_map.hpp:107
BaseType::difference_type difference_type
Definition: shared_map.hpp:52
void clear()
Erases all elements from the self.
Definition: shared_map.hpp:147
iterator end()
Returns an iterator pointing to the last element stored in the map; in other words, to the off-the-end value.
Definition: shared_map.hpp:115
PWIZ_API_DECL Position position(CVID cvid=CVID_Unknown)
returns a Position corresponding to one of the following CVIDs: CVID_Unknown: Position::Anywhere MS_m...
void erase(iterator start, iterator finish)
If the iterators start and finish point to the same map and last is reachable from first...
Definition: shared_map.hpp:171
copy semantics of shared_ptr<map<K,V> > with a std::map interface
Definition: shared_map.hpp:40
BaseType::mapped_type mapped_type
Definition: shared_map.hpp:51
std::pair< iterator, bool > insert(const value_type &x)
If a value_type with the same key as x is not present in the map, then x is inserted into the map...
Definition: shared_map.hpp:187
KernelTraitsBase< Kernel >::space_type::abscissa_type x
BaseType::allocator_type allocator_type
Definition: shared_map.hpp:45
shared_map(const key_compare &predicate=key_compare(), const allocator_type &allocator=allocator_type())
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.
Definition: shared_map.hpp:68
BaseType::const_iterator const_iterator
Definition: shared_map.hpp:58
const_iterator find(const key_type &x) const
Same as find above but returns a const_iterator.
Definition: shared_map.hpp:183
iterator upper_bound(const key_type &x)
Returns an iterator for the first entry with a key greater than x.
Definition: shared_map.hpp:224
boost::shared_ptr< BaseType > _base
Definition: shared_map.hpp:63