generated from eigerco/beerus
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathqueue.cairo
53 lines (43 loc) · 1.2 KB
/
queue.cairo
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
const ZERO_USIZE: usize = 0;
pub struct Queue<T> {
elements: Array<T>,
}
pub trait QueueTrait<T> {
fn new() -> Queue<T>;
fn enqueue(ref self: Queue<T>, value: T);
fn dequeue(ref self: Queue<T>) -> Option<T>;
fn peek_front(self: @Queue<T>) -> Option<Box<@T>>;
fn len(self: @Queue<T>) -> usize;
fn is_empty(self: @Queue<T>) -> bool;
}
impl QueueImpl<T> of QueueTrait<T> {
#[inline(always)]
fn new() -> Queue<T> {
queue_new()
}
fn enqueue(ref self: Queue<T>, value: T) {
let mut elements = self.elements;
elements.append(value);
self = Queue { elements }
}
fn dequeue(ref self: Queue<T>) -> Option<T> {
let mut elements = self.elements;
let first = elements.pop_front();
self = Queue { elements };
first
}
fn peek_front(self: @Queue<T>) -> Option<Box<@T>> {
self.elements.get(ZERO_USIZE)
}
fn len(self: @Queue<T>) -> usize {
self.elements.len()
}
fn is_empty(self: @Queue<T>) -> bool {
self.len() == ZERO_USIZE
}
}
fn queue_new<T>() -> Queue<T> {
let mut arr = array![];
Queue { elements: arr }
}
impl Queuefelt252Drop of Drop<Queue<felt252>>;