Skip to content

Commit

Permalink
Implement MatchType in TimeseriesRef for precise data retrieval
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Sebastian Almagro committed Apr 29, 2024
1 parent 0c6521b commit ae9850e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
9 changes: 7 additions & 2 deletions plotjuggler_base/include/PlotJuggler/reactive_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class CreatedSeriesXY;

namespace PJ
{
enum class MatchType {
Exact, // Returns an index only if the exact time is found
Nearest // Returns the nearest time index (current behavior)
};

struct TimeseriesRef
{
TimeseriesRef(PlotData* data);
Expand All @@ -25,9 +30,9 @@ struct TimeseriesRef

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

double atTime(double t) const;
double atTime(double t, MatchType match_type) const;

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

unsigned size() const;

Expand Down
29 changes: 24 additions & 5 deletions plotjuggler_base/src/reactive_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,34 @@ void TimeseriesRef::set(unsigned index, double x, double y)
p = { x, y };
}

double TimeseriesRef::atTime(double t) const
double TimeseriesRef::atTime(double t, MatchType match_type) const
{
int i = _plot_data->getIndexFromX(t);
return _plot_data->at(i).y;
auto index = getRawIndexAtTime(t, match_type);
if (!index)
{
throw std::runtime_error("Time point not found for exact match requirement");
}
return _plot_data->at(*index).y;
}

int TimeseriesRef::getRawIndexAtTime(double t) const
std::optional<unsigned> TimeseriesRef::getRawIndexAtTime(double t, MatchType match_type) const
{
return _plot_data->getIndexFromX(t);
if (match_type == MatchType::Exact)
{
auto it = std::find_if(
_plot_data->begin(),
_plot_data->end(),
[t](const auto& point) { return point.x == t; });

Check notice

Code scanning / CodeQL

Equality test on floating-point values Note

Equality checks on floating point values can yield unexpected results.
if (it != _plot_data->end())
{
return std::distance(_plot_data->begin(), it);
}
return std::nullopt; // Exact time not found
}
else
{
return _plot_data->getIndexFromX(t); // Nearest match
}
}

unsigned TimeseriesRef::size() const
Expand Down

0 comments on commit ae9850e

Please sign in to comment.