-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest5.cpp
113 lines (92 loc) · 2.7 KB
/
test5.cpp
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
#include <iostream>
#include <array>
#include <type_traits>
namespace exercise
{
template <typename T, size_t S>
class circular_buffer
{
static_assert(S > 0, "Size of the buffer must be greater than 0");
T data_[S];
size_t head_ = 0;
size_t tail_ = 0;
bool full_ = false;
public:
constexpr void push(const T& value)
{
data_[head_] = value;
if (full_)
{
tail_ = (tail_ + 1) % S;
}
head_ = (head_ + 1) % S;
full_ = head_ == tail_;
}
constexpr T pop()
{
// TODO: Implement the pop logic to remove and return the value from the tail
// If the buffer is empty, return a default-constructed value of type T
if (is_empty())
{
return T{};
}
T value = data_[tail_];
full_ = false;
tail_ = (tail_ + 1) % S;
return value;
}
constexpr bool is_empty() const
{
return (!full_ && (head_ == tail_));
}
constexpr bool is_full() const
{
return full_;
}
constexpr size_t size() const
{
size_t size = S;
if (!full_)
{
if (head_ >= tail_)
{
size = head_ - tail_;
}
else
{
size = S + head_ - tail_;
}
}
return size;
}
constexpr T const& operator[](size_t const index) const
{
return data_[(tail_ + index) % S];
}
};
template <typename T, size_t S>
circular_buffer<T, S> make_circular_buffer()
{
return {};
}
}
int main()
{
using namespace exercise;
circular_buffer<int, 5> buffer;
buffer.push(1);
buffer.push(2);
buffer.push(3);
std::cout << "Buffer size: " << buffer.size() << "\n";
std::cout << "Is buffer full? " << std::boolalpha << buffer.is_full() << "\n";
buffer.push(4);
buffer.push(5);
buffer.push(6); // This should overwrite the first element (1)
std::cout << "Buffer size after pushing 6 elements: " << buffer.size() << "\n";
std::cout << "Is buffer full? " << std::boolalpha << buffer.is_full() << "\n";
std::cout << "Element at index 0: " << buffer[0] << "\n"; // Should be 2
// TODO: Implement the pop logic and then test it here
// std::cout << "Popped element: " << buffer.pop() << "\n"; // Should be 2
// std::cout << "Buffer size after popping one element: " << buffer.size() << "\n";
return 0;
}