Skip to main content

DataViewer::AnalogVertexCache

DataViewer::AnalogVertexCache

Ring buffer-based cache for analog series vertices. More…

#include <AnalogVertexCache.hpp>

Public Functions

Name
AnalogVertexCache() =default
void initialize(size_t capacity)
Initialize the cache with a given capacity.
void invalidate()
Clear all cached data and reset state.
bool isInitialized() const
Check if the cache has been initialized.
bool isValid() const
Check if the cache contains valid data.
TimeFrameIndex cachedStart() const
Get the currently cached time range.
TimeFrameIndex cachedEnd() const
bool covers(TimeFrameIndex start, TimeFrameIndex end) const
Check if the requested range is fully covered by the cache.
bool needsUpdate(TimeFrameIndex start, TimeFrameIndex end) const
Check if the cache needs updating for a new time range.
std::vector< MissingRange > getMissingRanges(TimeFrameIndex start, TimeFrameIndex end) const
Calculate which ranges need to be generated.
void prependVertices(std::vector< CachedAnalogVertex > const & vertices, TimeFrameIndex requested_start)
Add vertices to the front of the cache (for scrolling left).
void appendVertices(std::vector< CachedAnalogVertex > const & vertices, TimeFrameIndex requested_end)
Add vertices to the back of the cache (for scrolling right).
void setVertices(std::vector< CachedAnalogVertex > const & vertices, TimeFrameIndex start, TimeFrameIndex end)
Replace all cached vertices (for cache misses or initialization).
std::vector< float > getVerticesForRange(TimeFrameIndex start, TimeFrameIndex end, ClockTicks x_origin_master_absolute_time) const
Extract vertices for a specific time range.
void extractVerticesForRange(TimeFrameIndex start, TimeFrameIndex end, ClockTicks x_origin_master_absolute_time, std::vector< float > & out_buffer) const
Extract vertices for a specific time range into a caller-supplied buffer.
std::vector< float > getVerticesForRangeDecimated(TimeFrameIndex start, TimeFrameIndex end, int bucket_count, ClockTicks x_origin_master_absolute_time) const
Extract vertices for a specific time range with fast-path min-max decimation.
void extractVerticesForRangeDecimated(TimeFrameIndex start, TimeFrameIndex end, int bucket_count, ClockTicks x_origin_master_absolute_time, std::vector< float > & out_buffer) const
Extract decimated vertices into a caller-supplied buffer.
size_t size() const
Get statistics about cache usage.
size_t capacity() const
float utilizationRatio() const

Detailed Description

class DataViewer::AnalogVertexCache;

Ring buffer-based cache for analog series vertices.

Maintains a circular buffer of vertices with tracking of the cached time range. Automatically discards old data when new data is pushed, enabling efficient incremental updates during scrolling.

Public Functions Documentation

function AnalogVertexCache

AnalogVertexCache() =default

function initialize

void initialize(
    size_t capacity
)

Initialize the cache with a given capacity.

Parameters:

  • capacity Maximum number of vertices to cache

Recommended capacity is 2-3x the visible window size to allow for smooth scrolling in both directions.

function invalidate

void invalidate()

Clear all cached data and reset state.

function isInitialized

inline bool isInitialized() const

Check if the cache has been initialized.

function isValid

inline bool isValid() const

Check if the cache contains valid data.

function cachedStart

inline TimeFrameIndex cachedStart() const

Get the currently cached time range.

function cachedEnd

inline TimeFrameIndex cachedEnd() const

function covers

bool covers(
    TimeFrameIndex start,
    TimeFrameIndex end
) const

Check if the requested range is fully covered by the cache.

Parameters:

  • start Start of requested range (inclusive)
  • end End of requested range (exclusive)

Return: true if the entire range is cached

Precondition:

  • start <= end; if end < start (inverted range), the condition start >= m_cached_start && end <= m_cached_end can evaluate to true even though no meaningful range is requested, giving a spurious “covered” result — wrong but harmless as the caller would then skip regeneration and getVerticesForRange() returns empty for an inverted range (enforcement: none) [LOW]
  • start and end must be in the same time coordinate system as the cached data (series time frame, not master time frame); mixing coordinate systems produces wrong coverage decisions (enforcement: none) [IMPORTANT]

Postcondition:

  • Returns false when !isValid() or vertices are empty
  • Returns true only when start >= cachedStart() && end <= cachedEnd()

Returns true only when the cache is valid, non-empty, and the entire interval [start, end) lies within [cachedStart(), cachedEnd()). Returns false (conservatively) when the cache is invalid or empty.

This function is purely computational: no memory allocation, no dereferences, safe to call regardless of cache validity.

function needsUpdate

bool needsUpdate(
    TimeFrameIndex start,
    TimeFrameIndex end
) const

Check if the cache needs updating for a new time range.

Parameters:

  • start Start of requested range (inclusive)
  • end End of requested range (exclusive)

Return: true if vertex generation is required before rendering

