|
VectorPro
High-performance, header-only dynamic array with configurable growth policy, custom allocator support, and optional modification-event notifications.
|
A dynamically resizable contiguous array. More...
#include <Vector.h>
Classes | |
| struct | EventData |
| Describes a vector modification event. More... | |
Public Types | |
| enum class | EventType : std::uint8_t { PUSHBACK , EMPLACEBACK , POPBACK , INSERT , REMOVE , ERASE , CLEAR , SHRINK , RESERVE } |
| Event types emitted when the vector is modified. More... | |
| using | allocator_type = Allocator |
| using | value_type = T |
| using | pointer = typename std::allocator_traits< Allocator >::pointer |
| using | const_pointer = typename std::allocator_traits< Allocator >::const_pointer |
| using | reference = T & |
| using | const_reference = const T & |
| using | size_type = std::size_t |
| using | difference_type = std::ptrdiff_t |
| using | ListenerFn = std::function< void(const Vector &, EventData)> |
| Callback type invoked with the vector and the event that occurred. | |
| using | ListenerHandle = std::size_t |
| Opaque handle returned by subscribe(), used to unsubscribe() later. | |
| using | iterator = VectorPro::Iterator< T > |
| using | const_iterator = VectorPro::Iterator< const T > |
| using | reverse_iterator = std::reverse_iterator< iterator > |
| using | const_reverse_iterator = std::reverse_iterator< const_iterator > |
Public Member Functions | |
| Vector (std::size_t count, const T &value={}) | |
Constructs a vector with count copies of value. | |
| Vector (std::initializer_list< T > init) | |
| Constructs a vector from an initializer list. | |
| template<std::input_iterator It> | |
| Vector (It first, It last) | |
| Constructs a vector from an iterator range via repeated push_back(). | |
| Vector (const Allocator &alloc=Allocator{}) | |
| Constructs an empty vector with no allocated storage. | |
| ~Vector () noexcept | |
| Destroys all elements and releases storage. | |
| Vector (const Vector &other) | |
Copy-constructs a vector, deep-copying other's elements. | |
| Vector & | operator= (const Vector &other) |
Copy-assigns from other, replacing this vector's contents. | |
| Vector (Vector &&other) noexcept | |
Move-constructs a vector, taking ownership of other's storage. | |
| Vector & | operator= (Vector &&other) noexcept |
Move-assigns from other, replacing this vector's contents. | |
| void | push_back (const T &value) |
Appends a copy of value to the end, growing capacity if needed. | |
| void | push_back (T &&value) |
Appends value to the end by move, growing capacity if needed. | |
| template<typename... Args> requires std::constructible_from<T, Args...> | |
| void | emplace_back (Args &&... args) |
| Constructs a new element in place at the end of the vector. | |
| iterator | insert (const_iterator pos, const T &value) |
Inserts a copy of value before pos. | |
| iterator | insert (const_iterator pos, T &&value) |
Inserts value before pos by move. | |
| template<std::input_iterator It> | |
| iterator | insert (const_iterator pos, It first, It last) |
Inserts a copy of each element in [first, last) before pos. | |
| template<typename... Args> requires std::constructible_from<T, Args...> | |
| iterator | emplace (const_iterator pos, Args &&... args) |
Constructs a new element in place before pos. | |
| template<typename Predicate > requires std::predicate<Predicate, const T&> | |
| std::size_t | remove_if (Predicate pred) |
Removes all elements for which pred returns true. | |
| void | pop_back () |
| Removes the last element. No-op if the vector is empty. | |
| iterator | erase (const_iterator pos) |
Erases the element at pos. | |
| iterator | erase (const_iterator first, const_iterator last) |
Erases the elements in [first, last). | |
| void | clear () noexcept |
| Destroys all elements, leaving the vector empty. Capacity is unchanged. | |
| void | reserve (std::size_t newCap) |
Ensures capacity is at least newCap, reallocating if needed. | |
| void | shrink_to_fit () |
| Reduces capacity to match the current size, reallocating if needed. | |
| template<typename F > requires EnableEvents | |
| &&Listener< F, Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents > > ListenerHandle | subscribe (F &&listeners) |
| Registers a listener to be notified of modification events. | |
| void | unsubscribe (ListenerHandle handle) |
| Removes a previously registered listener. | |
| bool | operator== (const Vector &other) const noexcept(noexcept(std::declval< const T & >()==std::declval< const T & >())) |
| Compares two vectors for equality. | |
| auto | operator<=> (const Vector &other) const noexcept(noexcept(std::declval< const T & >()<=> std::declval< const T & >())) |
| Lexicographically compares two vectors. | |
| std::span< T > | as_span () noexcept |
| Returns a mutable span over the vector's current elements. | |
| std::span< const T > | as_span () const noexcept |
| Returns a read-only span over the vector's current elements. | |
| pointer | data_ptr () noexcept |
| Returns a pointer to the underlying contiguous storage. | |
| const_pointer | data_ptr () const noexcept |
| Returns a const pointer to the underlying contiguous storage. | |
| reference | at (std::size_t index) |
Returns a reference to the element at index, with bounds checking. | |
| const_reference | at (std::size_t index) const |
Returns a const reference to the element at index, with bounds checking. | |
| reference | front () |
| Returns a reference to the first element. Undefined behavior if empty. | |
| const_reference | front () const |
| Returns a const reference to the first element. Undefined behavior if empty. | |
| reference | back () |
| Returns a reference to the last element. Undefined behavior if empty. | |
| const_reference | back () const |
| Returns a const reference to the last element. Undefined behavior if empty. | |
| reference | operator[] (std::size_t index) noexcept |
Returns a reference to the element at index, without bounds checking. | |
| const_reference | operator[] (std::size_t index) const noexcept |
Returns a const reference to the element at index, without bounds checking. | |
| bool | contains (const T &value) const noexcept(noexcept(std::declval< const T & >()==std::declval< const T & >())) |
Checks whether value is present in the vector. | |
| iterator | find (const T &value) noexcept(noexcept(std::declval< const T & >()==std::declval< const T & >())) |
Finds the first element equal to value. | |
| const_iterator | find (const T &value) const noexcept(noexcept(std::declval< const T & >()==std::declval< const T & >())) |
Finds the first element equal to value. | |
| void | swap (Vector &other) noexcept |
| Exchanges the contents of two vectors, including allocators. | |
| bool | empty () const noexcept |
| Returns whether the vector has no elements. | |
| std::size_t | size () const noexcept |
| Returns the number of elements currently stored. | |
| std::size_t | capacity () const noexcept |
| Returns the number of elements the vector can hold without reallocating. | |
| iterator | begin () noexcept |
| Returns an iterator to the first element. | |
| iterator | end () noexcept |
| Returns an iterator one past the last element. | |
| const_iterator | begin () const noexcept |
| Returns a const iterator to the first element. | |
| const_iterator | end () const noexcept |
| Returns a const iterator one past the last element. | |
| const_iterator | cbegin () const noexcept |
| Returns a const iterator to the first element. | |
| const_iterator | cend () const noexcept |
| Returns a const iterator one past the last element. | |
| reverse_iterator | rbegin () noexcept |
| Returns a reverse iterator to the last element. | |
| reverse_iterator | rend () noexcept |
| Returns a reverse iterator one before the first element. | |
| const_reverse_iterator | rbegin () const noexcept |
| Returns a const reverse iterator to the last element. | |
| const_reverse_iterator | rend () const noexcept |
| Returns a const reverse iterator one before the first element. | |
| const_reverse_iterator | crbegin () const noexcept |
| Returns a const reverse iterator to the last element. | |
| const_reverse_iterator | crend () const noexcept |
| Returns a const reverse iterator one before the first element. | |
A dynamically resizable contiguous array.
| T | Element type. Must satisfy std::destructible. |
| Allocator | Allocator type used for storage. Defaults to std::allocator<T>. |
| GrowthNum | Numerator of the capacity growth factor (GrowthNum / GrowthDen). |
| GrowthDen | Denominator of the capacity growth factor. |
| EnableEvents | Whether modification-event notifications (subscribe()/ unsubscribe()) are compiled in. Defaults to false; when disabled, no listener storage exists and no notification code is emitted anywhere in the class — see VectorPro::ObservableVector for the enabled alias. |
Provides std::vector-like semantics with configurable growth, custom allocator support, iterator support, and modification event notifications. GrowthNum must be greater than GrowthDen so that capacity strictly increases on each reallocation.
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::allocator_type = Allocator |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::const_iterator = VectorPro::Iterator<const T> |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::const_pointer = typename std::allocator_traits<Allocator>::const_pointer |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::const_reference = const T& |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::const_reverse_iterator = std::reverse_iterator<const_iterator> |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::difference_type = std::ptrdiff_t |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::iterator = VectorPro::Iterator<T> |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::ListenerFn = std::function<void(const Vector&, EventData)> |
Callback type invoked with the vector and the event that occurred.
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::ListenerHandle = std::size_t |
Opaque handle returned by subscribe(), used to unsubscribe() later.
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::pointer = typename std::allocator_traits<Allocator>::pointer |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::reference = T& |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::reverse_iterator = std::reverse_iterator<iterator> |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::size_type = std::size_t |
| using VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::value_type = T |
|
strong |
Event types emitted when the vector is modified.
| Enumerator | |
|---|---|
| PUSHBACK | Emitted by push_back(). |
| EMPLACEBACK | Emitted by emplace_back(). |
| POPBACK | Emitted by pop_back(). |
| INSERT | |
| REMOVE | Emitted by remove_if(). |
| ERASE | Emitted by erase(). |
| CLEAR | Emitted by clear(). |
| SHRINK | Emitted by shrink_to_fit(). |
| RESERVE | Emitted by reserve(). |
|
explicit |
Constructs a vector with count copies of value.
| count | Number of elements to construct. |
| value | Value used to initialize each element. Defaults to T{}. |
| VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::Vector | ( | std::initializer_list< T > | init | ) |
Constructs a vector from an initializer list.
| init | Elements to copy into the vector, in order. |
| VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::Vector | ( | It | first, |
| It | last | ||
| ) |
Constructs a vector from an iterator range via repeated push_back().
| It | Input iterator type. |
|
explicit |
Constructs an empty vector with no allocated storage.
| alloc | Allocator instance to use. Defaults to Allocator{}. |
|
noexcept |
Destroys all elements and releases storage.
| VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::Vector | ( | const Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents > & | other | ) |
Copy-constructs a vector, deep-copying other's elements.
| other | Vector to copy from. |
|
noexcept |
Move-constructs a vector, taking ownership of other's storage.
| other | Vector to move from, left empty afterward. |
|
noexcept |
Returns a read-only span over the vector's current elements.
|
noexcept |
Returns a mutable span over the vector's current elements.
| reference VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::at | ( | std::size_t | index | ) |
Returns a reference to the element at index, with bounds checking.
| index | Zero-based index of the element to access. |
index. | std::out_of_range | if index >= size(). |
| const_reference VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::at | ( | std::size_t | index | ) | const |
Returns a const reference to the element at index, with bounds checking.
| index | Zero-based index of the element to access. |
index. | std::out_of_range | if index >= size(). |
| reference VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::back | ( | ) |
Returns a reference to the last element. Undefined behavior if empty.
| const_reference VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::back | ( | ) | const |
Returns a const reference to the last element. Undefined behavior if empty.
|
noexcept |
Returns a const iterator to the first element.
|
noexcept |
Returns an iterator to the first element.
|
noexcept |
Returns the number of elements the vector can hold without reallocating.
|
noexcept |
Returns a const iterator to the first element.
|
noexcept |
Returns a const iterator one past the last element.
|
noexcept |
Destroys all elements, leaving the vector empty. Capacity is unchanged.
|
noexcept |
Checks whether value is present in the vector.
| value | Value to search for. |
true if an equal element is found.
|
noexcept |
Returns a const reverse iterator to the last element.
|
noexcept |
Returns a const reverse iterator one before the first element.
|
noexcept |
Returns a const pointer to the underlying contiguous storage.
|
noexcept |
Returns a pointer to the underlying contiguous storage.
| iterator VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::emplace | ( | const_iterator | pos, |
| Args &&... | args | ||
| ) |
Constructs a new element in place before pos.
| Args | Constructor argument types for T. |
| pos | Position to insert before. |
| args | Arguments forwarded to T's constructor. |
| void VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::emplace_back | ( | Args &&... | args | ) |
Constructs a new element in place at the end of the vector.
| Args | Constructor argument types for T. |
| args | Arguments forwarded to T's constructor. |
|
noexcept |
Returns whether the vector has no elements.
|
noexcept |
Returns a const iterator one past the last element.
|
noexcept |
Returns an iterator one past the last element.
| iterator VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::erase | ( | const_iterator | first, |
| const_iterator | last | ||
| ) |
| iterator VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::erase | ( | const_iterator | pos | ) |
Erases the element at pos.
| pos | Position of the element to erase. |
|
noexcept |
Finds the first element equal to value.
| value | Value to search for. |
| reference VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::front | ( | ) |
Returns a reference to the first element. Undefined behavior if empty.
| const_reference VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::front | ( | ) | const |
Returns a const reference to the first element. Undefined behavior if empty.
| iterator VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::insert | ( | const_iterator | pos, |
| const T & | value | ||
| ) |
Inserts a copy of value before pos.
| pos | Position to insert before. |
| value | Value to insert. Safe to pass a reference into this vector. |
| iterator VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::insert | ( | const_iterator | pos, |
| It | first, | ||
| It | last | ||
| ) |
Inserts a copy of each element in [first, last) before pos.
| It | Input iterator type. |
| pos | Position to insert before. |
| first | Iterator to the first element to insert. |
| last | Iterator one past the last element to insert. |
| iterator VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::insert | ( | const_iterator | pos, |
| T && | value | ||
| ) |
Inserts value before pos by move.
| pos | Position to insert before. |
| value | Value to move-insert. Safe to pass a reference into this vector. |
|
noexcept |
Lexicographically compares two vectors.
| other | Vector to compare against. |
| Vector & VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::operator= | ( | const Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents > & | other | ) |
Copy-assigns from other, replacing this vector's contents.
| other | Vector to copy from. |
*this.Reuses existing capacity when sufficient; otherwise falls back to copy-and-swap for the strong exception guarantee.
|
noexcept |
Move-assigns from other, replacing this vector's contents.
| other | Vector to move from, left empty afterward. |
*this.
|
noexcept |
Compares two vectors for equality.
| other | Vector to compare against. |
true if both vectors have equal size and equal elements in order.
|
noexcept |
Returns a const reference to the element at index, without bounds checking.
| index | Zero-based index of the element to access. |
index.
|
noexcept |
Returns a reference to the element at index, without bounds checking.
| index | Zero-based index of the element to access. |
index. | void VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::pop_back | ( | ) |
Removes the last element. No-op if the vector is empty.
| void VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::push_back | ( | const T & | value | ) |
Appends a copy of value to the end, growing capacity if needed.
| value | Value to append. Safe to pass a reference into this vector. |
| void VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::push_back | ( | T && | value | ) |
Appends value to the end by move, growing capacity if needed.
| value | Value to move-append. Safe to pass a reference into this vector. |
|
noexcept |
Returns a const reverse iterator to the last element.
|
noexcept |
Returns a reverse iterator to the last element.
| std::size_t VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::remove_if | ( | Predicate | pred | ) |
Removes all elements for which pred returns true.
| Predicate | Unary predicate type invocable on const T&. |
| pred | Predicate used to test each element. |
Relative order of retained elements is preserved.
|
noexcept |
Returns a const reverse iterator one before the first element.
|
noexcept |
Returns a reverse iterator one before the first element.
| void VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::reserve | ( | std::size_t | newCap | ) |
Ensures capacity is at least newCap, reallocating if needed.
| newCap | Minimum capacity to reserve. No-op if not greater than current capacity. |
| void VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::shrink_to_fit | ( | ) |
Reduces capacity to match the current size, reallocating if needed.
|
noexcept |
Returns the number of elements currently stored.
| &&Listener< F, Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents > > ListenerHandle VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::subscribe | ( | F && | listeners | ) |
Registers a listener to be notified of modification events.
| F | Callable type satisfying the Listener concept. |
| listeners | Callable to invoke with (*this, EventData) on each modification. |
Only callable when EnableEvents is true; a Vector with events disabled has no listener storage, so subscribing is a compile error rather than a silent no-op.
|
noexcept |
Exchanges the contents of two vectors, including allocators.
| other | Vector to swap with. |
| void VectorPro::Vector< T, Allocator, GrowthNum, GrowthDen, EnableEvents >::unsubscribe | ( | ListenerHandle | handle | ) |
Removes a previously registered listener.
| handle | Handle returned by subscribe(). No-op if out of range. |
Only callable when EnableEvents is true.