FE 0.16.1
Fast, Effecient FrontEnds
Loading...
Searching...
No Matches
worklist.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
5#include <ranges>
6#include <stack>
7#include <type_traits>
8#include <utility>
9
10#include "fe/container.h"
11#include "fe/vector.h"
12
13namespace fe {
14
15/// A FIFO queue over a Vector.
16/// Popped elements stay in place until they make up half of the storage, so `pop` is amortized constant.
17template<class T, size_t N = Default_Inlined_Size<T>>
19public:
20 using value_type = T;
21
22 bool empty() const { return head_ == vec_.size(); }
23 size_t size() const { return vec_.size() - head_; }
24
25 T& front() { return vec_[head_]; }
26 const T& front() const { return vec_[head_]; }
27 T& back() { return vec_.back(); }
28 const T& back() const { return vec_.back(); }
29
30 void push(const T& val) { vec_.push_back(val); }
31 void push(T&& val) { vec_.push_back(std::move(val)); }
32 template<class... Args>
33 decltype(auto) emplace(Args&&... args) {
34 return vec_.emplace_back(std::forward<Args>(args)...);
35 }
36
37 void pop() {
38 if (++head_ == vec_.size()) {
39 vec_.clear();
40 head_ = 0;
41 } else if (head_ >= 2 * N && 2 * head_ >= vec_.size()) {
42 vec_.erase(vec_.begin(), vec_.begin() + head_);
43 head_ = 0;
44 }
45 }
46
47private:
48 Vector<T, N> vec_;
49 size_t head_ = 0;
50};
51
52/// A worklist that pushes each element at most once.
53/// @p Set remembers what has already been pushed and may be a reference to share it with the caller.
54/// Use it through the BFSWorklist/DFSWorklist aliases.
55template<class Set, class C>
56class Worklist {
57public:
58 using T = typename std::remove_reference_t<Set>::value_type;
59 static_assert(std::is_same_v<T, typename C::value_type>);
60
61 /// @name Constructors
62 ///@{
63 Worklist() = default;
64 explicit Worklist(Set set)
65 : done_(std::forward<Set>(set)) {}
66 Worklist(std::initializer_list<T> init) { push(init); }
67 ///@}
68
69 /// @name push
70 ///@{
71 bool push(T val) {
72 if (done_.emplace(val).second) {
73 c_.emplace(std::move(val));
74 return true;
75 }
76 return false;
77 }
78 template<std::ranges::input_range R>
79 void push(R&& r) {
80 for (auto&& val : r)
81 push(val);
82 }
83 ///@}
84
85 /// @name Access
86 ///@{
87 bool empty() const { return c_.empty(); }
88 size_t size() const { return c_.size(); }
89 T pop() { return fe::pop(c_); }
90
91 T& front() requires Queuelike<C> { return c_.front(); }
92 const T& front() const requires Queuelike<C> { return c_.front(); }
93 T& back() requires Queuelike<C> { return c_.back(); }
94 const T& back() const requires Queuelike<C> { return c_.back(); }
95 T& top() requires Stacklike<C> { return c_.top(); }
96 const T& top() const requires Stacklike<C> { return c_.top(); }
97 ///@}
98
99 void clear() {
100 done_.clear();
101 c_ = {};
102 }
103
104private:
105 Set done_;
106 C c_;
107};
108
109namespace detail {
110template<class Set>
111using WorklistElem = typename std::remove_reference_t<Set>::value_type;
112}
113
114template<class Set>
116template<class Set>
118
119} // namespace fe
A FIFO queue over a Vector.
Definition worklist.h:18
void push(const T &val)
Definition worklist.h:30
bool empty() const
Definition worklist.h:22
const T & back() const
Definition worklist.h:28
decltype(auto) emplace(Args &&... args)
Definition worklist.h:33
const T & front() const
Definition worklist.h:26
size_t size() const
Definition worklist.h:23
void push(T &&val)
Definition worklist.h:31
This is a thin wrapper for ankerl::svector<T, N, A> which is a drop-in replacement for std::vector<T,...
Definition vector.h:25
A worklist that pushes each element at most once.
Definition worklist.h:56
size_t size() const
Definition worklist.h:88
bool push(T val)
Definition worklist.h:71
const T & top() const
Definition worklist.h:96
Worklist()=default
const T & back() const
Definition worklist.h:94
T & front()
Definition worklist.h:91
Worklist(std::initializer_list< T > init)
Definition worklist.h:66
void clear()
Definition worklist.h:99
typename std::remove_reference_t< Set >::value_type T
Definition worklist.h:58
Worklist(Set set)
Definition worklist.h:64
T & top()
Definition worklist.h:95
void push(R &&r)
Definition worklist.h:79
bool empty() const
Definition worklist.h:87
const T & front() const
Definition worklist.h:92
T & back()
Definition worklist.h:93
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
S::value_type pop(S &s)
Definition container.h:27
Worklist< Set, std::stack< detail::WorklistElem< Set >, Vector< detail::WorklistElem< Set > > > > DFSWorklist
Definition worklist.h:117
Worklist< Set, VectorQueue< detail::WorklistElem< Set > > > BFSWorklist
Definition worklist.h:115
Definition span.h:150