Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
sstack.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2014-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#ifndef JAU_SSTACK_HPP_
25#define JAU_SSTACK_HPP_
26
27#include <cmath>
28#include <cstdarg>
29#include <cstdint>
30#include <cassert>
31#include <limits>
32#include <vector>
33#include <type_traits>
34#include <algorithm>
35
36#include <jau/cpp_lang_util.hpp>
37#include <jau/math/mat4f.hpp>
38
39namespace jau::math::util {
40
41 /** \addtogroup Math
42 *
43 * @{
44 */
45
46 /**
47 * A simple stack of compounds, each consisting of `element_size` * `T`
48 * @tparam T type of one element used in each compound
49 * @tparam element_size number of T elements making up one compound
50 */
51 template<typename Value_type, size_t Element_size,
52 std::enable_if_t<std::is_floating_point_v<Value_type> ||
53 std::is_integral_v<Value_type>,
54 bool> = true>
56 public:
58 constexpr static const size_t element_size = Element_size;
59
60 private:
61 int growSize;
62 std::vector<value_type> buffer;
63
64 public:
65 /**
66 * Start w/ zero size and growSize is 16, half GL-min size (32)
67 */
69 : growSize(16*element_size), buffer(0) {}
70
71 /**
72 * @param initialSize initial size
73 * @param growSize grow size if {@link #position()} is reached, maybe <code>0</code>
74 */
75 constexpr_cxx20 SimpleStack(int initialSize, int growSize_) noexcept
76 : growSize(growSize_), buffer(initialSize) {}
77
78 constexpr_cxx20 size_t growIfNecessary(int length) noexcept {
79 const size_t p = buffer.size();
80 const size_t nsz = buffer.size() + length;
81 if( nsz > buffer.capacity() ) {
82 buffer.reserve(buffer.size() + std::max(length, growSize));
83 }
84 buffer.resize(nsz);
85 return p;
86 }
87
88 constexpr_cxx20 void push(const value_type* src) noexcept {
89 size_t p = growIfNecessary(element_size);
90 for(size_t i=0; i<element_size; ++i) {
91 buffer[p+i] = src[i];
92 }
93 }
94
95 constexpr_cxx20 void pop(value_type* dest) noexcept {
96 const size_t sz = buffer.size();
97 assert( sz >= element_size );
98 const size_t p = sz - element_size;
99 for(size_t i=0; i<element_size; ++i) {
100 dest[i] = buffer[p+i];
101 }
102 buffer.resize(p);
103 }
104 };
105
106 /**
107 * 4x4 float matrix stack based on single float elements
108 */
109 typedef SimpleStack<float, 16 /* Element_size */> Stack16f;
110
111 /**
112 * A Matrix stack of compounds, each consisting of 16 * `T`
113 * @tparam T type of one element used in each compound
114 */
115 template<typename Value_type,
116 std::enable_if_t<std::is_floating_point_v<Value_type> ||
117 std::is_integral_v<Value_type>,
118 bool> = true>
120 public:
123
124 private:
125 int growSize;
126 std::vector<matrix_t> buffer;
127
128 public:
129 /**
130 * Start w/ zero size and growSize is 16, half GL-min size (32)
131 */
133 : growSize(16), buffer(0) {}
134
135 /**
136 * @param initialSize initial size
137 * @param growSize grow size if {@link #position()} is reached, maybe <code>0</code>
138 */
139 constexpr_cxx20 MatrixStack(int initialSize, int growSize_) noexcept
140 : growSize(growSize_), buffer(initialSize) {}
141
142 constexpr_cxx20 void growIfNecessary(int length) noexcept {
143 const size_t nsz = buffer.size() + length;
144 if( nsz > buffer.capacity() ) {
145 buffer.reserve(buffer.size() + std::max(length, growSize));
146 }
147 }
148
149 constexpr_cxx20 void push(const matrix_t& src) noexcept {
151 buffer.push_back(src);
152 }
153
154 constexpr_cxx20 void pop(matrix_t& dest) noexcept {
155 const size_t sz = buffer.size();
156 assert( sz >= 1 );
157 const size_t p = sz - 1;
158 dest.load( buffer[p] );
159 buffer.resize(p);
160 }
161 };
162
163 /**
164 * 4x4 float matrix stack
165 */
167
168 /**@}*/
169
170} // namespace jau::math
171
172#endif // JAU_SSTACK_HPP_
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
Definition: mat4f.hpp:112
A Matrix stack of compounds, each consisting of 16 * T
Definition: sstack.hpp:119
constexpr_cxx20 MatrixStack() noexcept
Start w/ zero size and growSize is 16, half GL-min size (32)
Definition: sstack.hpp:132
constexpr_cxx20 void growIfNecessary(int length) noexcept
Definition: sstack.hpp:142
constexpr_cxx20 void pop(matrix_t &dest) noexcept
Definition: sstack.hpp:154
constexpr_cxx20 void push(const matrix_t &src) noexcept
Definition: sstack.hpp:149
constexpr_cxx20 MatrixStack(int initialSize, int growSize_) noexcept
Definition: sstack.hpp:139
Matrix4< value_type > matrix_t
Definition: sstack.hpp:122
A simple stack of compounds, each consisting of element_size * T
Definition: sstack.hpp:55
constexpr_cxx20 SimpleStack(int initialSize, int growSize_) noexcept
Definition: sstack.hpp:75
static constexpr const size_t element_size
Definition: sstack.hpp:58
constexpr_cxx20 size_t growIfNecessary(int length) noexcept
Definition: sstack.hpp:78
constexpr_cxx20 void push(const value_type *src) noexcept
Definition: sstack.hpp:88
constexpr_cxx20 SimpleStack() noexcept
Start w/ zero size and growSize is 16, half GL-min size (32)
Definition: sstack.hpp:68
constexpr_cxx20 void pop(value_type *dest) noexcept
Definition: sstack.hpp:95
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
Definition: base_math.hpp:191
SimpleStack< float, 16 > Stack16f
4x4 float matrix stack based on single float elements
Definition: sstack.hpp:109
MatrixStack< float > Mat4fStack
4x4 float matrix stack
Definition: sstack.hpp:166
uint8_t Value_type