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
13
16#include <OpenMS/OpenMSConfig.h>
17
18namespace OpenMS
19{
20 class ParamValue;
21
31 class OPENMS_DLLAPI DataValue
32 {
33
34public:
35
37 static const DataValue EMPTY;
38
51
53 static const std::string NamesOfDataType[SIZE_OF_DATATYPE];
54
56 enum UnitType : unsigned char
57 {
60 OTHER
61 };
62
64
65
70 DataValue(DataValue&&) noexcept;
72 DataValue(const char*);
74 DataValue(const std::string&);
82 DataValue(long double);
84 DataValue(double);
86 DataValue(float);
88 DataValue(short int);
90 DataValue(unsigned short int);
94 DataValue(unsigned);
96 DataValue(long int);
98 DataValue(unsigned long);
100 DataValue(long long);
102 DataValue(unsigned long long);
108
112
113
117 operator ParamValue() const;
118
135 operator std::string() const;
136
142 operator StringList() const;
143
149 operator IntList() const;
150
156 operator DoubleList() const;
157
165 operator long double() const;
166
174 operator double() const;
175
183 operator float() const;
184
192 operator short int() const;
193
201 operator unsigned short int() const;
202
211 operator int() const;
212
220 operator unsigned int() const;
221
229 operator long int() const;
230
238 operator unsigned long int() const;
239
247 operator long long() const;
248
256 operator unsigned long long() const;
257
265 bool toBool() const;
266
273 const char* toChar() const;
274
280 StringList toStringList() const;
281
287 IntList toIntList() const;
288
294 DoubleList toDoubleList() const;
296
299
300
301 DataValue& operator=(const DataValue&);
303 DataValue& operator=(DataValue&&) noexcept;
305 DataValue& operator=(const char*);
307 DataValue& operator=(const std::string&);
309 DataValue& operator=(const StringList&);
311 DataValue& operator=(const IntList&);
313 DataValue& operator=(const DoubleList&);
315 DataValue& operator=(const long double);
317 DataValue& operator=(const double);
319 DataValue& operator=(const float);
321 DataValue& operator=(const short int);
323 DataValue& operator=(const unsigned short int);
325 DataValue& operator=(const int);
327 DataValue& operator=(const unsigned);
329 DataValue& operator=(const long int);
331 DataValue& operator=(const unsigned long);
333 DataValue& operator=(const long long);
335 DataValue& operator=(const unsigned long long);
337
341
342
347 std::string toString(bool full_precision = true) const;
348
350
352 DataType valueType() const;
353
359 bool isEmpty() const;
360
363
364
366 UnitType getUnitType() const;
367
368 void setUnitType(const UnitType & u);
369
371 bool hasUnit() const;
372
374 const int32_t & getUnit() const;
375
377 void setUnit(const int32_t & unit);
378
380
382 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream&, const DataValue&);
383
385 friend OPENMS_DLLAPI bool operator==(const DataValue&, const DataValue&);
386
388 friend OPENMS_DLLAPI bool operator<(const DataValue&, const DataValue&);
389
391 friend OPENMS_DLLAPI bool operator>(const DataValue&, const DataValue&);
392
394 friend OPENMS_DLLAPI bool operator!=(const DataValue&, const DataValue&);
395
396protected:
397
399 DataType value_type_;
400
402 UnitType unit_type_;
403
405 int32_t unit_;
406
408 union
409 {
410 SignedSize ssize_;
411 double dou_;
412 std::string* str_;
413 StringList* str_list_;
414 IntList* int_list_;
415 DoubleList* dou_list_;
416 } data_;
417
418private:
419
421 void clear_() noexcept;
422 };
423} // namespace OpenMS
424
425// Hash function specialization for DataValue
426namespace std
427{
438 template<>
439 struct hash<OpenMS::DataValue>
440 {
441 std::size_t operator()(const OpenMS::DataValue& dv) const noexcept
442 {
443 std::size_t seed = 0;
444
445 // Hash the value type
446 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.valueType())));
447
448 // Hash the value content based on type
449 if (!dv.isEmpty())
450 {
451 switch (dv.valueType())
452 {
454 {
455 // operator== uses fabs(a - b) < 1e-6 for doubles, so we round to 6 decimal places
456 // to ensure equal values (per operator==) produce identical hashes
457 double val = static_cast<double>(dv);
458 // Round to 6 decimal places: multiply by 1e6, round, then hash as int64
459 int64_t rounded = static_cast<int64_t>(std::round(val * 1e6));
461 break;
462 }
463 default:
464 // For all other types (string, int, lists), use toString() which is consistent with operator==
466 break;
467 }
468 }
469
470 // Hash unit information if present
471 if (dv.hasUnit())
472 {
473 OpenMS::hash_combine(seed, OpenMS::hash_int(static_cast<int>(dv.getUnitType())));
474 OpenMS::hash_combine(seed, OpenMS::hash_int(dv.getUnit()));
475 }
476
477 return seed;
478 }
479 };
480} // namespace std
481
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:32
static const DataValue EMPTY
Empty data value for comparisons.
Definition DataValue.h:37
UnitType
Supported types for DataValue.
Definition DataValue.h:57
@ MS_ONTOLOGY
ms.ontology MS
Definition DataValue.h:59
@ UNIT_ONTOLOGY
unit.ontology UO
Definition DataValue.h:58
DataValue(const DataValue &)
Copy constructor.
DataType
Supported types for DataValue.
Definition DataValue.h:41
@ DOUBLE_VALUE
double value
Definition DataValue.h:44
@ EMPTY_VALUE
empty
Definition DataValue.h:48
@ INT_LIST
integer list
Definition DataValue.h:46
@ STRING_VALUE
string value
Definition DataValue.h:42
@ STRING_LIST
string list
Definition DataValue.h:45
@ INT_VALUE
integer value
Definition DataValue.h:43
@ DOUBLE_LIST
double list
Definition DataValue.h:47
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 ListUtils.h:29
std::vector< double > DoubleList
Vector of double precision real types.
Definition ListUtils.h:36
std::vector< std::string > StringList
Vector of String.
Definition ListUtils.h:44
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:441