Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
ChronologicalSerie.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
5
6
7 == GNU General Public License Usage ==
8
9 OpenFLUID is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OpenFLUID is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21
22
23 == Other Usage ==
24
25 Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26 license, and requires a written agreement between You and INRA.
27 Licensees for Other Usage of OpenFLUID may use this file in accordance
28 with the terms contained in the written agreement between You and INRA.
29
30*/
31
32
33/**
34 @file ChronologicalSerie.hpp
35
36 @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37 @author Armel THÖNI <armel.thoni@inrae.fr>
38 */
39
40
41#ifndef __OPENFLUID_TOOLS_CHRONOLOGICALSERIE_HPP__
42#define __OPENFLUID_TOOLS_CHRONOLOGICALSERIE_HPP__
43
44
45#include <list>
46
49
50
51namespace openfluid { namespace tools {
52
53
54template <typename DataType=double>
55using ChronItem_t = std::pair<openfluid::core::DateTime,DataType>;
56
57
58/**
59 Container for a chronological data serie
60*/
61template <typename DataType=double>
62class ChronologicalSerie : public std::list<ChronItem_t<DataType>>
63{
64 private:
65
66 typename std::list<ChronItem_t<DataType>>::iterator m_InternalIterator;
67 typename std::list<ChronItem_t<DataType>>::iterator m_PreviousInternalIterator;
68
69
70 public:
71
72 /**
73 Default constructor
74 */
75 ChronologicalSerie() : std::list<ChronItem_t<DataType>>(), m_InternalIterator(this->begin()),
76 m_PreviousInternalIterator(this->begin())
77 {
78 }
79 /**
80 Resets the internal iterator
81 */
82 void reset()
83 {
84 if (this->size() >= 2)
85 {
86 m_PreviousInternalIterator = (this->begin());
87 m_InternalIterator = (++this->begin());
88 }
89 }
90
91 /**
92 Finds the two surrending values for a given date. If the given date is exactly found in the serie,
93 the two surrounding values are the same.
94 @param[in] DT The date to find surrounding values
95 @param[out] Before The closest date before the given date
96 @param[out] After The closest date after the given date
97 @return true if the closest dates before and after have been found
98 */
100 ChronItem_t<DataType>& Before,
102 {
103 if (this->size() <2)
104 {
105 return false;
106 }
107
108 if ((*m_PreviousInternalIterator).first > DT || m_InternalIterator == this->end())
109 {
110 reset();
111 }
112
113
114 while (m_InternalIterator != this->end() &&
115 !((*m_InternalIterator).first >= DT &&
116 (*m_PreviousInternalIterator).first <= DT))
117 {
118
119 // found exact time
120 if ((*m_InternalIterator).first == DT)
121 {
122 Before = (*m_InternalIterator);
123 After = (*m_InternalIterator);
124 return true;
125 }
126 else if ((*m_PreviousInternalIterator).first == DT)
127 {
128 Before = (*m_PreviousInternalIterator);
129 After = (*m_PreviousInternalIterator);
130 return true;
131 }
132
133 ++m_PreviousInternalIterator;
134 ++m_InternalIterator;
135 }
136
137
138 if (m_InternalIterator != this->end())
139 {
140 if ((*m_PreviousInternalIterator).first == DT)
141 {
142 Before = (*m_InternalIterator);
143 After = (*m_InternalIterator);
144 return true;
145 }
146 else if ((*m_InternalIterator).first == DT)
147 {
148 Before = (*m_InternalIterator);
149 After = (*m_InternalIterator);
150 return true;
151 }
152 else
153 {
154 Before = (*(m_PreviousInternalIterator));
155 After = (*m_InternalIterator);
156 return true;
157 }
158 }
159
160
161 return false;
162 }
163
164};
165
166
167} } // namespaces
168
169
170#endif /* __OPENFLUID_TOOLS_CHRONOLOGICALSERIE_HPP__ */
Class for management of date and time information.
Definition DateTime.hpp:88
Definition ChronologicalSerie.hpp:63
bool getSurroundingValues(const openfluid::core::DateTime &DT, ChronItem_t< DataType > &Before, ChronItem_t< DataType > &After)
Definition ChronologicalSerie.hpp:99
ChronologicalSerie()
Definition ChronologicalSerie.hpp:75
void reset()
Definition ChronologicalSerie.hpp:82
std::pair< openfluid::core::DateTime, DataType > ChronItem_t
Definition ChronologicalSerie.hpp:55
Definition ApplicationException.hpp:47