-
Notifications
You must be signed in to change notification settings - Fork 292
/
CircularQueue.cs
116 lines (91 loc) · 2.44 KB
/
CircularQueue.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.Distributed;
/// <summary>
/// Cicular queue aka Ring Buffer using fixed size array.
/// </summary>
public class CircularQueue<T>
{
//points to the index new element should be inserted
private int end;
private readonly T[] queue;
//points to the index of next element to be deleted
private int start;
public CircularQueue(int size)
{
queue = new T[size];
}
public int Count { get; private set; }
/// <summary>
/// Note: When buffer overflows oldest data will be erased.
/// Time complexity: O(1)
/// </summary>
public T Enqueue(T data)
{
var deleted = default(T);
//wrap around removing oldest element
if (end > queue.Length - 1)
{
end = 0;
if (start == 0)
{
deleted = queue[start];
start++;
}
}
//when end meets start after wraping around
if (end == start && Count > 1)
{
deleted = queue[start];
start++;
}
queue[end] = data;
end++;
if (Count < queue.Length) Count++;
return deleted;
}
/// <summary>
/// Time complexity: O(n).
/// </summary>
/// <returns>Deleted items.</returns>
public IEnumerable<T> Enqueue(T[] bulk)
{
return bulk.Select(item => Enqueue(item))
.Where(deleted => !deleted.Equals(default(T))).ToList();
}
/// <summary>
/// O(1) Time complexity.
/// </summary>
public T Dequeue()
{
if (Count == 0) throw new Exception("Empty queue.");
var element = queue[start];
start++;
//wrap around
if (start > queue.Length - 1)
{
start = 0;
if (end == 0) end++;
}
Count--;
if (start == end && Count > 1) end++;
//reset
if (Count == 0) start = end = 0;
return element;
}
/// <summary>
/// Time complexity: O(n).
/// </summary>
public IEnumerable<T> Dequeue(int bulkNumber)
{
var deletedList = new List<T>();
while (bulkNumber > 0 && Count > 0)
{
var deleted = Dequeue();
if (!deleted.Equals(default(T))) deletedList.Add(deleted);
bulkNumber--;
}
return deletedList;
}
}