Precondition:

  • start <= end; if end < start, the overlap check end <= m_cached_start may fire incorrectly, causing the function to return true (spurious cache miss) even when data is fully cached — wasted work but no crash or data corruption (enforcement: none) [LOW]
  • start and end must be in the same time coordinate system as the cached data (series time frame, not master time frame); mixing coordinate systems produces wrong update decisions (enforcement: none) [IMPORTANT]

Postcondition:

  • Return value is true when !isValid() or vertices are empty
  • Return value is false only when covers(start, end) would return true

Returns true if:

  • Cache is invalid or empty
  • Requested range has no overlap with the cached window (jumped location)
  • Requested range extends beyond the cached window at either end

Returns false (no update needed) only when the cache is valid and fully covers [start, end).

This function is purely computational: no memory allocation, no dereferences, safe to call regardless of cache validity.

function getMissingRanges

std::vector< MissingRange > getMissingRanges(
    TimeFrameIndex start,
    TimeFrameIndex end
) const

Calculate which ranges need to be generated.

Parameters:

  • start Start of requested range (inclusive)
  • end End of requested range (exclusive)

Return: Vector of at most 2 missing sub-ranges

Precondition:

  • start <= end; if end < start, the no-overlap branch may fire and return an inverted MissingRange (start > end) that will corrupt any downstream call to generateVerticesForRange, prependVertices, or appendVertices (enforcement: none) [IMPORTANT]
  • start and end must be expressed in the same time coordinate system as the indices stored in the cache (i.e., the series time frame, not the master time frame); mixing coordinate systems produces geometrically wrong gap ranges (enforcement: none) [IMPORTANT]

Postcondition:

  • Return value contains at most 2 entries
  • If return value has 2 entries, [0].prepend == true and [1].prepend == false
  • Every returned MissingRange satisfies start <= end when the input satisfies start <= end

Returns 0, 1, or 2 [MissingRange](/api_generated/Classes/structDataViewer_1_1MissingRange.md) entries describing the subranges of [start, end) that are not present in the cache:

  • 0 ranges — fully cached; nothing to generate
  • 1 range, prepend=false — complete cache miss (cache invalid/empty or no overlap); the single entry spans the entire requested window
  • 1 range, prepend=true — left edge missing (scrolling left)
  • 1 range, prepend=false — right edge missing (scrolling right)
  • 2 ranges — both edges missing; first has prepend=true, second has prepend=false

This function is purely computational (no memory allocation beyond the returned vector) and safe to call regardless of cache validity.

function prependVertices

void prependVertices(
    std::vector< CachedAnalogVertex > const & vertices,
    TimeFrameIndex requested_start
)

Add vertices to the front of the cache (for scrolling left).

Parameters:

  • vertices Vertices to prepend (may be empty; call is a no-op if so)
  • requested_start Start time index of the requested range (inclusive)

Precondition:

  • vertices must be sorted in ascending order by time_idx (enforcement: none) [IMPORTANT]
  • If isValid() is true, every element of vertices must have time_idx < cachedStart(); otherwise the buffer becomes unsorted at the join point and binary search in getVerticesForRange() produces wrong results (enforcement: none) [IMPORTANT]
  • capacity() > 0 when vertices is non-empty; if capacity is 0, push_front is a no-op yet m_valid is set to true, leaving the cache in an inconsistent state (enforcement: none) [IMPORTANT]

Postcondition:

