-
Notifications
You must be signed in to change notification settings - Fork 14
/
deque.hh
85 lines (79 loc) · 1 KB
/
deque.hh
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
/*
Double-ended queue
Copyright 2020 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
namespace DSP {
template <typename TYPE, int SIZE>
class Deque
{
TYPE buf[SIZE];
int head, tail, count;
public:
Deque() : head(SIZE-1), tail(0), count(0)
{
}
void clear()
{
head = SIZE-1;
tail = 0;
count = 0;
}
void push_back(TYPE input)
{
assert(count < SIZE);
++count;
if (--tail < 0)
tail = SIZE-1;
buf[tail] = input;
}
void push_front(TYPE input)
{
assert(count < SIZE);
++count;
if (++head >= SIZE)
head = 0;
buf[head] = input;
}
void pop_back()
{
assert(count > 0);
--count;
if (++tail >= SIZE)
tail = 0;
}
void pop_front()
{
assert(count > 0);
--count;
if (--head < 0)
head = SIZE-1;
}
TYPE back()
{
assert(count > 0);
return buf[tail];
}
TYPE front()
{
assert(count > 0);
return buf[head];
}
bool empty()
{
return count == 0;
}
bool full()
{
return count == SIZE;
}
int size()
{
return count;
}
int max_size()
{
return SIZE;
}
};
}