-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstaticlinkedlist.h
234 lines (212 loc) · 5.61 KB
/
staticlinkedlist.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2021 Charles Julian Knight
// https://github.com/rabidaudio/midi-voicesteal
#ifndef LIB_MIDIMANAGER_STATICLINKEDLIST_H_
#define LIB_MIDIMANAGER_STATICLINKEDLIST_H_
#include <assert.h>
#include "./types.h"
#define SLL_ASSERT_SANITY_CHECKS() \
assert((size_ == 0) == (cur_ == nullptr)); \
if (TSize > 1) { \
assert(head_->up == nullptr); \
assert(tail_->down == nullptr); \
assert(cur_ == nullptr || cur_->up != cur_->down); \
}
template <typename T>
struct Node {
T data;
Node<T>* up;
Node<T>* down;
};
template <typename T>
class Iterator {
private:
Node<T>* index_;
public:
explicit Iterator(Node<T>* start) {
index_ = start;
}
bool hasNext() {
return index_ != nullptr;
}
T& next() {
T* value = &index_->data;
index_ = index_->down;
return *value;
}
};
// StaticLinkedList is a linked list implementation which
// uses no dynamic allocations. It's max size is determined
// at compile time by TSize. If it's max size is exceeded, it
// will forget elements at the end of the list.
// It has 2 push implementations, allowing it to be used
// as a stack or as a queue (or a combination).
// It also has the ability to remove elements from arbitrary
// positions in the list. All operations are constant time
// except for arbitrary access and removal, which are linear.
// This class is *not* thread-safe.
// TSize must be positive.
template <typename T, size_t TSize> class StaticLinkedList {
private:
Node<T> nodes_[TSize];
Node<T>* head_; // the max slot
Node<T>* tail_; // the min slot
Node<T>* cur_; // the top of the stack, from tail
size_t size_;
public:
StaticLinkedList() {
assert(TSize > 0);
cur_ = nullptr;
size_ = 0;
tail_ = nodes_;
head_ = nodes_ + TSize - 1;
for (size_t i = 1; i < TSize-1; i++) {
nodes_[i].up = nodes_ + i + 1;
nodes_[i].down = nodes_ + i - 1;
}
tail_->down = nullptr;
head_->up = nullptr;
if (TSize == 1) {
tail_->up = nullptr;
head_->down = nullptr;
return;
}
tail_->up = nodes_ + 1;
head_->down = nodes_ + TSize - 2;
SLL_ASSERT_SANITY_CHECKS();
}
~StaticLinkedList() {}
bool isEmpty() {
SLL_ASSERT_SANITY_CHECKS();
return size_ == 0;
}
size_t size() {
SLL_ASSERT_SANITY_CHECKS();
return size_;
}
void clear() {
size_ = 0;
cur_ = nullptr;
SLL_ASSERT_SANITY_CHECKS();
}
// Index into an arbitrary position in the list. If you want
// to loop through all elements, use the iterator instead. O(n)
T& operator[](size_t index) {
Iterator<T> it = iterator();
for (size_t i = 0; i < index; i++) {
it.next();
}
return it.next();
}
// iterator provides a method to iterate through the items
// in the list in order. Note that if you mutate the list during
// the iteration, the iterator becomes invalid, and continuing to
// use it results in undefined behavior.
Iterator<T> iterator() {
return Iterator<T>(cur_);
}
// pushStack adds an item to the top, i.e. the next item to be popped.
// Use this if you wish to use the list as a stack. If the stack is full,
// it will forget the oldest item in the stack. O(1).
void pushStack(const T item) {
if (cur_ == nullptr) {
tail_->data = item;
cur_ = tail_;
size_ = 1;
SLL_ASSERT_SANITY_CHECKS();
return;
}
if (TSize == 1) {
cur_->data = item;
SLL_ASSERT_SANITY_CHECKS();
return;
}
if (cur_->up == nullptr) {
// we're full, so steal from the bottom
freeToHead(tail_);
size_--;
}
// now there's definitely space
cur_ = cur_->up;
cur_->data = item;
size_++;
SLL_ASSERT_SANITY_CHECKS();
return;
}
// pushQueue adds the item to the bottom, i.e. the last
// item to be popped. Use this if you which to use the list
// as a queue. Steals from the top if the list is full. O(1).
void pushQueue(const T item) {
if (isEmpty() || TSize == 1) {
pushStack(item);
return;
}
if (cur_ == head_) {
size_--;
cur_ = head_->down;
}
freeToTail(head_);
tail_->data = item;
size_++;
SLL_ASSERT_SANITY_CHECKS();
return;
}
// look at the next item without chaning the list.
T& peek() {
return cur_->data;
}
T& pop() {
T* result = &cur_->data;
// this will set cur_ to nullptr if we're at tail, which is expected
cur_ = cur_->down;
size_--;
SLL_ASSERT_SANITY_CHECKS();
return *result;
}
// removeAt removes elements from anywhere in the list.
// index zero is the top of the stack. O(n)
void removeAt(size_t index) {
if (index >= size_) return; // outside bounds
if (index == 0) {
pop();
return;
}
Node<T>* item = cur_;
for (size_t i = 0; i < index; i++) {
item = item->down;
}
freeToHead(item);
size_--;
}
private:
void freeToTail(Node<T>* item) {
if (item->up == nullptr) {
head_ = head_->down;
head_->up = nullptr;
} else {
item->down->up = item->up;
item->up->down = item->down;
}
tail_->down = item;
item->up = tail_;
item->down = nullptr;
tail_ = item;
SLL_ASSERT_SANITY_CHECKS();
}
void freeToHead(Node<T>* item) {
// remove this one from the chain
if (item->down == nullptr) {
tail_ = tail_->up;
tail_->down = nullptr;
} else {
item->down->up = item->up;
item->up->down = item->down;
}
// put this one at the head
head_->up = item;
item->down = head_;
item->up = nullptr;
head_ = item;
SLL_ASSERT_SANITY_CHECKS();
}
};
#endif // LIB_MIDIMANAGER_STATICLINKEDLIST_H_