Inserts vertices before all currently cached vertices. Vertices are inserted via push_front in reverse so that the buffer remains sorted by ascending time_idx. If the buffer overflows, the newest vertices at the back are discarded and [cachedEnd()](/api_generated/Classes/classDataViewer_1_1AnalogVertexCache.md#function-cachedend) is narrowed accordingly.

If the cache is currently invalid (empty), this call initialises it using the extent of vertices alone.

function appendVertices

void appendVertices(
    std::vector< CachedAnalogVertex > const & vertices,
    TimeFrameIndex requested_end
)

Add vertices to the back of the cache (for scrolling right).

Parameters:

  • vertices Vertices to append (may be empty; call is a no-op if so)
  • requested_end End time index of the requested range (exclusive)

Precondition:

  • vertices must be sorted in ascending order by time_idx (enforcement: none) [IMPORTANT]
  • If isValid() is true, every element of vertices must have time_idx >= cachedEnd(); otherwise the buffer becomes unsorted at the join point and binary search in getVerticesForRange() produces wrong results (enforcement: none) [IMPORTANT]
  • capacity() > 0 when vertices is non-empty; if capacity is 0, push_back is a no-op yet m_valid is set to true and m_cached_end is overwritten with a value derived from vertices, leaving the cache in an inconsistent state (enforcement: none) [IMPORTANT]

Postcondition:

Inserts vertices after all currently cached vertices via push_back. The circular buffer automatically discards the oldest (front) entries on overflow and [cachedStart()](/api_generated/Classes/classDataViewer_1_1AnalogVertexCache.md#function-cachedstart) is advanced to match.

If the cache is currently invalid (empty), this call initialises it using the extent of vertices alone.

function setVertices

void setVertices(
    std::vector< CachedAnalogVertex > const & vertices,
    TimeFrameIndex start,
    TimeFrameIndex end
)

Replace all cached vertices (for cache misses or initialization).

Parameters:

  • vertices New vertex data (may be empty, in which case the cache is invalidated)
  • start Start time index of the data (inclusive)
  • end End time index of the data (exclusive)

Precondition:

  • vertices must be sorted in ascending order by time_idx (enforcement: none) [IMPORTANT]
  • start <= end (enforcement: none) [IMPORTANT]
  • If vertices is non-empty, vertices.front().time_idx >= start (enforcement: none) [LOW]
  • If vertices is non-empty, vertices.back().time_idx < end (enforcement: none) [LOW]
  • capacity() > 0 when vertices is non-empty, otherwise all data is silently discarded (enforcement: runtime_check)

Postcondition:

Clears the cache and loads vertices. If the circular buffer capacity is smaller than vertices.size(), the oldest (front) entries are silently discarded and the cached range is narrowed to match what was actually stored.

function getVerticesForRange

std::vector< float > getVerticesForRange(
    TimeFrameIndex start,
    TimeFrameIndex end,
    ClockTicks x_origin_master_absolute_time
) const

Extract vertices for a specific time range.

Parameters:

  • start Start of range to extract (inclusive)
  • end End of range to extract (exclusive)
  • x_origin_master_absolute_time Absolute physical time at the current view’s left edge (same as AnalogBatchParams::x_origin_master_absolute_time)

Return: Flat vertex array [x0,y0,x1,y1,…], or empty if range not cached

Precondition:

  • start <= end; if end < start, the reserve() call computes static_cast(negative_int64) * 2, which wraps to near SIZE_MAX and causes std::length_error or std::bad_alloc (enforcement: none) [CRITICAL]
  • The buffer must be sorted in ascending order by time_idx; the binary search in findIndexForTime() produces wrong results if the buffer is unsorted (enforcement: none) [IMPORTANT]
  • start >= cachedStart() and end <= cachedEnd() for a non-empty result; requesting a range outside the cached window returns empty (enforcement: runtime_check)

Postcondition:

  • Return value size is even (pairs of x,y floats)
  • Return value is empty when cache is invalid or start is not found

Returns a flat float array suitable for GPU upload: [x0, y0, x1, y1, …]. Each output x is [CachedAnalogVertex::x](/api_generated/Classes/structDataViewer_1_1CachedAnalogVertex.md#variable-x) minus x_origin_master_absolute_time (integer-to-double subtraction, then cast to float), matching TimeSeriesMapper view-relative coordinates. y is copied from the cache. Uses binary search to locate start in the buffer, then linearly scans forward until a vertex with time_idx >= end is encountered.

Returns an empty vector (without crashing) when:

  • The cache is invalid or empty
  • start is not found in the buffer (beyond the cached range)

function extractVerticesForRange

void extractVerticesForRange(
    TimeFrameIndex start,
    TimeFrameIndex end,
    ClockTicks x_origin_master_absolute_time,
    std::vector< float > & out_buffer
) const

Extract vertices for a specific time range into a caller-supplied buffer.

Parameters:

  • start Start of range to extract (inclusive)
  • end End of range to extract (exclusive)
  • x_origin_master_absolute_time Absolute physical time at the current view’s left edge
  • out_buffer Output buffer to populate in-place

Reuses the capacity of out_buffer (via .clear()), eliminating dynamic heap allocations.

function getVerticesForRangeDecimated

std::vector< float > getVerticesForRangeDecimated(
    TimeFrameIndex start,
    TimeFrameIndex end,
    int bucket_count,
    ClockTicks x_origin_master_absolute_time
) const

Extract vertices for a specific time range with fast-path min-max decimation.

Parameters:

  • start Start of range to extract (inclusive)
  • end End of range to extract (exclusive)
  • bucket_count Number of temporal buckets to decimate into
  • x_origin_master_absolute_time Absolute physical time at the current view’s left edge

Return: Flat vertex array [x0,y0,x1,y1,…] of decimated points

Directly downsamples cached vertices into min-max pairs across bucket_count buckets. Avoids intermediate buffer allocations and floating-point bucket hashing.

function extractVerticesForRangeDecimated

void extractVerticesForRangeDecimated(
    TimeFrameIndex start,
    TimeFrameIndex end,
    int bucket_count,
    ClockTicks x_origin_master_absolute_time,
    std::vector< float > & out_buffer
) const

Extract decimated vertices into a caller-supplied buffer.

Parameters:

  • start Start of range to extract (inclusive)
  • end End of range to extract (exclusive)
  • bucket_count Number of temporal buckets to decimate into
  • x_origin_master_absolute_time Absolute physical time at the current view’s left edge
  • out_buffer Output buffer to populate in-place

Reuses the capacity of out_buffer (via .clear()), eliminating dynamic heap allocations.

function size

inline size_t size() const

Get statistics about cache usage.

function capacity

inline size_t capacity() const

function utilizationRatio

inline float utilizationRatio() const

Updated on 2026-11-13 at 20:25:42 +0000