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 <utility>
19#include <vector>
20#include <functional>
21
22namespace OpenMS
23{
24
44 class OPENMS_DLLAPI MetaInfo
45 {
46public:
48 using MapType = std::vector<std::pair<UInt, DataValue>>;
53
55 MetaInfo() = default;
56
58 MetaInfo(const MetaInfo&) = default;
59
61 MetaInfo(MetaInfo&&) = default;
62
65
67 MetaInfo& operator=(const MetaInfo&) = default;
69 MetaInfo& operator=(MetaInfo&&) & = default;
70
72 bool operator==(const MetaInfo& rhs) const;
74 bool operator!=(const MetaInfo& rhs) const;
75
89
91 const DataValue& getValue(const std::string& name, const DataValue& default_value = DataValue::EMPTY) const;
93 const DataValue& getValue(UInt index, const DataValue& default_value = DataValue::EMPTY) const;
94
96 bool exists(const std::string& name) const;
98 bool exists(UInt index) const;
99
101 void setValue(const std::string& name, const DataValue& value);
103 void setValue(UInt index, const DataValue& value);
104
106 void removeValue(const std::string& name);
108 void removeValue(UInt index);
109
112
114 void getKeys(std::vector<std::string>& keys) const;
115
117 void getKeys(std::vector<UInt>& keys) const;
118
120 bool empty() const;
121
123 void clear();
124
131
136 const_iterator begin() const { return index_to_value_.begin(); }
137
142 const_iterator end() const { return index_to_value_.end(); }
143
148 const_iterator cbegin() const { return index_to_value_.cbegin(); }
149
154 const_iterator cend() const { return index_to_value_.cend(); }
155
161 iterator begin() { return index_to_value_.begin(); }
162
167 iterator end() { return index_to_value_.end(); }
169
174 Size size() const { return index_to_value_.size(); }
175
176private:
179
180
183
186
187 // Grant access to hash implementation
188 friend struct std::hash<MetaInfo>;
189 };
190
191} // namespace OpenMS
192
193// Hash function specialization for MetaInfo
194namespace std
195{
196 template<>
197 struct hash<OpenMS::MetaInfo>
198 {
199 std::size_t operator()(const OpenMS::MetaInfo& mi) const noexcept
200 {
201 std::size_t seed = 0;
202 // Hash each key-value pair in sorted order (flat_map is already sorted by key)
203 for (const auto& [key, value] : mi.index_to_value_)
204 {
206 OpenMS::hash_combine(seed, std::hash<OpenMS::DataValue>{}(value));
207 }
208 return seed;
209 }
210 };
211} // namespace std
212
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:33
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:45
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:136
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:148
bool operator!=(const MetaInfo &rhs) const
Equality operator.
MapType::const_iterator const_iterator
Const iterator type.
Definition MetaInfo.h:52
~MetaInfo()
Destructor.
std::vector< std::pair< UInt, DataValue > > MapType
Internal map type (UInt key to DataValue)
Definition MetaInfo.h:48
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:185
const_iterator cend() const
Returns a const iterator to the end of the meta info entries.
Definition MetaInfo.h:154
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.
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:182
void clear()
Removes all meta values.
iterator find_(UInt index)
iterator end()
Returns a mutable iterator to the end of the meta info entries.
Definition MetaInfo.h:167
Size size() const
Returns the number of meta value entries.
Definition MetaInfo.h:174
const_iterator end() const
Returns a const iterator to the end of the meta info entries.
Definition MetaInfo.h:142
iterator begin()
Returns a mutable iterator to the beginning of the meta info entries.
Definition MetaInfo.h:161
bool exists(UInt index) const
Returns whether an entry with the given index exists.
const_iterator find_(UInt index) const
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:50
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:199