44#ifndef TPL_DFS_STACK_H
45#define TPL_DFS_STACK_H
79#if defined(ALEPH_NO_FLATTEN) || defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__) || \
80 defined(__NO_INLINE__)
82#elif defined(__has_feature)
83#if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer)
86#define ALEPH_FLATTEN [[gnu::flatten]]
89#define ALEPH_FLATTEN [[gnu::flatten]]
95#if defined(ALEPH_ANNOTATE_CONTAINERS)
96#if defined(__SANITIZE_ADDRESS__)
97#define ALEPH_DFS_STACK_POISON 1
98#elif defined(__has_feature)
99#if __has_feature(address_sanitizer)
100#define ALEPH_DFS_STACK_POISON 1
104#ifdef ALEPH_DFS_STACK_POISON
105#include <sanitizer/asan_interface.h>
109namespace dfs_stack_detail {
114#ifdef ALEPH_DFS_STACK_POISON
115inline constexpr bool Poisoned =
true;
123inline void poison([[maybe_unused]]
const void *p, [[maybe_unused]]
size_t bytes)
noexcept
125#ifdef ALEPH_DFS_STACK_POISON
126 ASAN_POISON_MEMORY_REGION(p, bytes);
131inline void unpoison([[maybe_unused]]
const void *p, [[maybe_unused]]
size_t bytes)
noexcept
133#ifdef ALEPH_DFS_STACK_POISON
134 ASAN_UNPOISON_MEMORY_REGION(p, bytes);
139inline void free_room(
void *
r, [[maybe_unused]]
size_t bytes)
noexcept
144 ::operator
delete (
r);
159 static thread_local constinit Room room;
170#if defined(__GLIBC__)
171#if __GLIBC_PREREQ(2, 30)
172 return ::gettid() == ::getpid();
242 static_assert(
alignof(
T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__,
243 "the frames need more alignment than operator new gives");
276 if (kept.
open and room_bytes <= dfs_stack_detail::Kept_Bytes and room_bytes > kept.
bytes)
297 if (kept.
bytes >=
sizeof(
T))
323 const size_t n =
size();
325 const size_t new_bytes = frames *
sizeof(
T);
326 void *new_room = ::operator
new (new_bytes);
327 T *new_base =
static_cast<T *
>(new_room);
329 if constexpr (std::is_nothrow_move_constructible_v<T>)
337 if constexpr (std::is_copy_constructible_v<T>)
338 std::uninitialized_copy(
static_cast<const T *
>(
base),
static_cast<const T *
>(
top_ptr),
360 template <
class... Args>
366 if constexpr (std::is_nothrow_constructible_v<
T, Args &&...>)
367 frame = ::new (place)
T(std::forward<Args>(args)...);
371 frame = ::new (place)
T(std::forward<Args>(args)...);
400 std::swap(
room, other.room);
402 std::swap(
base, other.base);
403 std::swap(
top_ptr, other.top_ptr);
404 std::swap(
limit, other.limit);
429 template <
class... Args>
430 [[gnu::always_inline]]
T &
push(Args &&...args)
438 T *frame = ::new (
static_cast<void *
>(
top_ptr))
T(std::forward<Args>(args)...);
454 [[nodiscard]]
T &
top() noexcept
466 [[nodiscard]]
size_t size() const noexcept
520 using Node = std::remove_pointer_t<decltype(std::declval<It &>().get_tgt_node_ne())>;
526 template <
class... Args>
527 requires std::constructible_from<
It, Args &&...>
529 :
it(
std::forward<Args>(args)...)
550 template <
int Bit,
bool Marked_Too = false>
558 auto arc =
it.get_current_arc_ne();
559 if (arc->attrs.control_bits.get_bit(Bit))
564 auto tgt =
it.get_tgt_node_ne();
565 arc->attrs.control_bits.set_bit(Bit,
true);
566 if (Marked_Too or tgt == goal or not tgt->attrs.control_bits.get_bit(Bit))
591using Dfs_Here = std::conditional_t<std::is_move_assignable_v<T>,
T, std::optional<T>>;
601template <
class T,
class... Args>
604 if constexpr (std::is_move_assignable_v<T>)
605 return T(std::forward<Args>(args)...);
607 return Dfs_Here<T>(std::in_place, std::forward<Args>(args)...);
630[[gnu::always_inline]]
inline T &
dfs_frame(std::optional<T> &here)
noexcept
644template <
class T,
class... Args>
647 here =
T(std::forward<Args>(args)...);
659template <
class T,
class... Args>
660[[gnu::always_inline]]
inline void dfs_replace(std::optional<T> &here, Args &&...args)
662 here.emplace(std::forward<Args>(args)...);
676[[gnu::always_inline]]
inline decltype(
auto)
dfs_curr(
const It &it)
Stack of the frames of a depth-first search that does not recurse.
static constexpr size_t Initial_Capacity
static constexpr size_t Second_Capacity
void clear() noexcept
Destroys all the frames, and keeps the room.
size_t capacity() const noexcept
The number of frames that fit in the room of the stack, 0 while it has none.
T & push(Args &&...args)
Builds a frame on top of the stack.
void give_room_back() noexcept
bool is_empty() const noexcept
Whether the stack has no frames.
~Dfs_Stack()
Destroys the frames; the thread keeps the room, if it fits in 1 MiB and is larger than the one kept.
Dfs_Stack & operator=(Dfs_Stack &&other) noexcept
Takes the frames and the room of other, which is left with the room of this stack and no frames.
T & top() noexcept
The top frame; the stack must not be empty.
void release() noexcept
Destroys all the frames, and gives the room back.
size_t size() const noexcept
The number of frames.
void set_room(void *r, size_t bytes) noexcept
Dfs_Stack() noexcept=default
Builds an empty stack, which takes the room that the thread kept when it first needs room.
void pop() noexcept
Destroys the top frame; the stack must not be empty.
T & push_in_poisoned_room(Args &&...args)
T & get_curr() const
Return the current item.
T & get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
bool has_curr() const noexcept
void dfs_replace(T &here, Args &&...args)
Replaces the frame that a Dfs_Here holds with one built with args, by assignment.
Dfs_Here< T > dfs_here(Args &&...args)
Builds the variable of the frame of a search, with the frame built with args.
decltype(auto) dfs_curr(const It &it)
The current item of an iterator that has one.
T & dfs_frame(T &here) noexcept
The frame that a Dfs_Here holds.
std::conditional_t< std::is_move_assignable_v< T >, T, std::optional< T > > Dfs_Here
The variable that holds the frame of the node that a depth-first search visits.
#define ALEPH_FLATTEN
[[gnu::flatten]], or nothing where it would only make the compilation slower.
constexpr size_t Kept_Bytes
bool in_main_thread() noexcept
Room & open_room() noexcept
void free_room(void *r, size_t bytes) noexcept
Room & kept_room() noexcept
void poison(const void *p, size_t bytes) noexcept
void unpoison(const void *p, size_t bytes) noexcept
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
The next arc of a node to look at, for a depth-first search that keeps only that in a frame.
Dfs_Arc_Frame(Args &&...args)
Builds the iterator with args, if they build one, so that a frame is copied by the copy constructor; ...
std::remove_pointer_t< decltype(std::declval< It & >().get_tgt_node_ne())> Node
The type of the nodes of the graph.
It it
The arcs of the node, on the next one to look at.
void next_arc() noexcept(noexcept(it.next_ne()))
Moves to the next arc; inlined, with all it calls.
Node * next_target(Node *goal=nullptr) noexcept(noexcept(it.next_ne()))
Moves the iterator to the next arc to follow, and gives its target.
Opens the room of the thread, and frees it when the thread ends, but for the thread of main() with gl...
Room of a destroyed stack, kept for the next stack of the thread.
Filter_Iterator< DynList< int >, DynList< int >::Iterator, Par > It