24template <
typename T, u
int64 S = 32,
typename I=u
int64>
43 FArray(std::initializer_list<T> l);
49 template <u
int64 S2>
bool operator==(
const FArray<T,S2,I> &f)
const;
53 I
size()
const {
return end_ - begin_; }
60 bool empty()
const {
return end_ == begin_; }
63 T *
data() {
return begin_; }
66 const T *
data()
const {
return begin_; }
70 const T &
front()
const {
return *begin_; }
72 T &
back() {
return *(end_ - 1); }
74 const T &
back()
const {
return *(end_ - 1); }
79 size_type capacity()
const {
return capacity_ - begin_; }
83 void push_back(
const T &t) { checkAllocated(
size() + 1);
new(end_) T(t); ++end_; }
102 void put(I i,
const T &t);
104 template <
class C>
void append(
const C &in);
121 void reserve(
size_type s) { checkAllocated(s); }
153 const T &
at(I i)
const { assert(i<
size());
return begin_[
static_cast<size_type>(i)]; }
164 T
value(I i,
const T &t = {})
const {
if (to<size_type>(i) >=
size())
return t;
return at(i); }
178 std::array<T, S> arr_;
179 char buffer[
sizeof(T)*S];
181 T *begin_ =
reinterpret_cast<T *
>(buffer);
183 T *capacity_ = begin_ + S;
190class FArray<T, 0> :
public std::vector<T> {
192 using std::vector<T>::vector;
201template <
typename T,u
int64 S,
typename I>
205 for (T *b = begin_; b < end_; ++b)
209template <
typename T,u
int64 S,
typename I>
211 for (
auto it = l.begin(); it != l.end(); ++it)
215template <
typename T,u
int64 S,
typename I>
217 return operator=<S>(f);
220template <
typename T,u
int64 S,
typename I>
223 if (size()!=f.
size())
return false;
224 for (uint64 i=0;i<size();++i) {
225 if (
operator[](i)!=f[i])
231template <
typename T,u
int64 S,
typename I>
235 checkAllocated(f.
size());
236 end_ = begin_ + f.
size();
238 for (
auto br = f.
begin(); bl < end(); ++bl, ++br)
243template <
typename T,u
int64 S,
typename I>
246 if (at(i) == t)
return i;
251template <
typename T,u
int64 S,
typename I>
253 checkAllocated(size() + n);
259template <
typename T,u
int64 S,
typename I>
261 if (size() ==
static_cast<uint64
>(i))
return;
262 if (size() >
static_cast<uint64
>(i)) {
264 end_ = begin_ +
static_cast<uint64
>(i);
265 for (T *p = e - 1; p >= end_; --p)
271 end_ = begin_ +
static_cast<uint64
>(i);
272 for (T *p = begin_ + s; p < end_; ++p)
276template <
typename T,u
int64 S,
typename I>
278 i = std::min(size(), i);
279 if (i + 1 > size()) push_back(t);
282 for (
iterator p = end_ - 2; p > begin_ + i; --p)
289template <
typename T,u
int64 S,
typename I>
298template <
typename T,u
int64 S,
typename I>
301 for (
auto i = in.begin(); i != in.end(); ++i)
305template <
typename T,u
int64 S,
typename I>
308 i = std::min(size(), i);
309 if (i + 1 > size()) push_back(t);
312 for (
iterator p = end_ - 2; p > begin_ + i; --p)
319template <
typename T,u
int64 S,
typename I>
321 if (i >= size())
return false;
322 for (
iterator it = begin_ + i; it + 1 < end_; ++it)
328template <
typename T,u
int64 S,
typename I>
341template <
typename T,u
int64 S,
typename I>
344 if (!std::is_trivially_copyable<T>::value)
345 for (T *b = begin_; b < end_; ++b)
350template <
typename T,u
int64 S,
typename I>
353 if (begin_ !=
reinterpret_cast<T *
>(arr_.data()))
355 end_ = begin_ =
reinterpret_cast<T *
>(arr_.data());
356 capacity_ = begin_ + S;
359template <
typename T,u
int64 S,
typename I>
361 static constexpr uint64 max_increase = 1024*1000;
362 if (begin_+target <= capacity_)
return;
363 uint64 newsize = std::max<uint64>(capacity(), 4) * 2;
364 while (newsize < target)
365 newsize += to<size_type>(std::min(max_increase, newsize));
366 changeAllocated(newsize);
369template <
typename T,u
int64 S,
typename I>
371 size_type old_size = size();
372 assert(old_size <= newSize);
373 T *newBegin =
reinterpret_cast<T *
>(arr_.data());
375#ifdef ITASCA_MEMORY_CHECKING
376 newBegin =
reinterpret_cast<T *
>(itasca::memory::imalloc(newSize *
sizeof(T), __FILE__, __LINE__));
378 newBegin =
reinterpret_cast<T *
>(std::malloc(newSize *
sizeof(T)));
381 for (T *tr = begin_; tr < end_; ++tl, ++tr) {
382 new(tl) T(std::move(*tr));
385 if (begin_ !=
reinterpret_cast<T *
>(arr_.data()))
388 end_ = begin_ + old_size;
389 capacity_ = begin_ + newSize;
One stop include for all objects defined as part of base interface.
An array class that attempts to minimize unnecessary heap access.
Definition farray.h:25
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
T & operator[](I i)
Definition farray.h:157
constexpr FArray(const FArray< T, S, I > &f)
Specialized copy constructor, for the special case of when the stack lengths are the same.
Definition farray.h:42
bool empty() const
Definition farray.h:60
const T & at(I i) const
Definition farray.h:153
iterator begin()
Definition farray.h:130
T * emplace_back()
Definition farray.h:87
uint64 size_type
Typedef to assist in STL compatibility.
Definition farray.h:27
size_type allocated() const
Definition farray.h:58
const_iterator end() const
Definition farray.h:142
T * data()
Definition farray.h:63
const_iterator constBegin() const
Definition farray.h:136
T & back()
Definition farray.h:72
const T * data() const
Definition farray.h:66
T & at(I i)
Definition farray.h:149
T & front()
Definition farray.h:68
void clip()
Definition farray.h:125
T * iterator
Typedef to assist in STL compatibility.
Definition farray.h:29
const T & back() const
Definition farray.h:74
size_type stackSize() const
Returns the size of the array pre-allocated on the stack.
Definition farray.h:55
const T & operator[](I i) const
Definition farray.h:161
T value(I i, const T &t={}) const
Definition farray.h:164
const_iterator begin() const
Definition farray.h:133
iterator end()
Definition farray.h:139
~FArray()
Destructor.
Definition farray.h:45
const_iterator constEnd() const
Definition farray.h:145
const T * const_iterator
Typedef to assist in STL compatibility.
Definition farray.h:30
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_type
Typedef to assist in STL compatibility.
Definition farray.h:28
const T & front() const
Definition farray.h:70
constexpr FArray()
Default constructor - the array size is zero.
Definition farray.h:33
I size() const
Definition farray.h:53
FArray(const FArray< T, S2, I > &f)
Copy constructor, valid for FArrays of the same data type but different stack lengths.
Definition farray.h:40
debug checked shorthand for std::numeric_limits<T>::
Definition limit.h:25
void put(I i, const T &t)
Adds a value to the array, first making certain it is big enough to hold it.
Definition farray.h:290
void resize(I i, const T &t=T())
Definition farray.h:260
I find(const T &t) const
Definition farray.h:244
iterator insert(I i, const T &t)
Definition farray.h:277
constexpr const FArray< T, S, I > & operator=(const FArray< T, S2, I > &f)
Assignment operator, valid for FArrays of the same data type but different stack lengths.
Definition farray.h:233
FArray(size_type s, const T &t=T())
Definition farray.h:202
void clear()
Definition farray.h:342
iterator insert(iterator it, const T &t)
Definition farray.h:306
bool remove(I i)
Definition farray.h:320
T * emplace_n_back(size_type n)
Definition farray.h:252
void append(const C &in)
Appends the contents of one FArray onto another.
Definition farray.h:300
size_type removeAll(const T &t)
Definition farray.h:329
void reset()
Definition farray.h:351