forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.mpl
executable file
·94 lines (75 loc) · 1.53 KB
/
Deque.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
"Deque" module
"control" includeModule
"Array" includeModule
Deque: [{
# front: head tail :back
virtual CONTAINER: ();
virtual DEQUE: ();
schema elementType:;
head: @elementType Array;
tail: @elementType Array;
getSize: [head.getSize tail.getSize +];
pushBack: [@tail.pushBack];
pushFront: [@head.pushBack];
popBack: [
tail fieldCount 0 = [@head @tail swapBuffers] when
@tail.popBack
];
popFront: [
head fieldCount 0 = [@tail @head swapBuffers] when
@head.popBack
];
back: [
tail fieldCount 0 = [
0 @head.at
] [
@tail.last
] if
];
front: [
head fieldCount 0 = [
0 @tail.at
] [
@head.last
] if
];
at: [
copy index:;
index head.getSize < [
head.getSize index - 1 - @head.at
] [
index head.getSize - @tail.at
] if
];
swapBuffers: [
from:to:;;
swapCount: from.getSize 1 + 2 /;
[to.getSize 0 =] "Destination must be empty!" assert
swapCount @to.resize
swapCount [
i @from.at move
swapCount 1 - i - @to.at set
] times
from.getSize swapCount - [
i swapCount + @from.at move
i @from.at set
] times
from.getSize swapCount - @from.shrink
];
clear: [
@head.clear
@tail.clear
];
release: [
@head.release
@tail.release
];
}];
@: ["DEQUE" has] [.at] pfunc;
!: ["DEQUE" has] [.at set] pfunc;
fieldCount: ["DEQUE" has] [.getSize] pfunc;
each: [drop "DEQUE" has] [
deque:body:;;
@deque.@head @body each
@deque.@tail @body each
] pfunc;