Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CA::Stationary_Detector Class Reference

Detects fixed points and short cycles via frame hashing. More...

#include <ca-observer.H>

Collaboration diagram for Aleph::CA::Stationary_Detector:
[legend]

Public Member Functions

 Stationary_Detector ()=default
 Construct a detector with the default 16-frame window.
 
 Stationary_Detector (std::size_t max_cycle_length)
 Construct a detector with an explicit window size.
 
bool cycle_detected () const noexcept
 Return whether a cycle has been detected.
 
std::optional< std::size_t > cycle_length () const noexcept
 Return the detected cycle length.
 
std::optional< std::size_t > cycle_start () const noexcept
 Return the cycle start step.
 
std::size_t buffered_hashes () const noexcept
 Return the number of buffered hashes.
 
void reset () noexcept
 Clear buffered hashes and detected-cycle state.
 
template<typename Lattice >
void on_step_begin (const std::size_t step, const Lattice &frame)
 Seed the detector with the initial frame hash.
 
template<typename Lattice >
void on_step_end (std::size_t step, const Lattice &frame)
 Check the completed frame against the recent hash window.
 

Private Member Functions

void push (const std::uint64_t h, const std::size_t step)
 
bool spans_a_full_window_at_most (const std::size_t length) const noexcept
 

Private Attributes

std::size_t max_cycle_length_ = 0
 
Array< std::uint64_t > hashes_
 
Array< std::size_t > steps_
 
std::optional< std::size_t > cycle_length_
 
std::optional< std::size_t > cycle_start_
 

Detailed Description

Detects fixed points and short cycles via frame hashing.

Keeps a ring buffer of the last max_cycle_length + 1 frame hashes paired with the step at which they were seen. After every step the new hash is compared against the buffer; on a hit the observer records the cycle length (current step minus the step at which the matching frame was seen) and exposes it through cycle_length().

Multiple periodic patterns active at once (e.g. a blinker plus a glider in different regions) result in the longest reachable cycle within the buffer being detected.

Complexity
Each callback computes one frame hash in O(N) and scans at most max_cycle_length + 1 hashes.
Thread-safety
Thread-safe for concurrent readers after simulation stops; not thread-safe for concurrent callbacks.
Exception-safety
Buffering the hash of a frame may throw std::bad_alloc while the window grows, which reaches the caller of the callback: the two buffers, of the hashes and of their steps, are then left as they were. A cycle found in that step stays reported. Once the window is full, buffering allocates nothing.

Definition at line 749 of file ca-observer.H.

Constructor & Destructor Documentation

◆ Stationary_Detector() [1/2]

Aleph::CA::Stationary_Detector::Stationary_Detector ( )
default

Construct a detector with the default 16-frame window.

Exceptions
Thisfunction does not throw.

◆ Stationary_Detector() [2/2]

Aleph::CA::Stationary_Detector::Stationary_Detector ( std::size_t  max_cycle_length)
inlineexplicit

Construct a detector with an explicit window size.

Parameters
[in]max_cycle_lengthmaximum cycle length to retain; 0 takes the default of 16, and SIZE_MAX keeps every frame, so that the window grows with the run.
Exceptions
Thisfunction does not throw.

Definition at line 806 of file ca-observer.H.

Member Function Documentation

◆ buffered_hashes()

std::size_t Aleph::CA::Stationary_Detector::buffered_hashes ( ) const
inlinenoexcept

Return the number of buffered hashes.

Returns
current ring-buffer occupancy.
Exceptions
Thisfunction does not throw.

Definition at line 840 of file ca-observer.H.

References hashes_, and Aleph::Array< T >::size().

Referenced by main(), TEST(), and TEST().

◆ cycle_detected()

bool Aleph::CA::Stationary_Detector::cycle_detected ( ) const
inlinenoexcept

Return whether a cycle has been detected.

Returns
true after the first matching frame hash.
Exceptions
Thisfunction does not throw.

Definition at line 813 of file ca-observer.H.

References cycle_length_.

Referenced by main(), TEST(), TEST(), TEST(), and TEST().

◆ cycle_length()

std::optional< std::size_t > Aleph::CA::Stationary_Detector::cycle_length ( ) const
inlinenoexcept

Return the detected cycle length.

