forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.mpl
114 lines (93 loc) · 2.16 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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.
"Array.Array" use
"control.Ref" use
"control.assert" use
"control.dup" use
"control.times" use
"control.when" use
Deque: [
Item:;
{
SCHEMA_NAME: "Deque<" @Item schemaName & ">" & virtual;
# front: head tail :back
virtual CONTAINER: ();
virtual DEQUE: ();
virtual elementType: @Item Ref;
head: @elementType Array;
tail: @elementType Array;
iter: [
{
deque: @self;
key: 0;
next: [
key deque.size = [@deque.@elementType FALSE] [
key @deque.at
key 1 + !key
TRUE
] if
];
}
];
size: [head.size tail.size +];
pushBack: [@tail.append];
pushFront: [@head.append];
popBack: [
tail.size 0 = [@head @tail swapBuffers] when
@tail.popBack
];
popFront: [
head.size 0 = [@tail @head swapBuffers] when
@head.popBack
];
back: [
tail.size 0 = [
0 @head.at
] [
@tail.last
] if
];
front: [
head.size 0 = [
0 @tail.at
] [
@head.last
] if
];
at: [
index:;
index head.size < [
head.size index - 1 - @head.at
] [
index head.size - @tail.at
] if
];
swapBuffers: [
from:to:;;
swapCount: from.size 1 + 2 /;
[to.size 0 =] "Destination must be empty!" assert
swapCount @to.resize
swapCount [
i @from.at
swapCount 1 - i - @to.at set
] times
from.size swapCount - [
i swapCount + @from.at
i @from.at set
] times
from.size swapCount - @from.shrink
];
clear: [
@head.clear
@tail.clear
];
release: [
@head.release
@tail.release
];
}
];