repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
range.h
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <cstddef>
4#include <utility>
5#include <string_view>
6#include <bit>
7#include "util.h"
8
9
10namespace librepr::ctei {
11template <auto First, auto Last = First>
12struct Range {
13 static_assert(Last >= First);
14
15 constexpr static auto size = Last - First + 1;
16 constexpr static auto min = First;
17 constexpr static auto max = Last;
18
19 template <auto Idx>
20 requires (Idx <= size)
21 constexpr static auto get = Idx + First;
22
23 template <std::size_t amount = 1>
25
26private:
27 template <typename T, EnumKind Kind, std::size_t... Idx>
28 static consteval auto get_enum_names(std::index_sequence<Idx...>){
30 }
31
32public:
33 template <typename T, EnumKind Kind>
35
36 [[nodiscard]] constexpr static bool is_binary_powers() noexcept{
37 if constexpr (min >= 0 && max <= 2) {
38 // special case ranges containing only 0-2
39 return true;
40 } else if constexpr (std::has_single_bit(static_cast<std::size_t>(min))) {
41 // offset is power of 2 - range must contain element at (offset + 0)
42 return size == 1;
43 }
44 return false;
45 }
46
47 [[nodiscard]] constexpr static bool contains(auto value) noexcept {
48 return value >= min && value <= max;
49 }
50};
51} // namespace librepr::ctei
T has_single_bit(T... args)
Definition accessor.h:14
EnumKind
Definition enum/util.h:13
std::string code_for()
Definition repr:39
Definition range.h:12
static constexpr bool is_binary_powers() noexcept
Definition range.h:36
static constexpr auto get
Definition range.h:21
static constexpr auto min
Definition range.h:16
static constexpr auto max
Definition range.h:17
static constexpr auto enum_names
Definition range.h:34
static constexpr auto size
Definition range.h:15
static constexpr bool contains(auto value) noexcept
Definition range.h:47