-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagedlist.cpp
More file actions
183 lines (146 loc) · 3.52 KB
/
pagedlist.cpp
File metadata and controls
183 lines (146 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <bitset>
#include <iostream>
#include <string>
template <typename T, size_t Ps = 32>
class PagedList {
private:
consteval static size_t roundUp(size_t n) {
if ((n & (n - 1)) == 0)
return n;
--n;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
if (4 < sizeof(n))
n |= n >> 32;
return n + 1;
}
struct Node;
struct Page;
struct PageData {
Node nodes[Ps];
size_t size;
size_t last;
std::bitset<Ps> present;
Page *prev{};
Page *next{};
};
struct Node {
alignas(sizeof(T)) char raw[sizeof(T)];
Node *prev{};
Node *next{};
operator T &() {
return *reinterpret_cast<T *>(raw);
}
template <typename... Args>
T & construct(Args &&...args) {
return *new (raw) T(std::forward<Args>(args)...);
}
void destruct() {
static_cast<T &>(*this).~T();
}
Page * getPage() const {
const std::uintptr_t address(this);
return reinterpret_cast<Page *>(address - address % sizeof(PageData));
}
size_t getIndex() const {
const std::uintptr_t address(this);
return (address % sizeof(PageData)) / sizeof(Node);
}
};
struct alignas(roundUp(sizeof(PageData))) Page: PageData {
~Page() {
for (size_t i = 0; i < Ps; ++i) {
if (this->present.test(i))
this->nodes[i].destruct();
}
}
template <typename... Args>
T & construct(size_t index, Args &&...args) {
if (!this->present.test(index)) {
++this->size;
this->present.set(index);
}
this->nodes[index].construct(std::forward<Args>(args)...);
}
T & at(size_t index) {
if (!this->present.test(index))
throw std::out_of_range("Index not contained");
return static_cast<T &>(this->nodes[index]);
}
void erase(size_t index) {
if (!this->present.test(index))
throw std::out_of_range("Index not contained");
searchLast(index);
this->nodes[index].destruct();
--this->size;
this->present.reset(index);
}
void searchLast(size_t index) {
if (this->last != index || this->last == 0)
return;
for (--this->last; this->last != 0 && this->present.test(index); --this->last);
if (this->last == 0 && this->present.test(0)) {
if (index == Ps - 1) {
this->last = index;
} else {
for (this->last = index + 1; this->last != Ps - 1 && this->present.test(index); ++this->last);
}
}
}
};
std::vector<Page *> pages;
public:
class iterator {
private:
Node *node{};
public:
iterator() = default;
iterator(Node *node_):
node(node_) {}
inline T & operator*() const {
return static_cast<T &>(*node);
}
inline T * operator->() const {
return &static_cast<T &>(*node);
}
inline bool operator==(const iterator &other) const {
return node == other.node;
}
inline bool operator!=(const iterator &other) const {
return node != other.node;
}
iterator & operator++() {
node = node->next;
return *this;
}
iterator & operator--() {
node = node->prev;
return *this;
}
iterator operator++(int) {
iterator original{*this};
node = node->next;
return original;
}
iterator operator--(int) {
iterator original{*this};
node = node->prev;
return original;
}
};
PagedList() = default;
~PagedList() {
for (Page *page: pages)
delete page;
}
void insert(iterator iter, const T &item) {
Node *node = iter->node;
size_t index = node->getIndex();
}
};
int main() {
PagedList<int> list;
}