##// END OF EJS Templates
Implementation of the addDateTime method of the cache
perrinel -
r230:b886fd9a8729
parent child
Show More
@@ -18,6 +18,13 public:
18
18
19 void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
19 void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
20
20
21 /// Return all of the SqpDataTime part of the dateTime whose are not in the cache
22 QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
23 const SqpDateTime &dateTime);
24
25
26 QVector<SqpDateTime> dateCacheList(std::shared_ptr<Variable> variable) const noexcept;
27
21 private:
28 private:
22 class VariableCacheControllerPrivate;
29 class VariableCacheControllerPrivate;
23 spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl;
30 spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl;
@@ -15,6 +15,10 struct VariableCacheController::VariableCacheControllerPrivate {
15 void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
15 void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
16 QVector<SqpDateTime> &notInCache, int cacheIndex,
16 QVector<SqpDateTime> &notInCache, int cacheIndex,
17 double currentTStart);
17 double currentTStart);
18
19
20 void addDateTimeRecurse(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
21 int cacheIndex);
18 };
22 };
19
23
20
24
@@ -27,8 +31,27 void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable,
27 const SqpDateTime &dateTime)
31 const SqpDateTime &dateTime)
28 {
32 {
29 if (variable) {
33 if (variable) {
30 // TODO: squeeze the map to let it only some SqpDateTime without intersection
34 auto findVariableIte = impl->m_VariableToSqpDateTimeListMap.find(variable);
31 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
35 if (findVariableIte == impl->m_VariableToSqpDateTimeListMap.end()) {
36 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
37 }
38 else {
39
40 // addDateTime modify the list<SqpDateTime> of the variable in a way to ensure
41 // that the list is ordered : l(0) < l(1). We assume also a < b
42 // (with a & b of type SqpDateTime) means ts(b) > te(a)
43
44 // The algorithm will try the merge of two interval:
45 // - dateTime will be compare with the first interval of the list:
46 // A: if it is inferior, it will be inserted and it's finished.
47 // B: if it is in intersection, it will be merge then the merged one
48 // will be compared to the next interval. The old one is remove from the list
49 // C: if it is superior, we do the same with the next interval of the list
50
51 int cacheIndex = 0;
52 impl->addDateTimeRecurse(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
53 cacheIndex);
54 }
32 }
55 }
33 }
56 }
34
57
@@ -49,6 +72,45 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable>
49 return notInCache;
72 return notInCache;
50 }
73 }
51
74
75 QVector<SqpDateTime>
76 VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept
77 {
78 return impl->m_VariableToSqpDateTimeListMap.at(variable);
79 }
80
81 void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse(
82 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex)
83 {
84 const auto dateTimeListSize = dateTimeList.count();
85 if (cacheIndex >= dateTimeListSize) {
86 dateTimeList.push_back(dateTime);
87 // there is no anymore interval to compore, we can just push_back it
88 return;
89 }
90
91 auto currentDateTime = dateTimeList[cacheIndex];
92
93 if (dateTime.m_TEnd < currentDateTime.m_TStart) {
94 // The compared one is < to current one compared, we can insert it
95 dateTimeList.insert(cacheIndex, dateTime);
96 }
97
98 else if (dateTime.m_TStart > currentDateTime.m_TEnd) {
99 // The compared one is > to current one compared we can comparet if to the next one
100 addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex);
101 }
102 else {
103 // Merge cases: we need to merge the two interval, remove the old one from the list then
104 // rerun the algo from this index with the merged interval
105 auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart);
106 auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd);
107 auto mergeDateTime = SqpDateTime{mTStart, mTEnd};
108
109 dateTimeList.remove(cacheIndex);
110 addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex);
111 }
112 }
113
52
114
53 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
115 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
54 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
116 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
@@ -11,6 +11,8 class TestVariableCacheController : public QObject {
11
11
12 private slots:
12 private slots:
13 void testProvideNotInCacheDateTimeList();
13 void testProvideNotInCacheDateTimeList();
14
15 void testAddDateTime();
14 };
16 };
15
17
16
18
@@ -241,5 +243,101 void TestVariableCacheController::testProvideNotInCacheDateTimeList()
241 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
243 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
242 }
244 }
243
245
246
247 void TestVariableCacheController::testAddDateTime()
248 {
249 VariableCacheController variableCacheController{};
250
251 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
252 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
253 auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()),
254 static_cast<double>(te0.toMSecsSinceEpoch())};
255
256 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
257 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
258 auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()),
259 static_cast<double>(te1.toMSecsSinceEpoch())};
260
261 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
262 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
263 auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()),
264 static_cast<double>(te2.toMSecsSinceEpoch())};
265
266 auto ts01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
267 auto te01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
268 auto sqp01 = SqpDateTime{static_cast<double>(ts01.toMSecsSinceEpoch()),
269 static_cast<double>(te01.toMSecsSinceEpoch())};
270
271 auto ts3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 14, 0, 0}};
272 auto te3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 16, 0, 0}};
273 auto sqp3 = SqpDateTime{static_cast<double>(ts3.toMSecsSinceEpoch()),
274 static_cast<double>(te3.toMSecsSinceEpoch())};
275
276 auto ts03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
277 auto te03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
278 auto sqp03 = SqpDateTime{static_cast<double>(ts03.toMSecsSinceEpoch()),
279 static_cast<double>(te03.toMSecsSinceEpoch())};
280
281
282 auto var0 = std::make_shared<Variable>("", "", "", sqp0);
283
284
285 // First case: add the first interval to the variable :sqp0
286 variableCacheController.addDateTime(var0, sqp0);
287 auto dateCacheList = variableCacheController.dateCacheList(var0);
288 QCOMPARE(dateCacheList.count(), 1);
289 auto dateCache = dateCacheList.at(0);
290 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
291 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
292
293 // 2nd case: add a second interval : sqp1 > sqp0
294 variableCacheController.addDateTime(var0, sqp1);
295 dateCacheList = variableCacheController.dateCacheList(var0);
296 QCOMPARE(dateCacheList.count(), 2);
297 dateCache = dateCacheList.at(0);
298 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
299 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
300
301 dateCache = dateCacheList.at(1);
302 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts1.toMSecsSinceEpoch()));
303 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
304
305 // 3th case: merge sqp0 & sqp1 with sqp01
306 variableCacheController.addDateTime(var0, sqp01);
307 dateCacheList = variableCacheController.dateCacheList(var0);
308 QCOMPARE(dateCacheList.count(), 1);
309 dateCache = dateCacheList.at(0);
310 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
311 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
312
313
314 // 4th case: add a second interval : sqp1 > sqp0
315 variableCacheController.addDateTime(var0, sqp2);
316 variableCacheController.addDateTime(var0, sqp3);
317 dateCacheList = variableCacheController.dateCacheList(var0);
318 QCOMPARE(dateCacheList.count(), 3);
319 dateCache = dateCacheList.at(0);
320 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
321 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
322
323 dateCache = dateCacheList.at(1);
324 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts3.toMSecsSinceEpoch()));
325 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te3.toMSecsSinceEpoch()));
326
327 dateCache = dateCacheList.at(2);
328 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts2.toMSecsSinceEpoch()));
329 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te2.toMSecsSinceEpoch()));
330
331
332 // 5th case: merge all interval
333 variableCacheController.addDateTime(var0, sqp03);
334 dateCacheList = variableCacheController.dateCacheList(var0);
335 QCOMPARE(dateCacheList.count(), 1);
336 dateCache = dateCacheList.at(0);
337 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
338 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te03.toMSecsSinceEpoch()));
339 }
340
341
244 QTEST_MAIN(TestVariableCacheController)
342 QTEST_MAIN(TestVariableCacheController)
245 #include "TestVariableCacheController.moc"
343 #include "TestVariableCacheController.moc"
General Comments 0
You need to be logged in to leave comments. Login now