Itasca C++ Interface
Loading...
Searching...
No Matches
farray.h
Go to the documentation of this file.
1#pragma once
22#include "basetoqt.h"
23
24template <typename T, uint64 S = 32>
25class FArray {
26public:
27 using size_type = uint64;
28 using value_type = T;
29 using iterator = T *;
30 using const_iterator = const T *;
31
33 FArray() {}
38 explicit FArray(size_type s, const T &t = T());
40 template <uint64 S2> FArray(const FArray<T, S2> &f) { operator=(f); }
42 FArray(const FArray<T, S> &f) { operator=(f); }
43 FArray(std::initializer_list<T> l);
45 ~FArray() { reset(); }
46 const FArray<T,S> &operator=(const FArray<T,S> &f);
48 template <uint64 S2> const FArray<T, S> &operator=(const FArray<T, S2> &f);
49 template <uint64 S2> bool operator==(const FArray<T,S2> &f) const;
50
51 // Accessors
53 size_type size() const { return end_ - begin_; }
55 size_type stackSize() const { return S; }
58 size_type allocated() const { return capacity_ - begin_; }
60 bool empty() const { return end_ == begin_; }
63 T *data() { return begin_; }
66 const T *data() const { return begin_; }
68 T &front() { return *begin_; }
70 const T &front() const { return *begin_; }
72 T &back() { return *(end_ - 1); }
74 const T &back() const { return *(end_ - 1); }
78 size_type find(const T &t) const;
79 size_type capacity() const { return capacity_ - begin_; }
80
81 // Manipulators
83 void push_back(const T &t) { checkAllocated(size() + 1); new(end_) T(t); ++end_; }
87 T *emplace_back() { checkAllocated(size() + 1); return end_++; }
90 T *emplace_n_back(uint64 n);
92 void pop_back() { assert(size()); --end_; end_->~T(); }
97 void resize(size_type i, const T &t = T());
100 iterator insert(size_type i, const T &t);
102 void put(size_type i, const T &t);
104 template <class C> void append(const C &in);
107 iterator insert(iterator it, const T &t);
113 size_type removeAll(const T &t);
116 void clear();
120 void reset();
121 void reserve(size_type s) { checkAllocated(s); }
125 void clip() { if (size() * 2 < capacity() && size() > S) changeAllocated(size()); }
126
127 // Member access functions
130 iterator begin() { return begin_; }
133 const_iterator begin() const { return begin_; }
136 const_iterator constBegin() const { return begin_; }
139 iterator end() { return end_; }
142 const_iterator end() const { return end_; }
145 const_iterator constEnd() const { return end_; }
149 T &at(size_type i) { return begin_[i]; }
153 const T &at(size_type i) const { return begin_[i]; }
157 T &operator[](size_type i) { return at(i); }
161 const T &operator[](size_type i) const { return at(i); }
165 T value(int i, const T &t = T()) const { if (i < 0) return t; if (to<size_type>(i) >= size()) return t; return at(i); }
169 T value(uint32 i, const T &t = T()) const { if (to<size_type>(i) >= size()) return t; return at(i); }
173 T value(uint64 i, const T &t = T()) const { if (to<size_type>(i) >= size()) return t; return at(i); }
174private:
175 void checkAllocated(size_type target);
176 void changeAllocated(size_type newSize);
177 union { // Note: std::array<> used so you can see contents in debugger (which has been really irritating).
178 T *start_; // Placed in this union so constructors don't get called on creation.
179 std::array<T, S> arr_;
180 char buffer[sizeof(T)*S];
181 };
182 T *begin_ = reinterpret_cast<T *>(buffer);
183 T *end_ = begin_;
184 T *capacity_ = begin_ + S;
185};
186
190template <class T>
191class FArray<T, 0> : public std::vector<T> {
192public:
193 using std::vector<T>::vector;
194};
195
196
197
198// ---------------------------------------------------------------------------
199// IMPLEMENTATIONS
200// ---------------------------------------------------------------------------
201
202template <typename T,uint64 S>
204 checkAllocated(s);
205 end_ = begin_ + s;
206 for (T *b = begin_; b < end_; ++b)
207 new(b) T(t);
208}
209
210template <typename T,uint64 S>
211FArray<T,S>::FArray(std::initializer_list<T> l) {
212 for (auto it = l.begin(); it != l.end(); ++it)
213 push_back(*it);
214}
215
216template <typename T,uint64 S>
218 return operator=<S>(f);
219}
220
221template <typename T,uint64 S>
222template <uint64 S2>
223bool FArray<T,S>::operator==(const FArray<T,S2> &f) const {
224 if (size()!=f.size()) return false;
225 for (uint64 i=0;i<size();++i) {
226 if (operator[](i)!=f[i])
227 return false;
228 }
229 return true;
230}
231
232template <typename T,uint64 S>
233template <uint64 S2>
235 clear();
236 checkAllocated(f.size());
237 end_ = begin_ + f.size();
238 T *bl = begin_;
239 for (auto br = f.begin(); bl < end(); ++bl, ++br)
240 new(bl) T(*br);
241 return *this;
242}
243
244template <typename T,uint64 S>
245typename FArray<T,S>::size_type FArray<T,S>::find(const T &t) const {
246 for (size_type i = 0; i < size(); ++i) {
247 if (at(i) == t) return i;
248 }
249 return limits<size_type>::max();
250}
251
252template <typename T,uint64 S>
254 checkAllocated(size() + n);
255 T *ret = end_;
256 end_ += n;
257 return ret;
258}
259
260template <typename T,uint64 S>
261void FArray<T,S>::resize(size_type i, const T &t) {
262 if (size() == i) return;
263 if (size() > i) {
264 T *e = end_;
265 end_ = begin_ + i;
266 for (T *p = e - 1; p >= end_; --p)
267 p->~T();
268 return;
269 }
270 size_type s = size();
271 checkAllocated(i);
272 end_ = begin_ + i;
273 for (T *p = begin_ + s; p < end_; ++p)
274 new(p) T(t);
275}
276
277template <typename T,uint64 S>
279 i = std::min(size(), i);
280 if (i + 1 > size()) push_back(t);
281 else {
282 push_back(back());
283 for (iterator p = end_ - 2; p > begin_ + i; --p)
284 *p = *(p - 1);
285 *(begin_ + i) = t;
286 }
287 return begin_ + i;
288}
289
290template <typename T,uint64 S>
291void FArray<T,S>::put(size_type i, const T &t) {
292 if (size() <= i) {
293 resize(i);
294 push_back(t);
295 } else
296 at(i) = t;
297}
298
299template <typename T,uint64 S>
300template <class C>
301void FArray<T,S>::append(const C &in) {
302 for (auto i = in.begin(); i != in.end(); ++i)
303 push_back(*i);
304}
305
306template <typename T,uint64 S>
308 size_type i = it - begin_;
309 i = std::min(size(), i);
310 if (i + 1 > size()) push_back(t);
311 else {
312 push_back(back());
313 for (iterator p = end_ - 2; p > begin_ + i; --p)
314 *p = *(p - 1);
315 *(begin_ + i) = t;
316 }
317 return begin_ + i;
318}
319
320template <typename T,uint64 S>
322 if (i >= size()) return false;
323 for (iterator it = begin_ + i; it + 1 < end_; ++it)
324 *it = *(it + 1);
325 pop_back();
326 return true;
327}
328
329template <typename T,uint64 S>
331 size_type ret = 0;
332 for (size_type i = 0; i < size();) {
333 if (t == at(i)) {
334 remove(i);
335 ++ret;
336 } else
337 ++i;
338 }
339 return ret;
340}
341
342template <typename T,uint64 S>
344 // if type T is not plain old data, call the destructor for each array element;
345 if (!std::is_trivially_copyable<T>::value)
346 for (T *b = begin_; b < end_; ++b)
347 b->~T();
348 end_ = begin_;
349}
350
351template <typename T,uint64 S>
353 clear();
354 if (begin_ != reinterpret_cast<T *>(arr_.data()))
355 ::free(begin_);
356 end_ = begin_ = reinterpret_cast<T *>(arr_.data());
357 capacity_ = begin_ + S;
358}
359
360template <typename T,uint64 S>
361void FArray<T,S>::checkAllocated(size_type target) {
362 static constexpr size_type max_increase = 1024*1000;
363 if (begin_+target <= capacity_) return;
364 size_type newsize = std::max<size_type>(capacity(), 4) * 2;
365 while (newsize < target)
366 newsize += to<size_type>(std::min(max_increase, newsize));
367 changeAllocated(newsize);
368}
369
370template <typename T,uint64 S>
371void FArray<T,S>::changeAllocated(size_type newSize) {
372 size_type old_size = size();
373 assert(old_size <= newSize);
374 T *newBegin = reinterpret_cast<T *>(arr_.data());
375 if (newSize > S)
376#ifdef ITASCA_MEMORY_CHECKING
377 newBegin = reinterpret_cast<T *>(itasca::memory::imalloc(newSize * sizeof(T), __FILE__, __LINE__));
378#else
379 newBegin = reinterpret_cast<T *>(std::malloc(newSize * sizeof(T)));
380#endif
381 T *tl = newBegin;
382 for (T *tr = begin_; tr < end_; ++tl, ++tr) {
383 new(tl) T(std::move(*tr));
384 tr->~T();
385 }
386 if (begin_ != reinterpret_cast<T *>(arr_.data()))
387 std::free(begin_);
388 begin_ = newBegin;
389 end_ = begin_ + old_size;
390 capacity_ = begin_ + newSize;
391}
393// EoF
Combines base interface with Qt – allows Qt to interact with other Base types transparently.
An array class that attempts to minimize unnecessary heap access.
Definition farray.h:25
T & front()
Definition farray.h:68
void push_back(const T &t)
Adds a new element to the end of the array, increasing the array size by one.
Definition farray.h:83
bool empty() const
Definition farray.h:60
size_type stackSize() const
Returns the size of the array pre-allocated on the stack.
Definition farray.h:55
T value_type
Typedef to assist in STL compatibility.
Definition farray.h:28
FArray()
Default constructor - the array size is zero.
Definition farray.h:33
T & operator[](size_type i)
Definition farray.h:157
iterator begin()
Definition farray.h:130
FArray(const FArray< T, S2 > &f)
Copy constructor, valid for FArrays of the same data type but different stack lengths.
Definition farray.h:40
const_iterator constEnd() const
Definition farray.h:145
uint64 size_type
Typedef to assist in STL compatibility.
Definition farray.h:27
void clip()
Definition farray.h:125
const T & front() const
Definition farray.h:70
T value(uint64 i, const T &t=T()) const
Definition farray.h:173
T * emplace_back()
Definition farray.h:87
const_iterator begin() const
Definition farray.h:133
const T & operator[](size_type i) const
Definition farray.h:161
T & at(size_type i)
Definition farray.h:149
FArray(const FArray< T, S > &f)
Specialized copy constructor, for the special case of when the stack lengths are the same.
Definition farray.h:42
const T & back() const
Definition farray.h:74
~FArray()
Destructor.
Definition farray.h:45
const_iterator end() const
Definition farray.h:142
size_type allocated() const
Definition farray.h:58
iterator end()
Definition farray.h:139
void pop_back()
Removes the last element in the array. The results are undefined if the array is of zero length.
Definition farray.h:92
T value(int i, const T &t=T()) const
Definition farray.h:165
T value(uint32 i, const T &t=T()) const
Definition farray.h:169
const_iterator constBegin() const
Definition farray.h:136
T * data()
Definition farray.h:63
T * iterator
Typedef to assist in STL compatibility.
Definition farray.h:29
size_type size() const
Definition farray.h:53
T & back()
Definition farray.h:72
const T * const_iterator
Typedef to assist in STL compatibility.
Definition farray.h:30
const T & at(size_type i) const
Definition farray.h:153
const T * data() const
Definition farray.h:66
debug checked shorthand for std::numeric_limits<T>::
Definition limit.h:25
bool remove(size_type i)
Definition farray.h:321
iterator insert(iterator it, const T &t)
Definition farray.h:307
const FArray< T, S > & operator=(const FArray< T, S2 > &f)
Assignment operator, valid for FArrays of the same data type but different stack lengths.
Definition farray.h:234
iterator insert(size_type i, const T &t)
Definition farray.h:278
void resize(size_type i, const T &t=T())
Definition farray.h:261
void append(const C &in)
Appends the contents of one FArray onto another.
Definition farray.h:301
size_type find(const T &t) const
Definition farray.h:245
void reset()
Definition farray.h:352
void clear()
Definition farray.h:343
void put(size_type i, const T &t)
Adds a value to the array, first making certain it is big enough to hold it.
Definition farray.h:291
FArray(size_type s, const T &t=T())
Definition farray.h:203
size_type removeAll(const T &t)
Definition farray.h:330
T * emplace_n_back(uint64 n)
Definition farray.h:253