0

I'm using the ranges library (eric niebler ranges) and I'm trying to write a ranges combination but the ranges::views::counted(1) option I'm using doesn't work.

I'm trying to ignore from the right, to the target time minus 1 further period (5mins). Then, extract next 1x value.

Here's an visual example (not related to the code below) of what I'm trying to achieve.

time
1:00 1:01 1:02 1:03 1:04 1:05 1:06 1:07 1:08 1:09 1:10 1:11 1:12 1:13 1:14 1:15 1:16
                                                                                ^start_reading
                                                                      ^target_time
                         ^target_time minus 1x5mins periods, 
                          start extraction here 
                    ^extract the value associated with this time

The code I've developed so far is as follows:

#include <fmt/format.h>
#include <range/v3/all.hpp>
#include <chrono>
#include <vector>
#include <iostream>

struct th {
    std::chrono::system_clock::time_point timestamp;
    double h;
};

constexpr auto period_start(const std::chrono::system_clock::time_point timestamp,
                            const std::chrono::minutes timeframe)
    -> std::chrono::system_clock::time_point
{
    using namespace std::chrono;

    const auto time = hh_mm_ss{floor<minutes>(timestamp - floor<days>(timestamp))};
    return timestamp - (time.minutes() % timeframe);
}

auto main() -> int
{
    using namespace std::chrono_literals;
    namespace views = ranges::views;

    const auto timeframe = 5min;

    { 
        const auto tp = std::chrono::system_clock::from_time_t(0); // just used for populating the example vector
        const auto timestamped_h = std::vector<th>{
            {tp + 0min , 1.0}, {tp + 1min , 1.1}, {tp + 3min , 1.2}, {tp + 4min , 1.2},
            {tp + 5min , 0.8}, {tp + 6min , 0.9}, {tp + 7min , 1.0},
            {tp + 10min, 1.1}, {tp + 11min, 1.3}, {tp + 12min, 1.2}, {tp + 13min, 1.1}, {tp + 14min, 0.9},
            {tp + 15min, 0.8}, {tp + 16min, 1.4}, {tp + 17min, 1.3}, {tp + 18min, 1.6}, {tp + 19min, 1.5}
        };

        const auto target_tp = tp + 17min;
        const auto remove_candle = [target_tp,timeframe](const auto& lhs) { 
            return lhs.timestamp >= period_start(target_tp - 1* timeframe,timeframe); 
        };

        auto pivots = timestamped_h 
            | views::reverse
            | views::remove_if(remove_candle)
            //| views::filter(remove_candle)
            | ranges::views::counted(1);

        for(auto p : pivots)
            std::cout << p.h << std::endl;
    }
}

But the ranges::views::counted(1) option produces the following error: error: no match for call to '(const ranges::views::counted_fn) (int)'

A trail area is provided here

What am I doing wrong, please?

Andrew
  • 626
  • 6
  • 16

1 Answers1

0

It was ::counted that I needed, it was ::take. Final result:

        auto pivots = timestamped_h 
            | views::reverse
            | views::remove_if(remove_candle)
            | ranges::views::take(1);
Andrew
  • 626
  • 6
  • 16