Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Map.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2017.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, 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 any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_DATASTRUCTURES_MAP_H
36 #define OPENMS_DATASTRUCTURES_MAP_H
37 
39 #include <OpenMS/config.h>
40 
41 #include <map>
42 
43 namespace OpenMS
44 {
50  template <class Key, class T>
51  class Map :
52  private std::map<Key, T>
53  {
54 public:
55 
64  class IllegalKey :
66  {
67 public:
68  IllegalKey(const char* file, int line, const char* function) :
69  Exception::BaseException(file, line, function)
70  {
71  }
72 
73  };
74 
76 
77  typedef std::map<Key, T> Base;
78  typedef typename Base::value_type ValueType;
79  typedef Key KeyType;
80  typedef typename Base::value_type* PointerType;
81  typedef typename Base::iterator Iterator;
82  typedef typename Base::const_iterator ConstIterator;
83  typedef typename Base::reverse_iterator ReverseIterator;
84  typedef typename Base::const_reverse_iterator ConstReverseIterator;
86 
88 
89  using Base::erase;
90  using Base::size;
91  using Base::begin;
92  using Base::rbegin;
93  using Base::end;
94  using Base::rend;
95  using Base::clear;
96  using Base::insert;
97  using Base::find;
98  using Base::empty;
99  using Base::count;
100 
101  using typename Base::iterator;
102  using typename Base::const_iterator;
103 
104  using typename Base::mapped_type;
105  using typename Base::value_type;
107 
109  inline bool has(const Key& key) const
110  {
111  return Base::find(key) != Base::end();
112  }
113 
119  const T& operator[](const Key& key) const;
120 
122  T& operator[](const Key& key);
123 
124 
125  inline bool equals(const Map<Key, T>& other) const
126  {
127  return operator==(
128  static_cast<typename Map<Key, T>::Base>(*this),
129  static_cast<typename Map<Key, T>::Base>(other)
130  );
131  }
132 
133  };
134 
135  //******************************************************************************************
136  // Implementations of template methods
137  //******************************************************************************************
138 
139  template <class Key, class T>
140  const T& Map<Key, T>::operator[](const Key& key) const
141  {
142  ConstIterator it = this->find(key);
143  if (it == Base::end())
144  {
145  throw IllegalKey(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
146  }
147  else
148  {
149  return it->second;
150  }
151  }
152 
153  template <class Key, class T>
154  T& Map<Key, T>::operator[](const Key& key)
155  {
156  Iterator it = this->find(key);
157  if (it == Base::end())
158  {
159  it = this->insert(ValueType(key, T())).first;
160  }
161  return it->second;
162  }
163 
164 } // namespace OPENMS
165 
166 #endif // OPENMS_DATASTRUCTURES_MAP_H
Base::value_type * PointerType
Definition: Map.h:80
Base::iterator Iterator
Definition: Map.h:81
Base::const_reverse_iterator ConstReverseIterator
Definition: Map.h:84
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806
Base::reverse_iterator ReverseIterator
Definition: Map.h:83
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Map illegal key exception.
Definition: Map.h:64
Key KeyType
Definition: Map.h:79
bool has(const Key &key) const
Test whether the map contains the given key.
Definition: Map.h:109
Exception base class.
Definition: Exception.h:90
Base::const_iterator ConstIterator
Definition: Map.h:82
std::map< Key, T > Base
Definition: Map.h:77
IllegalKey(const char *file, int line, const char *function)
Definition: Map.h:68
BaseException() noexcept
Default constructor.
bool equals(const Map< Key, T > &other) const
Definition: Map.h:125
const T & operator[](const Key &key) const
Return a constant reference to the element whose key is key.
Definition: Map.h:140
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:51
Base::value_type ValueType
Definition: Map.h:78

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:01 using doxygen 1.8.13