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/utility.hpp"
8#include "../macros.hpp"
9#include <cstddef>
10#include <climits>
11#include <concepts>
12#include <array>
13
14
15
16namespace hi::inline v1 {
17
25template<size_t BitsPerInteger, size_t N>
27public:
30 constexpr static size_t bits_per_integer = BitsPerInteger;
31
36 constexpr static size_t store_size = ceil(bits_per_integer + CHAR_BIT - 1, size_t{CHAR_BIT}) / size_t{CHAR_BIT};
37 static_assert(sizeof(unsigned long long) >= store_size);
38
39 // clang-format off
45 using value_type =
46 std::conditional_t<sizeof(unsigned char) >= store_size, unsigned char,
47 std::conditional_t<sizeof(unsigned short) >= store_size, unsigned short,
48 std::conditional_t<sizeof(unsigned int) >= store_size, unsigned int,
49 std::conditional_t<sizeof(unsigned long) >= store_size, unsigned long,
50 unsigned long long>>>>;
51 // clang-format on
52
53
58 template<std::integral... Args>
59 constexpr packed_int_array(Args... args) noexcept : _v(make_v(args...)) {}
60
63 [[nodiscard]] constexpr size_t size() const noexcept
64 {
65 return N;
66 }
67
77 [[nodiscard]] constexpr value_type operator[](size_t i) const noexcept
78 {
79 hi_axiom(i < N);
80
81 hilet offset = i * bits_per_integer;
82 hilet byte_offset = offset / CHAR_BIT;
83 hilet bit_offset = offset % CHAR_BIT;
84
85 return (unaligned_load<value_type>(_v.data() + byte_offset) >> bit_offset) & mask;
86 }
87
98 template<size_t I>
99 [[nodiscard]] friend constexpr value_type get(packed_int_array const &rhs) noexcept
100 {
101 static_assert(I < N);
102 constexpr size_t offset = I * bits_per_integer;
103 constexpr size_t byte_offset = offset / CHAR_BIT;
104 constexpr size_t bit_offset = offset % CHAR_BIT;
105
106 return (load<value_type>(rhs._v.data() + byte_offset) >> bit_offset) & mask;
107 }
108
109private:
110 constexpr static size_t total_num_bits = bits_per_integer * N;
111 constexpr static size_t total_num_bytes = (total_num_bits + CHAR_BIT - 1) / CHAR_BIT;
112 constexpr static size_t mask = (1_uz << bits_per_integer) - 1;
113
114 using array_type = std::array<uint8_t, total_num_bytes>;
115
116 array_type _v;
117
123 template<std::integral... Args>
124 [[nodiscard]] constexpr static array_type make_v(Args... args) noexcept
125 {
126 static_assert(sizeof...(Args) == N);
127
128 auto r = array_type{};
129
130 hilet args_ = std::array<value_type, N>{narrow_cast<value_type>(args)...};
131 for (auto i = 0_uz; i != N; ++i) {
132 hilet offset = i * bits_per_integer;
133 hilet byte_offset = offset / CHAR_BIT;
134 hilet bit_offset = offset % CHAR_BIT;
135
136 hilet arg = args_[i];
137 hi_axiom(arg <= mask);
138 store_or(arg << bit_offset, r.data() + byte_offset);
139 }
140
141 return r;
142 }
143};
144
145template<size_t BitsPerInteger, std::unsigned_integral... Args>
146packed_int_array(Args...) -> packed_int_array<BitsPerInteger, sizeof...(Args)>;
147
148}
149
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
An array of integers.
Definition packed_int_array.hpp:26
constexpr size_t size() const noexcept
The number of integers stored in the array.
Definition packed_int_array.hpp:63
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:45
constexpr value_type operator[](size_t i) const noexcept
Get the integer at an index.
Definition packed_int_array.hpp:77
constexpr packed_int_array(Args... args) noexcept
Constructor of the array.
Definition packed_int_array.hpp:59
friend constexpr value_type get(packed_int_array const &rhs) noexcept
Get the integer at an index.
Definition packed_int_array.hpp:99