nfx-containers 0.6.0
Modern C++20 header-only library providing high-performance hash containers with Robin Hood and perfect hashing
Loading...
Searching...
No Matches
StackVector.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
31
32#pragma once
33
34#include <algorithm>
35#include <cstddef>
36#include <cstring>
37#include <initializer_list>
38#include <iterator>
39#include <memory>
40#include <stdexcept>
41#include <type_traits>
42#include <vector>
43
44namespace nfx::containers
45{
53 template <typename T, std::size_t N = 8>
54 class StackVector final
55 {
56 public:
57 //----------------------------------------------
58 // Type aliases
59 //----------------------------------------------
60
62 using value_type = T;
63
65 using size_type = std::size_t;
66
68 using difference_type = std::ptrdiff_t;
69
71 using reference = T&;
72
74 using const_reference = const T&;
75
77 using pointer = T*;
78
80 using const_pointer = const T*;
81
82 //----------------------------------------------
83 // Construction
84 //----------------------------------------------
85
87 inline StackVector() noexcept;
88
92 inline StackVector( std::initializer_list<T> init );
93
97 inline StackVector( const StackVector& other );
98
102 inline StackVector( StackVector&& other ) noexcept;
103
105 inline ~StackVector() noexcept;
106
112 inline StackVector& operator=( const StackVector& other );
113
119 inline StackVector& operator=( StackVector&& other ) noexcept;
120
126 inline bool operator==( const StackVector& other ) const noexcept;
127
133 inline bool operator!=( const StackVector& other ) const noexcept;
134
139 inline void push_back( const T& value );
140
145 inline void push_back( T&& value );
146
152 template <typename... Args>
153 inline void emplace_back( Args&&... args );
154
160 inline reference operator[]( size_type pos ) noexcept;
161
167 inline const_reference operator[]( size_type pos ) const noexcept;
168
175 inline reference at( size_type pos );
176
183 inline const_reference at( size_type pos ) const;
184
189 inline reference back() noexcept;
190
195 inline const_reference back() const noexcept;
196
201 inline reference front() noexcept;
202
207 inline const_reference front() const noexcept;
208
213 inline typename StackVector<T, N>::size_type size() const noexcept;
214
219 inline bool isEmpty() const noexcept;
220
225 inline size_type capacity() const noexcept;
226
231 inline void resize( size_type count );
232
238 inline void resize( size_type count, const T& value );
239
244 inline void reserve( size_type newCapacity );
245
250 inline pointer data() noexcept;
251
256 inline const_pointer data() const noexcept;
257
262 inline pointer begin() noexcept;
263
268 inline const_pointer begin() const noexcept;
269
274 inline pointer end() noexcept;
275
280 inline const_pointer end() const noexcept;
281
286 inline const_pointer cbegin() const noexcept;
287
292 inline const_pointer cend() const noexcept;
293
297 inline void clear() noexcept;
298
302 inline void pop_back() noexcept;
303
304 private:
306 inline void transitionToHeap();
307
309 inline T* stackData() noexcept;
310
312 inline const T* stackData() const noexcept;
313
315 inline std::vector<T>& heapData() noexcept;
316
318 inline const std::vector<T>& heapData() const noexcept;
319
320 union alignas( alignof( T ) > alignof( std::vector<T> ) ? alignof( T ) : alignof( std::vector<T> ) )
321 {
322 unsigned char m_stackStorage[N * sizeof( T )];
323 unsigned char m_heapStorage[sizeof( std::vector<T> )];
324 };
325
326 size_type m_size;
327 bool m_usingStack;
328 };
329} // namespace nfx::containers
330
331#include "nfx/detail/containers/StackVector.inl"
T value_type
Type alias for element type.
Definition StackVector.h:62
reference back() noexcept
Access last element.
void emplace_back(Args &&... args)
Construct element in-place at end.
reference at(size_type pos)
Access element at position (with bounds checking).
const T * const_pointer
Type alias for const pointer to element.
Definition StackVector.h:80
const_pointer cend() const noexcept
Get const iterator to end.
void resize(size_type count)
Resize container to specified size.
size_type capacity() const noexcept
Get current capacity.
void push_back(const T &value)
Add element to end (copy).
pointer begin() noexcept
Get iterator to beginning.
void clear() noexcept
Clear all elements.
void reserve(size_type newCapacity)
Reserve storage for at least specified capacity.
T & reference
Type alias for reference to element.
Definition StackVector.h:71
std::size_t size_type
Type alias for size type.
Definition StackVector.h:65
StackVector() noexcept
Default constructor.
T * pointer
Type alias for pointer to element.
Definition StackVector.h:77
pointer data() noexcept
Get pointer to underlying data.
void pop_back() noexcept
Remove last element.
StackVector< T, N >::size_type size() const noexcept
Get number of elements.
const_pointer cbegin() const noexcept
Get const iterator to beginning.
reference front() noexcept
Access first element.
const T & const_reference
Type alias for const reference to element.
Definition StackVector.h:74
std::ptrdiff_t difference_type
Type alias for difference type.
Definition StackVector.h:68
bool isEmpty() const noexcept
Check if container is empty.
pointer end() noexcept
Get iterator to end.