-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwardList.easy
165 lines (134 loc) · 4.81 KB
/
forwardList.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
# 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
ForwardListInternal: {
INTERNAL: {};
makeNodeWithNext: [{
FORWARD_LIST_NODE: {};
value:next:;;
}] func;
makeNode: [() makeNodeWithNext] func;
insertBetween: [
node1:node2:newNode:;;;
node2 newNode.!next
newNode node1.!next
] func;
eraseNodeAfter: [
node:;
node.next.next node.!next
() node.next.!next
] func;
insertAfter: [
newNode: node:;;
node node.next newNode insertBetween
] func;
makeIterator: [{
ITERATOR: {};
FORWARD_LIST_ITERATOR: {};
node:;
get: [node.@value] func;
set: [node.!value] func;
next: [node.next ForwardListInternal.makeIterator] func;
eraseAfter: [node ForwardListInternal.eraseNodeAfter () !node] func;
insertAfter: [ForwardListInternal.makeNode node ForwardListInternal.insertAfter] func;
isLast: [node.next () same] func;
}] func;
makeRange: [{
RANGE: {};
FORWARD_LIST_RANGE: {};
firstIter:;
first: [firstIter.get] func;
empty: [firstIter.node () same] func;
single: [empty [FALSE] [firstIter.node.next () same] if] func;
unheadIter: [empty ~] [firstIter firstIter.next ForwardListInternal.makeRange] pfunc;
unhead: [empty ~] [firstIter.get firstIter.next ForwardListInternal.makeRange] pfunc;
makeRange: [firstIter ForwardListInternal.makeRange] func;
}] func;
flEnumIter: [enumIterRange:enumIterRangeBody:;; enumIterRange.unheadIter swap @enumIterRangeBody call [@enumIterRangeBody enumIter] [drop] if] func;
flEnumIter: [drop .single] [enumIterRangeBody:; .firstIter enumIterRangeBody call drop] pfunc;
flEnumIter: [drop .empty] [drop drop] pfunc;
flEnum: [enumRangeBody:; [.get @enumRangeBody call] enumIter] func;
};
ForwardList: [{
CONTAINER: {};
FORWARD_LIST: {};
root: ();
empty: [() root same] func;
single: [empty [FALSE] [root.next () same] if] func;
pushFront: [root ForwardListInternal.makeNodeWithNext !root] func;
popFront: [empty ~] [
current: root;
root.next !root
() current.!next
] pfunc;
first: [empty ~] [root.@value] pfunc;
firstIter: [root ForwardListInternal.makeIterator] func;
makeRange: [firstIter ForwardListInternal.makeRange] func;
clear: [popFront clear] func;
clear: [empty] [] pfunc;
insertAfterIter: [1 fieldName "FORWARD_LIST_ITERATOR" =] [
.insertAfter
] pfunc;
eraseAfterIter: [1 fieldName "FORWARD_LIST_ITERATOR" =] [
.eraseAfter
] pfunc;
DIE: [
clear
];
}] func;
copy: [1 fieldName "FORWARD_LIST" =] [
t:;
t.makeRange makeForwardList
] pfunc;
makeForwardList: [
value:count:;;
result: ForwardList;
[count 0 >] [
@value copy result.pushFront
count 1 - !count
] while
result
] func;
makeForwardListByRange: [
.unhead # h r'
result: makeForwardListByRange; # h l
result.pushFront
result
] func;
makeForwardListByRange: [.empty] [
drop
ForwardList
] pfunc;
makeForwardList: [0 fieldName "RANGE" =] [
makeForwardListByRange
] pfunc;
makeForwardList: [isPureList] [
list:;
result: ForwardList;
i: list fieldCount 1 -;
[i 0 < ~] [
i list @ result.pushFront
i 1 - !i
] while
result
] pfunc;
=: [1 fieldName "FORWARD_LIST_ITERATOR" =] [
.node swap .node is
] pfunc;
enumIter: [drop 1 fieldName "FORWARD_LIST_RANGE" =] [ForwardListInternal.flEnumIter] pfunc;
enum: [drop 1 fieldName "FORWARD_LIST_RANGE" =] [ForwardListInternal.flEnum] pfunc;
enumIter: [drop 1 fieldName "FORWARD_LIST" =] [enumBody:; .makeRange @enumBody ForwardListInternal.flEnumIter] pfunc;
enum: [drop 1 fieldName "FORWARD_LIST" =] [enumBody:; .makeRange @enumBody ForwardListInternal.flEnum] pfunc;
@: [1 fieldName "FORWARD_LIST_ITERATOR" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST_ITERATOR] pfunc;
!: [1 fieldName "FORWARD_LIST_ITERATOR" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST_ITERATOR] pfunc;
fieldCount: [1 fieldName "FORWARD_LIST_ITERATOR" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST_ITERATOR] pfunc;
@: [1 fieldName "FORWARD_LIST_RANGE" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST_RANGE] pfunc;
!: [1 fieldName "FORWARD_LIST_RANGE" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST_RANGE] pfunc;
fieldCount: [1 fieldName "FORWARD_LIST_RANGE" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST_RANGE] pfunc;
@: [1 fieldName "FORWARD_LIST" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST] pfunc;
!: [1 fieldName "FORWARD_LIST" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST] pfunc;
fieldCount: [1 fieldName "FORWARD_LIST" =] [0 .NOT_ALLOWED_FOR_FORWARD_LIST] pfunc;