OpenMS
Loading...
Searching...
No Matches
MetaInfoInterface.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
18namespace OpenMS
19{
20 class String;
21 class MetaInfo;
22
35 class OPENMS_DLLAPI MetaInfoInterface
36 {
37public:
38
40 MetaInfoInterface() = default;
47
49 MetaInfoInterface& operator=(const MetaInfoInterface& rhs);
51 MetaInfoInterface& operator=(MetaInfoInterface&&) noexcept;
52
54 void swap(MetaInfoInterface& rhs);
55
57 bool operator==(const MetaInfoInterface& rhs) const;
59 bool operator!=(const MetaInfoInterface& rhs) const;
60
62 const DataValue& getMetaValue(const String& name) const;
63
65 DataValue getMetaValue(const String& name, const DataValue& default_value) const; // Note: return needs to be by value to prevent life-time issues at caller site (e.g. if he passes a temporary to default-value)
66
68 const DataValue& getMetaValue(UInt index) const;
69
71 DataValue getMetaValue(UInt index, const DataValue& default_value) const; // Note: return needs to be by value to prevent life-time issues at caller site
72
74 bool metaValueExists(const String& name) const;
76 bool metaValueExists(UInt index) const;
77
79 void setMetaValue(const String& name, const DataValue& value);
81 void setMetaValue(UInt index, const DataValue& value);
82
84 void removeMetaValue(const String& name);
86 void removeMetaValue(UInt index);
87
90 void addMetaValues(const MetaInfoInterface& from);
91
93 static MetaInfoRegistry& metaRegistry();
94
96 void getKeys(std::vector<String>& keys) const;
97
99 void getKeys(std::vector<UInt>& keys) const;
100
102 bool isMetaEmpty() const;
103
105 void clearMetaInfo();
106
107protected:
108
110 inline void createIfNotExists_();
111
113 MetaInfo* meta_ = nullptr;
114 };
115
116} // namespace OpenMS
117
118// Hash function specialization for MetaInfoInterface
119namespace std
120{
132 template<>
133 struct hash<OpenMS::MetaInfoInterface>
134 {
135 std::size_t operator()(const OpenMS::MetaInfoInterface& meta) const noexcept
136 {
137 // Use additive accumulation for order-independent hashing (O(n) vs O(n log n))
138 std::size_t hash = 0;
139
140 // Hash String keys (names)
141 std::vector<OpenMS::String> str_keys;
142 meta.getKeys(str_keys);
143 for (const auto& key : str_keys)
144 {
145 std::size_t pair_hash = OpenMS::fnv1a_hash_string(key);
146 OpenMS::hash_combine(pair_hash, std::hash<OpenMS::DataValue>{}(meta.getMetaValue(key)));
147 hash += pair_hash; // Order-independent accumulation
148 }
149
150 // Hash UInt keys (indices) - critical for correctness
151 std::vector<OpenMS::UInt> uint_keys;
152 meta.getKeys(uint_keys);
153 for (const auto& key : uint_keys)
154 {
155 std::size_t pair_hash = OpenMS::hash_int(static_cast<int64_t>(key));
156 OpenMS::hash_combine(pair_hash, std::hash<OpenMS::DataValue>{}(meta.getMetaValue(key)));
157 hash += pair_hash; // Order-independent accumulation
158 }
159
160 return hash;
161 }
162 };
163} // namespace std
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:34
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition MetaInfoInterface.h:36
MetaInfoInterface(MetaInfoInterface &&) noexcept
Move constructor.
MetaInfoInterface()=default
Constructor.
MetaInfoInterface(const MetaInfoInterface &rhs)
Copy constructor.
Registry which assigns unique integer indices to strings.
Definition MetaInfoRegistry.h:50
A Type-Name-Value tuple class.
Definition MetaInfo.h:45
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
std::size_t fnv1a_hash_string(const std::string &s) noexcept
FNV-1a hash for a string.
Definition HashUtils.h:70
STL namespace.
std::size_t operator()(const OpenMS::MetaInfoInterface &meta) const noexcept
Definition MetaInfoInterface.h:135