Commit acc1fa08 authored by Dominik Charousset's avatar Dominik Charousset Committed by Dominik Charousset

Add task_queue::peek_all member function

parent 1e8bf53a
...@@ -59,6 +59,8 @@ public: ...@@ -59,6 +59,8 @@ public:
using iterator = forward_iterator<value_type>; using iterator = forward_iterator<value_type>;
using const_iterator = forward_iterator<const value_type>;
// -- constructors, destructors, and assignment operators ------------------- // -- constructors, destructors, and assignment operators -------------------
task_queue(policy_type p) : old_last_(nullptr), policy_(std::move(p)) { task_queue(policy_type p) : old_last_(nullptr), policy_(std::move(p)) {
...@@ -124,6 +126,13 @@ public: ...@@ -124,6 +126,13 @@ public:
return ptr != &tail_ ? promote(ptr) : nullptr; return ptr != &tail_ ? promote(ptr) : nullptr;
} }
/// Applies `f` to each element in the queue.
template <class F>
void peek_all(F f) const {
for (auto i = begin(); i != end(); ++i)
f(*promote(i.ptr));
}
// -- modifiers ------------------------------------------------------------- // -- modifiers -------------------------------------------------------------
/// Removes all elements from the queue. /// Removes all elements from the queue.
...@@ -165,21 +174,48 @@ public: ...@@ -165,21 +174,48 @@ public:
inc_total_task_size(policy_.task_size(x)); inc_total_task_size(policy_.task_size(x));
} }
// -- iterator access --------------------------------------------------------
/// Returns an iterator to the dummy before the first element. /// Returns an iterator to the dummy before the first element.
iterator before_begin() noexcept { iterator before_begin() noexcept {
return &head_; return &head_;
} }
/// Returns an iterator to the dummy before the first element.
const_iterator before_begin() const noexcept {
return &head_;
}
/// Returns an iterator to the dummy before the first element. /// Returns an iterator to the dummy before the first element.
iterator begin() noexcept { iterator begin() noexcept {
return head_.next; return head_.next;
} }
/// Returns an iterator to the dummy before the first element.
const_iterator begin() const noexcept {
return head_.next;
}
/// Returns an iterator to the dummy before the first element.
const_iterator cbegin() const noexcept {
return head_.next;
}
/// Returns a pointer to the dummy past the last element. /// Returns a pointer to the dummy past the last element.
iterator end() noexcept { iterator end() noexcept {
return &tail_; return &tail_;
} }
/// Returns a pointer to the dummy past the last element.
const_iterator end() const noexcept {
return &tail_;
}
/// Returns a pointer to the dummy past the last element.
const_iterator cend() const noexcept {
return &tail_;
}
/// Returns a pointer to the first element. /// Returns a pointer to the first element.
pointer front() noexcept { pointer front() noexcept {
return promote(head_.next); return promote(head_.next);
...@@ -292,7 +328,6 @@ protected: ...@@ -292,7 +328,6 @@ protected:
policy_type policy_; policy_type policy_;
}; };
} // namespace intrusive } // namespace intrusive
} // namespace caf } // namespace caf
......
...@@ -143,6 +143,28 @@ CAF_TEST(peek) { ...@@ -143,6 +143,28 @@ CAF_TEST(peek) {
CAF_CHECK_EQUAL(queue.peek()->value, 1); CAF_CHECK_EQUAL(queue.peek()->value, 1);
} }
CAF_TEST(peek_all) {
auto queue_to_string = [&] {
std::string str;
auto peek_fun = [&](const inode& x) {
if (!str.empty())
str += ", ";
str += std::to_string(x.value);
};
queue.peek_all(peek_fun);
return str;
};
CAF_CHECK_EQUAL(queue_to_string(), "");
queue.emplace_back(1);
CAF_CHECK_EQUAL(queue_to_string(), "1");
queue.emplace_back(2);
CAF_CHECK_EQUAL(queue_to_string(), "1, 2");
queue.emplace_back(3);
CAF_CHECK_EQUAL(queue_to_string(), "1, 2, 3");
queue.emplace_back(4);
CAF_CHECK_EQUAL(queue_to_string(), "1, 2, 3, 4");
}
CAF_TEST(task_size) { CAF_TEST(task_size) {
fill(queue, 1, 2, 3); fill(queue, 1, 2, 3);
CAF_CHECK_EQUAL(queue.total_task_size(), 6); CAF_CHECK_EQUAL(queue.total_task_size(), 6);
......
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