89 #include "../submodules/CoreLibrary/CoreLibrary/types.h"
99 template<
typename T>
class list {
101 static const int32 null_ = -1;
108 cell() : next_(null_), prev_(null_) {}
111 std::vector<cell> cells_;
113 int32 used_cells_head_;
114 int32 used_cells_tail_;
116 uint32 used_cell_count_;
117 uint32 free_cell_count_;
119 void push_back_free_cell(
const T &t) {
121 int32 free = free_cells_;
122 free_cells_ = cells_[free_cells_].next_;
124 cells_[free].data_ = t;
125 cells_[free].next_ = null_;
126 cells_[free].prev_ = used_cells_tail_;
127 used_cells_tail_ = free;
130 void push_back_new_cell(
const T &t) {
135 c.prev_ = used_cells_tail_;
137 used_cells_tail_ = cells_.size() - 1;
140 void update_used_cells_tail_state() {
142 if (cells_[used_cells_tail_].prev_ != null_)
143 cells_[cells_[used_cells_tail_].prev_].next_ = used_cells_tail_;
144 if (used_cells_head_ == null_)
145 used_cells_head_ = used_cells_tail_;
149 void push_front_free_cell(
const T &t) {
151 int32 free = free_cells_;
152 free_cells_ = cells_[free_cells_].next_;
154 cells_[free].data_ = t;
155 cells_[free].next_ = used_cells_head_;
156 cells_[free].prev_ = null_;
157 used_cells_head_ = free;
160 void push_front_new_cell(
const T &t) {
164 c.next_ = used_cells_head_;
167 used_cells_head_ = cells_.size() - 1;
170 void update_used_cells_head_state() {
172 if (cells_[used_cells_head_].next_ != null_)
173 cells_[cells_[used_cells_head_].next_].prev_ = used_cells_head_;
174 if (used_cells_tail_ == null_)
175 used_cells_tail_ = used_cells_head_;
179 void __erase(int32 c) {
181 if (cells_[c].prev_ != null_)
182 cells_[cells_[c].prev_].next_ = cells_[c].next_;
184 used_cells_head_ = cells_[c].next_;
185 if (cells_[c].next_ != null_)
186 cells_[cells_[c].next_].prev_ = cells_[c].prev_;
188 used_cells_tail_ = cells_[c].prev_;
189 cells_[c].next_ = free_cells_;
194 int32 _erase(int32 c) {
196 int32 next_ = cells_[c].next_;
201 list() : used_cells_head_(null_), used_cells_tail_(null_), free_cells_(null_), used_cell_count_(0), free_cell_count_(0) {}
203 uint32 size()
const {
return used_cell_count_; }
204 void reserve(uint32 size) { cells_.reserve(size); }
207 used_cells_head_ = used_cells_tail_ = free_cells_ = null_;
208 used_cell_count_ = free_cell_count_ = 0;
211 void push_back(
const T &t) {
213 if (free_cell_count_)
214 push_back_free_cell(t);
216 push_back_new_cell(t);
217 update_used_cells_tail_state();
219 void push_back(
const T &t, int32 &location) {
221 if (free_cell_count_) {
223 location = free_cells_;
224 push_back_free_cell(t);
227 push_back_new_cell(t);
228 location = used_cells_tail_;
230 update_used_cells_tail_state();
232 void push_front(
const T &t) {
234 if (free_cell_count_)
235 push_front_free_cell(t);
237 push_front_new_cell(t);
238 update_used_cells_head_state();
240 void push_front(
const T &t, int32 &location) {
242 if (free_cell_count_) {
244 location = free_cells_;
245 push_front_free_cell(t);
248 push_front_new_cell(t);
249 location = used_cells_tail_;
251 update_used_cells_head_state();
260 bool operator ==(
const _iterator &i)
const {
return cell_ == i.cell_; }
261 bool operator !=(
const _iterator &i)
const {
return cell_ != i.cell_; }
273 const T &operator *()
const {
return list_->cells_[cell_].data_; }
274 const T *operator ->()
const {
return &(list_->cells_[cell_].data_); }
277 cell_ = list_->cells_[cell_].next_;
303 T &operator *()
const {
return list_->cells_[cell_].data_; }
304 T *operator ->()
const {
return &(list_->cells_[cell_].data_); }
307 cell_ = list_->cells_[cell_].next_;
321 const_iterator begin()
const {
return const_iterator(
this, used_cells_head_); }
322 const_iterator &end()
const {
return end_iterator_; }
323 iterator erase(iterator &i) {
return iterator(
this, _erase(i.cell_)); }
324 const_iterator erase(const_iterator &i) {
return const_iterator(
this, _erase(i.cell_)); }
325 void erase(int32 c) { __erase(c); }
326 void remove(
const T &t) {
328 const_iterator i(
this, used_cells_head_);
329 for (; i != end_iterator_; ++i) {
338 T &front() {
return cells_[used_cells_head_].data_; }
339 T &back() {
return cells_[used_cells_tail_].data_; }
342 template<
typename T>
typename list<T>::const_iterator list<T>::end_iterator_;