Skip to main content

src/DataManager/utils/TableView/computers/IntervalPropertyComputer.h

src/DataManager/utils/TableView/computers/IntervalPropertyComputer.h

Classes

Name
class IntervalPropertyComputer
Templated computer for extracting properties from time intervals.

Types

Name
enum class std::uint8_t IntervalProperty { Start, End, Duration}
Enumeration of operations that can be performed on interval properties.

Types Documentation

enum IntervalProperty

Enumerator Value Description
Start Returns the interval start in absolute clock ticks.
End Returns the interval end in absolute clock ticks.
Duration Returns the duration in clock ticks (end - start).

Enumeration of operations that can be performed on interval properties.

Source code

#ifndef INTERVAL_PROPERTY_COMPUTER_H
#define INTERVAL_PROPERTY_COMPUTER_H

#include "DigitalTimeSeries/Digital_Interval_Series.hpp"
#include "Entity/EntityTypes.hpp"
#include "utils/TableView/columns/IColumn.h"
#include "utils/TableView/core/ExecutionPlan.h"
#include "utils/TableView/interfaces/IColumnComputer.h"


#include <cstdint>
#include <memory>
#include <string>
#include <vector>

class DigitalIntervalSeries;

enum class IntervalProperty : std::uint8_t {
    Start,   
    End,     
    Duration 
};

template<typename T>
class IntervalPropertyComputer : public IColumnComputer<T> {
public:
    IntervalPropertyComputer(std::shared_ptr<DigitalIntervalSeries> source,
                             IntervalProperty property,
                             std::string sourceName)
        : m_source(std::move(source)),
          m_property(property),
          m_sourceName(std::move(sourceName)) {}

    [[nodiscard]] std::pair<std::vector<T>, ColumnEntityIds> compute(ExecutionPlan const & plan) const override {
        if (!plan.hasIntervals()) {
            throw std::runtime_error("IntervalPropertyComputer requires an ExecutionPlan with intervals");
        }

        auto intervals = plan.getIntervals();
        auto destinationTimeFrame = plan.getTimeFrame();

        // Validate that row intervals are a subset of source intervals
        _validateRowIntervalsAreSubset(intervals, destinationTimeFrame.get());

        std::vector<T> results;
        results.reserve(intervals.size());
        std::vector<EntityId> entity_ids;
        entity_ids.reserve(intervals.size());

        for (auto const & interval: intervals) {

            auto intervals_with_ids = m_source->viewInRange(interval.start,
                                                            interval.end,
                                                            *destinationTimeFrame);

            auto this_interval = intervals_with_ids.back();
            entity_ids.push_back(this_interval.entity_id);

            switch (m_property) {
                case IntervalProperty::Start:
                    results.push_back(static_cast<T>(this_interval.interval.start.getValue()));
                    break;
                case IntervalProperty::End:
                    results.push_back(static_cast<T>(this_interval.interval.end.getValue()));
                    break;
                case IntervalProperty::Duration:
                    results.push_back(static_cast<T>(this_interval.interval.end.getValue() - this_interval.interval.start.getValue()));
                    break;
                default:
                    throw std::runtime_error("Unknown IntervalProperty");
            }
        }

        return {results, entity_ids};
    }

    [[nodiscard]] auto getSourceDependency() const -> std::string override {
        return m_sourceName;
    }

    [[nodiscard]] EntityIdStructure getEntityIdStructure() const override {
        return EntityIdStructure::Simple;
    }

private:
    std::shared_ptr<DigitalIntervalSeries> m_source;
    IntervalProperty m_property;
    std::string m_sourceName;

    void _validateRowIntervalsAreSubset(std::vector<TimeFrameInterval> const & rowIntervals,
                                        TimeFrame const * destinationTimeFrame) const;
};


#endif// INTERVAL_PROPERTY_COMPUTER_H

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