Pixie
Loading...
Searching...
No Matches
integer_vector.h
Go to the documentation of this file.
1#pragma once
2
10
11#include <concepts>
12#include <cstddef>
13#include <cstdint>
14#include <limits>
15#include <span>
16#include <stdexcept>
17#include <type_traits>
18
19namespace pixie {
20
22template <class Value>
24 std::unsigned_integral<Value> &&
25 std::same_as<Value, std::remove_cv_t<Value>> &&
26 !std::same_as<Value, bool> && (std::numeric_limits<Value>::digits <= 64);
27
39template <class Impl, IntegerVectorValue Value = std::uint64_t>
41 public:
43 using value_type = Value;
44
46 using size_type = std::size_t;
47
49 size_type size() const { return impl().size_impl(); }
50
52 bool empty() const { return size() == 0; }
53
61 return impl().value_at_impl(position);
62 }
63
68 value_type at(size_type position) const {
69 if (position >= size()) {
70 throw std::out_of_range("Integer-vector position is out of range");
71 }
72 return (*this)[position];
73 }
74
84 void copy_to(size_type begin, std::span<value_type> output) const {
85 const size_type vector_size = size();
86 if (begin > vector_size || output.size() > vector_size - begin) {
87 throw std::out_of_range("Integer-vector copy range is out of bounds");
88 }
89 impl().copy_to_impl(begin, output);
90 }
91
98 requires requires(const Impl& concrete) {
99 { concrete.memory_usage_bytes_impl() } -> std::convertible_to<size_type>;
100 }
101 {
102 return impl().memory_usage_bytes_impl();
103 }
104
105 private:
107 const Impl& impl() const { return static_cast<const Impl&>(*this); }
108};
109
121template <class Impl, IntegerVectorValue Value = std::uint64_t>
122class MonotoneIntegerVectorBase : public IntegerVectorBase<Impl, Value> {
123 public:
126
132 return impl().lower_bound_index_impl(x);
133 }
134
140 return impl().upper_bound_index_impl(x);
141 }
142
144 bool contains(value_type x) const {
145 const size_type position = lower_bound_index(x);
146 return position != this->size() && (*this)[position] == x;
147 }
148
152 }
153
154 private:
156 const Impl& impl() const { return static_cast<const Impl&>(*this); }
157};
158
159} // namespace pixie
CRTP facade for an immutable positional integer vector.
Definition integer_vector.h:40
bool empty() const
Return whether the vector has no elements.
Definition integer_vector.h:52
value_type at(size_type position) const
Read the element at zero-based position with bounds checking.
Definition integer_vector.h:68
void copy_to(size_type begin, std::span< value_type > output) const
Copy a checked contiguous source range into output.
Definition integer_vector.h:84
value_type operator[](size_type position) const
Read the element at zero-based position without bounds checking.
Definition integer_vector.h:60
size_type size() const
Return the logical number of elements.
Definition integer_vector.h:49
Value value_type
Stored integer type.
Definition integer_vector.h:43
size_type memory_usage_bytes() const
Return total memory owned by this vector when supported.
Definition integer_vector.h:97
std::size_t size_type
Type used for element counts and zero-based positions.
Definition integer_vector.h:46
CRTP refinement for duplicate-preserving monotone integer vectors.
Definition integer_vector.h:122
size_type upper_bound_index(value_type x) const
Return the first index whose value is greater than x.
Definition integer_vector.h:139
size_type lower_bound_index(value_type x) const
Return the first index whose value is greater than or equal to x.
Definition integer_vector.h:131
size_type count(value_type x) const
Return the number of elements equal to x.
Definition integer_vector.h:150
bool contains(value_type x) const
Return whether at least one element equals x.
Definition integer_vector.h:144
An unsigned integer-vector value type no wider than 64 bits.
Definition integer_vector.h:23