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 class String;
24
44 class OPENMS_DLLAPI MetaInfo
45 {
46public:
48 MetaInfo() = default;
49
51 MetaInfo(const MetaInfo&) = default;
52
54 MetaInfo(MetaInfo&&) = default;
55
58
60 MetaInfo& operator=(const MetaInfo&) = default;
62 MetaInfo& operator=(MetaInfo&&) & = default;
63
65 bool operator==(const MetaInfo& rhs) const;
67 bool operator!=(const MetaInfo& rhs) const;
68
72 {
73 // index_to_value_.insert(rhs.index_to_value_.begin(), rhs.index_to_value_.end()); // does not overwrite existing data
74 for (const auto& kv : rhs.index_to_value_)
75 {
76 index_to_value_[kv.first] = kv.second;
77 }
78 return *this;
79 }
80
82 const DataValue& getValue(const String& name, const DataValue& default_value = DataValue::EMPTY) const;
84 const DataValue& getValue(UInt index, const DataValue& default_value = DataValue::EMPTY) const;
85
87 bool exists(const String& name) const;
89 bool exists(UInt index) const;
90
92 void setValue(const String& name, const DataValue& value);
94 void setValue(UInt index, const DataValue& value);
95
97 void removeValue(const String& name);
99 void removeValue(UInt index);
100
103
105 void getKeys(std::vector<String>& keys) const;
106
108 void getKeys(std::vector<UInt>& keys) const;
109
111 bool empty() const;
112
114 void clear();
115
116private:
117 using MapType = boost::container::flat_map<UInt, DataValue>;
118
121
124
125 // Grant access to hash implementation
126 friend struct std::hash<MetaInfo>;
127 };
128
129} // namespace OpenMS
130
131// Hash function specialization for MetaInfo
132namespace std
133{
134 template<>
135 struct hash<OpenMS::MetaInfo>
136 {
137 std::size_t operator()(const OpenMS::MetaInfo& mi) const noexcept
138 {
139 std::size_t seed = 0;
140 // Hash each key-value pair in sorted order (flat_map is already sorted by key)
141 for (const auto& [key, value] : mi.index_to_value_)
142 {
144 OpenMS::hash_combine(seed, std::hash<OpenMS::DataValue>{}(value));
145 }
146 return seed;
147 }
148 };
149} // namespace std
150
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:34
Registry which assigns unique integer indices to strings.
Definition MetaInfoRegistry.h:50
A Type-Name-Value tuple class.
Definition MetaInfo.h:45
void setValue(UInt index, const DataValue &value)
Sets the DataValue corresponding to an index.
void removeValue(const String &name)
Removes the DataValue corresponding to name if it exists.
MetaInfo(const MetaInfo &)=default
Copy constructor.
bool operator!=(const MetaInfo &rhs) const
Equality operator.
~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.
bool exists(const String &name) const
Returns whether an entry with the given name exists.
MetaInfo & operator=(const MetaInfo &)=default
Assignment operator.
MapType index_to_value_
The actual mapping of indexes to values.
Definition MetaInfo.h:123
const DataValue & getValue(const 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...
void getKeys(std::vector< String > &keys) const
Fills the given vector with a list of all keys for which a value is set.
MetaInfo & operator+=(const MetaInfo &rhs)
Definition MetaInfo.h:71
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
Definition MetaInfo.h:117
MetaInfo()=default
Constructor.
void setValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
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:120
void clear()
Removes all meta values.
bool exists(UInt index) const
Returns whether an entry with the given index exists.
A more convenient string class.
Definition String.h:34
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
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:137