OpenMS
Loading...
Searching...
No Matches
DataValue.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
12
15#include <OpenMS/OpenMSConfig.h>
16
17#include <cmath>
18
19namespace OpenMS
20{
21 class ParamValue;
22
32 class OPENMS_DLLAPI DataValue
33 {
34
35public:
36
38 static const DataValue EMPTY;
39
52
54 static const std::string NamesOfDataType[SIZE_OF_DATATYPE];
55
57 enum UnitType : unsigned char
58 {
61 OTHER
62 };
63
65
66
71 DataValue(DataValue&&) noexcept;
73 DataValue(const char*);
75 DataValue(const std::string&);
83 DataValue(long double);
85 DataValue(double);
87 DataValue(float);
89 DataValue(short int);
91 DataValue(unsigned short int);
95 DataValue(unsigned);
97 DataValue(long int);
99 DataValue(unsigned long);
101 DataValue(long long);
103 DataValue(unsigned long long);
109
113
114
118 operator ParamValue() const;
119
136 operator std::string() const;
137
143 operator StringList() const;
144
150 operator IntList() const;
151
157 operator DoubleList() const;
158
166 operator long double() const;
167
175 operator double() const;
176
184 operator float() const;
185
193 operator short int() const;
194
202 operator unsigned short int() const;
203
212 operator int() const;
213
221 operator unsigned int() const;
222
230 operator long int() const;
231
239 operator unsigned long int() const;
240
248 operator long long() const;
249
257 operator unsigned long long() const;
258
266 bool toBool() const;
267
274 const char* toChar() const;
275
281 StringList toStringList() const;
282
288 IntList toIntList() const;
289
295 DoubleList toDoubleList() const;
297
300
301
302 DataValue& operator=(const DataValue&);
304 DataValue& operator=(DataValue&&) noexcept;
306 DataValue& operator=(const char*);
308 DataValue& operator=(const std::string&);
310 DataValue& operator=(const StringList&);
312 DataValue& operator=(const IntList&);
314 DataValue& operator=(const DoubleList&);
316 DataValue& operator=(const long double);
318 DataValue& operator=(const double);
320 DataValue& operator=(const float);
322 DataValue& operator=(const short int);
324 DataValue& operator=(const unsigned short int);
326 DataValue& operator=(const int);
328 DataValue& operator=(const unsigned);
330 DataValue& operator=(const long int);
332 DataValue& operator=(const unsigned long);
334 DataValue& operator=(const long long);
336 DataValue& operator=(const unsigned long long);
338
342
343
348 std::string toString(bool full_precision = true) const;
349
351
353 DataType valueType() const;
354
360 bool isEmpty() const;
361
364
365
367 UnitType getUnitType() const;
368
369 void setUnitType(const UnitType & u);
370
372 bool hasUnit() const;
373
375 const int32_t & getUnit() const;
376
378 void setUnit(const int32_t & unit);
379
381
383 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream&, const DataValue&);
384
386 friend OPENMS_DLLAPI bool operator==(const DataValue&, const DataValue&);
387
389 friend OPENMS_DLLAPI bool operator<(const DataValue&, const DataValue&);
390
392 friend OPENMS_DLLAPI bool operator>(const DataValue&, const DataValue&);
393
395 friend OPENMS_DLLAPI bool operator!=(const DataValue&, const DataValue&);
396
397protected:
398
400 DataType value_type_;
401
403 UnitType unit_type_;
404
406 int32_t unit_;
407
409 union
410 {
411 SignedSize ssize_;
412 double dou_;
413 std::string* str_;
414 StringList* str_list_;
415 IntList* int_list_;
416 DoubleList* dou_list_;
417 } data_;
418
419private:
420
422 void clear_() noexcept;
423 };
424} // namespace OpenMS
425
426// Hash function specialization for DataValue
427namespace std
428{
439 template<>
440 struct hash<OpenMS::DataValue>
441 {
442 std::size_t operator()(const OpenMS::DataValue& dv) const noexcept
443 {
444 std::size_t seed = 0;
445
446 // Hash the value type
447 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.valueType())));
448
449 // Hash the value content based on type
450 if (!dv.isEmpty())
451 {
452 switch (dv.valueType())
453 {
455 {
456 // operator== uses fabs(a - b) < 1e-6 for doubles, so we round to 6 decimal places
457 // to ensure equal values (per operator==) produce identical hashes
458 double val = static_cast<double>(dv);
459 // Round to 6 decimal places: multiply by 1e6, round, then hash as int64
460 int64_t rounded = static_cast<int64_t>(std::round(val * 1e6));
462 break;
463 }
464 default:
465 // For all other types (string, int, lists), use toString() which is consistent with operator==
467 break;
468 }
469 }
470
471 // Hash unit information if present
472 if (dv.hasUnit())
473 {
474 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.getUnitType())));
475 OpenMS::hash_combine(seed, OpenMS::hash_int(dv.getUnit()));
476 }
477
478 return seed;
479 }
480 };
481} // namespace std
482
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:33
static const DataValue EMPTY
Empty data value for comparisons.
Definition DataValue.h:38
UnitType
Supported types for DataValue.
Definition DataValue.h:58
@ MS_ONTOLOGY
ms.ontology MS
Definition DataValue.h:60
@ UNIT_ONTOLOGY
unit.ontology UO
Definition DataValue.h:59
DataValue(const DataValue &)
Copy constructor.
DataType
Supported types for DataValue.
Definition DataValue.h:42
@ DOUBLE_VALUE
double value
Definition DataValue.h:45
@ EMPTY_VALUE
empty
Definition DataValue.h:49
@ INT_LIST
integer list
Definition DataValue.h:47
@ STRING_VALUE
string value
Definition DataValue.h:43
@ STRING_LIST
string list
Definition DataValue.h:46
@ INT_VALUE
integer value
Definition DataValue.h:44
@ DOUBLE_LIST
double list
Definition DataValue.h:48
void clear_() noexcept
Clears the current state of the DataValue and release every used memory.
DataValue(DataValue &&) noexcept
Move constructor.
DataValue()
Default constructor.
Class to hold strings, numeric values, vectors of strings and vectors of numeric values using the stl...
Definition ParamValue.h:31
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition Types.h:104
std::vector< Int > IntList
Vector of signed integers.
Definition TypeAliases.h:24
std::vector< double > DoubleList
Vector of double precision real types.
Definition TypeAliases.h:31
std::vector< std::string > StringList
Vector of String.
Definition TypeAliases.h:39
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::DataValue &dv) const noexcept
Definition DataValue.h:442