FE
0.13.1
Header-only C++ frontend library
Toggle main menu visibility
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
13
namespace
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.
18
template
<
class
Set,
class
C>
19
class
Worklist
{
20
public
:
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
67
private
:
68
Set done_;
69
C c_;
70
};
71
72
namespace
detail {
73
template
<
class
Set>
74
using
WorklistElem =
typename
std::remove_reference_t<Set>::value_type;
75
}
76
77
template
<
class
Set>
78
using
BFSWorklist
=
Worklist<Set, std::queue<detail::WorklistElem<Set>
>>;
79
template
<
class
Set>
80
using
DFSWorklist
=
Worklist<Set, std::stack<detail::WorklistElem<Set>
>>;
81
82
}
// namespace fe
fe::Worklist
A worklist that pushes each element at most once.
Definition
worklist.h:19
fe::Worklist::size
size_t size() const
Definition
worklist.h:51
fe::Worklist::push
bool push(T val)
Definition
worklist.h:34
fe::Worklist::top
const T & top() const
Definition
worklist.h:59
fe::Worklist::Worklist
Worklist()=default
fe::Worklist::back
const T & back() const
Definition
worklist.h:57
fe::Worklist::front
T & front()
Definition
worklist.h:54
fe::Worklist::Worklist
Worklist(std::initializer_list< T > init)
Definition
worklist.h:29
fe::Worklist::clear
void clear()
Definition
worklist.h:62
fe::Worklist::T
typename std::remove_reference_t< Set >::value_type T
Definition
worklist.h:21
fe::Worklist::Worklist
Worklist(Set set)
Definition
worklist.h:27
fe::Worklist::top
T & top()
Definition
worklist.h:58
fe::Worklist::push
void push(R &&r)
Definition
worklist.h:42
fe::Worklist::empty
bool empty() const
Definition
worklist.h:50
fe::Worklist::pop
T pop()
Definition
worklist.h:52
fe::Worklist::front
const T & front() const
Definition
worklist.h:55
fe::Worklist::back
T & back()
Definition
worklist.h:56
fe::Queuelike
Something which behaves like std::queue.
Definition
container.h:19
fe::Stacklike
Something which behaves like std::stack or std::priority_queue.
Definition
container.h:12
container.h
fe
Definition
algo.h:17
fe::BFSWorklist
Worklist< Set, std::queue< detail::WorklistElem< Set > > > BFSWorklist
Definition
worklist.h:78
fe::pop
S::value_type pop(S &s)
Definition
container.h:27
fe::DFSWorklist
Worklist< Set, std::stack< detail::WorklistElem< Set > > > DFSWorklist
Definition
worklist.h:80
std
Definition
span.h:129
fe
worklist.h
Generated by
1.18.0