-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue.d
64 lines (53 loc) · 1.29 KB
/
queue.d
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
// Written in the D programming language
/**
A simple queue module. It's from an old codebase and will be transformed, maybe fused with stack.
License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
Authors: Philippe Sigaud
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
module dranges.queue;
///
struct Queue(T) {
T[] queue;
size_t begin, end;
///
this(int initialLength = 1000) {
queue.length = initialLength;
begin = 0;
end = 0;
}
///
void push(T value) {
if (end == queue.length) {
queue.length = queue.length*2;
}
queue[end] = value;
end++;
}
///
T top() {
return queue[begin];
}
///
T pop() {
T pop = queue[begin];
begin++;
if (begin>(queue.length/2)) { // Half-empty -> put the values at the beginning.
int l = queue.length;
queue = queue[begin..$].dup;
queue.length = l;
end -= begin;
begin = 0;
}
return pop;
}
///
bool empty() {
return (begin == end);
}
///
size_t length() {
return end-begin;
}
}