Skip to content

Commit ae9850e

Browse files
author
Sebastian Almagro
committed
Implement MatchType in TimeseriesRef for precise data retrieval
- Added an enumeration MatchType with options Exact and Nearest. - Modified atTime and getRawIndexAtTime in TimeseriesRef to support MatchType, allowing callers to specify whether they need an exact timestamp match or the nearest available match. - Adjusted the atTime method to throw an error when an exact match is required but not found, enhancing error handling for strict match requirements.
1 parent 0c6521b commit ae9850e

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

plotjuggler_base/include/PlotJuggler/reactive_function.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class CreatedSeriesXY;
1717

1818
namespace PJ
1919
{
20+
enum class MatchType {
21+
Exact, // Returns an index only if the exact time is found
22+
Nearest // Returns the nearest time index (current behavior)
23+
};
24+
2025
struct TimeseriesRef
2126
{
2227
TimeseriesRef(PlotData* data);
@@ -25,9 +30,9 @@ struct TimeseriesRef
2530

2631
void set(unsigned index, double x, double y);
2732

28-
double atTime(double t) const;
33+
double atTime(double t, MatchType match_type) const;
2934

30-
int getRawIndexAtTime(double t) const;
35+
std::optional<unsigned> getRawIndexAtTime(double t, MatchType match_type) const; // Method signature updated
3136

3237
unsigned size() const;
3338

plotjuggler_base/src/reactive_function.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,34 @@ void TimeseriesRef::set(unsigned index, double x, double y)
185185
p = { x, y };
186186
}
187187

188-
double TimeseriesRef::atTime(double t) const
188+
double TimeseriesRef::atTime(double t, MatchType match_type) const
189189
{
190-
int i = _plot_data->getIndexFromX(t);
191-
return _plot_data->at(i).y;
190+
auto index = getRawIndexAtTime(t, match_type);
191+
if (!index)
192+
{
193+
throw std::runtime_error("Time point not found for exact match requirement");
194+
}
195+
return _plot_data->at(*index).y;
192196
}
193197

194-
int TimeseriesRef::getRawIndexAtTime(double t) const
198+
std::optional<unsigned> TimeseriesRef::getRawIndexAtTime(double t, MatchType match_type) const
195199
{
196-
return _plot_data->getIndexFromX(t);
200+
if (match_type == MatchType::Exact)
201+
{
202+
auto it = std::find_if(
203+
_plot_data->begin(),
204+
_plot_data->end(),
205+
[t](const auto& point) { return point.x == t; });
206+
if (it != _plot_data->end())
207+
{
208+
return std::distance(_plot_data->begin(), it);
209+
}
210+
return std::nullopt; // Exact time not found
211+
}
212+
else
213+
{
214+
return _plot_data->getIndexFromX(t); // Nearest match
215+
}
197216
}
198217

199218
unsigned TimeseriesRef::size() const

0 commit comments

Comments
 (0)