9#ifndef __HRCORE_LINKEDLIST_HPP__
10#define __HRCORE_LINKEDLIST_HPP__
29 ival_t data[N] = { 0 };
30 node_t* prev =
nullptr;
31 node_t* next =
nullptr;
33 struct data_t :
public item_t {
38 node_t* _head =
nullptr;
39 node_t* _tail =
nullptr;
44 bool _addNode(node_t* pos)
48 node_t* tmp =
new node_t;
49 tmp->
next = pos->next;
53 tmp->next->prev = tmp;
62 bool _removeNode(node_t* pos)
64 if (!pos || this->_len == 1)
66 if (pos == this->_head && pos == this->_tail)
69 pos->prev->next = pos->next;
71 this->_head = pos->next;
75 pos->next->prev = pos->prev;
77 this->_tail = pos->prev;
87 this->_head =
new node_t;
88 this->_tail = this->_head;
89 this->_head->
next =
nullptr;
90 this->_head->prev =
nullptr;
95 for (node_t* tmp =
nullptr; _head !=
nullptr; _head = tmp) {
100 size_t length()
override {
return _count; }
107 if (pos >= this->_count)
108 return ret.data =
nullptr, ret;
109 size_t rpos = pos / N;
111 return ret.data =
nullptr, ret;
112 size_t opos = pos % N;
115 if (rpos >= this->_len / 2) {
117 for (
size_t i = 0; n !=
nullptr && i < this->_len - rpos - 1; ++i)
121 for (
size_t i = 0; n !=
nullptr && i < rpos; ++i)
124 ret.data = n->data + opos;
125 ret.appendix = (
void*)n;
132 size_t rlen = len / N + (len % N ? 1 : 0);
133 if (rlen > this->_len)
135 size_t rcount = this->_len - rlen;
137 for (
size_t i = 0; i < rcount; ++i)
138 ret &= this->_removeNode(this->_tail);
145 size_t elen = len / N + (len % N ? 1 : 0);
146 if (elen < this->_len)
148 size_t ecount = elen - this->_len;
150 for (
size_t i = 0; i < ecount; ++i)
151 ret &= this->_addNode(this->_tail);
158 data_t* tmp = (data_t*)&pos;
159 if (!tmp->appendix || tmp->index >= N || (tmp->appendix == this->_tail && tmp->index + 1 >= this->_count - (this->_len - 1) * N))
160 return tmp->data =
nullptr, *tmp;
161 if (tmp->index < N - 1) {
166 tmp->appendix = (
void*)(((node_t*)(tmp->appendix))->next);
167 tmp->data = (tmp->appendix ? ((node_t*)tmp->appendix)->data :
nullptr);
175 data_t* tmp = (data_t*)&pos;
176 if (!tmp->appendix || tmp->index >= N || (tmp->appendix == this->_head && tmp->index == 0))
177 return tmp->data =
nullptr, *tmp;
178 if (tmp->index > 0) {
183 tmp->appendix = (
void*)(((node_t*)(tmp->appendix))->prev);
184 tmp->data = (tmp->appendix ? ((node_t*)tmp->appendix)->data + N - 1 :
nullptr);
A class as a storage interface, pure virtual.
An implement of Storage::Interface using linked list.
bool extend(size_t len) override
Extend the storage to a given length.
item_t end() override
Returns the last item of storage.
bool shrink(size_t len) override
Remove data from the end to shrink the size to a given length.
item_t next(item_t pos) override
Get next item.
item_t prev(item_t pos) override
size_t length() override
Get the length of storage.
item_t begin() override
Return the first item of storage.
void delObject(const Interface *ptr) override
Delete a object from newObject()
item_t at(size_t pos) override
Access to storage[pos].
Interface * newObject() override
Get a new object of same type.
HRCore main namespace, contains all classes.