StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
vector.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
42#include <StormByte/size.hxx>
46
47#include <compare>
48#include <cstddef>
49#include <initializer_list>
50#include <iterator>
51#include <limits>
52#include <span>
53#include <utility>
54#include <vector>
55
60namespace StormByte {
65 namespace Safe {
71 [[noreturn]] STORMBYTE_PUBLIC void ThrowVectorOutOfBounds(const Size& index);
72
80 template<Type::SafeValue T>
82 public:
83 using value_type = T;
84 using size_type = Size;
85 using difference_type = std::ptrdiff_t;
86 using reference = T&;
87 using const_reference = const T&;
88 using pointer = T*;
89 using const_pointer = const T*;
90 using iterator = T*;
91 using const_iterator = const T*;
92 using reverse_iterator = std::reverse_iterator<iterator>;
93 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
94
98 Vector() noexcept;
99
105 explicit Vector(size_type count);
106
113 Vector(size_type count, const T& value);
114
122 template<std::input_iterator InputIt>
123 Vector(InputIt first, InputIt last);
124
130 Vector(std::initializer_list<T> values);
131
137 explicit Vector(const std::vector<T>& values);
138
144 STORMBYTE_FORCE_INLINE explicit Vector(std::vector<T>&& values): Vector() {
145 reserve(values.size());
146 if constexpr (Type::SameAs<T, bool>) {
147 for (bool value : values)
148 push_back(value);
149 } else {
150 for (T& value : values)
151 push_back(std::move(value));
152 }
153 values.clear();
154 }
155
161 Vector(const Vector& other);
162
167 Vector(Vector&& other) noexcept;
168
172 ~Vector() noexcept;
173
180 Vector& operator=(const Vector& other);
181
187 Vector& operator=(Vector&& other) noexcept;
188
195 Vector& operator=(std::initializer_list<T> values);
196
203 Vector& operator=(const std::vector<T>& values);
204
211 STORMBYTE_FORCE_INLINE Vector& operator=(std::vector<T>&& values) {
212 Vector replacement(std::move(values));
213 swap(replacement);
214 return *this;
215 }
216
221 iterator begin() noexcept;
222
227 iterator end() noexcept;
228
233 const_iterator begin() const noexcept;
234
239 const_iterator end() const noexcept;
240
245 const_iterator cbegin() const noexcept;
246
251 const_iterator cend() const noexcept;
252
260
268
276
284
290 return rbegin();
291 }
292
298 return rend();
299 }
300
305 bool empty() const noexcept;
306
311 size_type size() const noexcept;
312
318 return std::numeric_limits<size_type>::max() / sizeof(T);
319 }
320
325 size_type capacity() const noexcept;
326
331 pointer data() noexcept;
332
337 const_pointer data() const noexcept;
338
343 STORMBYTE_FORCE_INLINE operator std::span<T>() noexcept {
344 return std::span<T>(data(), size());
345 }
346
351 STORMBYTE_FORCE_INLINE operator std::span<const T>() const noexcept {
352 return std::span<const T>(data(), size());
353 }
354
360 void reserve(size_type count);
361
366
370 void clear() noexcept;
371
376 void swap(Vector& other) noexcept;
377
383 reference operator[](size_type index) noexcept;
384
390 const_reference operator[](size_type index) const noexcept;
391
399
407
414
421
428
435
441 void push_back(const T& value);
442
448 void push_back(T&& value);
449
456 template<std::ranges::input_range R>
457 void append_range(R&& range);
458
463 void pop_back();
464
472 template<class... Args>
473 reference emplace_back(Args&&... args);
474
482 iterator insert(const_iterator position, const T& value);
483
491 iterator insert(const_iterator position, T&& value);
492
501 iterator insert(const_iterator position, size_type count, const T& value);
502
512 template<std::input_iterator InputIt>
513 iterator insert(const_iterator position, InputIt first, InputIt last);
514
522 iterator insert(const_iterator position, std::initializer_list<T> values);
523
532 template<std::ranges::input_range R>
534
543 template<class... Args>
544 iterator emplace(const_iterator position, Args&&... args);
545
552
560
567 void assign(size_type count, const T& value);
568
576 template<std::input_iterator InputIt>
577 void assign(InputIt first, InputIt last);
578
584 void assign(std::initializer_list<T> values);
585
592 template<std::ranges::input_range R>
593 void assign_range(R&& range);
594
600 void resize(size_type count);
601
608 void resize(size_type count, const T& value);
609
615 bool operator==(const Vector& other) const requires Type::EqualityComparable<T>;
616
622 STORMBYTE_FORCE_INLINE std::strong_ordering operator<=>(const Vector& other) const requires requires(const T& left, const T& right) { left < right; };
623
629 bool operator<(const Vector& other) const requires requires(const T& left, const T& right) { left < right; };
630
636 bool operator<=(const Vector& other) const requires requires(const T& left, const T& right) { left < right; };
637
643 bool operator>(const Vector& other) const requires requires(const T& left, const T& right) { left < right; };
644
650 bool operator>=(const Vector& other) const requires requires(const T& left, const T& right) { left < right; };
651
656 STORMBYTE_FORCE_INLINE explicit operator std::vector<T>() const {
657 return std::vector<T>(begin(), end());
658 }
659
660 private:
667 static T* Allocate(size_type capacity);
668
674 static void Release(T* data, size_type count) noexcept;
675
680 void Grow();
681
682 T* m_data;
683 size_type m_size;
684 size_type m_capacity;
685 };
686
692 template<Type::SafeValue T>
693 void swap(Vector<T>& lhs, Vector<T>& rhs) noexcept {
694 lhs.swap(rhs);
695 }
696 }
697
702 namespace Type {
707 template<SafeValue T>
708 requires IsSafe<T>::value
709 struct IsSafe<Safe::Vector<T>>: std::true_type {};
710
715 template<SafeValue T>
716 requires MaybeSafe<T>
717 struct IsMaybeSafe<Safe::Vector<T>>: std::true_type {};
718
723 template<SafeValue T>
724 struct IsSafeValue<Safe::Vector<T>>: std::true_type {};
725 }
726}
727
746
Contiguous sequence stored on Base's heap.
Definition vector.hxx:81
STORMBYTE_FORCE_INLINE const_reverse_iterator crend() const noexcept
Return the read-only reverse end iterator.
Definition vector.hxx:297
T * pointer
Mutable element pointer.
Definition vector.hxx:88
void push_back(const T &value)
Append a copy.
void reserve(size_type count)
Reserve room for at least count elements.
reference at(size_type index)
Access an element.
iterator end() noexcept
Return the mutable end iterator.
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only reverse iterator.
Definition vector.hxx:93
void assign_range(R &&range)
Replace the contents with a copy of range.
void shrink_to_fit()
Release unused capacity.
STORMBYTE_FORCE_INLINE reverse_iterator rend() noexcept
Return the mutable reverse end iterator.
Definition vector.hxx:265
STORMBYTE_FORCE_INLINE const_reverse_iterator crbegin() const noexcept
Return a read-only reverse iterator to the last element.
Definition vector.hxx:289
bool operator<(const Vector &other) const
Order sequences lexicographically.
iterator begin() noexcept
Return a mutable iterator to the first element.
Vector(Vector &&other) noexcept
Take the block.
reference front()
Return the first element.
void pop_back()
Remove the last element.
Size size_type
Element count. Size, not std::size_t.
Definition vector.hxx:84
const T & const_reference
Read-only element reference.
Definition vector.hxx:87
iterator emplace(const_iterator position, Args &&... args)
Construct an element before position.
void swap(Vector &other) noexcept
Exchange blocks.
Vector() noexcept
Construct an empty sequence.
T & reference
Mutable element reference.
Definition vector.hxx:86
~Vector() noexcept
Destroy every element and release the block.
size_type size() const noexcept
Return the element count.
iterator erase(const_iterator position)
Erase one element.
STORMBYTE_FORCE_INLINE const_reverse_iterator rend() const noexcept
Return the read-only reverse end iterator.
Definition vector.hxx:281
void assign(size_type count, const T &value)
Replace the contents with count copies.
void clear() noexcept
Destroy every element.
pointer data() noexcept
Return a pointer to the block.
reference back()
Return the last element.
bool operator>(const Vector &other) const
Order sequences lexicographically.
Vector(const Vector &other)
Copy every element into a new Base block.
bool operator>=(const Vector &other) const
Order sequences lexicographically.
iterator insert_range(const_iterator position, R &&range)
Insert a copy of each element of range before position.
void resize(size_type count)
Change the size, value-initializing new elements.
T value_type
Element type.
Definition vector.hxx:83
const T * const_pointer
Read-only element pointer.
Definition vector.hxx:89
STORMBYTE_FORCE_INLINE const_reverse_iterator rbegin() const noexcept
Return a read-only reverse iterator to the last element.
Definition vector.hxx:273
size_type capacity() const noexcept
Return the allocated capacity.
bool empty() const noexcept
Test whether the sequence is empty.
STORMBYTE_FORCE_INLINE size_type max_size() const noexcept
Return the largest representable element count.
Definition vector.hxx:317
const T * const_iterator
Read-only contiguous iterator.
Definition vector.hxx:91
std::reverse_iterator< iterator > reverse_iterator
Mutable reverse iterator.
Definition vector.hxx:92
const_iterator cend() const noexcept
Return the read-only end iterator.
std::ptrdiff_t difference_type
Iterator distance type.
Definition vector.hxx:85
reference emplace_back(Args &&... args)
Construct an element at the end.
STORMBYTE_FORCE_INLINE reverse_iterator rbegin() noexcept
Return a mutable reverse iterator to the last element.
Definition vector.hxx:257
T * iterator
Mutable contiguous iterator.
Definition vector.hxx:90
void append_range(R &&range)
Append a copy of each element of range.
const_iterator cbegin() const noexcept
Return a read-only iterator to the first element.
iterator insert(const_iterator position, const T &value)
Insert a copy before position.
bool operator<=(const Vector &other) const
Order sequences lexicographically.
Count of abstract units (uint64_t storage).
Definition size.hxx:107
Recognizes a conditional Safe component after stripping cv and references.
Definition safe.hxx:301
Same type after stripping cv and references from both sides.
Definition relations.hxx:139
Private Base heap used by StormByte::Safe::Shared, StormByte::Safe::Unique and StormByte::Safe::Clona...
Owned values that cross a DLL without the caller's CRT.
void swap(Binary &left, Binary &right) noexcept
Exchange two sequences.
Definition binary.hxx:760
void ThrowVectorOutOfBounds()
Throw the vector out-of-bounds error from Base.
@ Release
Release.
Definition memory_order.hxx:64
Root namespace of the StormByte suite.
Octet count. Defined in byte_size.hxx. Forwarded so Type::Numeral can name it.
STL namespace.
#define STORMBYTE_FORCE_INLINE
Definition platform.h:134
Host-width-stable count (uint64_t), implicit only to std::size_t.
Recognizes types whose DLL-boundary safety depends on documented provider requirements.
Definition safe.hxx:262
Recognizes collection values with Base-controlled lifetime.
Definition safe.hxx:347
Recognizes Base Safe components, not universal ABI compatibility.
Definition safe.hxx:138
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57
#define STORMBYTE_PUBLIC
Definition visibility.h:52