FE 0.13.1
Header-only C++ frontend library
Loading...
Searching...
No Matches
worklist.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
5#include <queue>
6#include <ranges>
7#include <stack>
8#include <type_traits>
9#include <utility>
10
11#include "fe/container.h"
12
13namespace fe {
14
15/// A worklist that pushes each element at most once.
16/// @p Set remembers what has already been pushed and may be a reference to share it with the caller.
17/// Use it through the BFSWorklist/DFSWorklist aliases.
18template<class Set, class C>
19class Worklist {
20public:
21 using T = typename std::remove_reference_t<Set>::value_type;
22 static_assert(std::is_same_v<T, typename C::value_type>);
23
24 /// @name Constructors
25 ///@{
26 Worklist() = default;
27 explicit Worklist(Set set)
28 : done_(std::forward<Set>(set)) {}
29 Worklist(std::initializer_list<T> init) { push(init); }
30 ///@}
31
32 /// @name push
33 ///@{
34 bool push(T val) {
35 if (done_.emplace(val).second) {
36 c_.emplace(std::move(val));
37 return true;
38 }
39 return false;
40 }
41 template<std::ranges::input_range R>
42 void push(R&& r) {
43 for (auto&& val : r)
44 push(val);
45 }
46 ///@}
47
48 /// @name Access
49 ///@{
50 bool empty() const { return c_.empty(); }
51 size_t size() const { return c_.size(); }
52 T pop() { return fe::pop(c_); }
53
54 T& front() requires Queuelike<C> { return c_.front(); }
55 const T& front() const requires Queuelike<C> { return c_.front(); }
56 T& back() requires Queuelike<C> { return c_.back(); }
57 const T& back() const requires Queuelike<C> { return c_.back(); }
58 T& top() requires Stacklike<C> { return c_.top(); }
59 const T& top() const requires Stacklike<C> { return c_.top(); }
60 ///@}
61
62 void clear() {
63 done_.clear();
64 c_ = {};
65 }
66
67private:
68 Set done_;
69 C c_;
70};
71
72namespace detail {
73template<class Set>
74using WorklistElem = typename std::remove_reference_t<Set>::value_type;
75}
76
77template<class Set>
79template<class Set>
81
82} // namespace fe
A worklist that pushes each element at most once.
Definition worklist.h:19
size_t size() const
Definition worklist.h:51
bool push(T val)
Definition worklist.h:34
const T & top() const
Definition worklist.h:59
Worklist()=default
const T & back() const
Definition worklist.h:57
T & front()
Definition worklist.h:54
Worklist(std::initializer_list< T > init)
Definition worklist.h:29
void clear()
Definition worklist.h:62
typename std::remove_reference_t< Set >::value_type T
Definition worklist.h:21
Worklist(Set set)
Definition worklist.h:27
T & top()
Definition worklist.h:58
void push(R &&r)
Definition worklist.h:42
bool empty() const
Definition worklist.h:50
const T & front() const
Definition worklist.h:55
T & back()
Definition worklist.h:56
Something which behaves like std::queue.
Definition container.h:19
Something which behaves like std::stack or std::priority_queue.
Definition container.h:12
Definition algo.h:17
Worklist< Set, std::queue< detail::WorklistElem< Set > > > BFSWorklist
Definition worklist.h:78
S::value_type pop(S &s)
Definition container.h:27
Worklist< Set, std::stack< detail::WorklistElem< Set > > > DFSWorklist
Definition worklist.h:80
Definition span.h:129