Skip to main content

src/DataManager/utils/TimeSeriesConcepts.hpp

src/DataManager/utils/TimeSeriesConcepts.hpp

C++20 concepts for unified time series element types. More…

Namespaces

Name
Neuralyzer
Neuralyzer::Concepts

Classes

Name
struct Neuralyzer::Concepts::is_time_series_element
Type trait to check if a type satisfies [TimeSeriesElement].
struct Neuralyzer::Concepts::is_entity_element
Type trait to check if a type satisfies [EntityElement].

Detailed Description

C++20 concepts for unified time series element types.

This header defines concepts that enable generic programming across all time series data types in Neuralyzer. The concepts establish a common interface for element access while respecting the different characteristics of each data type.

Element Type Hierarchy

All time series element types satisfy the TimeSeriesElement concept, which requires a .time() accessor returning [TimeFrameIndex](/api_generated/Classes/structTimeFrameIndex.md).

Types with EntityId support additionally satisfy EntityElement:

  • [EventWithId](/api_generated/Classes/structEventWithId.md) (DigitalEventSeries)
  • [IntervalWithId](/api_generated/Classes/structIntervalWithId.md) (lazy-pipeline / index-space element type)
  • [ClockTicksIntervalWithId](/api_generated/Classes/structClockTicksIntervalWithId.md) (DigitalIntervalSeries view())
  • RaggedElement<TData> (RaggedTimeSeries::RaggedElement)

Types without EntityId support:

Backward Compatibility

The main elements() method on containers returns pair-based types for backward compatibility with TransformPipeline. Use elementsView() for concept-compliant iteration with proper element types.

Usage Example


// Generic function that works with any time series element
template<TimeSeriesElement T>
TimeFrameIndex extractTime(T const& elem) {
    return elem.time();
}

// Generic function only for entity-bearing elements
template<EntityElement T>
EntityId extractEntityId(T const& elem) {
    return elem.id();
}

AnalogTimeSeries::TimeValuePoint

RaggedAnalogTimeSeries::FlatElement

EventWithId

IntervalWithId

ClockTicksIntervalWithId

RaggedTimeSeries::RaggedElement

Source code

#ifndef TIME_SERIES_CONCEPTS_HPP
#define TIME_SERIES_CONCEPTS_HPP


#include "Entity/EntityTypes.hpp"
#include "Observer/Observer_Data.hpp"
#include "TimeFrame/ClockTicks.hpp"
#include "TimeFrame/TimeFrameIndex.hpp"

#include <concepts>
#include <cstddef>
#include <optional>
#include <type_traits>
#include <unordered_set>

namespace Neuralyzer::Concepts {

template<typename T>
concept TimeSeriesElement = requires(T const & t) {
    { t.time() } -> std::convertible_to<TimeFrameIndex>;
};

template<typename T>
concept EntityElement = TimeSeriesElement<T> && requires(T const & t) {
    { t.id() } -> std::convertible_to<EntityId>;
};

template<typename T, typename V>
concept ValueElement = TimeSeriesElement<T> && requires(T const & t) {
    { t.value() } -> std::convertible_to<V>;
};

template<typename T, typename V>
concept FullElement = EntityElement<T> && ValueElement<T, V>;

template<typename T>
concept ClockTimeSeriesElement = requires(T const & t) {
    { t.time() } -> std::convertible_to<ClockTicks>;
};

template<typename T>
concept ClockEntityElement = ClockTimeSeriesElement<T> && requires(T const & t) {
    { t.id() } -> std::convertible_to<EntityId>;
};

template<typename T, typename V>
concept ClockValueElement = ClockTimeSeriesElement<T> && requires(T const & t) {
    { t.value() } -> std::convertible_to<V>;
};

template<typename T, typename V>
concept ClockFullElement = ClockEntityElement<T> && ClockValueElement<T, V>;

template<typename T>
concept MergeOverwriteCapable = requires(T const & source, T & target) {
    {
        source.mergeOverwriteTo(target, NotifyObservers::Yes)
    } -> std::same_as<std::optional<std::size_t>>;
};

// ========== Type Traits for Concept Detection ==========

template<typename T>
struct is_time_series_element : std::bool_constant<TimeSeriesElement<T>> {};

template<typename T>
inline constexpr bool is_time_series_element_v = is_time_series_element<T>::value;

template<typename T>
struct is_entity_element : std::bool_constant<EntityElement<T>> {};

template<typename T>
inline constexpr bool is_entity_element_v = is_entity_element<T>::value;

// ========== Concept-Based Utility Functions ==========

template<TimeSeriesElement T>
[[nodiscard]] constexpr TimeFrameIndex getTime(T const & elem) {
    return elem.time();
}

template<EntityElement T>
[[nodiscard]] constexpr EntityId getEntityId(T const & elem) {
    return elem.id();
}

template<TimeSeriesElement T>
[[nodiscard]] constexpr bool isInTimeRange(
        T const & elem,
        TimeFrameIndex start,
        TimeFrameIndex end) {
    auto const time = elem.time();
    return time >= start && time <= end;
}

template<ClockTimeSeriesElement T>
[[nodiscard]] bool isInClockTimeRange(
        T const & elem,
        ClockTicks start,
        ClockTicks end) {
    auto const time = elem.time();
    return time >= start && time <= end;
}

template<typename T>
concept EntityIdElement = requires(T const & t) {
    { t.id() } -> std::convertible_to<EntityId>;
};

template<EntityIdElement T>
[[nodiscard]] bool isInEntitySet(
        T const & elem,
        std::unordered_set<EntityId> const & ids) {
    return ids.contains(elem.id());
}

}// namespace Neuralyzer::Concepts

#endif// TIME_SERIES_CONCEPTS_HPP

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