-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.easy
250 lines (212 loc) · 7.49 KB
/
linkedList.easy
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
# Copyright (C) 2021 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" useFile
ListInternal: {
INTERNAL: {};
makeNode: [{
LIST_NODE: {};
value:;
prev:();
next:();
}] func;
makeRootNode: [
result: () makeNode;
result result.!prev
result result.!next
result
] func;
insertBetween: [
node1:node2:newNode:;;;
node1 newNode.!prev
node2 newNode.!next
newNode node2.!prev
newNode node1.!next
] func;
eraseNode: [
node:;
node.next node.prev.!next
node.prev node.next.!prev
() node.!next
() node.!prev
] func;
insertBefore: [
newNode: node:;;
node.prev node newNode insertBetween
] func;
insertAfter: [
newNode: node:;;
node node.next newNode insertBetween
] func;
makeIterator: [{
ITERATOR: {};
LIST_ITERATOR: {};
node:;
get: [node.@value] func;
set: [node.!value] func;
next: [node.next ListInternal.makeIterator] func;
prev: [node.prev ListInternal.makeIterator] func;
erase: [node ListInternal.eraseNode () !node] func;
insertBefore: [ListInternal.makeNode node ListInternal.insertBefore] func;
insertAfter: [ListInternal.makeNode node ListInternal.insertAfter] func;
}] func;
makeRange: [{
RANGE: {};
LIST_RANGE: {};
firstIter:afterLastIter:;;
first: [firstIter.get] func;
last: [afterLastIter.prev.get] func;
lastIter: [afterLastIter.prev] func;
endIter: [afterLastIter] func;
empty: [firstIter () same] func;
single: [empty [FALSE] [firstIter.node.next afterLastIter.node is] if] func;
unheadIter: [empty ~] [firstIter firstIter.next afterLastIter ListInternal.makeRange] pfunc;
unheadIter: [single] [firstIter () () ListInternal.makeRange] pfunc;
untailIter: [empty ~] [firstIter afterLastIter.prev ListInternal.makeRange afterLastIter.prev ] pfunc;
untailIter: [single] [() () ListInternal.makeRange afterLastIter.prev] pfunc;
unhead: [empty ~] [firstIter.get firstIter.next afterLastIter ListInternal.makeRange] pfunc;
unhead: [single] [firstIter.get () () ListInternal.makeRange] pfunc;
untail: [empty ~] [firstIter afterLastIter.prev ListInternal.makeRange afterLastIter.prev.get] pfunc;
untail: [single] [() () ListInternal.makeRange afterLastIter.prev.get] pfunc;
makeRange: [firstIter afterLastIter ListInternal.makeRange] func;
merge: [
range1:range2:;;
beforeFirst: range1.firstIter.prev;
firstRangeLast: range1.afterLastIter.prev; # opened range quirk
afterLast: range2.afterLastIter;
[
range1.empty range2.empty or [
FALSE
] [
headIter1:range1': range1.unheadIter;;
headIter1.get range2.first < [
range1.firstIter firstRangeLast = [
() () ListInternal.makeRange !range1 # opened range quirk
] [
range1' !range1
] if
] [
headIter2:range2': range2.unheadIter;;
headIter2.node ListInternal.eraseNode
headIter2.node headIter1.node ListInternal.insertBefore
range2' !range2
] if
TRUE
] if
] loop
beforeFirst.next afterLast ListInternal.makeRange
] func;
sort: [
subLast:firstIter;
subFirst:afterLastIter.prev;
condition: [subLast subFirst =] func;
condition ~ [
[
subLast.next !subLast
condition [FALSE] [subFirst.prev !subFirst condition ~] if
] loop
] when
result1: firstIter subLast ListInternal.makeRange.sort;
result1Last: result1.afterLastIter.prev;
result2: subFirst afterLastIter ListInternal.makeRange.sort; # here we corrupted result1.afterLast
newRange:
result1.firstIter result1Last.next ListInternal.makeRange
result2
merge;
newRange.firstIter !firstIter
newRange.afterLastIter !afterLastIter
newRange
] func;
sort: [single] [firstIter afterLastIter ListInternal.makeRange] pfunc;
sort: [empty] [() () ListInternal.makeRange] pfunc;
}] func;
listEnumIter: [enumIterObject:enumIterRangeBody:;; enumIterObject.unheadIter swap @enumIterRangeBody call [@enumIterRangeBody listEnumIter] [drop] if] func;
listEnumIter: [drop .single] [enumIterRangeBody:; .firstIter @enumIterRangeBody call drop] pfunc;
listEnumIter: [drop .empty] [drop drop] pfunc;
listEnum: [enumRangeBody:; [.get @enumRangeBody call] listEnumIter] func;
};
List: [{
CONTAINER: {};
LIST: {};
root: ListInternal.makeRootNode;
empty: [root root.next is] func;
single: [empty [FALSE] [root.next root.prev is] if] func;
pushFront: [ListInternal.makeNode root ListInternal.insertAfter] func;
pushBack: [ListInternal.makeNode root ListInternal.insertBefore] func;
popFront: [empty ~] [root.next ListInternal.eraseNode] pfunc;
popBack: [empty ~] [root.prev ListInternal.eraseNode] pfunc;
firstIter: [root.next ListInternal.makeIterator] func;
lastIter: [root.prev ListInternal.makeIterator] func;
first: [root.next.@value] func;
last: [root.prev.@value] func;
endIter: [root ListInternal.makeIterator] func;
makeRange: [firstIter endIter ListInternal.makeRange] func;
makeRange: [empty] [() () ListInternal.makeRange] pfunc;
clear: [root.next ListInternal.eraseNode clear] func;
clear: [empty] [] pfunc;
eraseIter: [dup 1 fieldName "LIST_ITERATOR" = swap endIter = ~ and] [
.erase
] pfunc;
DIE: [
clear
() root.!prev
() root.!next
];
}] func;
enumIter: [drop 1 fieldName "LIST_RANGE" =] [ListInternal.listEnumIter] pfunc;
enum: [drop 1 fieldName "LIST_RANGE" =] [ListInternal.listEnum] pfunc;
enumIter: [drop 1 fieldName "LIST" =] [enumBody:; .makeRange @enumBody ListInternal.listEnumIter] pfunc;
enum: [drop 1 fieldName "LIST" =] [enumBody:; .makeRange @enumBody ListInternal.listEnum ] pfunc;
=: [1 fieldName "LIST_ITERATOR" =] [
.node swap .node is
] pfunc;
copy: [1 fieldName "LIST" =] [
src:;
dst: List;
src [copy dst.pushBack TRUE] enum
dst
] pfunc;
makeList: [
value:count:;;
result: List;
[count 0 >] [
@value copy result.pushBack
count 1 - !count
] while
result
] func;
makeListByRange: [
.untail swap # t r'
result: makeListByRange; # t a
result.pushBack
result
] func;
makeListByRange: [.empty] [
drop
List
] pfunc;
makeList: [0 fieldName "RANGE" =] [
makeListByRange
] pfunc;
makeList: [isPureList] [
list:;
result: List;
i: 0;
[i list fieldCount <] [
i list @ result.pushBack
i 1 + !i
] while
result
] pfunc;
@: [1 fieldName "LIST_ITERATOR" =] [0 .NOT_ALLOWED_FOR_LIST_ITERATOR] pfunc;
!: [1 fieldName "LIST_ITERATOR" =] [0 .NOT_ALLOWED_FOR_LIST_ITERATOR] pfunc;
fieldCount: [1 fieldName "LIST_ITERATOR" =] [0 .NOT_ALLOWED_FOR_LIST_ITERATOR] pfunc;
@: [1 fieldName "LIST_RANGE" =] [0 .NOT_ALLOWED_FOR_LIST_RANGE] pfunc;
!: [1 fieldName "LIST_RANGE" =] [0 .NOT_ALLOWED_FOR_LIST_RANGE] pfunc;
fieldCount: [1 fieldName "LIST_RANGE" =] [0 .NOT_ALLOWED_FOR_LIST_RANGE] pfunc;
@: [1 fieldName "LIST" =] [0 .NOT_ALLOWED_FOR_LIST] pfunc;
!: [1 fieldName "LIST" =] [0 .NOT_ALLOWED_FOR_LIST] pfunc;
fieldCount: [1 fieldName "LIST" =] [0 .NOT_ALLOWED_FOR_LIST] pfunc;