|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Detects fixed points and short cycles via frame hashing. More...
#include <ca-observer.H>
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_ |
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.
max_cycle_length + 1 hashes.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.
|
default |
Construct a detector with the default 16-frame window.
| This | function does not throw. |
|
inlineexplicit |
Construct a detector with an explicit window size.
| [in] | max_cycle_length | maximum cycle length to retain; 0 takes the default of 16, and SIZE_MAX keeps every frame, so that the window grows with the run. |
| This | function does not throw. |
Definition at line 806 of file ca-observer.H.
|
inlinenoexcept |
Return the number of buffered hashes.
| This | function does not throw. |
Definition at line 840 of file ca-observer.H.
References hashes_, and Aleph::Array< T >::size().
|
inlinenoexcept |
Return whether a cycle has been detected.
| This | function does not throw. |
Definition at line 813 of file ca-observer.H.
References cycle_length_.
|
inlinenoexcept |
Return the detected cycle length.
std::nullopt if none has been found. | This | function does not throw. |
Definition at line 822 of file ca-observer.H.
References cycle_length_.
|
inlinenoexcept |
Return the cycle start step.
std::nullopt if no cycle has been found. | This | function does not throw. |
Definition at line 831 of file ca-observer.H.
References cycle_start_.
|
inline |
Seed the detector with the initial frame hash.
| Lattice | lattice type. |
| [in] | step | step index about to run. |
| [in] | frame | frame before the step. |
| std::bad_alloc | if 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().
|
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.
| Lattice | lattice type. |
| [in] | step | completed step index. |
| [in] | frame | frame after the step. |
| std::bad_alloc | if 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_.
|
inlineprivate |
Definition at line 765 of file ca-observer.H.
References Aleph::Array< T >::append(), Aleph::close_gap(), h, hashes_, max_cycle_length_, Aleph::Array< T >::reserve(), Aleph::Array< T >::size(), and steps_.
Referenced by on_step_begin(), and on_step_end().
|
inlinenoexcept |
Clear buffered hashes and detected-cycle state.
| This | function does not throw. |
Definition at line 851 of file ca-observer.H.
References Aleph::Array< T >::clear(), cycle_length_, cycle_start_, hashes_, and steps_.
|
inlineprivatenoexcept |
Definition at line 789 of file ca-observer.H.
References max_cycle_length_.
Referenced by on_step_end().
|
private |
Definition at line 754 of file ca-observer.H.
Referenced by cycle_detected(), cycle_length(), on_step_end(), and reset().
|
private |
Definition at line 755 of file ca-observer.H.
Referenced by cycle_start(), on_step_end(), and reset().
|
private |
Definition at line 752 of file ca-observer.H.
Referenced by buffered_hashes(), on_step_begin(), on_step_end(), push(), and reset().
|
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().
|
private |
Definition at line 753 of file ca-observer.H.
Referenced by on_step_end(), push(), and reset().