BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
Number.h
Go to the documentation of this file.
1#ifndef BPMNOS_Number_H
2#define BPMNOS_Number_H
3
4#include <cnl/all.h>
5#include <limits>
6#include <string>
7#include <vector>
8#include <optional>
9#include <functional>
10
11#include "Value.h"
12
13//#define BPMNOS_NUMBER_TYPE double
14
15//scaled_integer< int32_t, power<-8> > has max: 8.4 million, and precision: ~ 0.004
16//scaled_integer< int64_t, power<-16> > has max: 1.4e14, and precision: ~ 0.000015
17
18#ifndef BPMNOS_NUMBER_TYPE
19#define BPMNOS_NUMBER_REP int64_t
20#define BPMNOS_NUMBER_SCALE 16
21#define BPMNOS_NUMBER_TYPE cnl::scaled_integer< BPMNOS_NUMBER_REP, cnl::power<-BPMNOS_NUMBER_SCALE> >
22// Specialize std::hash for BPMNOS_NUMBER_TYPE
23namespace std {
24 template <>
25 struct hash<BPMNOS_NUMBER_TYPE> {
26 std::size_t operator()(const BPMNOS_NUMBER_TYPE& value) const {
27 // Hash the underlying value
28 return std::hash<BPMNOS_NUMBER_REP>()(cnl::unwrap(value));
29 }
30 };
31}
32#define BPMNOS_NUMBER_PRECISION (1.0 / (1 << BPMNOS_NUMBER_SCALE))
33#endif
34
35#ifndef BPMNOS_NUMBER_PRECISION
36#define BPMNOS_NUMBER_PRECISION 1e-6
37#endif
38
39
40namespace BPMNOS {
41
43 typedef std::unordered_map< std::string, std::optional<number> > ValueMap;
44
45 struct SharedValues;
46
47 struct Values : std::vector<std::optional<number>> {
48 Values() = default;
49 Values(const SharedValues& values);
50 };
51
52 struct SharedValues : std::vector< std::reference_wrapper< std::optional<number> > > {
53 SharedValues() = default;
54 SharedValues(const SharedValues& other,Values& values);
55 SharedValues(Values& values);
56 void add(Values& values);
57 };
58
59 typedef std::unordered_map< std::string, std::variant< std::optional<number>, std::string > > VariedValueMap;
60
61 double stod(const std::string& str);
62 int stoi(const std::string& str);
63
64 /**
65 * @brief Converts a string to a number.
66 */
67 number to_number(const std::string& valueString, const ValueType& type);
68
69 /**
70 * @brief Converts a value to a number.
71 */
72 number to_number(const Value& value, const ValueType& type);
73
74 /**
75 * @brief Converts a number to a string.
76 */
77 std::string to_string(number numberValue, const ValueType& type);
78
79 /**
80 * Returns merged values from a set of values
81 **/
82 BPMNOS::Values mergeValues(const std::vector<BPMNOS::Values>& valueSets);
83
84} // namespace BPMNOS
85
86#endif // BPMNOS_Number_H
87
#define BPMNOS_NUMBER_TYPE
Definition Number.h:21
std::unordered_map< std::string, std::optional< number > > ValueMap
Definition Number.h:43
std::unordered_map< std::string, std::variant< std::optional< number >, std::string > > VariedValueMap
Definition Number.h:59
std::string to_string(number numericValue, const ValueType &type)
Converts a number to a string.
Definition Number.cpp:150
number to_number(const std::string &valueString, const ValueType &type)
Converts a string to a number.
Definition Number.cpp:57
BPMNOS_NUMBER_TYPE number
Definition Number.h:42
double stod(const std::string &str)
Definition Number.cpp:37
BPMNOS::Values mergeValues(const std::vector< BPMNOS::Values > &valueSets)
Definition Number.cpp:166
ValueType
Definition Value.h:9
int stoi(const std::string &str)
Definition Number.cpp:47
std::variant< bool, int, double, std::string > Value
Definition Value.h:10
STL namespace.
void add(Values &values)
Definition Number.cpp:31
Values()=default
std::size_t operator()(const BPMNOS_NUMBER_TYPE &value) const
Definition Number.h:26