Skip to content

Commit

Permalink
Pop returns removed object
Browse files Browse the repository at this point in the history
  • Loading branch information
torrentg committed Oct 10, 2023
1 parent c2ecb70 commit 2832353
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cqueue-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void example1() {
queue[1] = 99;
std::cout << prefix << to_string(queue) << std::endl;
std::cout << prefix << "queue[1] = " << queue[1] << std::endl;
queue.pop();
std::cout << prefix << "pop() = " << queue.pop() << std::endl;
std::cout << prefix << to_string(queue) << std::endl;
std::cout << prefix << "front = " << queue.front() << std::endl;
std::cout << prefix << "back = " << queue.back() << std::endl;
Expand Down
18 changes: 9 additions & 9 deletions cqueue-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ TEST_CASE("cqueue") {
CHECK_THROWS(queue.front());
CHECK_THROWS(queue.back());
CHECK(queue.begin() == queue.end());
CHECK(queue.pop() == false);
CHECK(queue.pop_back() == false);
CHECK_THROWS(queue.pop());
CHECK_THROWS(queue.pop_back());
CHECK_THROWS(queue[0]);
queue.clear();
CHECK(queue.size() == 0);
Expand Down Expand Up @@ -465,37 +465,37 @@ TEST_CASE("cqueue") {

SECTION("pop") {
cqueue<int> queue(5);
CHECK(!queue.pop());
CHECK_THROWS(queue.pop());
queue.push(1);
CHECK(queue.size() == 1);
queue.pop();
CHECK(queue.pop() == 1);
CHECK(queue.empty());
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
CHECK(queue.size() == 5);
queue.pop();
CHECK(queue.pop() == 1);
CHECK(queue.size() == 4);
CHECK(queue.front() == 2);
CHECK(queue.back() == 5);
}

SECTION("pop_back") {
cqueue<int> queue(5);
CHECK(!queue.pop_back());
CHECK_THROWS(queue.pop_back());
queue.push(1);
CHECK(queue.size() == 1);
queue.pop_back();
CHECK(queue.pop_back() == 1);
CHECK(queue.empty());
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
CHECK(queue.size() == 5);
queue.pop_back();
CHECK(queue.pop_back() == 5);
CHECK(queue.size() == 4);
CHECK(queue.front() == 1);
CHECK(queue.back() == 4);
Expand Down Expand Up @@ -993,7 +993,7 @@ TEST_CASE("cqueue") {
CHECK(queue.empty());
CHECK_THROWS(queue.front());
CHECK_THROWS(queue.back());
CHECK(queue.pop() == false);
CHECK_THROWS(queue.pop());
CHECK_THROWS(queue[0]);
// content = [1,.]
queue.push(1);
Expand Down
39 changes: 19 additions & 20 deletions cqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class cqueue {
//! Construct and insert an element at the front.
template <class... Args>
constexpr reference emplace_front(Args&&... args);
//! Alias to emplace_back.
template <class... Args>
constexpr reference emplace(Args&&... args) { return emplace_back(std::forward<Args>(args)...); }

//! Insert an element at the end.
constexpr void push_back(const T &val);
Expand All @@ -200,21 +203,17 @@ class cqueue {
constexpr void push_front(const T &val);
//! Insert an element at the front.
constexpr void push_front(T &&val);

//! Remove the front element.
constexpr bool pop_front();
//! Remove the back element.
constexpr bool pop_back();

//! Alias to emplace_back.
template <class... Args>
constexpr reference emplace(Args&&... args) { return emplace_back(std::forward<Args>(args)...); }
//! Alias to push_back.
constexpr void push(const T &val) { return push_back(val); }
//! Alias to push_back.
constexpr void push(T &&val) { return push_back(std::move(val)); }

//! Remove the front element.
constexpr value_type pop_front();
//! Remove the back element.
constexpr value_type pop_back();
//! Alias to pop_front.
constexpr bool pop() { return pop_front(); }
constexpr value_type pop() { return pop_front(); }

//! Returns a reference to the element at position n.
constexpr reference operator[](size_type n) { return mData[getCheckedIndex(n)]; }
Expand Down Expand Up @@ -550,6 +549,7 @@ constexpr void gto::cqueue<T, Allocator>::push_front(T &&val) {

/**
* @param[in] args Arguments of the new item.
* @return Reference to emplaced object.
* @exception std::length_error Number of values exceed queue capacity.
*/
template<std::copyable T, typename Allocator>
Expand All @@ -564,6 +564,7 @@ constexpr auto gto::cqueue<T, Allocator>::emplace_back(Args&&... args) -> refere

/**
* @param[in] args Arguments of the new item.
* @return Reference to emplaced object.
* @exception std::length_error Number of values exceed queue capacity.
*/
template<std::copyable T, typename Allocator>
Expand All @@ -579,28 +580,26 @@ constexpr auto gto::cqueue<T, Allocator>::emplace_front(Args&&... args) -> refer

/**
* @return true = an element was erased, false = no elements in the queue.
* @exception std::out_of_range No elements to pop.
*/
template<std::copyable T, typename Allocator>
constexpr bool gto::cqueue<T, Allocator>::pop_front() {
if (mLength == 0) {
return false;
}
constexpr gto::cqueue<T, Allocator>::value_type gto::cqueue<T, Allocator>::pop_front() {
value_type ret{std::move(front())};
allocator_traits::destroy(mAllocator, mData + mFront);
mFront = getUncheckedIndex(1);
--mLength;
return true;
return ret;
}

/**
* @return true = an element was erased, false = no elements in the queue.
* @exception std::out_of_range No elements to pop.
*/
template<std::copyable T, typename Allocator>
constexpr bool gto::cqueue<T, Allocator>::pop_back() {
if (mLength == 0) {
return false;
}
constexpr gto::cqueue<T, Allocator>::value_type gto::cqueue<T, Allocator>::pop_back() {
value_type ret{std::move(back())};
size_type index = getUncheckedIndex(mLength - 1);
allocator_traits::destroy(mAllocator, mData + index);
--mLength;
return true;
return ret;
}

0 comments on commit 2832353

Please sign in to comment.