Commit 4357bdf4 authored by Dominik Charousset's avatar Dominik Charousset

Integrate review feedback

parent 20476587
...@@ -73,6 +73,8 @@ public: ...@@ -73,6 +73,8 @@ public:
} }
/// @private /// @private
/// @note Only used by the legacy test framework. Remove when dropping support
/// for it.
virtual mailbox_element* peek(message_id id) = 0; virtual mailbox_element* peek(message_id id) = 0;
}; };
......
...@@ -65,9 +65,7 @@ public: ...@@ -65,9 +65,7 @@ public:
linked_list& operator=(linked_list&& other) noexcept { linked_list& operator=(linked_list&& other) noexcept {
clear(); clear();
if (other.empty()) { if (!other.empty()) {
init();
} else {
head_.next = other.head_.next; head_.next = other.head_.next;
tail_.next = other.tail_.next; tail_.next = other.tail_.next;
tail_.next->next = &tail_; tail_.next->next = &tail_;
...@@ -92,28 +90,28 @@ public: ...@@ -92,28 +90,28 @@ public:
return size_; return size_;
} }
/// Returns whether the queue has no elements. /// Returns whether the list has no elements.
bool empty() const noexcept { bool empty() const noexcept {
return size() == 0; return size() == 0;
} }
// -- modifiers ------------------------------------------------------------- // -- modifiers -------------------------------------------------------------
/// Removes all elements from the queue. /// Removes all elements from the list.
void clear() noexcept { void clear() noexcept {
typename unique_pointer::deleter_type fn; typename unique_pointer::deleter_type fn;
drain(fn); drain(fn);
} }
/// Removes the first element from the queue and returns it. /// Removes the first element from the list and returns it.
unique_pointer pop_front() { unique_pointer pop_front() {
unique_pointer result; unique_pointer result;
if (!empty()) { if (!empty()) {
auto ptr = promote(head_.next); auto ptr = promote(head_.next);
head_.next = ptr->next; head_.next = ptr->next;
if (--size_ == 0) { if (--size_ == 0) {
CAF_ASSERT(head_.next == &(tail_)); CAF_ASSERT(head_.next == &tail_);
tail_.next = &(head_); tail_.next = &head_;
} }
result.reset(ptr); result.reset(ptr);
} }
...@@ -158,7 +156,7 @@ public: ...@@ -158,7 +156,7 @@ public:
} }
/// Returns an iterator to the last element or to the dummy before the first /// Returns an iterator to the last element or to the dummy before the first
/// element if the queue is empty. /// element if the list is empty.
iterator before_end() noexcept { iterator before_end() noexcept {
return tail_.next; return tail_.next;
} }
...@@ -176,14 +174,14 @@ public: ...@@ -176,14 +174,14 @@ public:
return promote(tail_.next); return promote(tail_.next);
} }
/// Like `front`, but returns `nullptr` if the queue is empty. /// Like `front`, but returns `nullptr` if the list is empty.
pointer peek() noexcept { pointer peek() noexcept {
return size_ > 0 ? front() : nullptr; return size_ > 0 ? front() : nullptr;
} }
// -- insertion -------------------------------------------------------------- // -- insertion --------------------------------------------------------------
/// Appends `ptr` to the queue. /// Appends `ptr` to the list.
/// @pre `ptr != nullptr` /// @pre `ptr != nullptr`
void push_back(pointer ptr) noexcept { void push_back(pointer ptr) noexcept {
CAF_ASSERT(ptr != nullptr); CAF_ASSERT(ptr != nullptr);
...@@ -193,7 +191,7 @@ public: ...@@ -193,7 +191,7 @@ public:
++size_; ++size_;
} }
/// Appends `ptr` to the queue. /// Appends `ptr` to the list.
/// @pre `ptr != nullptr` /// @pre `ptr != nullptr`
void push_back(unique_pointer ptr) noexcept { void push_back(unique_pointer ptr) noexcept {
push_back(ptr.release()); push_back(ptr.release());
...@@ -241,7 +239,7 @@ public: ...@@ -241,7 +239,7 @@ public:
// -- algorithms ------------------------------------------------------------- // -- algorithms -------------------------------------------------------------
/// Tries to find an element in the queue that matches the given predicate. /// Tries to find an element in the list that matches the given predicate.
template <class Predicate> template <class Predicate>
pointer find_if(Predicate pred) { pointer find_if(Predicate pred) {
for (auto i = begin(); i != end(); ++i) for (auto i = begin(); i != end(); ++i)
...@@ -275,10 +273,10 @@ private: ...@@ -275,10 +273,10 @@ private:
node_type head_; node_type head_;
/// Dummy past-the-last-element node. The `tail_->next` pointer is pointing to /// Dummy past-the-last-element node. The `tail_->next` pointer is pointing to
/// the last element in the queue or to `head_` if the queue is empty. /// the last element in the list or to `head_` if the list is empty.
node_type tail_; node_type tail_;
/// Stores the total size of all items in the queue. /// Stores the total size of all items in the list.
size_t size_ = 0; size_t size_ = 0;
}; };
......
...@@ -143,4 +143,15 @@ TEST("lists allow iterator-based access") { ...@@ -143,4 +143,15 @@ TEST("lists allow iterator-based access") {
12); 12);
} }
TEST("pop_front removes the oldest element of a list and returns it") {
list_type uut;
fill(uut, 1, 2, 3);
check_eq(uut.pop_front()->value, 1);
if (check_eq(uut.size(), 2u))
check_eq(uut.pop_front()->value, 1);
if (check_eq(uut.size(), 1u))
check_eq(uut.pop_front()->value, 1);
check(uut.empty());
}
CAF_TEST_MAIN() CAF_TEST_MAIN()
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
namespace caf::intrusive { namespace caf::intrusive {
/// A simple stack implementation with singly-linked nodes.
template <class T> template <class T>
class stack { class stack {
public: public:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment