11 FlatArray(
size_type s,
double fac=1.3) : defaultStorage_(s), multFac_(fac) { checkAllocated(s); }
14 for (
auto it=l.begin();it!=l.end();++it)
20 defaultStorage_ = f.defaultStorage_;
21 checkAllocated(f.size());
22 end_ = begin_ + f.size();
25 for (T *br=f.begin();bl<end();++bl,++br)
29 const size_type &size()
const {
return size_; }
30 const size_type &allocated()
const {
return allocated_; }
31 bool empty()
const {
return end_ == begin_; }
32 T * data() {
return begin_; }
33 const T * data()
const {
return begin_; }
34 T & front() {
return *begin_; }
35 const T & front()
const {
return *begin_; }
36 T & back() {
return *(end_-1); }
37 const T & back()
const {
return *(end_-1); }
39 void push_back(
const T &t) { checkAllocated(size()+1);
new(end_) T(t); ++end_; ++size_; }
40 T *emplace_back() { checkAllocated(size()+1); ++size_;
return end_++; }
41 void pop_back() { assert(size()); --end_; end_->~T(); --size_; }
43 if (size()==i)
return;
47 for (T *p=e-1;p>=end_;--p)
56 for (T *p=begin_+s;p<end_;++p)
60 if (i>=size())
return -1;
68 if (i>=size())
return -1;
78 if (i>=size())
return false;
79 for (
iterator it=begin_+i;it<end_;++it)
98 if (!std::is_trivial<T>::value)
99 for (T *b=begin_;b<end_;++b)
107 if (defaultStorage_ != allocated_) {
113 changeAllocated(defaultStorage_);
117 void clip() {
if (size()*multFac_<allocated_) changeAllocated(size()*multFac_); }
124 T & at(
size_type i) {
return begin_[i]; }
125 const T & at(
size_type i)
const {
return begin_[i]; }
126 T & operator[](
size_type i) {
return at(i); }
127 const T & operator[](
size_type i)
const {
return at(i); }
130 if (target<=allocated_)
return;
132 size_type oldSize = std::max<size_type>(allocated_,defaultStorage_);
133 size_type newsize = multFac_*std::max<size_type>(target,oldSize);
134 changeAllocated(newsize);
136 void changeAllocated(
size_type newSize) {
138 assert(old_size<=newSize);
142 newBegin =
reinterpret_cast<T *
>(itasca::memory::imalloc(newSize*
sizeof(T),__FILE__,__LINE__));
144 newBegin =
reinterpret_cast<T *
>(std::malloc(newSize*
sizeof(T)));
147 for (T *tr=begin_;tr<end_;++tl,++tr) {
153 end_ = begin_ + old_size;
154 allocated_ = newSize;
161 double multFac_ = 1.3;
166 typedef uint64 size_type;
168 FlatArrayVec(size_type s,
double =1.3) : defaultStorage_(s) { this->reserve(defaultStorage_); }
171 for (
auto it=l.begin();it!=l.end();++it)
176 defaultStorage_ = f.defaultStorage_;
178 reserve(f.capacity());
179 assign(f.begin(),f.end());
182 const size_type &allocated()
const {
return this->capacity(); }
183 size_type removeReplaceLast(size_type i) {
184 if (i>=this->size())
return -1;
185 std::swap(this->at(i),this->back());
189 size_type removeReplaceLastClip(size_type i) {
190 size_type ret = removeReplaceLast(i);
191 this->shrink_to_fit();
195 bool remove(size_type i) {
196 if (i>=this->size())
return false;
200 size_type removeAll(
const T &t) {
202 for (size_type i=0;i<this->size();) {
203 if (t==this->at(i)) {
213 if (defaultStorage_ != this->capacity()) {
214 this->resize(defaultStorage_);
215 this->shrink_to_fit();
219 void clip() { this->shrink_to_fit(); }
221 size_type defaultStorage_ = 1000;