2 #ifdef USE_INITIALIZER_LIST
3 # include "InitializerList.h"
22 Node* prior =
nullptr;
32 if (node->next!=
nullptr){
42 if (node->prior!=
nullptr){
51 inline Iterator operator+(
int offset) {
52 return getIteratorAtOffset(offset);
54 inline Iterator operator-(
int offset) {
55 return getIteratorAtOffset(-offset);
57 inline bool operator==(
Iterator it) {
58 return node == it.get_node();
60 inline bool operator!=(
Iterator it) {
61 return node != it.get_node();
63 inline T &operator*() {
66 inline T *operator->() {
69 inline Node *get_node() {
72 inline operator bool() {
79 Iterator getIteratorAtOffset(
int offset){
82 for (
int j=0;j<offset;j++){
83 if (tmp->next==
nullptr){
89 for (
int j=0;j<-offset;j++){
90 if (tmp->prior==
nullptr){
104 p_allocator = &allocator;
113 p_allocator = &allocator;
115 for(
int i = 0; i < N; ++i)
123 #ifdef USE_INITIALIZER_LIST
125 List(std::initializer_list<T> iniList) {
127 for(
auto &obj : iniList)
131 bool swap(List<T>&ref){
137 record_count = ref.record_count;
139 ref.first = tmp.first;
141 ref.record_count = tmp.record_count;
147 bool push_back(T data){
148 Node *node = createNode();
149 if (node==
nullptr)
return false;
153 Node *old_last_prior = last.prior;
155 node->prior = old_last_prior;
156 old_last_prior->next = node;
164 bool push_front(T data){
165 Node *node = createNode();
166 if (node==
nullptr)
return false;
170 Node *old_begin_next = first.next;
171 node->prior = &first;
172 node->next = old_begin_next;
173 old_begin_next->prior = node;
181 bool insert(Iterator it,
const T& data){
182 Node *node = createNode();
183 if (node==
nullptr)
return false;
187 Node *current_node = it.get_node();
188 Node *prior = current_node->prior;
191 current_node->prior = node;
193 node->next = current_node;
203 return pop_front(tmp);
208 return pop_back(tmp);
211 bool pop_front(T &data){
212 if (record_count==0)
return false;
214 Node *p_delete = firstDataNode();
215 Node *p_prior = p_delete->prior;
216 Node *p_next = p_delete->next;
218 data = p_delete->data;
221 p_prior->next = p_next;
222 p_next->prior = p_prior;
224 deleteNode(p_delete);
232 bool pop_back(T &data){
233 if (record_count==0)
return false;
234 Node *p_delete = lastDataNode();
235 Node *p_prior = p_delete->prior;
236 Node *p_next = p_delete->next;
239 data = p_delete->data;
242 p_prior->next = p_next;
243 p_next->prior = p_prior;
245 deleteNode(p_delete);
253 bool erase (Iterator it){
254 Node *p_delete = it.get_node();
256 if (empty() || p_delete==&first || p_delete==&last){
259 Node *p_prior = p_delete->prior;
260 Node *p_next = p_delete->next;
263 p_prior->next = p_next;
264 p_next->prior = p_prior;
266 deleteNode(p_delete);
274 Iterator it(firstDataNode());
284 Iterator it(lastDataNode());
308 inline T &operator[](
int index) {
309 Node *n = firstDataNode();
310 for (
int j=0;j<index;j++){
319 void setAllocator(Allocator &allocator){
320 p_allocator = &allocator;
332 size_t record_count=0;
333 Allocator *p_allocator = &DefaultAllocator;
337 Node *node = (Node*) p_allocator->
allocate(
sizeof(Node));
339 Node *node =
new Node();
344 void deleteNode(Node* p_delete){
346 p_allocator->
free(p_delete);
358 Node* lastDataNode(){
361 Node* firstDataNode(){
366 assert(first.next!=
nullptr);
367 assert(last.prior!=
nullptr);
369 assert(first.next = &last);
370 assert(last.prior = &first);