22 FlatArray(
size_type s,
double fac=1.3) : defaultStorage_(s), multFac_(fac) { checkAllocated(s); }
25 for (
auto it=l.begin();it!=l.end();++it)
31 defaultStorage_ = f.defaultStorage_;
32 checkAllocated(f.size());
33 end_ = begin_ + f.size();
36 for (T *br=f.begin();bl<end();++bl,++br)
40 const size_type &size()
const {
return size_; }
41 const size_type &allocated()
const {
return allocated_; }
42 bool empty()
const {
return end_ == begin_; }
43 T * data() {
return begin_; }
44 const T * data()
const {
return begin_; }
45 T & front() {
return *begin_; }
46 const T & front()
const {
return *begin_; }
47 T & back() {
return *(end_-1); }
48 const T & back()
const {
return *(end_-1); }
50 void push_back(
const T &t) { checkAllocated(size()+1);
new(end_) T(t); ++end_; ++size_; }
51 T *emplace_back() { checkAllocated(size()+1); ++size_;
return end_++; }
52 T *emplace_back(
const T &t) { checkAllocated(size()+1);
new(end_) T(t); ++size_;
return end_++; }
53 void pop_back() { assert(size()); --end_; end_->~T(); --size_; }
55 if (size()==i)
return;
59 for (T *p=e-1;p>=end_;--p)
68 for (T *p=begin_+s;p<end_;++p)
72 if (i>=size())
return -1;
80 if (i>=size())
return -1;
90 if (i>=size())
return false;
91 for (
iterator it=begin_+i;it<end_;++it)
110 if (!std::is_trivial<T>::value)
111 for (T *b=begin_;b<end_;++b)
119 if (defaultStorage_ != allocated_) {
125 changeAllocated(defaultStorage_);
129 void clip() {
if (size()*multFac_<allocated_) changeAllocated(size()*multFac_); }
136 T & at(
size_type i) {
return begin_[i]; }
137 const T & at(
size_type i)
const {
return begin_[i]; }
138 T & operator[](
size_type i) {
return at(i); }
139 const T & operator[](
size_type i)
const {
return at(i); }
142 if (target<=allocated_)
return;
144 size_type oldSize = std::max<size_type>(allocated_,defaultStorage_);
145 size_type newsize = multFac_*std::max<size_type>(target,oldSize);
146 changeAllocated(newsize);
148 void changeAllocated(
size_type newSize) {
150 assert(old_size<=newSize);
154 newBegin =
reinterpret_cast<T *
>(itasca::memory::imalloc(newSize*
sizeof(T),__FILE__,__LINE__));
156 newBegin =
reinterpret_cast<T *
>(std::malloc(newSize*
sizeof(T)));
159 for (T *tr=begin_;tr<end_;++tl,++tr) {
165 end_ = begin_ + old_size;
166 allocated_ = newSize;
173 double multFac_ = 1.3;
178 typedef uint64 size_type;
180 FlatArrayVec(size_type s,
double =1.3) : defaultStorage_(s) { this->reserve(defaultStorage_); }
183 for (
auto it=l.begin();it!=l.end();++it)
187 using std::vector<T>::reserve;
188 using std::vector<T>::assign;
190 defaultStorage_ = f.defaultStorage_;
192 reserve(f.capacity());
193 assign(f.begin(),f.end());
196 const size_type &allocated()
const {
return this->capacity(); }
197 size_type removeReplaceLast(size_type i) {
199 throw Exception(
"RemoveReplaceLast failed");
200 std::swap(this->at(i),this->back());
204 size_type removeReplaceLastClip(size_type i) {
205 size_type ret = removeReplaceLast(i);
206 this->shrink_to_fit();
210 bool remove(size_type i) {
216 size_type removeAll(
const T &t) {
218 for (size_type i=0;i<this->size();) {
219 if (t==this->at(i)) {
229 if (defaultStorage_ != this->capacity()) {
230 this->resize(defaultStorage_);
231 this->shrink_to_fit();
235 void clip() { this->shrink_to_fit(); }
236 const T & operator[] (
const int nIndex)
const {
239 if (nIndex >= (
int)this->size() or nIndex < 0)
240 throw Exception(
"Size check failed in FlatArray");
242 return std::vector<T>::operator[](nIndex);
248 T & operator[] (
const int nIndex) {
251 if (nIndex >= (
int)this->size() or nIndex < 0)
252 throw Exception(
"Size check failed in FlatArray");
254 return std::vector<T>::operator[](nIndex);
260 size_type defaultStorage_ = 1000;