-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArrayCython.cpp
More file actions
166 lines (142 loc) · 4.83 KB
/
Copy pathDynamicArrayCython.cpp
File metadata and controls
166 lines (142 loc) · 4.83 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "DynamicArrayCython.h"
DynamicArrayCython::DynamicArrayCython(size_t columns) : cols(columns) {}
void DynamicArrayCython::deleteRow(long timestamp) {
auto it = data.find(timestamp);
if (it != data.end()) {
data.erase(it);
} else {
std::cerr << "Error: Timestamp not found." << std::endl;
}
}
void DynamicArrayCython::removeFirstElement() {
if (!data.empty()) {
data.erase(data.begin());
} else {
std::cerr << "Error: Data is empty." << std::endl;
}
}
void DynamicArrayCython::deleteRange(long timestamp, int numberOfValues) {
auto it = data.lower_bound(timestamp);
// Retrieve iterator for item that is numberOfValues before the timestamp
if (it != data.begin()) {
std::advance(it, -numberOfValues);
}
data.erase(it, data.lower_bound(timestamp));
}
void DynamicArrayCython::deleteRowById(int index) {
if (index < 0 || static_cast<size_t>(index) >= data.size()) {
std::cerr << "Error: Index out of bounds." << std::endl;
return;
}
auto it = data.begin();
std::advance(it, index);
data.erase(it);
}
bool DynamicArrayCython::timestampExists(long timestamp) const {
return data.find(timestamp) != data.end();
}
size_t DynamicArrayCython::getNumRows() const { return data.size(); }
void DynamicArrayCython::print() const {
for (const auto &row : data) {
std::cout << row.first << ": ";
for (double val : row.second) {
std::cout << val << " ";
}
std::cout << std::endl;
}
}
long DynamicArrayCython::minKey() const {
if (data.empty()) {
std::cerr << "Error: No data available." << std::endl;
return -1; // Or any other indication of error or empty data
}
return data.begin()->first; // The smallest key
}
long DynamicArrayCython::maxKey() const {
if (data.empty()) {
std::cerr << "Error: No data available." << std::endl;
return -1; // Or any other indication of error or empty data
}
return data.rbegin()->first; // The biggest key
}
bool DynamicArrayCython::addOrUpdateRow(long timestamp, size_t column_index,
double value) {
if (column_index >= cols) {
throw std::out_of_range("Column index out of range");
}
auto entryIt = data.find(timestamp);
bool newEntry = true;
if (entryIt != data.end()) {
newEntry = false;
}
auto &row =
data[timestamp]; // This will default-construct a vector if not found
if (row.size() < cols) {
row.resize(cols, std::nan("")); // Ensure the row has the correct size,
// fill with NaNs if needed
}
row[column_index] = value;
return newEntry;
}
std::vector<double> DynamicArrayCython::getRow(long timestamp) {
auto it = data.find(timestamp);
if (it != data.end()) {
return it->second;
}
// Return an empty vector if no row with the specified timestamp is found
return {};
}
// extract row based on index
std::vector<double> DynamicArrayCython::getRowByIndex(int index) {
if (index < 0 || static_cast<size_t>(index) >= data.size()) {
std::cerr << "Error: Index out of bounds." << std::endl;
return {};
}
auto it = data.begin();
std::advance(it, index); // Advance the iterator to the desired index
return it->second; // Return the row at the index
}
std::vector<std::vector<double>> DynamicArrayCython::getSlice(long timestamp,
size_t numItems) {
if (data.empty()) {
std::cerr << "Error: Data is empty." << std::endl;
return {};
}
auto it = data.lower_bound(timestamp);
if (it == data.end()) {
it = std::prev(data.end());
}
std::vector<std::vector<double>> slice;
slice.reserve(numItems);
for (size_t count = 0; it != data.begin() && count < numItems;
--it, ++count) {
slice.push_back({static_cast<double>(it->first)});
slice.back().insert(slice.back().end(), it->second.begin(),
it->second.end());
}
if (it == data.begin() && numItems > slice.size()) {
slice.push_back({static_cast<double>(it->first)});
slice.back().insert(slice.back().end(), it->second.begin(),
it->second.end());
}
std::reverse(slice.begin(), slice.end()); // Ensure chronological order
return slice;
}
// Get all timestamps (first column) in the array
std::vector<long> DynamicArrayCython::getTimestamps() const {
std::vector<long> timestamps;
for (const auto &entry : data) {
timestamps.push_back(
entry.first); // entry.first is the key (timestamp) in the map
}
return timestamps;
}
std::vector<double> DynamicArrayCython::getFlattenedSlice(long timestamp,
size_t numItems) {
auto slice = getSlice(timestamp, numItems);
std::vector<double> flattenedSlice;
for (const auto &row : slice) {
flattenedSlice.insert(flattenedSlice.end(), row.begin(), row.end());
}
return flattenedSlice;
}