forked from wmww/Pinecone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.pn
110 lines (83 loc) · 1.61 KB
/
queue.pn
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
# this is an implementation of a queue using a dynamically sized circular buffer
# this was wretten for Pinecone v0.3, which does not yet have references, so a single global queue is used instead of a queue object
q: Queue
main
main :: {}:
(
enqueue: 1
enqueue: 2
enqueue: 3
enqueue: 4
enqueue: 5
enqueue: 6
print: dequeue
print: dequeue
print: dequeue
print: dequeue
enqueue: 7
enqueue: 8
enqueue: 9
print: dequeue
print: dequeue
print: q
)
Queue :: {array: IntArray, offset: Int, len: Int}
Queue :: {} -> {Queue}: (
(IntArray: 1), 0, 0
)
resize :: {Int}: (
newArray: IntArray: in
i: 0 | i<q.len | i: i+1 @ (
tmp: q.array
newArray.set: i, (tmp.get: (i+q.offset)%q.array.len)
)
q: newArray, 0, q.len
)
enqueue :: {Int}: (
q.len >= q.array.len ?
resize: q.array.len*2
q: q.array, q.offset, q.len+1
tmp: q.array
tmp.set: (q.offset+q.len-1)%q.array.len, in
)
dequeue :: {} -> {Int}: (
tmp: q.array
out: tmp.get: q.offset%q.array.len
q: q.array, q.offset+1, q.len-1
q.len < q.array.len/3+1 ?
resize: q.array.len/3+1
out
)
peek :: {}: (
tmp: q.array
tmp.get: q.offset%q.array.len
)
print :: {Queue}: (
print: "queue {"
i: 0 | i<in.len | i: i+1 @ (
#Bool: i ?
# print: ", "
ary: in.array
print: " "+(ary.get: (i+in.offset)%in.array.len).String
)
print: "}"
print
//
print: "raw array {"
i: 0 | i<in.array.len | i: i+1 @ (
#Bool: i ?
# print: ", "
ary: in.array
out: (ary.get: i).String
i = in.offset ?
out: " -> "+out
| i = (in.offset+in.len-1)%in.array.len ?
out: " -| "+out
|
out: " "+out
print: out
)
print: "}"
print
\\
)