Skip to content

Commit

Permalink
feat: add const size ringbuffer_queue implement
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
  • Loading branch information
Certseeds committed Mar 7, 2024
1 parent c4dc1bd commit b5e8407
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
2 changes: 2 additions & 0 deletions algorithm/queue/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ set(dependencies)

set(leetcode_order)
LIST(APPEND leetcode_order so_09 933 1532)
LIST(APPEND leetcode_order ringbuffer_queue)

LIST(TRANSFORM leetcode_order PREPEND leetcode_)

set(dependencies ${dependencies} ${leetcode_order})
Expand Down
10 changes: 9 additions & 1 deletion algorithm/queue/README.md
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地址.
49 changes: 49 additions & 0 deletions algorithm/queue/leetcode_ringbuffer_queue.cpp
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];
}

}
93 changes: 93 additions & 0 deletions algorithm/queue/leetcode_ringbuffer_queue.hpp
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

0 comments on commit b5e8407

Please sign in to comment.