Huffman Zipper  v-1.0
Data Compression and Decompression using Greedy Huffman Algorithm
Queue.h
Go to the documentation of this file.
1 #pragma once
2 #include <vector>
3 #include <stdexcept>
4 
5 #include "Constants.h"
6 
13 template <typename ValueType>
14 class Queue {
15 private:
16  std::vector<ValueType> ringBuffer;
17  int front;
18  int rear;
20  int count;
21  int capacity;
23 private:
24 
30 
31 public:
33  Queue();
34 
36  virtual ~Queue() = default;
37 
39  void clear();
40 
42  int size() const;
43 
45  bool isEmpty() const;
46 
48  void enqueue(const ValueType& value);
49 
51  ValueType dequeue();
52 
54  ValueType peek() const;
55 
57  ValueType& getFront();
58 
60  ValueType& getBack();
61 };
62 
63 /************************************************************************/
64 /* implementation */
65 /************************************************************************/
66 
67 template <typename ValueType>
68 Queue<ValueType>::Queue() :front(0), rear(0), count(0), capacity(INITIAL_QUEUE_CAPACITY) {
69  ringBuffer = std::vector<ValueType>(capacity);
70 }
71 
72 template <typename ValueType>
74  return count;
75 }
76 
77 template <typename ValueType>
79  return count == 0;
80 }
81 
82 template <typename ValueType>
84  capacity = INITIAL_QUEUE_CAPACITY;
85  ringBuffer = std::vector<ValueType>(capacity);
86  front = 0;
87  rear = 0;
88  count = 0;
89 }
90 
91 template <typename ValueType>
92 void Queue<ValueType>::enqueue(const ValueType& value) {
93  if (count >= capacity - 1)
94  expandRingBufferCapacity();
95 
96  ringBuffer[rear] = value;
97  rear = (rear + 1) % capacity;
98  count++;
99 }
100 
101 template <typename ValueType>
103  if (isEmpty())
104  throw std::runtime_error("dequeue: Attempting to dequeue an empty queue");
105 
106  ValueType result = ringBuffer[front];
107  front = (front + 1) % capacity;
108  count--;
109  return result;
110 }
111 
112 template <typename ValueType>
113 ValueType Queue<ValueType>::peek() const {
114  if (isEmpty())
115  throw std::runtime_error("peek: Attempting to peek at an empty queue");
116  return ringBuffer.get(front);
117 }
118 
119 template <typename ValueType>
121  if (isEmpty())
122  throw std::runtime_error("front: Attempting to read front of an empty queue");
123  return ringBuffer[front];
124 }
125 
126 template <typename ValueType>
128  if (isEmpty())
129  throw std::runtime_error("back: Attempting to read back of an empty queue");
130  return ringBuffer[(rear + capacity - 1) % capacity];
131 }
132 
133 template <typename ValueType>
135  std::vector<ValueType> copyBuffer = ringBuffer;
136  ringBuffer = std::vector<ValueType>(2 * capacity);
137  for (int i = 0; i < count; i++) {
138  ringBuffer[i] = copyBuffer[(front + i) % capacity];
139  }
140  front = 0;
141  rear = count;
142  capacity *= 2;
143 }
This file exports the constants used in throughout the project.
const int INITIAL_QUEUE_CAPACITY
Constants for Queue.
Definition: Constants.h:17
This class models a linear structure called a Queue.
Definition: Queue.h:14
int front
index to front of the queue
Definition: Queue.h:17
int size() const
Definition: Queue.h:73
int capacity
capacity of the queue
Definition: Queue.h:21
std::vector< ValueType > ringBuffer
Vector of ValueType for Circular Queue.
Definition: Queue.h:16
bool isEmpty() const
Definition: Queue.h:78
void expandRingBufferCapacity()
This private method doubles the capacity of the ringBuffer vector.
Definition: Queue.h:134
int count
number of value in the queue
Definition: Queue.h:20
void clear()
Removes all elements from the queue.
Definition: Queue.h:83
ValueType & getFront()
Definition: Queue.h:120
ValueType dequeue()
Removes and returns the first item in the queue.
Definition: Queue.h:102
virtual ~Queue()=default
Frees any heap storage associated with this queue.
ValueType & getBack()
Definition: Queue.h:127
void enqueue(const ValueType &value)
Adds value to the end of the queue.
Definition: Queue.h:92
Queue()
Initializes a new empty queue.
Definition: Queue.h:68
ValueType peek() const
Definition: Queue.h:113
int rear
index to rear of the queue
Definition: Queue.h:18