mercredi 2 octobre 2019

Complex std::map, structure, std::deque problem

My objective is to have an ordered map of timeframes (it's for timeseries data analysis), indexed via a vector (as I sometimes need to reference the structure sequentially and not via the map key), with references to the OHLCCandle structure and having the resulting data presented via the deque.

Data access will be like: data_[5].get().open[0] meaning accessing the 5 min timeframe retrieving the open data at shift 0 or as another example, data_[15].get().close[4] meaning accessing the 15 min timeframe retrieving the close data at shift 4 (0 being the most recent).

My code so far is:

#include <deque>
#include <vector>
#include <map>
#include <iostream>

template<typename Price>
struct OHLCCandle final
{
    static_assert(std::is_floating_point<Price>::value, "");

    using Container = std::deque<Price>;

    Container open;
    Container high;
    Container low;
    Container close;
};

using Candle = OHLCCandle<double>;
using CandleContainer = std::vector<Candle>;
using Timeframe = int;

std::map<Timeframe, std::reference_wrapper<Candle>> data_;

auto emplace(const int timeframe, const Candle& candle) -> decltype(auto)
{
    auto error = int{ 0 };
    try
    {
        auto candles = CandleContainer{};
        candles.push_back(candle);

        const auto pair = data_.emplace(std::make_pair(timeframe, candles));

        if (!pair.second)
        {
            std::cout << "ERROR: Emplacement failed, doing nothing. timeframe = " << timeframe << std::endl;
            error = (std::numeric_limits<int>::min)();
        }
    }
    catch (const std::exception& e) {
        std::cout << "ERROR: Unknown error occured, ignoring. timeframe = " << timeframe << ", erc = " << e.what() << std::endl;
        error = (std::numeric_limits<int>::min)();
    }

    return error;
};

But, I'm unsure how to construct the CandleContainer structure so that it can be combined with the std::map part of the code and I'm receiving the following errors under godbolt.org (code is godbolt.org ready!):

[x64 msvc v19.22 #1] note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>>>(std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>> &&)' being compiled
[x64 msvc v19.22 #1] note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>>>(std::pair<int,std::vector<Candle,std::allocator<OHLCCandle<double>>>> &&)' being compiled

What do I need to do to achieve my objectives and resolve the compilation errors?

Thanks.

Aucun commentaire:

Enregistrer un commentaire