|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | 3 |
|
6 | | -namespace Advanced.Algorithms.DataStructures.Queues |
| 4 | +/// <summary> |
| 5 | +/// Cicular queue aka Ring Buffer using fixed size array |
| 6 | +/// </summary> |
| 7 | +/// <typeparam name="T"></typeparam> |
| 8 | +public class CircularQueue<T> |
7 | 9 | { |
| 10 | + private T[] queue; |
| 11 | + |
| 12 | + //points to the index of next element to be deleted |
| 13 | + private int start = 0; |
| 14 | + |
| 15 | + //points to the index new element should be inserted |
| 16 | + private int end = 0; |
| 17 | + |
| 18 | + public int Count { get; private set; } |
| 19 | + |
| 20 | + public CircularQueue(int size) |
| 21 | + { |
| 22 | + queue = new T[size]; |
| 23 | + } |
| 24 | + |
8 | 25 | /// <summary> |
9 | | - /// Cicular queue aka Ring Buffer using fixed size array |
| 26 | + /// Note: When buffer overflows oldest data will be erased |
| 27 | + /// O(1) time complexity |
10 | 28 | /// </summary> |
11 | | - /// <typeparam name="T"></typeparam> |
12 | | - public class CircularQueue<T> |
| 29 | + /// <param name="data"></param> |
| 30 | + public T Enqueue(T data) |
13 | 31 | { |
14 | | - private T[] queue; |
15 | | - |
16 | | - //points to the index of next element to be deleted |
17 | | - private int start = 0; |
| 32 | + T deleted = default(T); |
18 | 33 |
|
19 | | - //points to the index new element should be inserted |
20 | | - private int end = 0; |
| 34 | + //wrap around removing oldest element |
| 35 | + if (end > queue.Length - 1) |
| 36 | + { |
| 37 | + end = 0; |
21 | 38 |
|
22 | | - public int Count { get; private set; } |
| 39 | + if (start == 0) |
| 40 | + { |
| 41 | + deleted = queue[start]; |
| 42 | + start++; |
| 43 | + } |
| 44 | + } |
23 | 45 |
|
24 | | - public CircularQueue(int size) |
| 46 | + //when end meets start after wraping around |
| 47 | + if (end == start && Count > 1) |
25 | 48 | { |
26 | | - queue = new T[size]; |
| 49 | + deleted = queue[start]; |
| 50 | + start++; |
27 | 51 | } |
28 | 52 |
|
29 | | - /// <summary> |
30 | | - /// Note: When buffer overflows oldest data will be erased |
31 | | - /// </summary> |
32 | | - /// <param name="data"></param> |
33 | | - public void Enqueue(T data) |
| 53 | + queue[end] = data; |
| 54 | + end++; |
| 55 | + |
| 56 | + if (Count < queue.Length) |
34 | 57 | { |
35 | | - //wrap around removing oldest element |
36 | | - if (end > queue.Length - 1) |
37 | | - { |
38 | | - end = 0; |
| 58 | + Count++; |
| 59 | + } |
39 | 60 |
|
40 | | - if(start == 0) |
41 | | - { |
42 | | - start++; |
43 | | - } |
44 | | - } |
| 61 | + return deleted; |
| 62 | + } |
45 | 63 |
|
46 | | - //when end meets start after wraping around |
47 | | - if (end == start && Count > 1) |
48 | | - { |
49 | | - start++; |
50 | | - } |
| 64 | + /// <summary> |
| 65 | + /// O(bulk.Length) time complexity |
| 66 | + /// </summary> |
| 67 | + /// <param name="bulk"></param> |
| 68 | + /// <returns></returns> |
| 69 | + public IEnumerable<T> Enqueue(T[] bulk) |
| 70 | + { |
| 71 | + var deletedList = new List<T>(); |
51 | 72 |
|
52 | | - queue[end] = data; |
53 | | - end++; |
| 73 | + foreach (var item in bulk) |
| 74 | + { |
| 75 | + var deleted = Enqueue(item); |
54 | 76 |
|
55 | | - if (Count < queue.Length) |
| 77 | + if (!deleted.Equals(default(T))) |
56 | 78 | { |
57 | | - Count++; |
| 79 | + deletedList.Add(deleted); |
58 | 80 | } |
59 | 81 | } |
60 | 82 |
|
| 83 | + return deletedList; |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// O(1) time complexity |
| 88 | + /// </summary> |
| 89 | + /// <returns></returns> |
| 90 | + public T Dequeue() |
| 91 | + { |
| 92 | + if (Count == 0) |
| 93 | + { |
| 94 | + throw new Exception("Empty queue."); |
| 95 | + } |
| 96 | + |
| 97 | + var element = queue[start]; |
| 98 | + start++; |
61 | 99 |
|
62 | | - public T Dequeue() |
| 100 | + //wrap around |
| 101 | + if (start > queue.Length - 1) |
63 | 102 | { |
64 | | - if (Count == 0) |
| 103 | + start = 0; |
| 104 | + |
| 105 | + if (end == 0) |
65 | 106 | { |
66 | | - throw new Exception("Empty queue."); |
| 107 | + end++; |
67 | 108 | } |
| 109 | + } |
68 | 110 |
|
69 | | - var element = queue[start]; |
70 | | - start++; |
| 111 | + Count--; |
71 | 112 |
|
72 | | - //wrap around |
73 | | - if (start > queue.Length - 1) |
74 | | - { |
75 | | - start = 0; |
| 113 | + if (start == end && Count > 1) |
| 114 | + { |
| 115 | + end++; |
| 116 | + } |
76 | 117 |
|
77 | | - if (end == 0) |
78 | | - { |
79 | | - end++; |
80 | | - } |
81 | | - } |
| 118 | + //reset |
| 119 | + if (Count == 0) |
| 120 | + { |
| 121 | + start = end = 0; |
| 122 | + } |
82 | 123 |
|
83 | | - Count--; |
| 124 | + return element; |
| 125 | + } |
84 | 126 |
|
85 | | - if (start == end && Count > 1) |
86 | | - { |
87 | | - end++; |
88 | | - } |
| 127 | + /// <summary> |
| 128 | + /// O(bulkNumber) time complexity |
| 129 | + /// </summary> |
| 130 | + /// <param name="bulkNumber"></param> |
| 131 | + public IEnumerable<T> Dequeue(int bulkNumber) |
| 132 | + { |
| 133 | + var deletedList = new List<T>(); |
| 134 | + while (bulkNumber > 0 && Count > 0) |
| 135 | + { |
| 136 | + var deleted = Dequeue(); |
89 | 137 |
|
90 | | - //reset |
91 | | - if (Count == 0) |
| 138 | + if (!deleted.Equals(default(T))) |
92 | 139 | { |
93 | | - start = end = 0; |
| 140 | + deletedList.Add(deleted); |
94 | 141 | } |
95 | | - |
96 | | - return element; |
97 | 142 | } |
| 143 | + |
| 144 | + return deletedList; |
98 | 145 | } |
| 146 | + |
99 | 147 | } |
| 148 | + |
0 commit comments