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 void pop_back() { assert(size()); --end_; end_->~T(); --size_; }
54 if (size()==i)
return;
58 for (T *p=e-1;p>=end_;--p)
67 for (T *p=begin_+s;p<end_;++p)
71 if (i>=size())
return -1;
79 if (i>=size())
return -1;
89 if (i>=size())
return false;
90 for (
iterator it=begin_+i;it<end_;++it)
109 if (!std::is_trivial<T>::value)
110 for (T *b=begin_;b<end_;++b)
118 if (defaultStorage_ != allocated_) {
124 changeAllocated(defaultStorage_);
128 void clip() {
if (size()*multFac_<allocated_) changeAllocated(size()*multFac_); }
135 T & at(
size_type i) {
return begin_[i]; }
136 const T & at(
size_type i)
const {
return begin_[i]; }
137 T & operator[](
size_type i) {
return at(i); }
138 const T & operator[](
size_type i)
const {
return at(i); }
141 if (target<=allocated_)
return;
143 size_type oldSize = std::max<size_type>(allocated_,defaultStorage_);
144 size_type newsize = multFac_*std::max<size_type>(target,oldSize);
145 changeAllocated(newsize);
147 void changeAllocated(
size_type newSize) {
149 assert(old_size<=newSize);
153 newBegin =
reinterpret_cast<T *
>(itasca::memory::imalloc(newSize*
sizeof(T),__FILE__,__LINE__));
155 newBegin =
reinterpret_cast<T *
>(std::malloc(newSize*
sizeof(T)));
158 for (T *tr=begin_;tr<end_;++tl,++tr) {
164 end_ = begin_ + old_size;
165 allocated_ = newSize;
172 double multFac_ = 1.3;
177 typedef uint64 size_type;
179 FlatArrayVec(size_type s,
double =1.3) : defaultStorage_(s) { this->reserve(defaultStorage_); }
182 for (
auto it=l.begin();it!=l.end();++it)
186 using std::vector<T>::reserve;
187 using std::vector<T>::assign;
189 defaultStorage_ = f.defaultStorage_;
191 reserve(f.capacity());
192 assign(f.begin(),f.end());
195 const size_type &allocated()
const {
return this->capacity(); }
196 size_type removeReplaceLast(size_type i) {
198 throw Exception(
"RemoveReplaceLast failed");
199 std::swap(this->at(i),this->back());
203 size_type removeReplaceLastClip(size_type i) {
204 size_type ret = removeReplaceLast(i);
205 this->shrink_to_fit();
209 bool remove(size_type i) {
215 size_type removeAll(
const T &t) {
217 for (size_type i=0;i<this->size();) {
218 if (t==this->at(i)) {
228 if (defaultStorage_ != this->capacity()) {
229 this->resize(defaultStorage_);
230 this->shrink_to_fit();
234 void clip() { this->shrink_to_fit(); }
235 const T & operator[] (
const int nIndex)
const {
238 if (nIndex >= (
int)this->size() or nIndex < 0)
239 throw Exception(
"Size check failed in FlatArray");
241 return std::vector<T>::operator[](nIndex);
247 T & operator[] (
const int nIndex) {
250 if (nIndex >= (
int)this->size() or nIndex < 0)
251 throw Exception(
"Size check failed in FlatArray");
253 return std::vector<T>::operator[](nIndex);
259 size_type defaultStorage_ = 1000;