-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListUtils.pas
273 lines (269 loc) · 7.5 KB
/
ListUtils.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
Unit ListUtils;
interface
type
ListItem<TValue> = class
public
constructor Create(val: TValue);
private
prev, next: ListItem<TValue>;
value: TValue;
end;
Predicate<TValue> = interface
function check(val:TValue):boolean;
end;
Iterator<TValue> = class
public
constructor create;
constructor Create(initial: ListItem<TValue>);
destructor Destroy;
procedure unSet;
function hasNext : boolean;
procedure moveNext;
function Iterator<TValue>.nextIter:Iterator<TValue>;
function getNext:TValue;
function takeNext : TValue;
private
next: ListItem<TValue>;
end;
List<TValue> = class
public
constructor Create;
destructor Destroy;
function length:word;
function isEmpty : boolean;
procedure push(val: TValue);
function get(ind: integer) : TValue;
function getFromEnd(ind: integer) : TValue;
function getFirst : TValue;
function getLast : TValue;
function getBegin : Iterator<TValue>;
function getEnd : Iterator<TValue>;
function getIterator(ind: integer) : Iterator<TValue>;
procedure change(iter:Iterator<TValue>;val:TValue);
procedure remove(it:Iterator<TValue>);
procedure removeAll(val:TValue);
procedure removeAll(condition:Predicate<TValue>);
procedure print;
function find(condition:Predicate<TValue>;from:Iterator<TValue>):Iterator<TValue>;
function find(condition:Predicate<TValue>):Iterator<TValue>;
function find(val:TValue; from:Iterator<TValue>):Iterator<TValue>;
function find(val:TValue):Iterator<TValue>;
private
head, tail: ListItem<TValue>;
leng:word;
end;
implementation
constructor ListItem<TValue>.Create(val: TValue);
begin
next:=nil;
prev:=nil;
value:=val;
end;
constructor Iterator<TValue>.create;
begin
next := nil;
end;
constructor Iterator<TValue>.Create(initial: ListItem<Tvalue>);
begin
next := initial;
end;
destructor Iterator<TValue>.Destroy;
begin
next:=nil;
end;
procedure Iterator<TValue>.unSet;
begin
next:=nil;
end;
function Iterator<TValue>.hasNext : boolean;
begin
hasNext := next <> nil;
end;
procedure Iterator<TValue>.moveNext;
begin
next:=next.next;
end;
function Iterator<TValue>.nextIter:Iterator<TValue>;
begin
nextIter:=new Iterator<TValue>(next.next);
end;
function Iterator<TValue>.getNext:TValue;
begin
assert(next<>nil,'next=nil!');
getNext:=next.value;
end;
function Iterator<TValue>.takeNext : TValue;
begin
Assert(hasNext, 'Iterator has no next item');
takeNext := getNext;
{prev := next;}
moveNext;
end;
constructor List<TValue>.Create;
begin
leng:=0;
head := nil;
tail := nil;
end;
destructor List<TValue>.Destroy;
{var iter:Iterator<TValue>;}
begin
// TODO: fix this function
{if head<>nil then begin
leng:=0;
tail:=nil;
while head<>nil do begin
iter:=new Iterator<TValue>(head);
head:=head^.next;
iter.next^.next:=nil;
iter.next^.prev:=nil;
dispose(iter.next);
Iter.Destroy;
end;
end;}
end;
function List<TValue>.length:word;
begin
length:=leng;
end;
function List<TValue>.isEmpty : boolean;
begin
isEmpty:= head=nil;
end;
procedure List<TValue>.push(val: TValue);
var item:ListItem<TValue>;
begin
item := new ListItem<TValue>(val);
Inc(leng);
if isEmpty then begin
item.prev:=nil;
head:=item;
tail:=item;
end
else begin
tail.next:=item;
item.prev:=tail;
tail:=item;
end;
end;
function List<TValue>.getBegin : Iterator<TValue>;
begin
getBegin := new Iterator<TValue>(head);
end;
function List<TValue>.getEnd : Iterator<TValue>;
begin
getEnd := new Iterator<TValue>(tail);
end;
function List<TValue>.get(ind: integer) : TValue;
begin
get:=getIterator(ind).getNext;
end;
function List<TValue>.getFromEnd(ind: integer) : TValue;
var iterator:integer;
point:ListItem<TValue>;
begin
assert(not isEmpty, 'List is emty');
assert( ((ind>0) and (ind<=length)), 'The wrong index of list item. Index "'+inttostr(ind)+'" is outside the boundaries of the array');
point:=tail;
for iterator:=2 to ind do
point:=point.next;
getFromEnd:=point.value;
end;
function List<TValue>.getFirst : TValue;
begin
getFirst:=getBegin.takeNext;
end;
function List<TValue>.getLast : TValue;
begin
getLast:=getEnd.takeNext;
end;
function List<TValue>.getIterator(ind: integer) : Iterator<TValue>;
var iter:Iterator<TValue>;
i:word;
begin
assert( ((ind>0) and (ind<=length)), 'The wrong index of list item. Index "'+inttostr(ind)+'" is outside the boundaries of the array');
iter:=new Iterator<TValue>(head);
for i:=2 to ind do begin
iter.moveNext;
end;
getIterator:=iter;
end;
procedure List<TValue>.change(iter:Iterator<TValue>;val:TValue);
begin
assert(iter.hasNext, 'Point to item of list is nil!');
iter.next.value:=val;
end;
procedure List<TValue>.remove(it:Iterator<TValue>);
var point:ListItem<TValue>;
begin
point:=it.next;
Inc(leng,-1);
if point=head then
head:=head.next
else
point.prev.next:=point.next;
if point=tail then
tail:=tail.prev
else
point.next.prev:=point.prev;
point.prev:=nil;
point.next:=nil;
end;
function List<Tvalue>.find(condition:Predicate<TValue>;from:Iterator<TValue>):Iterator<TValue>;
begin
if (isEmpty) or (not from.hasNext) then find:=new Iterator<TValue>
else if (not isEmpty) and (from.hasNext) then begin
while from.hasNext do
if condition.check(from.getNext) then break
else from.moveNext;
find:=from;
end;
end;
function list<Tvalue>.find(condition:Predicate<TValue>):Iterator<TValue>;
begin
find:=find(condition,getBegin);
end;
function List<Tvalue>.find(val:TValue; from:Iterator<TValue>):Iterator<TValue>;
begin
if (isEmpty) or (not from.hasNext) then find:=new Iterator<TValue>
else if (not isEmpty) and (from.hasNext) then begin
while from.next<>nil do
if val=from.getNext then break
else from.moveNext;
find:=from;
end;
end;
function List<Tvalue>.find(val:TValue):Iterator<TValue>;
begin
find:=find(val,getBegin);
end;
procedure List<TValue>.removeAll(val:TValue);
var finditer,iter:iterator<TValue>;
begin
finditer:=find(val);
while finditer.hasNext do begin
iter:=find(val,finditer.nextIter);
remove(finditer);
finditer:=iter;
end;
end;
procedure List<TValue>.removeAll(condition:Predicate<TValue>);
var finditer,iter:iterator<TValue>;
begin
finditer:=find(condition);
while finditer.hasNext do begin
iter:=find(condition,finditer.nextIter);
remove(finditer);
finditer:=iter;
end;
end;
procedure List<TValue>.print;
var it:Iterator<TValue>;
begin
it:=getBegin;
while it.hasNext do
write(it.takeNext,' ');
writeln;
end;
begin
end.