-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathulist.pas
275 lines (225 loc) · 5.4 KB
/
ulist.pas
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
unit ulist;
{$mode objfpc}
interface
uses Classes;
// lightweight double linked list, similar to TList but minimalistic and customized to our needs
type PLightListElement = ^TLightListElement;
TLightListElement = record
item: Pointer;
prev, next: PLightListElement;
end;
TLightList = class(TObject)
private
head, tail, current: PLightListElement;
FCount: LongInt;
function GetElement(index: Longint): PLightListElement;
public
property Count: LongInt read FCount;
Constructor Create; virtual;
procedure Add(item: Pointer); virtual;
procedure AddFirst(item: Pointer); virtual;
procedure Insert(index: Longint; item: Pointer);
function Get: Pointer;
function GetFirst: Pointer;
function GetSecond: Pointer;
function GetThird: Pointer;
function GetFourth: Pointer;
function Items(index: Longint): Pointer;
function Remove: Pointer; virtual;
function RemoveFirst: Pointer; virtual;
function Delete(index: Longint): Pointer; virtual;
Destructor Destroy; override;
end;
implementation
uses math;
// get a certain item, 0 based index, expensive function!
function TLightList.GetElement(index: Longint): PLightListElement;
var i: LongInt;
begin
if index <= Count div 2 then
begin
current := head;
for i := 0 to index - 1 do current := current^.next;
end
else
begin
current := tail;
for i := 0 to Count - index - 2 do current := current^.prev;
end;
Result := current;
end;
Constructor TLightList.Create;
begin
inherited;
head := nil;
tail := nil;
FCount := 0;
end;
// add item to tail
procedure TLightList.Add(item: Pointer);
begin
new(current);
current^.item := item;
current^.prev := tail;
current^.next := nil;
if tail <> nil then tail^.next := current;
if head = nil then head := current;
tail := current;
inc(FCount);
end;
// add item to head
procedure TLightList.AddFirst(item: Pointer);
begin
new(current);
current^.item := item;
current^.prev := nil;
current^.next := head;
if head <> nil then head^.prev := current;
if tail = nil then tail := current;
head := current;
inc(FCount);
end;
// replaces the item at Position with the new one, shifts the rest up
procedure TLightList.Insert(index: Longint; item: Pointer);
var temp: PLightListElement;
begin
// range check
index := min(Count - 1, max(0, index));
if index = 0 then AddFirst(item)
else
begin
temp := GetElement(index);
new(current);
current^.item := item;
current^.prev := temp^.prev;
current^.prev^.next := current;
current^.next := temp;
temp^.prev := current;
inc(FCount);
end;
end;
// get item from tail
function TLightList.Get: Pointer;
begin
if tail <> nil then
Result := tail^.item
else
Result := nil;
end;
// get item from head
function TLightList.GetFirst: Pointer;
begin
if head <> nil then
Result := head^.item
else
Result := nil;
end;
// optimizations
function TLightList.GetSecond: Pointer;
begin
if Count < 2 then
Result := nil
else
Result := head^.next^.item;
end;
// optimizations
function TLightList.GetThird: Pointer;
begin
if Count < 3 then
Result := nil
else
Result := head^.next^.next^.item;
end;
// optimizations
function TLightList.GetFourth: Pointer;
begin
if Count < 4 then
Result := nil
else
Result := head^.next^.next^.next^.item;
end;
// get a certain item, 0 based index, expensive function!
function TLightList.Items(index: Longint): Pointer;
begin
if (index < 0) or (index > Count - 1) then
begin
Result := nil;
Exit;
end;
// optimizations for most common accessed items
if index = 0 then
begin
Result := head^.item;
Exit;
end
else
if index = 1 then
begin
Result := head^.next^.item;
Exit;
end
else
if index = 2 then
begin
Result := head^.next^.next^.item;
Exit;
end
else
Result := GetElement(index)^.item;
end;
// remove item from tail
function TLightList.Remove: Pointer;
begin
if tail = nil then Exit;
current := tail;
tail := current^.prev;
if tail <> nil then
tail^.next := nil
else
head := nil;
Result := current^.item;
Dispose(current);
dec(FCount);
end;
// remove item from head
function TLightList.RemoveFirst: Pointer;
begin
if head = nil then Exit;
current := head;
head := current^.next;
if head <> nil then
head^.prev := nil
else
tail := nil;
Result := current^.item;
Dispose(current);
dec(FCount);
end;
// deletes the item at index position
function TLightList.Delete(index: Longint): Pointer;
begin
if (index < 0) or (index > Count - 1) then
begin
Result := nil;
Exit;
end;
if index = 0 then Result := RemoveFirst
else
if index = Count - 1 then Result := Remove
else
begin
current := GetElement(index);
current^.next^.prev := current^.prev;
current^.prev^.next := current^.next;
Result := current^.item;
Dispose(current);
dec(FCount);
end;
end;
// empty list
Destructor TLightList.Destroy;
begin
while Count > 0 do RemoveFirst;
inherited;
end;
end.