libhatchet
Loading...
Searching...
No Matches
hxarray.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
8
9#include "libhatchet.h"
10
11// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
12#if HX_USE_MACROS_WITH_MODULE
13#error Header does not provide macros alone.
14#endif
15
16#include "hxallocator.hpp"
17#include "hxrange.hpp"
19#include "hxsort.hpp"
20
21HX_NS_BEGIN_
22
23#if HX_CPLUSPLUS >= 202002L
25template<typename T_>
26concept hxarray_concept_ = requires(T_& x_) {
27 sizeof(T_);
28 x_.~T_();
29};
31#else
32#define hxarray_concept_ typename
33#endif
34
51template<hxarray_concept_ T_, hxsize_t capacity_=hxallocator_dynamic_capacity>
52class hxarray : private hxallocator<T_, capacity_> {
53public:
55 using iterator = T_*;
56
58 using const_iterator = const T_*;
59
61 using value_t = T_;
62
64 explicit hxarray(void) noexcept;
65
68 explicit hxarray(const T_& x_) noexcept;
69
72 hxarray(const hxarray& x_) noexcept;
73
77 hxarray(hxarray&& x_) noexcept;
78
86 template<typename other_value_t_, hxsize_t array_length_>
87 hxarray(const other_value_t_(&array_)[array_length_]) noexcept;
88
92 template<typename other_value_t_>
93 hxarray(std::initializer_list<other_value_t_> x_) noexcept;
94
95#if HX_CPLUSPLUS >= 202002L
98 template<hxrange_concept_ range_t_>
100 explicit hxarray(range_t_& range_) noexcept;
101
106 template<hxrange_concept_ range_t_>
109 explicit hxarray(range_t_&& range_) noexcept;
110#endif
111
113 ~hxarray(void) noexcept;
114
115#if HX_CPLUSPLUS >= 202302L
121 template<typename self_t_, typename callable_t_>
123 this self_t_&& self_, hxsize_t index_, callable_t_&& callable_)
124 -> hxremove_cvref_t<decltype(
125 hxforward<callable_t_>(callable_)(
127
131 template<typename self_t_, typename callable_t_>
133 this self_t_&& self_, const T_* it_, callable_t_&& callable_)
134 -> hxremove_cvref_t<decltype(
137#endif // HX_CPLUSPLUS >= 202302L
138
141 void operator=(const hxarray& x_) noexcept;
142
146 void operator=(hxarray&& x_) noexcept;
147
155 template<typename other_value_t_, hxsize_t array_length_>
156 void operator=(const other_value_t_(&array_)[array_length_]) noexcept;
157
160 const T_& operator[](hxsize_t index_) const;
161
162 T_& operator[](hxsize_t index_);
163
173 template<typename callable_t_>
174 hxattr_nodiscard bool all_of(callable_t_&& callable_) const;
175
176 template<typename callable_t_>
177 bool all_of(callable_t_&& callable_);
178
188 template<typename callable_t_>
189 hxattr_nodiscard bool any_of(callable_t_&& callable_) const;
190
191 template<typename callable_t_>
192 bool any_of(callable_t_&& callable_);
193
195 const T_* begin(void) const { return this->data(); }
196
197 T_* begin(void) { return this->data(); }
198
202 hxattr_nodiscard const T_* binary_search(const T_& value_) const;
203
204 hxattr_nodiscard T_* binary_search(const T_& value_);
205
208
210 const T_* cbegin(void) const { return this->data(); }
211
213 const T_* cend(void) const { return this->data() + this->capacity(); }
214
216 using hxallocator<T_, capacity_>::data;
217
219 hxinline hxattr_nodiscard bool empty(void) const { return this->size() == 0; }
220
222 const T_* end(void) const { return this->data() + this->capacity(); }
223
224 T_* end(void) { return this->data() + this->capacity(); }
225
229 hxattr_nodiscard bool equal(const hxarray& x_) const;
230
234 hxattr_nodiscard const T_* find(const T_& value_) const;
235
236 hxattr_nodiscard T_* find(const T_& value_);
237
241 template<typename callable_t_>
242 hxattr_nodiscard const T_* find_if(callable_t_&& callable_) const;
243
244 template<typename callable_t_>
245 hxattr_nodiscard T_* find_if(callable_t_&& callable_);
246
248 hxinline hxattr_nodiscard bool full(void) const { return this->capacity() != 0; }
249
252 template<typename callable_t_>
253 void for_each(callable_t_&& callable_) const;
254
255 template<typename callable_t_>
256 void for_each(callable_t_&& callable_);
257
261
263 void insertion_sort(void) noexcept;
264
269 hxattr_nodiscard bool less(const hxarray& x_) const;
270
272 hxinline hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
273
276 void memcpy(const hxarray& x_);
277
280 void memset(int byte_=0x00);
281
282#if HX_CPLUSPLUS >= 202302L
288 template<typename self_t_, typename callable_t_>
290 this self_t_&& self_, hxsize_t index_, callable_t_&& callable_)
291 -> decltype(self_.end());
292
297 template<typename self_t_, typename callable_t_>
299 this self_t_&& self_, const T_* it_, callable_t_&& callable_)
300 -> decltype(self_.end());
301#endif // HX_CPLUSPLUS >= 202302L
302
310 void reserve(hxsize_t size_,
313
315 hxinline hxattr_nodiscard hxsize_t size(void) const { return this->capacity(); }
316
319 return hxsizeof<T_>() * this->capacity();
320 }
321
323 void sort(void) noexcept;
324
329 void swap(hxarray& x_) noexcept;
330
331#if HX_CPLUSPLUS >= 202302L
336 template<typename self_t_, typename... args_t_>
338 this self_t_&& self_, hxsize_t index_, args_t_&&... args_);
339
343 template<typename self_t_, typename... args_t_>
345 this self_t_&& self_, const T_* it_, args_t_&&... args_);
346#endif // HX_CPLUSPLUS >= 202302L
347
348private:
349 void destruct_(T_* begin_, T_* end_) noexcept;
350};
351
354template<typename T_, hxsize_t capacity_>
355class hxkey_equal_t<hxarray<T_, capacity_> > {
356public:
359 const hxarray<T_, capacity_>& x_, const hxarray<T_, capacity_>& y_) const {
360 return x_.equal(y_);
361 }
362};
363
366template<typename T_, hxsize_t capacity_>
367class hxkey_less_t<hxarray<T_, capacity_> > {
368public:
371 const hxarray<T_, capacity_>& x_, const hxarray<T_, capacity_>& y_) const {
372 return x_.less(y_);
373 }
374};
375
378template<typename T_, hxsize_t capacity_>
379class hxkey_hash_t<hxarray<T_, capacity_> > {
380public:
382 hxhash_t operator()(const hxarray<T_, capacity_>& x_) const { return x_.hash(); }
383};
384
385#include "detail/hxarray.inl"
386HX_NS_END_
const T * data(void) const
Returns a pointer to a const and potentially uninitialized array of T.
Definition hxallocator.hpp:96
hxallocator(void)
Does not allocate until reserve_storage is called.
Definition hxallocator.hpp:78
hxallocator<1+> - Provides static allocation when capacity is greater than zero.
Definition hxallocator.hpp:25
hxarray - Implements std::array with a few things added.
Definition hxarray.hpp:52
auto or_else(this self_t_ &&self_, hxsize_t index_, callable_t_ &&callable_) -> decltype(self_.end())
Returns an iterator to the element at index, or the result of calling callable when the index is outs...
const T * find(const T &value_) const
Finds the first occurrence of value using hxkey_equal.
bool less(const hxarray &x_) const
Returns true if this array compares less than x using hxkey_equal and hxkey_less.
hxarray(void) noexcept
Default constructs all elements.
const T * cbegin(void) const
Returns a const T* to the beginning of the array (alias for begin).
Definition hxarray.hpp:210
hxarray(const T &x_) noexcept
Constructs all elements as copies of x.
bool all_of(callable_t_ &&callable_) const
Returns true if the predicate returns true for every element and false otherwise.
T * find(const T &value_)
auto and_then(this self_t_ &&self_, hxsize_t index_, callable_t_ &&callable_) -> hxremove_cvref_t< decltype(hxforward< callable_t_ >(callable_)(hxforward_like< self_t_ >(hxdeclval< T & >())))>
Returns the result of calling callable with the element at index, otherwise returns hxnil.
auto and_then(this self_t_ &&self_, const T *it_, callable_t_ &&callable_) -> hxremove_cvref_t< decltype(hxforward< callable_t_ >(callable_)(hxforward_like< self_t_ >(hxdeclval< const T & >())))>
Iterator overload of and_then.
void swap(hxarray &x_) noexcept
Swap contents with a temporary array.
hxsize_t size_bytes(void) const
Returns the number of bytes in the array. (Non-standard.).
Definition hxarray.hpp:318
const T * find_if(callable_t_ &&callable_) const
Finds the first element for which the predicate returns true.
~hxarray(void) noexcept
Destructs the array and destroys all elements.
T value_t
value_type - Publishes the value type.
Definition hxarray.hpp:61
const T * binary_search(const T &value_) const
Performs a binary search using hxkey_less.
const T * const_iterator
const_iterator - Const random access iterator.
Definition hxarray.hpp:58
hxsize_t size(void) const
Returns the number of elements in the array.
Definition hxarray.hpp:315
hxsize_t capacity(void) const
Returns the capacity of the array.
T * iterator
iterator - Random access iterator.
Definition hxarray.hpp:55
T value_or(this self_t_ &&self_, hxsize_t index_, args_t_ &&... args_)
Returns a copy of the element at index, or a T constructed from args when the index is outside the ar...
void for_each(callable_t_ &&callable_) const
Calls a function, lambda, or std::function on each element.
T * end(void)
Definition hxarray.hpp:224
hxarray(range_t_ &range_) noexcept
Constructs by copying elements from a range referenced by an lvalue.
const T * begin(void) const
Returns a const T* to the beginning of the array.
Definition hxarray.hpp:195
void insertion_sort(void) noexcept
Sorts the array with insertion sort using hxkey_less.
void memset(int byte_=0x00)
Calls memset on the array.
const T * end(void) const
Returns a const T* to the end of the array.
Definition hxarray.hpp:222
const T * cend(void) const
Returns a const T* to the end of the array.
Definition hxarray.hpp:213
T value_or(this self_t_ &&self_, const T *it_, args_t_ &&... args_)
Iterator overload of value_or.
void sort(void) noexcept
Sorts the array using hxkey_less.
hxarray(hxarray &&x_) noexcept
Move constructs from a temporary hxarray.
bool any_of(callable_t_ &&callable_) const
Returns true if the predicate returns true for any element and false otherwise.
auto or_else(this self_t_ &&self_, const T *it_, callable_t_ &&callable_) -> decltype(self_.end())
Iterator overload of or_else.
bool full(void) const
Returns true if the array has been allocated.
Definition hxarray.hpp:248
T * binary_search(const T &value_)
hxarray(range_t_ &&range_) noexcept
Constructs by moving elements from a temporary range.
bool any_of(callable_t_ &&callable_)
void reserve(hxsize_t size_, hxsystem_allocator_t allocator_=hxsystem_allocator_current, hxalignment_t alignment_=hxalignment) noexcept
Allocates storage for size elements and default constructs them when capacity is hxallocator_dynamic_...
hxsize_t max_size(void) const
Returns the capacity of the array.
Definition hxarray.hpp:272
hxhash_t hash(void) const
Returns a hash mixing the hashes of every element in order using hxkey_hash.
bool empty(void) const
Returns true if the array contains no elements.
Definition hxarray.hpp:219
T * begin(void)
Definition hxarray.hpp:197
hxarray(std::initializer_list< other_value_t_ > x_) noexcept
Pass values of std::initializer_list as initializers.
bool equal(const hxarray &x_) const
Returns true if the arrays compare equivalent using hxkey_equal.
void for_each(callable_t_ &&callable_)
T & operator[](hxsize_t index_)
hxarray(const other_value_t_(&array_)[array_length_]) noexcept
Constructs from a C-style array.
const T & operator[](hxsize_t index_) const
Returns a const reference to the element at the specified index.
void operator=(hxarray &&x_) noexcept
Move assigns from a temporary hxarray.
hxarray(const hxarray &x_) noexcept
Copy constructs from another hxarray of the same capacity.
T * find_if(callable_t_ &&callable_)
void operator=(const other_value_t_(&array_)[array_length_]) noexcept
Assign from a C-style array.
void memcpy(const hxarray &x_)
Copies another hxarray using memcpy.
void operator=(const hxarray &x_) noexcept
Assigns the contents of another hxarray of the same capacity.
bool all_of(callable_t_ &&callable_)
bool operator()(const hxarray< T, capacity_ > &x_, const hxarray< T, capacity_ > &y_) const
Definition hxarray.hpp:358
hxkey_equal_t<T> - A constexpr callable that returns true if two objects are equivalent.
Definition hxkey.hpp:57
hxhash_t operator()(const hxarray< T, capacity_ > &x_) const
Definition hxarray.hpp:382
hxkey_hash_t<T> - A callable that returns the xxhash32 of a numeric value cast to 32 bits.
Definition hxkey.hpp:146
bool operator()(const hxarray< T, capacity_ > &x_, const hxarray< T, capacity_ > &y_) const
Definition hxarray.hpp:370
hxkey_less_t<T> - A constexpr callable that returns true if the first object is less than the second.
Definition hxkey.hpp:91
Similar to std::allocator.
Implements or includes std::initializer_list depending on HX_USE_LIBCXX.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:80
HX_C_BEGIN_ typedef unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:62
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:75
hxattr_noexcept hxsystem_allocator_t allocator_
Definition hxmemory_manager.h:120
hxattr_noexcept hxsystem_allocator_t hxalignment_t alignment_
Definition hxmemory_manager.h:121
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:67
Read-only range utilities for libhatchet.
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:98
#define hxattr_flatten
hxattr_flatten - Inline every call inside a function's body into it, recursively, regardless of the c...
Definition hxsettings.h:90
Standard sorting utilities.
constexpr bool hxis_same(void)
hxis_same<A, B> - Implements std::is_same.
Definition hxutility.h:246
constexpr T && hxforward(hxremove_reference_t< T > &&x_)
hxforward - Implements std::forward.
Definition hxutility.h:353
hxremove_cv_t< hxremove_reference_t< T > > hxremove_cvref_t
hxremove_cvref_t<T> - Returns T with const, volatile, and references removed.
Definition hxutility.h:103
constexpr bool hxis_lvalue_reference(void)
hxis_lvalue_reference<T> - Implements std::is_lvalue_reference.
Definition hxutility.h:207
T && hxdeclval(void) noexcept
hxdeclval - Implements std::declval.
constexpr hxforward_like_t_< like_t_, value_t_ > hxforward_like(value_t_ &value_)
hxforward_like<T> - Returns value with the constness and value category of T.
Definition hxutility.h:387
Provides core macros, memory management and feature detection.
constexpr hxsize_t hxsizeof(void)
hxsizeof - Returns the size of a type or expression as hxsize_t.
Definition libhatchet.h:294
uint32_t hxhash_t
hxhash_t - Unsigned 32-bit hash value. Expect collisions.
Definition libhatchet.h:210
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:207