ProteoWizard
virtual_map.hpp
Go to the documentation of this file.
1 //
2 // $Id: virtual_map.hpp 1195 2009-08-14 22:12:04Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2008 Spielberg Family Center for Applied Proteomics
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 // Copyright 2008 Vanderbilt University - Nashville, TN 37232
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // Unless required by applicable law or agreed to in writing, software
18 // distributed under the License is distributed on an "AS IS" BASIS,
19 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 // See the License for the specific language governing permissions and
21 // limitations under the License.
22 //
23 
24 
25 #ifndef _VIRTUAL_MAP_HPP_
26 #define _VIRTUAL_MAP_HPP_
27 
28 #include <map>
29 
30 namespace pwiz {
31 namespace util {
32 
33 /// a wrapper for std::map that will behave properly with polymorphism
34 template<class keyT,
35  class valueT,
36  class _Pr = std::less<keyT>,
37  class _Alloc = std::allocator<std::pair<const keyT, valueT> > >
39 {
40  public:
41 
42  typedef std::map<keyT, valueT, _Pr, _Alloc> BaseType;
43  typedef typename BaseType::allocator_type allocator_type;
44  typedef typename BaseType::key_type key_type;
45  typedef typename BaseType::value_type value_type;
46  typedef typename BaseType::key_compare key_compare;
47  typedef typename BaseType::value_compare value_compare;
48  typedef typename BaseType::size_type size_type;
49  typedef typename BaseType::mapped_type mapped_type;
50  typedef typename BaseType::difference_type difference_type;
51  typedef typename BaseType::pointer pointer;
52  typedef typename BaseType::const_pointer const_pointer;
53  typedef typename BaseType::reference reference;
54  typedef typename BaseType::const_reference const_reference;
55  typedef typename BaseType::iterator iterator;
56  typedef typename BaseType::const_iterator const_iterator;
57  typedef typename BaseType::reverse_iterator reverse_iterator;
58  typedef typename BaseType::const_reverse_iterator const_reverse_iterator;
59 
60  private:
62 
63  public:
64 
65  /// 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.
66  explicit virtual_map(const key_compare& predicate = key_compare(), const allocator_type& allocator = allocator_type())
67  : _base(predicate, allocator)
68  {
69  }
70 
71  /// 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.
72  template<class _Iter>
73  virtual_map(_Iter _First, _Iter _Last)
75  {
76  for (; _First != _Last; ++_First)
77  this->insert(*_First);
78  }
79 
80  /// 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.
81  template<class _Iter>
82  virtual_map(_Iter _First, _Iter _Last, const key_compare& _Pred)
83  : _base(_Pred, allocator_type())
84  {
85  for (; _First != _Last; ++_First)
86  this->insert(*_First);
87  }
88 
89  /// 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.
90  template<class _Iter>
91  virtual_map(_Iter _First, _Iter _Last, const key_compare& _Pred, const allocator_type& _Al)
92  : _base(_Pred, _Al)
93  {
94  for (; _First != _Last; ++_First)
95  this->insert(*_First);
96  }
97 
98  virtual ~virtual_map() {}
99 
100  /// Returns a copy of the allocator used by self for storage management.
102  {return _base.get_allocator();}
103 
104  /// Returns an iterator pointing to the first element stored in the map. First is defined by the map's comparison operator, Compare.
105  virtual iterator begin()
106  {return _base.begin();}
107 
108  /// Returns a const_iterator pointing to the first element stored in the map.
109  virtual const_iterator begin() const
110  {return _base.begin();}
111 
112  /// Returns an iterator pointing to the last element stored in the map; in other words, to the off-the-end value.
113  virtual iterator end()
114  {return _base.end();}
115 
116  /// Returns a const_iterator pointing to the last element stored in the map.
117  virtual const_iterator end() const
118  {return _base.end();}
119 
120  /// Returns a reverse_iterator pointing to the first element stored in the map. First is defined by the map's comparison operator, Compare.
122  {return _base.rbegin();}
123 
124  /// Returns a const_reverse_iterator pointing to the first element stored in the map.
126  {return _base.rbegin();}
127 
128  /// Returns a reverse_iterator pointing to the last element stored in the map; in other words, to the off-the-end value).
130  {return _base.rend();}
131 
132  /// Returns a const_reverse_iterator pointing to the last element stored in the map.
133  virtual const_reverse_iterator rend() const
134  {return _base.rend();}
135 
136  /// Replaces the contents of *this with a copy of the map x.
138  {_base = x._base; return *this;}
139 
140  /// 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.
141  virtual mapped_type& operator[](const key_type& x)
142  {return _base[x];}
143 
144  /// Erases all elements from the self.
145  virtual void clear()
146  {_base.clear();}
147 
148  /// Returns a 1 if a value with the key x exists in the map. Otherwise returns a 0.
149  virtual size_type count(const key_type& x) const
150  {return _base.count(x);}
151 
152  /// Returns true if the map is empty, false otherwise.
153  virtual bool empty() const
154  {return _base.empty();}
155 
156  /// Returns the pair (lower_bound(x), upper_bound(x)).
157  virtual std::pair<iterator, iterator> equal_range(const key_type& x)
158  {return _base.equal_range(x);}
159 
160  /// Returns the pair (lower_bound(x), upper_bound(x)).
161  virtual std::pair<const_iterator,const_iterator> equal_range(const key_type& x) const
162  {return _base.equal_range(x);}
163 
164  /// Deletes the map element pointed to by the iterator position.
165  virtual void erase(iterator position)
166  {_base.erase(position);}
167 
168  /// 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.
169  virtual void erase(iterator start, iterator finish)
170  {_base.erase(start, finish);}
171 
172  /// Deletes the element with the key value x from the map, if one exists. Returns 1 if x existed in the map, 0 otherwise.
173  virtual size_type erase(const key_type& x)
174  {return _base.erase(x);}
175 
176  /// 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.
177  virtual iterator find(const key_type& x)
178  {return _base.find(x);}
179 
180  /// Same as find above but returns a const_iterator.
181  virtual const_iterator find(const key_type& x) const
182  {return _base.find(x);}
183 
184  /// 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.
185  virtual std::pair<iterator, bool> insert(const value_type& x)
186  {return _base.insert(x);}
187 
188  /// 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.
190  {return _base.insert(position, x);}
191 
192  /// 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.
193  template <class InputIterator>
194  void insert(InputIterator start, InputIterator finish)
195  {_base.insert(start, finish);}
196 
197  /// Returns a function object capable of comparing key values using the comparison operation, Compare, of the current map.
198  virtual key_compare key_comp() const
199  {return _base.key_comp();}
200 
201  /// Returns a reference to the first entry with a key greater than or equal to x.
202  virtual iterator lower_bound(const key_type& x)
203  {return _base.lower_bound(x);}
204 
205  /// Same as lower_bound above but returns a const_iterator.
206  virtual const_iterator lower_bound(const key_type& x) const
207  {return _base.lower_bound(x);}
208 
209  /// 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.
210  virtual size_type max_size() const
211  {return _base.max_size();}
212 
213  /// Returns the number of elements in the map.
214  virtual size_type size() const
215  {return _base.size();}
216 
217  /// Swaps the contents of the map x with the current map, *this.
219  {_base.swap(x._base);}
220 
221  /// Returns an iterator for the first entry with a key greater than x.
222  virtual iterator upper_bound(const key_type& x)
223  {return _base.upper_bound(x);}
224 
225  /// Same as upper_bound above, but returns a const_iterator.
226  virtual const_iterator upper_bound(const key_type& x) const
227  {return _base.upper_bound(x);}
228 
229  /// 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.
230  virtual value_compare value_comp() const
231  {return _base.value_comp();}
232 };
233 
234 
235 } // namespace util
236 } // namespace pwiz
237 
238 #endif // _VIRTUAL_MAP_HPP_