JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
StaticArray.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <JPLSpatial/Core.h>
24
25#include <algorithm>
26#include <iterator>
27#include <type_traits>
28
29namespace JPL
30{
31 //==========================================================================
32 // A simple utility for small buffers of trivial types
33 template<class T, std::size_t Capacity> requires(std::is_trivial<T>::value && std::is_copy_constructible_v<T>)
35 {
36 public:
37 static_assert(std::is_object_v<T>, "The C++ Standard forbids containers of non-object types "
38 "because of [container.requirements].");
39
40 using element_type = T;
41 using value_type = std::remove_cvref_t<T>;
42 using size_type = std::size_t;
44 using pointer = T*;
45 using const_pointer = const T*;
46 using reference = T&;
47 using const_reference = const T&;
48
51 using reverse_iterator = std::reverse_iterator<iterator>;
52 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
53
54 //==========================================================================
61
63 : mSize(size)
64 {
65 JPL_ASSERT(size <= Capacity);
66 }
67
68 constexpr JPL_INLINE explicit StaticArray(std::size_t size, T defaultValue) noexcept
69 : mSize(size)
70 {
71 JPL_ASSERT(size <= Capacity);
72 std::ranges::fill_n(mStorage, size, defaultValue);
73 }
74
75 //==========================================================================
76 // Capacity
77 [[nodiscard]] constexpr JPL_INLINE size_type size() const { return mSize; }
78 [[nodiscard]] constexpr JPL_INLINE size_type size_bytes() const { return mSize * sizeof(value_type); }
79 [[nodiscard]] constexpr JPL_INLINE bool empty() const { return size() == 0; }
80
81 // iteration
82 [[nodiscard]] constexpr JPL_INLINE iterator begin() noexcept { return mSize ? &mStorage[0] : nullptr; }
83 [[nodiscard]] constexpr JPL_INLINE iterator end() noexcept { return mSize ? &mStorage[mSize] : nullptr; }
84
85 [[nodiscard]] constexpr JPL_INLINE const_iterator begin() const noexcept { return mSize ? &mStorage[0] : nullptr; }
86 [[nodiscard]] constexpr JPL_INLINE const_iterator end() const noexcept { return mSize ? &mStorage[mSize] : nullptr; }
87
88 [[nodiscard]] constexpr JPL_INLINE const_iterator cbegin() const noexcept { return begin(); }
89 [[nodiscard]] constexpr JPL_INLINE const_iterator cend() const noexcept { return end(); }
90
93
94 [[nodiscard]] constexpr JPL_INLINE reference front() noexcept { JPL_ASSERT(mSize > 0); return mStorage[0]; }
95 [[nodiscard]] constexpr JPL_INLINE reference back() noexcept { JPL_ASSERT(mSize > 0); return mStorage[mSize - 1]; }
96
97 [[nodiscard]] constexpr JPL_INLINE const_reference front() const noexcept { JPL_ASSERT(mSize > 0); return mStorage[0]; }
98 [[nodiscard]] constexpr JPL_INLINE const_reference back() const noexcept { JPL_ASSERT(mSize > 0); return mStorage[mSize - 1]; }
99
100 [[nodiscard]] constexpr JPL_INLINE pointer data() noexcept { return mSize ? &mStorage[0] : nullptr; }
101 [[nodiscard]] constexpr JPL_INLINE const_pointer data() const noexcept { mSize ? &mStorage[0] : nullptr; }
102
103 //==========================================================================
105 {
106 JPL_ASSERT(offset < mSize);
107 return mStorage[offset];
108 }
109
110 [[nodiscard]] constexpr JPL_INLINE const_reference operator[](const size_type offset) const noexcept
111 {
112 JPL_ASSERT(offset < mSize);
113 return mStorage[offset];
114 }
115
116 constexpr JPL_INLINE void resize(std::size_t newSize) noexcept
117 {
119 mSize = newSize;
120 }
121
122 constexpr JPL_INLINE void resize(std::size_t newSize, const T& valueToInsert) noexcept
123 {
125 if (newSize > mSize)
126 {
127 std::fill_n(&mStorage[mSize], newSize - mSize, valueToInsert);
128 }
129 mSize = newSize;
130 }
131
132 // Replaces the contents of the container with `count` copies of value `value`
133 constexpr JPL_INLINE void assign(std::size_t count, const T& value) noexcept
134 {
136 mSize = count;
137 std::fill_n(mStorage, count, value);
138 }
139
140 constexpr JPL_INLINE void clear() noexcept
141 {
142 mSize = 0;
143 }
144
145 constexpr JPL_INLINE void push_back(const T& value)
146 {
147 JPL_ASSERT(mSize < Capacity);
148 mStorage[mSize++] = value;
149 }
150
151 template <class... Args>
153 {
154 JPL_ASSERT(mSize < Capacity);
155 mStorage[mSize] = T{ std::forward<Args>(args)... };
156 return mStorage[mSize++];
157 }
158
159 constexpr JPL_INLINE bool pop_back(T& outValue) noexcept
160 {
161 if (mSize > 0)
162 {
163 outValue = mStorage[mSize--];
164 return true;
165 }
166 else
167 {
168 return false;
169 }
170 }
171
172 constexpr JPL_INLINE bool pop_back() noexcept
173 {
174 if (mSize > 0)
175 {
176 mSize--;
177 return true;
178 }
179 else
180 {
181 return false;
182 }
183 }
184
185 private:
186 size_type mSize = 0;
187 T mStorage[Capacity];
188 };
189} // namespace JPL
#define JPL_ASSERT(inExpression,...)
Main assert macro, usage: JPL_ASSERT(condition, message) or JPL_ASSERT(condition)
Definition ErrorReporting.h:76
Definition StaticArray.h:35
std::remove_cvref_t< T > value_type
Definition StaticArray.h:41
constexpr JPL_INLINE pointer data() noexcept
Definition StaticArray.h:100
constexpr JPL_INLINE bool pop_back(T &outValue) noexcept
Definition StaticArray.h:159
constexpr JPL_INLINE void resize(std::size_t newSize, const T &valueToInsert) noexcept
Definition StaticArray.h:122
constexpr JPL_INLINE const_iterator cend() const noexcept
Definition StaticArray.h:89
T * pointer
Definition StaticArray.h:44
constexpr JPL_INLINE const_iterator end() const noexcept
Definition StaticArray.h:86
constexpr JPL_INLINE bool pop_back() noexcept
Definition StaticArray.h:172
constexpr StaticArray() noexcept=default
std::reverse_iterator< iterator > reverse_iterator
Definition StaticArray.h:51
constexpr JPL_INLINE const_pointer data() const noexcept
Definition StaticArray.h:101
T & reference
Definition StaticArray.h:46
constexpr JPL_INLINE reference front() noexcept
Definition StaticArray.h:94
constexpr JPL_INLINE iterator end() noexcept
Definition StaticArray.h:83
constexpr JPL_INLINE const_reference operator[](const size_type offset) const noexcept
Definition StaticArray.h:110
constexpr JPL_INLINE void assign(std::size_t count, const T &value) noexcept
Definition StaticArray.h:133
const T * const_pointer
Definition StaticArray.h:45
constexpr JPL_INLINE bool empty() const
Definition StaticArray.h:79
constexpr JPL_INLINE reference operator[](const size_type offset) noexcept
Definition StaticArray.h:104
constexpr JPL_INLINE iterator begin() noexcept
Definition StaticArray.h:82
constexpr JPL_INLINE reverse_iterator rend() const noexcept
Definition StaticArray.h:92
constexpr JPL_INLINE StaticArray(std::size_t size, T defaultValue) noexcept
Definition StaticArray.h:68
constexpr JPL_INLINE reference back() noexcept
Definition StaticArray.h:95
constexpr JPL_INLINE const_iterator cbegin() const noexcept
Definition StaticArray.h:88
constexpr JPL_INLINE void push_back(const T &value)
Definition StaticArray.h:145
ptrdiff_t difference_type
Definition StaticArray.h:43
constexpr JPL_INLINE const_reference front() const noexcept
Definition StaticArray.h:97
constexpr JPL_INLINE const_reference back() const noexcept
Definition StaticArray.h:98
const_pointer const_iterator
Definition StaticArray.h:50
constexpr JPL_INLINE const_iterator begin() const noexcept
Definition StaticArray.h:85
T element_type
Definition StaticArray.h:40
pointer iterator
Definition StaticArray.h:49
constexpr JPL_INLINE reverse_iterator rbegin() const noexcept
Definition StaticArray.h:91
constexpr JPL_INLINE size_type size_bytes() const
Definition StaticArray.h:78
constexpr JPL_INLINE reference emplace_back(Args &&... args)
Definition StaticArray.h:152
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition StaticArray.h:52
constexpr JPL_INLINE void clear() noexcept
Definition StaticArray.h:140
constexpr JPL_INLINE size_type size() const
Definition StaticArray.h:77
std::size_t size_type
Definition StaticArray.h:42
const T & const_reference
Definition StaticArray.h:47
constexpr JPL_INLINE void resize(std::size_t newSize) noexcept
Definition StaticArray.h:116
Definition AcousticMaterial.h:36
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Definition ChannelMap.h:268