forked from jamesjuett/eecs280_circularbuffer_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRingBuffer.hpp
More file actions
140 lines (111 loc) · 3.73 KB
/
RingBuffer.hpp
File metadata and controls
140 lines (111 loc) · 3.73 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef RINGBUFFER_HPP
#define RINGBUFFER_HPP
#include <iostream>
#include <cassert>
template <typename T>
class RingBuffer {
public:
// EFFECTS: Constructs an empty RingBuffer.
RingBuffer()
: data(new T[INITIAL_CAPACITY]),
head(0), tail(0), num_elts(0), capacity(INITIAL_CAPACITY) {
}
RingBuffer(const RingBuffer<T> &other)
: data(new T[other.capacity]),
capacity(other.capacity) {
copy_data_from(other);
}
// EFFECTS: Adds a new value to the back of the buffer
// moves tail to the new position
void push_back(const T &value) {
assert(false); // TODO: Replace with your implementation
}
// REQUIRES: buffer is not empty
// EFFECTS: Removes the element at the front of the buffer
// moves head to the new position
void pop_front() {
assert(false); // TODO: Replace with your implementation
}
// REQUIRES: buffer is not empty
// EFFECTS: Returns the value at the front of the buffer
T &front() {
assert(false); // TODO: Replace with your implementation
}
// EFFECTS: Returns the current size of the buffer
int size() const{
return num_elts;
}
// EFFECTS: Returns true if this RingBuffer is empty, false otherwise.
bool empty() const {
return num_elts == 0;
}
// REQUIRES: 0 <= index and index < number of elements in this RingBuffer.
// EFFECTS: Returns (by reference) the element that is index positions from head.
T & at(int index) {
return data[(head + index) % capacity];
}
// REQUIRES: 0 <= index and index < number of elements in this RingBuffer.
// EFFECTS: Returns (by const reference) the element that is index positions from head.
const T & at(int index) const {
return data[(head + index) % capacity];
}
// EFFECTS : prints the buffer to os
void print(std::ostream &os) const{
os << "[ ";
for (int i = 0; i < num_elts; ++i) {
os << at(i);
if (i < num_elts - 1) {
os << ", ";
}
}
os << " ]";
}
private:
// The capacity (maximum size) of an RingBuffer is 30 elements.
static const int INITIAL_CAPACITY = 4;
// The array to hold the N elements stored in this ring buffer in positions determined
// by head and tail
T * data;
// head is the index marking the beginning of the data in the buffer
// tail is the index where a new element would be added in the buffer
// num_elts is the number of elements currently in the buffer
int head;
int tail;
int num_elts;
int capacity;
void grow() {
// Place elements at the start of a new, larger array
T *new_data = new T[2*capacity];
for(int i = 0; i < num_elts; ++i) {
new_data[i] = at(i);
}
delete[] data;
data = new_data;
head = 0;
tail = num_elts;
capacity *= 2;
// num_elts remains the same
}
// REQUIRES: this RingBuffer has capacity >= other.num_elts
// EFFECTS: Copies all elements from the other ring buffer into this one,
// replacing any existing elements. They are copied to beginning
// of the data array and the head/tail are adjusted accordingly.
void copy_data_from(const RingBuffer &other) {
// Copy elements from other, placing at the start of our own array
for(int i = 0; i < other.num_elts; ++i) {
// using .at(i) accounts for possibly different head/tail of other
data[i] = other.at(i);
}
// Adjust head/tail and num_elts to match new data
head = 0;
tail = other.num_elts;
num_elts = other.num_elts;
}
};
// EFFECTS : overloaded operator that prints the buffer to os
template <typename T>
std::ostream& operator<<(std::ostream &os, const RingBuffer<T> &rb) {
rb.print(os);
return os;
}
#endif // Do not remove this. Write all your code above this line.