Skip to main content

SparseClause

Struct SparseClause 

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

Sparse clause representation storing only active literal indices.

Achieves 5-100x memory reduction compared to dense Clause by storing only indices of features that affect evaluation.

§Memory Layout

Typical clause with 20 active literals:

  • include_indices: 32 × 2 = 64 bytes (inline SmallVec)
  • negated_indices: 32 × 2 = 64 bytes (inline SmallVec)
  • weight: 4 bytes
  • polarity: 1 byte
  • Total: ~133 bytes vs ~8000 bytes for dense (1000 features)

§Performance

Uses early-exit evaluation: returns false on first violation. For sparse input data, this is often faster than dense bitmask evaluation.

§Example

use tsetlin_rs::{Clause, SparseClause};

let mut clause = Clause::new(100, 100, 1);
// ... train clause ...

let sparse = SparseClause::from_clause(&clause);
assert!(sparse.memory_usage() < 200); // vs 800+ bytes dense

Implementations§

Source§

impl SparseClause

Source

pub fn from_clause(clause: &Clause) -> Self

Creates a sparse clause from a dense Clause by extracting active literals.

Scans automata and records indices where action() == true.

§Arguments
  • clause - Dense clause to convert
§Example
use tsetlin_rs::{Clause, SparseClause};

let clause = Clause::new(100, 100, 1);
let sparse = SparseClause::from_clause(&clause);
assert_eq!(sparse.n_literals(), 0); // Fresh clause has no active literals
Source

pub fn new(include: &[u16], negated: &[u16], weight: f32, polarity: i8) -> Self

Creates a sparse clause from raw components.

§Arguments
  • include - Feature indices requiring x[k] = 1
  • negated - Feature indices requiring x[k] = 0
  • weight - Clause weight
  • polarity - Vote direction (+1 or -1)
Source

pub const fn polarity(&self) -> i8

Returns clause polarity (+1 or -1).

Source

pub const fn weight(&self) -> f32

Returns clause weight.

Source

pub fn include_indices(&self) -> &[u16]

Returns indices of include literals.

Source

pub fn negated_indices(&self) -> &[u16]

Returns indices of negated literals.

Source

pub fn evaluate(&self, x: &[u8]) -> bool

Evaluates clause with early exit on first violation.

Returns true if all conditions are satisfied:

  • For each index in include_indices: x[idx] == 1
  • For each index in negated_indices: x[idx] == 0
§Arguments
  • x - Binary input vector
§Safety

Uses unchecked indexing for performance. Caller must ensure all stored indices are within x.len().

Source

pub fn evaluate_checked(&self, x: &[u8]) -> bool

Evaluates clause with bounds checking.

Safe version of evaluate that performs bounds checks.

Source

pub fn evaluate_packed(&self, x: &[u64]) -> bool

Evaluates using packed u64 input (64 features per word).

Optimized for cases where input is already packed. Uses bit extraction instead of array indexing.

§Arguments
  • x - Binary input packed as u64 words
Source

pub fn vote(&self, x: &[u8]) -> f32

Returns weighted vote: polarity × weight if clause fires, 0.0 otherwise.

Source

pub fn vote_unweighted(&self, x: &[u8]) -> i32

Returns unweighted vote: polarity if clause fires, 0 otherwise.

Source

pub fn memory_usage(&self) -> usize

Returns approximate memory usage in bytes.

Accounts for SmallVec inline vs heap allocation.

Source

pub fn n_literals(&self) -> usize

Returns total number of active literals.

Trait Implementations§

Source§

impl Clone for SparseClause

Source§

fn clone(&self) -> SparseClause

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 SparseClause

Source§

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

Formats the value using the given formatter. 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> 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