forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntrusiveQueue.mpl
224 lines (197 loc) · 4.72 KB
/
IntrusiveQueue.mpl
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
# Copyright (C) Matway Burkow
#
# This repository and all its contents belong to Matway Burkow (referred here and below as "the owner").
# The content is for demonstration purposes only.
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.
"control.Ref" use
"control.assert" use
"control.dup" use
"control.ensure" use
"control.nil?" use
"control.when" use
"control.while" use
# Intrusive singly linked list
# Requires item objects to have field 'next' of type '[Item] Mref'
IntrusiveQueue: [
Item:;
{
SCHEMA_NAME: "IntrusiveQueue<" @Item schemaName & ">" & virtual;
INIT: [clear];
DIE: [];
empty?: [@first nil?];
append: [
item:;
@Item @[email protected]
@last nil? [
[@first nil?] "invalid linked list state" assert
@item !first
] [
[@last.next nil?] "invalid linked list state" assert
@item @[email protected]
] if
@item !last
];
clear: [
@Item !first
@Item !last
];
cutAllIf: [
body:;
count: 0;
empty? ~ [
item: @first;
skip: FALSE;
@item @body call [
[
count 1 + !count
prev: @item;
@item.next !item
@item nil? [
@Item !first
[@last @prev is] "invalid linked list state" assert
@Item !last
TRUE !skip
FALSE
] [
@item @body call dup ~ [
@item !first
] when
] if
] loop
] when
[skip ~] [
lastToKeep: @item;
[
@item.next !item
@item nil? [
TRUE !skip
FALSE
] [
@item @body call ~ dup [
@item !lastToKeep
] when
] if
] loop
skip ~ [
[
count 1 + !count
prev: @item;
@item.next !item
@item nil? [
@Item @[email protected]
[@last @prev is] "invalid linked list state" assert
@lastToKeep !last
TRUE !skip
FALSE
] [
@item @body call dup ~ [
@item @[email protected]
] when
] if
] loop
] when
] while
] when
count
];
cutFirst: [
[empty? ~] "queue is empty" assert
item: @first;
@item.next !first
@first nil? [
[@last @item is] "invalid linked list state" assert
@Item !last
] when
];
cutIf: [
body:;
count: 0;
empty? ~ [
item: @first;
@item @body call [
1 !count
cutFirst
] [
[
prev: @item;
@item.next !item
@item nil? [FALSE] [
@item @body call ~ dup ~ [
1 !count
next: @item.next;
@next @[email protected]
@next nil? [
[@last @item is] "invalid linked list state" assert
@prev !last
] when
] when
] if
] loop
] if
] when
count
];
iter: [{
Item: virtual @Item Ref;
item: @first;
next: [
@item nil? [@Item FALSE] [
@item
@item.next !item
TRUE
] if
];
}];
popFirst: [
[empty? ~] "queue is empty" assert
@first
cutFirst
];
prepend: [
item:;
next: @first;
@next @[email protected]
@item !first
@next nil? [
[@last nil?] "invalid linked list state" assert
@item !last
] when
];
reverse: [
empty? ~ [
item: @first.next;
@item nil? ~ [
prev: @first;
@Item @[email protected]
[
next: @item.next;
@prev @[email protected]
@next nil? ~ dup [
@item !prev
@next !item
] when
] loop
firstItem: @first;
@item !first
[@last @item is] "invalid linked list state" assert
@firstItem !last
] when
] when
];
validate: [
DEBUG [
item: @Item;
next: @first;
[@next nil? ~] [
@next !item
@next.next !next
] while
[@last @item is] "invalid linked list state" ensure
] when
];
Item: @Item Ref virtual;
first: @Item;
last: @Item;
}
];