-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeIndexer.h
235 lines (196 loc) · 5.94 KB
/
timeIndexer.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef __TIMEINDEXER_H__
#define __TIMEINDEXER_H__
struct timeInfo {
int id;
std::vector<int> indices;
};
class timeIndexer {
public:
timeIndexer() : initialized(false), sorted(false), nIds(0) {}
timeIndexer(const std::vector<int>& ids,
const std::vector<std::vector<double>>& times) {
initialize(ids, times);
}
timeIndexer(const std::vector<int>& ids,
const std::vector<double>& times) {
initialize(ids, times);
}
timeIndexer(Rcpp::IntegerVector& ids,
Rcpp::NumericVector& times) {
initialize(ids, times);
}
void initialize(const std::vector<int>& ids,
const std::vector<std::vector<double>>& times) {
nIds = ids.size();
size_t k = 0;
std::unordered_map<double, std::unordered_map<int, std::vector<int>>> timeIdIndices;
maxTime = -INFINITY;
for (size_t i = ids.size(); i--;) {
int id = ids[i];
for (size_t j = 0; j < times[i].size(); ++j) {
double time = times[i][j];
if (maxTime < time) {
maxTime = time;
}
timeIdIndices[time][id].push_back(k);
uniqueTimes.insert(time);
k++;
}
}
for (const auto& timeEntry : timeIdIndices) {
double time = timeEntry.first;
for (const auto& idEntry : timeEntry.second) {
int id = idEntry.first;
const std::vector<int>& indices = idEntry.second;
timeToInfo[time].emplace_back(timeInfo{id, indices});
}
}
initialized = true;
sorted = false;
}
void initialize(const std::vector<int>& ids, const std::vector<double>& times,
const bool optTime) {
if (optTime && initialized) {
return;
}
if (isSame(ids, times)) {
return;
}
std::unordered_map<int, std::vector<double>> idToTimesMap;
reset();
this->times = times;
this->modelSwitch = ids;
std::vector<int> outIds;
std::vector<std::vector<double>> outTimes;
for (size_t i = 0; i < times.size(); ++i) {
idToTimesMap[ids[i]].push_back(times[i]);
}
for (const auto& pair : idToTimesMap) {
outIds.push_back(pair.first);
outTimes.push_back(pair.second);
}
initialize(outIds, outTimes);
}
void initialize(const std::vector<int>& ids, const std::vector<double>& times) {
initialize(ids, times, false);
}
void initialize(Rcpp::IntegerVector& ids, Rcpp::NumericVector& time,
const bool optTime) {
std::vector<int> idsVec = Rcpp::as<std::vector<int>>(ids);
std::vector<double> timeVec = Rcpp::as<std::vector<double>>(time);
initialize(idsVec, timeVec, optTime);
}
void initialize(Rcpp::IntegerVector& ids, Rcpp::NumericVector& time) {
initialize(ids, time, false);
}
bool isSame(const std::vector<int>& ids, const std::vector<double>& times) {
if (ids.size() != this->modelSwitch.size()) {
return false;
}
if (times.size() != this->times.size()) {
return false;
}
if (!std::equal(ids.begin(), ids.end(), this->modelSwitch.begin())) {
return false;
}
if (!std::equal(times.begin(), times.end(), this->times.begin())) {
return false;
}
return true;
}
void reset() {
timeToInfo.clear();
uniqueTimes.clear();
sortedTimes.clear();
times.clear();
modelSwitch.clear();
initialized = false;
sorted = false;
nIds=0;
}
size_t getNid() {
return nIds;
}
double getMaxTime() {
return maxTime;
}
bool isInitialized() const {
return initialized;
}
std::vector<double> getSortedUniqueTimes() {
if (!initialized) {
throw std::runtime_error("timeIndexer has not been initialized");
}
if (!sorted) {
sortedTimes.assign(uniqueTimes.begin(), uniqueTimes.end());
gfx::timsort(sortedTimes.begin(), sortedTimes.end());
sorted = true;
}
return sortedTimes;
}
std::vector<double> getUniqueTimes() {
if (!initialized) {
throw std::runtime_error("timeIndexer has not been initialized");
}
if (!sorted) {
return std::vector<double>(uniqueTimes.begin(), uniqueTimes.end());
} else {
return sortedTimes;
}
}
const std::vector<timeInfo>& getTimeInfo(double time) const {
if (!initialized) {
throw std::runtime_error("timeIndexer has not been initialized");
}
try {
return timeToInfo.at(time);
} catch (const std::out_of_range& e) {
REprintf("times:\n");
Rcpp::print(Rcpp::wrap(this->times));
REprintf("modelSwitch:");
Rcpp::print(Rcpp::wrap(this->modelSwitch));
Rcpp::stop("time '%f' not found in timeIndexer",
time);
}
}
void setTimes(const std::vector<double>& times) {
this->times = times;
}
void setModelSwitch(const std::vector<int>& modelSwitch) {
this->modelSwitch = modelSwitch;
}
std::vector<int> getModelSwitch() {
return this->modelSwitch;
}
std::vector<double> getTimes() {
return this->times;
}
private:
std::unordered_map<double, std::vector<timeInfo>> timeToInfo;
std::set<double> uniqueTimes;
std::vector<double> sortedTimes;
bool initialized;
bool sorted;
size_t nIds;
double maxTime;
std::vector<double> times;
std::vector<int> modelSwitch;
};
extern timeIndexer globalTimeIndexer;
static inline void convertToTimeIndexerStructure(const std::vector<double>& times,
const std::vector<int>& ids,
std::vector<int>& outIds,
std::vector<std::vector<double>>& outTimes) {
if (globalTimeIndexer.isInitialized()) return;
std::unordered_map<int, std::vector<double>> idToTimesMap;
globalTimeIndexer.setTimes(times);
globalTimeIndexer.setModelSwitch(ids);
for (size_t i = 0; i < times.size(); ++i) {
idToTimesMap[ids[i]].push_back(times[i]);
}
for (const auto& pair : idToTimesMap) {
outIds.push_back(pair.first);
outTimes.push_back(pair.second);
}
}
#endif