HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
packed_int_array.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "utility/module.hpp"
8#include <cstddef>
9#include <climits>
10#include <concepts>
11#include <array>
12
13namespace hi::inline v1 {
14
22template<size_t BitsPerInteger, size_t N>
24public:
27 constexpr static size_t bits_per_integer = BitsPerInteger;
28
33 constexpr static size_t store_size = ceil(bits_per_integer + CHAR_BIT - 1, size_t{CHAR_BIT}) / size_t{CHAR_BIT};
34 static_assert(sizeof(unsigned long long) >= store_size);
35
36 // clang-format off
42 using value_type =
43 std::conditional_t<sizeof(unsigned char) >= store_size, unsigned char,
44 std::conditional_t<sizeof(unsigned short) >= store_size, unsigned short,
45 std::conditional_t<sizeof(unsigned int) >= store_size, unsigned int,
46 std::conditional_t<sizeof(unsigned long) >= store_size, unsigned long,
47 unsigned long long>>>>;
48 // clang-format on
49
50
55 template<std::integral... Args>
56 constexpr packed_int_array(Args... args) noexcept : _v(make_v(args...)) {}
57
60 [[nodiscard]] constexpr size_t size() const noexcept
61 {
62 return N;
63 }
64
74 [[nodiscard]] constexpr value_type operator[](size_t i) const noexcept
75 {
76 hi_axiom(i < N);
77
78 hilet offset = i * bits_per_integer;
79 hilet byte_offset = offset / CHAR_BIT;
80 hilet bit_offset = offset % CHAR_BIT;
81
82 return (unaligned_load<value_type>(_v.data() + byte_offset) >> bit_offset) & mask;
83 }
84
95 template<size_t I>
96 [[nodiscard]] friend constexpr value_type get(packed_int_array const &rhs) noexcept
97 {
98 static_assert(I < N);
99 constexpr size_t offset = I * bits_per_integer;
100 constexpr size_t byte_offset = offset / CHAR_BIT;
101 constexpr size_t bit_offset = offset % CHAR_BIT;
102
103 return (load<value_type>(rhs._v.data() + byte_offset) >> bit_offset) & mask;
104 }
105
106private:
107 constexpr static size_t total_num_bits = bits_per_integer * N;
108 constexpr static size_t total_num_bytes = (total_num_bits + CHAR_BIT - 1) / CHAR_BIT;
109 constexpr static size_t mask = (1_uz << bits_per_integer) - 1;
110
111 using array_type = std::array<uint8_t, total_num_bytes>;
112
113 array_type _v;
114
120 template<std::integral... Args>
121 [[nodiscard]] constexpr static array_type make_v(Args... args) noexcept
122 {
123 static_assert(sizeof...(Args) == N);
124
125 auto r = array_type{};
126
127 hilet args_ = std::array<value_type, N>{narrow_cast<value_type>(args)...};
128 for (auto i = 0_uz; i != N; ++i) {
129 hilet offset = i * bits_per_integer;
130 hilet byte_offset = offset / CHAR_BIT;
131 hilet bit_offset = offset % CHAR_BIT;
132
133 hilet arg = args_[i];
134 hi_axiom(arg <= mask);
135 store_or(arg << bit_offset, r.data() + byte_offset);
136 }
137
138 return r;
139 }
140};
141
142template<size_t BitsPerInteger, std::unsigned_integral... Args>
143packed_int_array(Args...) -> packed_int_array<BitsPerInteger, sizeof...(Args)>;
144
145}
146
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
An array of integers.
Definition packed_int_array.hpp:23
constexpr size_t size() const noexcept
The number of integers stored in the array.
Definition packed_int_array.hpp:60
std::conditional_t< sizeof(unsigned char) >=store_size, unsigned char, std::conditional_t< sizeof(unsigned short) >=store_size, unsigned short, std::conditional_t< sizeof(unsigned int) >=store_size, unsigned int, std::conditional_t< sizeof(unsigned long) >=store_size, unsigned long, unsigned long long > > > > value_type
The value type of the unsigned integers that are stored in the array.
Definition packed_int_array.hpp:42
constexpr value_type operator[](size_t i) const noexcept
Get the integer at an index.
Definition packed_int_array.hpp:74
constexpr packed_int_array(Args... args) noexcept
Constructor of the array.
Definition packed_int_array.hpp:56
friend constexpr value_type get(packed_int_array const &rhs) noexcept
Get the integer at an index.
Definition packed_int_array.hpp:96