OpenMS
Loading...
Searching...
No Matches
MetaInfo.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Marc Sturm $
7// --------------------------------------------------------------------------
8
9#pragma once
10
11#include <vector>
12
17
18#include <boost/container/flat_map.hpp>
19#include <functional>
20
21namespace OpenMS
22{
23
43 class OPENMS_DLLAPI MetaInfo
44 {
45public:
47 using MapType = boost::container::flat_map<UInt, DataValue>;
52
54 MetaInfo() = default;
55
57 MetaInfo(const MetaInfo&) = default;
58
60 MetaInfo(MetaInfo&&) = default;
61
64
66 MetaInfo& operator=(const MetaInfo&) = default;
68 MetaInfo& operator=(MetaInfo&&) & = default;
69
71 bool operator==(const MetaInfo& rhs) const;
73 bool operator!=(const MetaInfo& rhs) const;
74
88
90 const DataValue& getValue(const std::string& name, const DataValue& default_value = DataValue::EMPTY) const;
92 const DataValue& getValue(UInt index, const DataValue& default_value = DataValue::EMPTY) const;
93
95 bool exists(const std::string& name) const;
97 bool exists(UInt index) const;
98
100 void setValue(const std::string& name, const DataValue& value);
102 void setValue(UInt index, const DataValue& value);
103
105 void removeValue(const std::string& name);
107 void removeValue(UInt index);
108
111
113 void getKeys(std::vector<std::string>& keys) const;
114
116 void getKeys(std::vector<UInt>& keys) const;
117
119 bool empty() const;
120
122 void clear();
123
130
135 const_iterator begin() const { return index_to_value_.begin(); }
136
141 const_iterator end() const { return index_to_value_.end(); }
142
147 const_iterator cbegin() const { return index_to_value_.cbegin(); }
148
153 const_iterator cend() const { return index_to_value_.cend(); }
154
160 iterator begin() { return index_to_value_.begin(); }
161
166 iterator end() { return index_to_value_.end(); }
168
173 Size size() const { return index_to_value_.size(); }
174
175private:
176
179
182
183 // Grant access to hash implementation
184 friend struct std::hash<MetaInfo>;
185 };
186
187} // namespace OpenMS
188
189// Hash function specialization for MetaInfo
190namespace std
191{
192 template<>
193 struct hash<OpenMS::MetaInfo>
194 {
195 std::size_t operator()(const OpenMS::MetaInfo& mi) const noexcept
196 {
197 std::size_t seed = 0;
198 // Hash each key-value pair in sorted order (flat_map is already sorted by key)
199 for (const auto& [key, value] : mi.index_to_value_)
200 {
202 OpenMS::hash_combine(seed, std::hash<OpenMS::DataValue>{}(value));
203 }
204 return seed;
205 }
206 };
207} // namespace std
208
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:32
Base::iterator iterator
Definition MSExperiment.h:97
Base::const_iterator const_iterator
Definition MSExperiment.h:98
Registry which assigns unique integer indices to strings.
Definition MetaInfoRegistry.h:50
A Type-Name-Value tuple class.
Definition MetaInfo.h:44
void setValue(const std::string &name, const DataValue &value)
Sets the DataValue corresponding to a name.
void setValue(UInt index, const DataValue &value)
Sets the DataValue corresponding to an index.
const_iterator begin() const
Returns a const iterator to the beginning of the meta info entries.
Definition MetaInfo.h:135
void removeValue(const std::string &name)
Removes the DataValue corresponding to name if it exists.
bool exists(const std::string &name) const
Returns whether an entry with the given name exists.
MetaInfo(const MetaInfo &)=default
Copy constructor.
const_iterator cbegin() const
Returns a const iterator to the beginning of the meta info entries.
Definition MetaInfo.h:147
bool operator!=(const MetaInfo &rhs) const
Equality operator.
MapType::const_iterator const_iterator
Const iterator type.
Definition MetaInfo.h:51
~MetaInfo()
Destructor.
MetaInfo & operator=(MetaInfo &&) &=default
Move assignment operator.
static MetaInfoRegistry & registry()
Returns a reference to the MetaInfoRegistry.
bool empty() const
Returns if the MetaInfo is empty.
void removeValue(UInt index)
Removes the DataValue corresponding to index if it exists.
bool operator==(const MetaInfo &rhs) const
Equality operator.
MetaInfo & operator=(const MetaInfo &)=default
Assignment operator.
const DataValue & getValue(const std::string &name, const DataValue &default_value=DataValue::EMPTY) const
Returns the value corresponding to a string, or a default value (default: DataValue::EMPTY) if not fo...
MapType index_to_value_
The actual mapping of indexes to values.
Definition MetaInfo.h:181
const_iterator cend() const
Returns a const iterator to the end of the meta info entries.
Definition MetaInfo.h:153
MetaInfo & operator+=(const MetaInfo &rhs)
Merge another MetaInfo into this one.
void getKeys(std::vector< UInt > &keys) const
Fills the given vector with a list of all keys for which a value is set.
boost::container::flat_map< UInt, DataValue > MapType
Internal map type (UInt key to DataValue)
Definition MetaInfo.h:47
MetaInfo()=default
Constructor.
MetaInfo(MetaInfo &&)=default
Move constructor.
const DataValue & getValue(UInt index, const DataValue &default_value=DataValue::EMPTY) const
Returns the value corresponding to an index, or a default value (default: DataValue::EMPTY) if not fo...
static MetaInfoRegistry registry_
Static MetaInfoRegistry.
Definition MetaInfo.h:178
void clear()
Removes all meta values.
iterator end()
Returns a mutable iterator to the end of the meta info entries.
Definition MetaInfo.h:166
Size size() const
Returns the number of meta value entries.
Definition MetaInfo.h:173
const_iterator end() const
Returns a const iterator to the end of the meta info entries.
Definition MetaInfo.h:141
iterator begin()
Returns a mutable iterator to the beginning of the meta info entries.
Definition MetaInfo.h:160
bool exists(UInt index) const
Returns whether an entry with the given index exists.
void getKeys(std::vector< std::string > &keys) const
Fills the given vector with a list of all keys for which a value is set.
MapType::iterator iterator
Mutable iterator type.
Definition MetaInfo.h:49
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::size_t hash_int(T value) noexcept
Hash for an integer type.
Definition HashUtils.h:107
void hash_combine(std::size_t &seed, std::size_t value) noexcept
Combine a hash value with additional data using golden ratio mixing.
Definition HashUtils.h:87
STL namespace.
std::size_t operator()(const OpenMS::MetaInfo &mi) const noexcept
Definition MetaInfo.h:195