Skip to main content

ZobristTable

Struct ZobristTable 

Source
pub struct ZobristTable { /* private fields */ }
Expand description

Zobrist hash table for O(1) incremental state hashing (Zobrist 1970).

Used by ZobristSnapshotter for efficient entropy detection. Instead of computing SHA-256 every frame (O(N)), we can update the hash incrementally when state changes (O(1)).

§Theory

Per Zobrist (1970): “A hash value can be updated incrementally by XORing out the old value and XORing in the new value.”

Per Tridgell (1999): “Rolling checksums enable efficient delta detection without full state comparison.”

§Examples

use jugar_web::trace::ZobristTable;

let table = ZobristTable::new(42);
let state = [0u8; 32];
let hash = table.hash_bytes(&state);

// Incremental update is O(1)
let new_hash = table.update_hash(hash, 0, 0, 255);
assert_ne!(hash, new_hash);

Implementations§

Source§

impl ZobristTable

Source

pub fn new(seed: u64) -> Self

Create table with deterministic RNG (for reproducibility).

Uses xorshift64 for fast, deterministic random generation. Table is heap-allocated (65KB) to avoid stack overflow.

§Panics

Cannot panic - the Vec-to-array conversion is guaranteed to succeed because we create exactly NUM_ZOBRIST_FIELDS elements.

Source

pub fn hash_bytes(&self, bytes: &[u8]) -> u64

Compute Zobrist hash for byte slice (O(N) - used once per state).

Source

pub fn update_hash( &self, hash: u64, field: usize, old_byte: u8, new_byte: u8, ) -> u64

Update hash incrementally when single byte changes (O(1)).

This is the key optimization - no need to rehash entire state.

Source

pub const fn entropy(hash1: u64, hash2: u64) -> u32

Calculate entropy as Hamming distance between two hashes.

Returns number of differing bits (0-64).

Trait Implementations§

Source§

impl Clone for ZobristTable

Source§

fn clone(&self) -> ZobristTable

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ZobristTable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ZobristTable

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V