1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#pragma once


#include <initializer_list><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <valarray><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <numeric>


#include "snippet/iterations.hpp"

#include "internal/types.hpp"
#include "internal/dev_env.hpp"

#include "adaptor/valarray.hpp"

#include <concepts><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ranges><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


namespace uni {


template<class T = internal::size_t>
struct leveler {
    using value_type = T;
    using size_type = internal::size_t;

  private:
    uni::valarray<value_type> _bases;
    size_type _dim;
    value_type _max;

    inline value_type _compute_max() const noexcept(NO_EXCEPT) {
        return std::reduce(std::begin(this->_bases), std::end(this->_bases), 1, std::multiplies<value_type>{});
    }

  public:
    leveler(const std::initializer_list<value_type> bases) noexcept(NO_EXCEPT) : _bases(bases), _dim(std::ranges::size(bases)) {
        this->_max = this->_compute_max();
    }

    template<class I>
    leveler(I first, I last) noexcept(NO_EXCEPT) : _bases(first, last), _dim(std::ranges::distance(first, last)) {
        this->_max = this->_compute_max();
    }

    template<std::ranges::forward_range R>
    leveler(R&& range) noexcept(NO_EXCEPT) : leveler(std::ranges::begin(range), std::ranges::end(range)) {}


    template<std::integral... Values>
    leveler(const Values... bases) noexcept(NO_EXCEPT) : leveler(std::initializer_list<value_type>{ bases... }) {}

    inline size_type dimension() const noexcept(NO_EXCEPT) { return this->_dim; }

    inline value_type sup() const noexcept(NO_EXCEPT) { return this->_max; }

    inline value_type convert(const std::initializer_list<value_type> inds) const noexcept(NO_EXCEPT) {
        return this->convert(inds | std::views::all);
    }

    template<std::ranges::forward_range R>
    inline value_type convert(R&& range) const noexcept(NO_EXCEPT) {
        assert(std::ranges::distance(range) == std::ranges::ssize(this->_bases));

        value_type res = 0;
        {
            auto size = std::ranges::begin(this->_bases);
            ITR(v, range) {
                assert(0 <= v and v < *size);
                res *= *(size++);
                res += v;
            }
        }

        return res;
    }

    template<std::integral... Values>
    inline value_type convert(const Values... inds) const noexcept(NO_EXCEPT) {
        return this->convert({ inds... });
    }

    inline uni::valarray<value_type> revert(value_type id) const noexcept(NO_EXCEPT) {
        assert(0 <= id and id < this->sup());

        uni::valarray<value_type> res(std::size(this->_bases));

        REPD(i, std::size(this->_bases)) {<--- Unsigned positive
            res[i] = id % this->_bases[i];
            id /= this->_bases[i];
        }

        return res;
    }

    auto _debug() const { return this->_bases; }
};


}