27 template <
class T,u
int64 S=32>
class FArray {
35 FArray() : begin_(reinterpret_cast<T *>(arr_.
data())), end_(begin_), allocated_(S), size_(0) { }
40 explicit FArray(
size_type s,
const T &t=T()) : begin_(reinterpret_cast<T *>(arr_.
data())), end_(begin_), allocated_(S), size_(0) { checkAllocated(s); end_ = begin_ + s;
for (T *b=begin_;b<end_;++b)
new(b) T(t); size_ = s; }
45 FArray(std::initializer_list<T> l) : begin_(reinterpret_cast<T *>(arr_.
data())), end_(begin_), allocated_(S), size_(0) {
46 for (
auto it=l.begin();it!=l.end();++it)
54 checkAllocated(f.
size());
55 end_ = begin_ + f.
size();
58 for (
auto br=f.
begin();bl<
end();++bl,++br)
65 checkAllocated(f.
size());
66 end_ = begin_ + f.
size();
69 for (T *br=f.begin_;bl<end_;++bl,++br)
83 bool empty()
const {
return end_ == begin_; }
86 T *
data() {
return begin_; }
89 const T *
data()
const {
return begin_; }
93 const T &
front()
const {
return *begin_; }
95 T &
back() {
return *(end_-1); }
97 const T &
back()
const {
return *(end_-1); }
103 if (
at(i)==t)
return i;
110 void push_back(
const T &t) { checkAllocated(
size()+1);
new(end_) T(t); ++end_; ++size_; }
117 T *
emplace_n_back(uint64 n) { checkAllocated(
size() + n); T *ret = end_; end_ += n; size_ += n;
return ret; }
125 if (
size()==i)
return;
129 for (T *p=e-1;p>=end_;--p)
138 for (T *p=begin_+s;p<end_;++p)
144 i = std::min(
size(),i);
148 for (
iterator p=end_-2;p>begin_+i;--p)
163 template <
class C>
void append(
const C &in) {
for (
auto i=in.begin();i!=in.end();++i)
push_back(*i); }
168 i = std::min(
size(),i);
172 for (
iterator p=end_-2;p>begin_+i;--p)
181 if (i>=
size())
return false;
182 for (
iterator it=begin_+i;it+1<end_;++it)
205 if (!std::is_pod<T>::value)
206 for (T *b=begin_;b<end_;++b)
219 if (begin_!=reinterpret_cast<T *>(arr_.data()))
221 end_ = begin_ = reinterpret_cast<T *>(arr_.data());
267 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); }
271 T
value(quint32 i,
const T &t=T())
const {
if (to<size_type>(i)>=
size())
return t;
return at(i); }
275 T
value(uint64 i,
const T &t=T())
const {
if (to<size_type>(i)>=
size())
return t;
return at(i); }
278 static const size_type max_increase = 1000000;
279 if (target<=allocated_)
return;
280 size_type newsize = std::max<size_type>(allocated_,8);
281 while (newsize<target)
282 newsize += to<size_type>(std::min(max_increase,newsize));
283 changeAllocated(newsize);
285 void changeAllocated(
size_type newSize) {
287 assert(old_size<=newSize);
288 T *newBegin = reinterpret_cast<T *>(arr_.data());
291 newBegin = reinterpret_cast<T *>(itasca::memory::imalloc(newSize*
sizeof(T),__FILE__,__LINE__));
293 newBegin = reinterpret_cast<T *>(std::malloc(newSize*
sizeof(T)));
296 for (T *tr=begin_;tr<end_;++tl,++tr) {
300 if (begin_!=reinterpret_cast<T *>(arr_.data()))
303 end_ = begin_ + old_size;
304 allocated_ = newSize;
308 std::array<T, S> arr_;
310 T* begin_ = arr_.data();
321 using value_type = T;
324 FArray() : begin_(0), end_(0), allocated_(0), size_(0) { }
329 FArray(
size_type s,
const T &t=T()) : begin_(0), end_(begin_), allocated_(0), size_(0) { checkAllocated(s); end_ = begin_ + s;
for (T *b=begin_;b<end_;++b)
new(b) T(t); size_ = s; }
334 FArray(std::initializer_list<T> l) : begin_(0), end_(begin_), allocated_(0), size_(0) {
335 for (
auto it=l.begin();it!=l.end();++it)
343 checkAllocated(f.
size());
344 end_ = begin_ + f.
size();
347 for (T *br=f.
begin();bl<
end();++bl,++br)
354 checkAllocated(f.
size());
355 end_ = begin_ + f.
size();
358 for (T *br=f.begin_;bl<end_;++bl,++br)
370 bool empty()
const {
return end_ == begin_; }
376 const T *
data()
const {
return begin_; }
380 const T &
front()
const {
return *begin_; }
382 T &
back() {
return *(end_-1); }
384 const T &
back()
const {
return *(end_-1); }
388 void push_back(
const T &t) { checkAllocated(
size()+1);
new(end_) T(t); ++end_; ++size_; }
395 T *
emplace_n_back(uint64 n) { checkAllocated(
size() + n); T *ret = end_; end_ += n; size_ += n;
return ret; }
403 if (
size()==i)
return;
407 for (T *p=e-1;p>=end_;--p)
416 for (T *p=begin_+s;p<end_;++p)
422 i = std::min(
size(),i);
426 for (
iterator p=end_-2;p>begin_+i;--p)
436 i = std::min(
size(),i);
440 for (
iterator p=end_-2;p>begin_+i;--p)
455 template <
class C>
void append(
const C &in) {
for (
auto i=in.begin();i!=in.end();++i)
push_back(*i); }
459 if (i>=
size())
return false;
460 for (
iterator it=begin_+i;it<end_;++it)
483 if (!std::is_pod<T>::value)
484 for (T *b=begin_;b<end_;++b)
545 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); }
549 T
value(quint32 i,
const T &t=T())
const {
if (to<size_type>(i)>=
size())
return t;
return at(i); }
553 T
value(uint64 i,
const T &t=T())
const {
if (to<size_type>(i)>=
size())
return t;
return at(i); }
556 if (target<=allocated_)
return;
557 size_type newsize = std::max<size_type>(allocated_,8);
558 while (newsize<target)
559 newsize += std::min<size_type>(1000000,newsize);
560 changeAllocated(newsize);
562 void changeAllocated(
size_type newSize) {
564 assert(old_size<=newSize);
568 newBegin = reinterpret_cast<T *>(itasca::memory::imalloc(newSize*
sizeof(T),__FILE__,__LINE__));
570 newBegin = reinterpret_cast<T *>(std::malloc(newSize*
sizeof(T)));
573 for (T *tr=begin_;tr<end_;++tl,++tr) {
579 end_ = begin_ + old_size;
580 allocated_ = newSize;
const T & back() const
Definition: farray.h:384
void reset()
Definition: farray.h:495
const_iterator begin() const
Definition: farray.h:513
void reset()
Definition: farray.h:217
FArray(size_type s, const T &t=T())
Definition: farray.h:329
T * emplace_back()
Definition: farray.h:392
FArray(size_type s, const T &t=T())
Definition: farray.h:40
const_iterator constBegin() const
Definition: farray.h:516
const T & front() const
Definition: farray.h:93
FArray(const FArray< T, S2 > &f)
Copy constructor, valid for FArrays of the same data type but different stack lengths.
Definition: farray.h:331
iterator insert(iterator it, const T &t)
Definition: farray.h:166
iterator end()
Definition: farray.h:241
T value(quint32 i, const T &t=T()) const
Definition: farray.h:549
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:447
T value(uint64 i, const T &t=T()) const
Definition: farray.h:275
FArray(const FArray< T, S2 > &f)
Copy constructor, valid for FArrays of the same data type but different stack lengths.
Definition: farray.h:42
const_iterator end() const
Definition: farray.h:244
const_iterator end() const
Definition: farray.h:522
bool remove(size_type i)
Definition: farray.h:180
~FArray()
Destructor.
Definition: farray.h:50
const_iterator constEnd() const
Definition: farray.h:247
T value(uint64 i, const T &t=T()) const
Definition: farray.h:553
void clip()
Definition: farray.h:227
void append(const C &in)
Appends the contents of one FArray onto another.
Definition: farray.h:163
iterator insert(size_type i, const T &t)
Definition: farray.h:143
DVect * iterator
Typedef to assist in STL compatibility.
Definition: farray.h:31
const FArray< T, 0 > & operator=(const FArray< T, S2 > &f)
Assignment operator, valid for FArrays of the same data type but different stack lengths.
Definition: farray.h:341
const FArray< T, S > & operator=(const FArray< T, S > &f)
Specialized assignment operator, for the special case of when the stack lengths are the same.
Definition: farray.h:63
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:52
uint64 size_type
Typedef to assist in STL compatibility.
Definition: farray.h:29
T & at(size_type i)
Definition: farray.h:251
T & operator[](size_type i)
Definition: farray.h:537
FArray(const FArray< T, 0 > &f)
Specialized copy constructor, for the special case of when the stack lengths are the same.
Definition: farray.h:333
const_iterator begin() const
Definition: farray.h:235
T * emplace_n_back(uint64 n)
Definition: farray.h:395
bool remove(size_type i)
Definition: farray.h:458
void clip()
Definition: farray.h:505
T & front()
Definition: farray.h:91
debug checked shorthand for std::numeric_limits<T>::
Definition: limit.h:25
void resize(size_type i, const T &t=T())
Definition: farray.h:402
T value(int i, const T &t=T()) const
Definition: farray.h:267
void clear()
Definition: farray.h:202
iterator insert(iterator it, const T &t)
Definition: farray.h:434
const T * const_iterator
Typedef to assist in STL compatibility.
Definition: farray.h:320
size_type allocated() const
Definition: farray.h:81
void append(const C &in)
Appends the contents of one FArray onto another.
Definition: farray.h:455
T & back()
Definition: farray.h:382
void resize(size_type i, const T &t=T())
Definition: farray.h:124
bool empty() const
Definition: farray.h:370
T * data()
Definition: farray.h:373
const T * data() const
Definition: farray.h:376
size_type size() const
Definition: farray.h:76
T & at(size_type i)
Definition: farray.h:529
T & front()
Definition: farray.h:378
iterator insert(size_type i, const T &t)
Definition: farray.h:421
FArray(const FArray< T, S > &f)
Specialized copy constructor, for the special case of when the stack lengths are the same.
Definition: farray.h:44
iterator end()
Definition: farray.h:519
const T & operator[](size_type i) const
Definition: farray.h:541
FArray()
Default constructor - the array size is zero.
Definition: farray.h:324
T value(quint32 i, const T &t=T()) const
Definition: farray.h:271
const FArray< T, 0 > & operator=(const FArray< T, 0 > &f)
Specialized assignment operator, for the special case of when the stack lengths are the same.
Definition: farray.h:352
const_iterator constEnd() const
Definition: farray.h:525
size_type find(const T &t) const
Definition: farray.h:101
DVect value_type
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:397
T & back()
Definition: farray.h:95
const T & back() const
Definition: farray.h:97
const DVect * const_iterator
Typedef to assist in STL compatibility.
Definition: farray.h:32
const_iterator constBegin() const
Definition: farray.h:238
size_type removeAll(const T &t)
Definition: farray.h:467
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:27
const T * data() const
Definition: farray.h:89
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:110
void pop_back()
Removes the last element in the array. The results are undefined if the array is of zero length.
Definition: farray.h:119
T * emplace_n_back(uint64 n)
Definition: farray.h:117
size_type stackSize() const
Returns the size of the array pre-allocated on the stack.
Definition: farray.h:78
uint64 size_type
Typedef to assist in STL compatibility.
Definition: farray.h:318
const size_type & size() const
Definition: farray.h:365
const T & front() const
Definition: farray.h:380
const T & at(size_type i) const
Definition: farray.h:255
size_type removeAll(const T &t)
Definition: farray.h:189
const T & at(size_type i) const
Definition: farray.h:533
T * emplace_back()
Definition: farray.h:114
T * iterator
Typedef to assist in STL compatibility.
Definition: farray.h:319
void clear_no_destruct()
Definition: farray.h:213
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:155
void clear_no_destruct()
Definition: farray.h:491
T * data()
Definition: farray.h:86
const T & operator[](size_type i) const
Definition: farray.h:263
void clear()
Definition: farray.h:480
FArray()
Default constructor - the array size is zero.
Definition: farray.h:35
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:388
T value(int i, const T &t=T()) const
Definition: farray.h:545
bool empty() const
Definition: farray.h:83
Template specialization for S=0 - indication no stack space at all should be made.
Definition: farray.h:316
iterator begin()
Definition: farray.h:510
T & operator[](size_type i)
Definition: farray.h:259
iterator begin()
Definition: farray.h:232
const size_type & allocated() const
Definition: farray.h:368
~FArray()
Destructor.
Definition: farray.h:339