-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add const size ringbuffer_queue implement
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
# queue | ||
|
||
TODO: using ring buffer to simulate a queue | ||
DOING: using ring buffer to simulate a queue | ||
|
||
ring buffer应该有的接口 | ||
|
||
1. `bool isFull()`, push前先检查, 如果满了就不能push, 什么也不干. | ||
2. `bool isEmpty()`, pop前先检查, 如果空了就不能pop, 什么也不干. | ||
3. `void push(T value)` push对象, 满了什么也不干. | ||
4. `void pop()`, 弹出第一个对象(对POD对象来说不清理), 如果空的话什么也不干. | ||
5. `T front()`, 获取第一个对象, 空的话直接解引用head地址. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2024 nanoseeds | ||
*/ | ||
|
||
#include "leetcode_ringbuffer_queue.hpp" | ||
|
||
namespace rbqueue { | ||
|
||
ringbuffer_queue::ringbuffer_queue(size_t size) : vec(vector<int32_t>(size, 0)) { | ||
csize = size; | ||
} | ||
|
||
bool ringbuffer_queue::isFull() const { | ||
if (write - read == csize) { | ||
return true; | ||
} else if (read - write == csize) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool ringbuffer_queue::isEmpty() const { | ||
return read == write; // if read == write, it must be empty | ||
} | ||
|
||
void ringbuffer_queue::push(int32_t value) { | ||
if (isFull()) { | ||
return; | ||
} | ||
vec[write % csize] = value; | ||
write = (write + 1) % (csize << 1); | ||
} | ||
|
||
void ringbuffer_queue::pop() { | ||
if (isEmpty()) { | ||
return; | ||
} | ||
read = (read + 1) % (csize << 1); | ||
} | ||
|
||
int32_t ringbuffer_queue::front() { | ||
return vec[read % csize]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2024 nanoseeds | ||
*/ | ||
|
||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_QUEUE_RINGBUFFER_QUEUE_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_QUEUE_RINGBUFFER_QUEUE_HPP | ||
|
||
#include <catch_main.hpp> | ||
|
||
#include <optional> | ||
#include <cstdint> | ||
#include <queue> | ||
|
||
namespace rbqueue { | ||
|
||
class ringbuffer_queue { | ||
private: | ||
std::vector<int32_t> vec; | ||
size_t read{0}, write{0}; | ||
size_t csize{}; | ||
public: | ||
explicit ringbuffer_queue(size_t size); | ||
|
||
bool isFull() const; | ||
|
||
bool isEmpty() const; | ||
|
||
void push(int32_t value); | ||
|
||
void pop(); | ||
|
||
int32_t front(); | ||
}; | ||
|
||
TEST_CASE("test case pure-1 [test_rbqueue_09]", "[test_rbqueue_09]") { | ||
ringbuffer_queue rbq(3); | ||
CHECK(rbq.isEmpty()); | ||
rbq.push(1); | ||
CHECK_FALSE(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 1); | ||
CHECK(rbq.front() == 1); | ||
rbq.push(2); | ||
CHECK_FALSE(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 1); | ||
rbq.push(3); | ||
CHECK_FALSE(rbq.isEmpty()); | ||
CHECK(rbq.isFull()); | ||
CHECK(rbq.front() == 1); | ||
rbq.pop(); | ||
CHECK_FALSE(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 2); | ||
rbq.pop(); | ||
CHECK_FALSE(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 3); | ||
rbq.pop(); | ||
|
||
CHECK(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
rbq.push(4); | ||
CHECK_FALSE(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 4); | ||
rbq.pop(); | ||
CHECK(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 2); | ||
rbq.push(6); | ||
CHECK_FALSE(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
rbq.pop(); | ||
CHECK(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 3); | ||
rbq.pop(); | ||
CHECK(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 3); | ||
rbq.pop(); | ||
CHECK(rbq.isEmpty()); | ||
CHECK_FALSE(rbq.isFull()); | ||
CHECK(rbq.front() == 3); | ||
} | ||
} | ||
|
||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_QUEUE_RINGBUFFER_QUEUE_HPP |