1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#pragma once
#include <util/generic/string.h>
#include <util/generic/hash.h>
#include <util/generic/vector.h>
#include <util/string/builder.h>
#include <util/string/cast.h>
#include <variant>
namespace NAnalytics {
using TRowValue = std::variant<i64, ui64, double, TString>;
TString ToString(const TRowValue& val) {
TStringBuilder builder;
std::visit([&builder] (auto&& arg) {
builder << arg;
}, val);
return builder;
}
struct TRow : public THashMap<TString, TRowValue> {
TString Name;
template<typename T>
bool Get(const TString& name, T& value) const {
if constexpr (std::is_same_v<double, T>) {
if (name == "_count") { // Special values
value = 1.0;
return true;
}
}
auto iter = find(name);
if (iter != end()) {
try {
value = std::get<T>(iter->second);
return true;
} catch (...) {}
}
return false;
}
template<typename T = double>
T GetOrDefault(const TString& name, T dflt = T()) {
Get(name, dflt);
return dflt;
}
bool GetAsString(const TString& name, TString& value) const {
auto iter = find(name);
if (iter != end()) {
value = ToString(iter->second);
return true;
}
return false;
}
};
using TAttributes = THashMap<TString, TString>;
struct TTable : public TVector<TRow> {
TAttributes Attributes;
};
struct TMatrix : public TVector<double> {
size_t Rows;
size_t Cols;
explicit TMatrix(size_t rows = 0, size_t cols = 0)
: TVector<double>(rows * cols)
, Rows(rows)
, Cols(cols)
{}
void Reset(size_t rows, size_t cols)
{
Rows = rows;
Cols = cols;
clear();
resize(rows * cols);
}
double& Cell(size_t row, size_t col)
{
Y_VERIFY(row < Rows);
Y_VERIFY(col < Cols);
return operator[](row * Cols + col);
}
double Cell(size_t row, size_t col) const
{
Y_VERIFY(row < Rows);
Y_VERIFY(col < Cols);
return operator[](row * Cols + col);
}
double CellSum() const
{
double sum = 0.0;
for (double x : *this) {
sum += x;
}
return sum;
}
};
}
|