LibGimbal  1.0.0
Ultimate C17/C++20 Cross-Language Runtime Framework and Meta Type System
gimbal_vector.h
Go to the documentation of this file.
1 
6 #ifndef GIMBAL_VECTOR_H
7 #define GIMBAL_VECTOR_H
8 
9 #include "../core/gimbal_api_frame.h"
10 #include "../algorithms/gimbal_numeric.h"
11 
12 #define SELF GblVector* pSelf
13 #define CSELF const SELF
14 
15 GBL_DECLS_BEGIN
16 
20 typedef struct GblVector { // Size (32-bit / 64-bit)
21  GblContext* pCtx; // 4/8 bytes
22  uint8_t* pData; // 4/8 bytes
23  GblSize size; // 4/8 bytes
24  GblSize capacity; // 4/8 bytes
25  uint16_t elementSize; // 2 bytes
26  uint16_t stackSize; // 2 bytes
27 } GblVector; // 20/36 bytes
28 
29 GBL_EXPORT GBL_RESULT GblVector_construct_6 (SELF,
30  uint16_t elementSize,
31  GblSize elementCount,
32  const void* pInitialData,
33  GblSize structSize,
34  GblContext* pCtx) GBL_NOEXCEPT;
35 GBL_INLINE GBL_RESULT GblVector_construct_5 (SELF,
36  uint16_t elementSize,
37  GblSize elementCount,
38  const void* pInitialData,
39  GblSize structSize) GBL_NOEXCEPT;
40 GBL_INLINE GBL_RESULT GblVector_construct_4 (SELF,
41  uint16_t elementSize,
42  GblSize elementCount,
43  const void* pInitialData) GBL_NOEXCEPT;
44 GBL_INLINE GBL_RESULT GblVector_construct_3 (SELF,
45  uint16_t elementSize,
46  GblSize elementCount) GBL_NOEXCEPT;
47 GBL_INLINE GBL_RESULT GblVector_construct_2 (SELF,
48  uint16_t elementSize) GBL_NOEXCEPT;
49 #define GblVector_construct(...) \
50  GBL_VA_OVERLOAD_SELECT(GblVector_construct, GBL_VA_OVERLOAD_SUFFIXER_ARGC, __VA_ARGS__)(__VA_ARGS__)
51 #define GblVector_alloca(...) \
52  GBL_VA_OVERLOAD_SELECT(GBL_VECTOR_ALLOCA, GBL_VA_OVERLOAD_SUFFIXER_ARGC, __VA_ARGS__)(__VA_ARGS__)
53 
54 GBL_EXPORT GBL_RESULT GblVector_destruct (SELF) GBL_NOEXCEPT;
55 GBL_EXPORT GBL_RESULT GblVector_assign (SELF, const void* pData, GblSize elementCount) GBL_NOEXCEPT;
56 GBL_EXPORT GBL_RESULT GblVector_release (SELF, void** ppVecPtr, GblSize* pSize, GblSize* pCapacity) GBL_NOEXCEPT;
57 GBL_EXPORT GBL_RESULT GblVector_acquire (SELF, void* pData, GblSize size, GblSize capacity) GBL_NOEXCEPT;
58 
59 GBL_INLINE GblContext* GblVector_context (CSELF) GBL_NOEXCEPT;
60 GBL_INLINE uint16_t GblVector_stackBytes (CSELF) GBL_NOEXCEPT;
61 GBL_INLINE void* GblVector_stackBuffer (CSELF) GBL_NOEXCEPT;
62 GBL_INLINE GblSize GblVector_size (CSELF) GBL_NOEXCEPT;
63 GBL_INLINE GblSize GblVector_capacity (CSELF) GBL_NOEXCEPT;
64 GBL_INLINE uint16_t GblVector_elementSize (CSELF) GBL_NOEXCEPT;
65 GBL_INLINE void* GblVector_data (CSELF) GBL_NOEXCEPT;
66 
67 GBL_INLINE GblBool GblVector_empty (CSELF) GBL_NOEXCEPT;
68 GBL_INLINE GblBool GblVector_stack (CSELF) GBL_NOEXCEPT;
69 
70 GBL_INLINE void* GblVector_at (CSELF, GblSize index) GBL_NOEXCEPT;
71 GBL_INLINE void* GblVector_front (CSELF) GBL_NOEXCEPT;
72 GBL_INLINE void* GblVector_back (CSELF) GBL_NOEXCEPT;
73 
74 GBL_EXPORT void* GblVector_insert (SELF, GblSize index, GblSize count, const void* pData) GBL_NOEXCEPT;
75 GBL_INLINE GBL_RESULT GblVector_pushFront (SELF, const void* pData) GBL_NOEXCEPT;
76 GBL_INLINE GBL_RESULT GblVector_pushBack (SELF, const void* pData) GBL_NOEXCEPT;
77 GBL_EXPORT GBL_RESULT GblVector_append (SELF, const void* pData, GblSize elementCount) GBL_NOEXCEPT;
78 GBL_EXPORT GBL_RESULT GblVector_prepend (SELF, const void* pData, GblSize elementCount) GBL_NOEXCEPT;
79 
80 GBL_EXPORT GBL_RESULT GblVector_popFront (SELF, void* pOut) GBL_NOEXCEPT;
81 GBL_EXPORT GBL_RESULT GblVector_popBack (SELF, void* pOut) GBL_NOEXCEPT;
82 
83 GBL_EXPORT GBL_RESULT GblVector_erase (SELF, GblSize begin, GblSize count) GBL_NOEXCEPT;
84 GBL_EXPORT GBL_RESULT GblVector_clear (SELF) GBL_NOEXCEPT;
85 
86 GBL_EXPORT GBL_RESULT GblVector_reserve (SELF, GblSize capacity) GBL_NOEXCEPT;
87 GBL_EXPORT GBL_RESULT GblVector_resize (SELF, GblSize size) GBL_NOEXCEPT;
88 GBL_EXPORT GBL_RESULT GblVector_shrinkToFit (SELF) GBL_NOEXCEPT;
89 
90 // ========== IMPL ==========
91 
93 #define GBL_VECTOR_ALLOCA_5(elemSize, elemCount, pData, size, pCtx) \
94  GblVector_createInPlace_(GBL_ALLOCA(size), elemSize, elemCount, pData, size, pCtx)
95 
96 #define GBL_VECTOR_ALLOCA_4(elemSize, elemCount, pData, size) \
97  GBL_VECTOR_ALLOCA_5(elemSize, elemCount, pData, size, NULL)
98 
99 #define GBL_VECTOR_ALLOCA_3(elemSize, elemCount, pData) \
100  GBL_VECTOR_ALLOCA_4(elemSize, elemCount, pData, 0)
101 
102 #define GBL_VECTOR_ALLOCA_2(elemSize, elemCount) \
103  GBL_VECTOR_ALLOCA_3(elemSize, elemCount, NULL)
104 
105 #define GBL_VECTOR_ALLOCA_1(elemSize) \
106  GBL_VECTOR_ALLOCA_2(elemSize, 0)
107 
108 GBL_INLINE GblSize GBL_VECTOR_CAPACITY_FROM_SIZE_(GblSize size) GBL_NOEXCEPT {
109  return gblPow2Next(size);
110 }
112 
113 GBL_INLINE void* GblVector_stackBuffer(CSELF) GBL_NOEXCEPT {
114  return (uint8_t*)((pSelf && pSelf->stackSize)? (pSelf + 1) : NULL);
115 }
116 
117 GBL_INLINE GblBool GblVector_empty(CSELF) GBL_NOEXCEPT {
118  return (pSelf && pSelf->size)? GBL_FALSE : GBL_TRUE;
119 }
120 
121 GBL_INLINE GblBool GblVector_stack(CSELF) GBL_NOEXCEPT {
122  return (pSelf && (pSelf->pData == GblVector_stackBuffer(pSelf)))? GBL_TRUE : GBL_FALSE;
123 }
124 
125 GBL_INLINE GBL_RESULT GblVector_construct_5(SELF, uint16_t elementSize, GblSize elementCount, const void* pInitialData, GblSize structSize) GBL_NOEXCEPT {
126  return GblVector_construct_6(pSelf, elementSize, elementCount, pInitialData, structSize, NULL);
127 }
128 
129 GBL_INLINE GBL_RESULT GblVector_construct_4(SELF, uint16_t elementSize, GblSize elementCount, const void* pInitialData) GBL_NOEXCEPT {
130  return GblVector_construct_5(pSelf, elementSize, elementCount, pInitialData, sizeof(GblVector));
131 }
132 
133 GBL_INLINE GBL_RESULT GblVector_construct_3(SELF, uint16_t elementSize, GblSize elementCount) GBL_NOEXCEPT {
134  return GblVector_construct_4(pSelf, elementSize, elementCount, NULL);
135 }
136 
137 GBL_INLINE GBL_RESULT GblVector_construct_2(SELF, uint16_t elementSize) GBL_NOEXCEPT {
138  return GblVector_construct_3(pSelf, elementSize, 0);
139 }
140 
141 GBL_INLINE GblContext* GblVector_context(CSELF) GBL_NOEXCEPT {
142  return (pSelf)? pSelf->pCtx : NULL;
143 }
144 
145 GBL_INLINE uint16_t GblVector_stackBytes(CSELF) GBL_NOEXCEPT {
146  return (pSelf)? pSelf->stackSize : 0;
147 }
148 
149 GBL_INLINE GblSize GblVector_size(CSELF) GBL_NOEXCEPT {
150  return (pSelf)? pSelf->size : 0;
151 }
152 
153 GBL_INLINE GblSize GblVector_capacity(CSELF) GBL_NOEXCEPT {
154  return (pSelf)? pSelf->capacity : 0;
155 }
156 
157 GBL_INLINE uint16_t GblVector_elementSize(CSELF) GBL_NOEXCEPT {
158  return (pSelf)? pSelf->elementSize : 0;
159 }
160 
161 GBL_INLINE void* GblVector_data(CSELF) GBL_NOEXCEPT {
162  return (pSelf)? pSelf->pData : NULL;
163 }
164 
165 GBL_INLINE void* GblVector_at(CSELF, GblSize index) GBL_NOEXCEPT {
166  void* pData = NULL;
167  GBL_API_BEGIN(pSelf->pCtx);
168  GBL_API_VERIFY(index < pSelf->size,
169  GBL_RESULT_ERROR_OUT_OF_RANGE);
170  pData = &pSelf->pData[index * pSelf->elementSize];
171  GBL_API_END_BLOCK();
172  return pData;
173 }
174 
175 GBL_INLINE void* GblVector_front(CSELF) GBL_NOEXCEPT {
176  return GblVector_at(pSelf, 0);
177 }
178 
179 GBL_INLINE void* GblVector_back(CSELF) GBL_NOEXCEPT {
180  return GblVector_at(pSelf, pSelf->size-1);
181 }
182 
183 GBL_INLINE GBL_RESULT GblVector_pushBack(SELF, const void* pData) GBL_NOEXCEPT {
184  GBL_API_BEGIN(pSelf->pCtx);
185  GblVector_insert(pSelf, GblVector_size(pSelf), 1, pData);
186  GBL_API_VERIFY_LAST_RECORD();
187  GBL_API_END();
188 }
189 
190 GBL_INLINE GBL_RESULT GblVector_pushFront(SELF, const void* pData) GBL_NOEXCEPT {
191  GBL_API_BEGIN(pSelf->pCtx);
192  GblVector_insert(pSelf, 0, 1, pData);
193  GBL_API_VERIFY_LAST_RECORD();
194  GBL_API_END();
195 }
196 
197 GBL_INLINE GblVector* GblVector_createInPlace (SELF,
198  uint16_t elementSize,
199  GblSize elementCount,
200  const void* pInitialData,
201  GblSize structSize,
202  GblContext* pCtx) GBL_NOEXCEPT
203 {
204  GblVector* pOut = NULL;
205  GBL_API_BEGIN(pCtx);
206  GBL_API_VERIFY_CALL(GblVector_construct_6(pSelf, elementSize, elementCount, pInitialData, structSize, pCtx));
207  pOut = pSelf;
208  GBL_API_END_BLOCK();
209  return pOut;
210 }
211 
212 #undef CSELF
213 #undef SELF
214 
215 GBL_DECLS_END
216 
217 #endif // GIMBAL_VECTOR_H
GblContext
Definition: gimbal_context.h:29
GblVector
Contiguous array based abstract list container with C++-style STL API.
Definition: gimbal_vector.h:20