OpenMS  2.4.0
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-2018.
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 #pragma once
36 
38 #include <OpenMS/config.h>
39 
40 #include <map>
41 
42 namespace OpenMS
43 {
49  template <class Key, class T>
50  class Map :
51  private std::map<Key, T>
52  {
53 public:
54 
63  class IllegalKey :
65  {
66 public:
67  IllegalKey(const char* file, int line, const char* function) :
68  Exception::BaseException(file, line, function)
69  {
70  }
71 
72  };
73 
75 
76  typedef std::map<Key, T> Base;
77  typedef typename Base::value_type ValueType;
78  typedef Key KeyType;
79  typedef typename Base::value_type* PointerType;
80  typedef typename Base::iterator Iterator;
81  typedef typename Base::const_iterator ConstIterator;
82  typedef typename Base::reverse_iterator ReverseIterator;
83  typedef typename Base::const_reverse_iterator ConstReverseIterator;
85 
87 
88  using Base::erase;
89  using Base::size;
90  using Base::begin;
91  using Base::rbegin;
92  using Base::end;
93  using Base::rend;
94  using Base::clear;
95  using Base::insert;
96  using Base::find;
97  using Base::empty;
98  using Base::count;
99 
100  using typename Base::iterator;
101  using typename Base::const_iterator;
102 
103  using typename Base::mapped_type;
104  using typename Base::value_type;
106 
108  inline bool has(const Key& key) const
109  {
110  return Base::find(key) != Base::end();
111  }
112 
118  const T& operator[](const Key& key) const;
119 
121  T& operator[](const Key& key);
122 
123 
124  inline bool equals(const Map<Key, T>& other) const
125  {
126  return operator==(
127  static_cast<typename Map<Key, T>::Base>(*this),
128  static_cast<typename Map<Key, T>::Base>(other)
129  );
130  }
131 
132  };
133 
134  //******************************************************************************************
135  // Implementations of template methods
136  //******************************************************************************************
137 
138  template <class Key, class T>
139  const T& Map<Key, T>::operator[](const Key& key) const
140  {
141  ConstIterator it = this->find(key);
142  if (it == Base::end())
143  {
144  throw IllegalKey(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
145  }
146  else
147  {
148  return it->second;
149  }
150  }
151 
152  template <class Key, class T>
153  T& Map<Key, T>::operator[](const Key& key)
154  {
155  Iterator it = this->find(key);
156  if (it == Base::end())
157  {
158  it = this->insert(ValueType(key, T())).first;
159  }
160  return it->second;
161  }
162 
163 } // namespace OPENMS
164 
Base::value_type * PointerType
Definition: Map.h:79
Base::iterator Iterator
Definition: Map.h:80
Base::const_reverse_iterator ConstReverseIterator
Definition: Map.h:83
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806
Base::reverse_iterator ReverseIterator
Definition: Map.h:82
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Map illegal key exception.
Definition: Map.h:63
bool find(TFinder &finder, const Pattern< TNeedle, FuzzyAC > &me, PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:884
Key KeyType
Definition: Map.h:78
bool has(const Key &key) const
Test whether the map contains the given key.
Definition: Map.h:108
Exception base class.
Definition: Exception.h:89
Base::const_iterator ConstIterator
Definition: Map.h:81
std::map< Key, T > Base
Definition: Map.h:76
IllegalKey(const char *file, int line, const char *function)
Definition: Map.h:67
BaseException() noexcept
Default constructor.
bool equals(const Map< Key, T > &other) const
Definition: Map.h:124
const T & operator[](const Key &key) const
Return a constant reference to the element whose key is key.
Definition: Map.h:139
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:50
Base::value_type ValueType
Definition: Map.h:77