Returns
cycle length, or std::nullopt if none has been found.
Exceptions
Thisfunction does not throw.

Definition at line 822 of file ca-observer.H.

References cycle_length_.

Referenced by main(), TEST(), TEST(), TEST(), and TEST().

◆ cycle_start()

std::optional< std::size_t > Aleph::CA::Stationary_Detector::cycle_start ( ) const
inlinenoexcept

Return the cycle start step.

Returns
start step, or std::nullopt if no cycle has been found.
Exceptions
Thisfunction does not throw.

Definition at line 831 of file ca-observer.H.

References cycle_start_.

Referenced by main(), TEST(), TEST(), and TEST().

◆ on_step_begin()

template<typename Lattice >
void Aleph::CA::Stationary_Detector::on_step_begin ( const std::size_t  step,
const Lattice &  frame 
)
inline

Seed the detector with the initial frame hash.

Template Parameters
Latticelattice type.
Parameters
[in]stepstep index about to run.
[in]frameframe before the step.
Exceptions
std::bad_allocif buffering the hash allocates.

Definition at line 866 of file ca-observer.H.

References Aleph::and, Aleph::CA::frame_hash(), hashes_, Aleph::Array< T >::is_empty(), max_cycle_length_, and push().

Referenced by TEST(), TEST(), and TEST().

◆ on_step_end()

template<typename Lattice >
void Aleph::CA::Stationary_Detector::on_step_end ( std::size_t  step,
const Lattice &  frame 
)
inline

Check the completed frame against the recent hash window.

A buffered hash matches if it was seen at most max_cycle_length + 1 steps before, the steps that a full window spans. A frame that a failed buffering lost leaves a full window with an older hash, which it would have dropped otherwise: a match with it is not taken.

Template Parameters
Latticelattice type.
Parameters
[in]stepcompleted step index.
[in]frameframe after the step.
Exceptions
std::bad_allocif buffering the hash allocates.

Definition at line 888 of file ca-observer.H.

References Aleph::and, cycle_length_, cycle_start_, Aleph::CA::frame_hash(), h, hashes_, max_cycle_length_, push(), Aleph::Array< T >::size(), spans_a_full_window_at_most(), and steps_.

Referenced by TEST(), TEST(), and TEST().

◆ push()

void Aleph::CA::Stationary_Detector::push ( const std::uint64_t  h,
const std::size_t  step 
)
inlineprivate

◆ reset()

void Aleph::CA::Stationary_Detector::reset ( )
inlinenoexcept

Clear buffered hashes and detected-cycle state.

Exceptions
Thisfunction does not throw.
Complexity
O(buffered_hashes()).

Definition at line 851 of file ca-observer.H.

References Aleph::Array< T >::clear(), cycle_length_, cycle_start_, hashes_, and steps_.

◆ spans_a_full_window_at_most()

bool Aleph::CA::Stationary_Detector::spans_a_full_window_at_most ( const std::size_t  length) const
inlineprivatenoexcept

Definition at line 789 of file ca-observer.H.

References max_cycle_length_.

Referenced by on_step_end().

Member Data Documentation

◆ cycle_length_

std::optional<std::size_t> Aleph::CA::Stationary_Detector::cycle_length_
private

Definition at line 754 of file ca-observer.H.

Referenced by cycle_detected(), cycle_length(), on_step_end(), and reset().

◆ cycle_start_

std::optional<std::size_t> Aleph::CA::Stationary_Detector::cycle_start_
private

Definition at line 755 of file ca-observer.H.

Referenced by cycle_start(), on_step_end(), and reset().

◆ hashes_

Array<std::uint64_t> Aleph::CA::Stationary_Detector::hashes_
private

Definition at line 752 of file ca-observer.H.

Referenced by buffered_hashes(), on_step_begin(), on_step_end(), push(), and reset().

◆ max_cycle_length_

std::size_t Aleph::CA::Stationary_Detector::max_cycle_length_ = 0
private

Definition at line 751 of file ca-observer.H.

Referenced by on_step_begin(), on_step_end(), push(), and spans_a_full_window_at_most().

◆ steps_

Array<std::size_t> Aleph::CA::Stationary_Detector::steps_
private

Definition at line 753 of file ca-observer.H.

Referenced by on_step_end(), push(), and reset().


The documentation for this class was generated from the following file: