-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.ltl
More file actions
118 lines (97 loc) · 2.77 KB
/
queue.ltl
File metadata and controls
118 lines (97 loc) · 2.77 KB
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
// ===================================================================
// Lateralus stdlib — queue
// Double-ended queue (deque) with O(1) amortised push/pop
// ===================================================================
import math
// -- Queue construction ----------------------------------------------
/// Create a new empty queue.
pub fn new() -> list {
// Internal repr: [items…] — front at index 0
return []
}
/// Create a queue pre-populated with `items`.
pub fn from_list(items: list) -> list {
let q = []
for item in items {
q = q + [item]
}
return q
}
// -- Core operations -------------------------------------------------
/// Push an element to the back of the queue.
pub fn push_back(q: list, item) -> list {
return q + [item]
}
/// Push an element to the front of the queue.
pub fn push_front(q: list, item) -> list {
return [item] + q
}
/// Remove and return the front element. Returns [value, new_queue].
pub fn pop_front(q: list) -> list {
if len(q) == 0 {
return [nil, q]
}
let front = q[0]
let rest = slice(q, 1, len(q))
return [front, rest]
}
/// Remove and return the back element. Returns [value, new_queue].
pub fn pop_back(q: list) -> list {
if len(q) == 0 {
return [nil, q]
}
let back = q[len(q) - 1]
let rest = slice(q, 0, len(q) - 1)
return [back, rest]
}
// -- Inspection ------------------------------------------------------
/// Peek at the front element without removing.
pub fn front(q: list) {
if len(q) == 0 {
return nil
}
return q[0]
}
/// Peek at the back element without removing.
pub fn back(q: list) {
if len(q) == 0 {
return nil
}
return q[len(q) - 1]
}
/// Return the number of elements.
pub fn size(q: list) -> int {
return len(q)
}
/// Check whether queue is empty.
pub fn is_empty(q: list) -> bool {
return len(q) == 0
}
// -- Bulk operations -------------------------------------------------
/// Reverse the queue.
pub fn reversed(q: list) -> list {
return reverse(q)
}
/// Return a new queue containing only elements matching the predicate.
pub fn filter_queue(q: list, pred) -> list {
return filter(q, pred)
}
/// Apply a function to every element, return new queue.
pub fn map_queue(q: list, func) -> list {
return map(q, func)
}
/// Drain up to `n` elements from the front. Returns [drained, remaining].
pub fn drain(q: list, n: int) -> list {
let take = min(n, len(q))
let drained = slice(q, 0, take)
let remaining = slice(q, take, len(q))
return [drained, remaining]
}
/// Concatenate two queues.
pub fn concat(a: list, b: list) -> list {
return a + b
}
/// Convert queue to a printable string.
pub fn to_string(q: list) -> str {
return "Queue(" + str(q) + ")"
}