Skip to main content

HaitsmaIndex

Struct HaitsmaIndex 

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

An in-memory inverted index over several Haitsma fingerprints.

Uses the same sub-fingerprint LUT strategy as HaitsmaMatcher: build u32 → Vec<(ref_id, frame_pos)> per frame, probe each query frame to discover candidate alignments, then verify the best per-reference BER.

§Performance

  • Build: O(Σ frames) — one LUT insertion per frame across all refs.
  • Query: O(Q + C × overlap) where Q = query frames probed, C = candidate refs with LUT hits, overlap = BER verification window.
  • Memory: 8 bytes × total_frames (LUT) plus 4 bytes × total_frames (per-ref frame clone for BER verification).

§Scoring note

Only exact sub-fingerprint matches are probed (no bit-flip neighbours). This is faster than the probe_bit_flips option on HaitsmaMatcher but may miss weaker matches. Prominence uses 0.5 / BER — not directly comparable to HaitsmaMatcher’s median_BER / (BER + ε) formula.

Implementations§

Source§

impl HaitsmaIndex

Source

pub fn build(refs: &[HaitsmaFingerprint], max_postings_per_hash: u32) -> Self

Build from a slice of Haitsma fingerprints.

max_postings_per_hash caps the size of each sub-fingerprint’s posting list. Hashes that appear in more than this many positions (silence / DC / highly repetitive content) are dropped entirely — the same TF-IDF-style stop-hash pruning used by WangIndex and PanakoIndex. This keeps query-time memory and work bounded on pathological catalogs (audit B7 / A1).

§Panics

Panics if refs.len() exceeds u32::MAX (reference ids are stored as u32).

Source

pub fn insert( &mut self, fp: &HaitsmaFingerprint, max_postings_per_hash: u32, ) -> usize

Append one fingerprint to the catalog, returning its stable ref_id.

Same stop-hash parity contract as WangIndex::insert: pruning the touched LUT lists yields exactly the map state a full build + retain would produce for the same append-only input sequence (pinned by the insert_matches_build_parity test; parity is over append-only histories — interleaved remove calls physically delete postings, which build has no equivalent for).

The reference’s full frame vector is cloned for BER verification (same as build); on vacated-slot reuse the old vector is replaced, freeing the previous allocation. Ids of live references never change.

§Performance

O(F) amortised for F frames: one LUT lookup per frame plus touched-list pruning. Pre-reserved map capacity and reused touched-key scratch keep steady-state insertion free of auxiliary allocation beyond posting pushes (and rare map growth).

§Panics

Panics if the catalog already holds u32::MAX live references (same bound HaitsmaIndex::build enforces).

Source

pub fn remove(&mut self, ref_id: usize) -> bool

Erase all postings for ref_id. Returns false if the id is out of range or already vacant (no-op).

Removal is physical: postings are deleted from every LUT list and the reference’s frame vector is freed immediately (replaced with an empty Vec), so the dominant HaitsmaIndex memory term — the per-ref frame clone (4 bytes × frames) — is returned on erase. query needs no liveness guard; throughput is bit-identical before and after removes. Ids of live references never shift.

§Performance

O(P) where P = total LUT postings: one in-place retain scan per list (memmove-compressed, no allocation) plus the frame-vec free. Lists are pre-scanned read-only for the id first (see WangIndex::remove): most lists skip the write pass.

Source

pub fn remove_many(&mut self, ref_ids: &[usize]) -> usize

Erase many references in a SINGLE LUT pass (plus their frame vectors). Returns the number of ids actually erased. Same batching rationale as WangIndex::remove_many: one O(P) pass instead of K. Vacated ids are pushed ascending (smallest reused first — documented).

Source

pub fn insert_many( &mut self, fps: &[HaitsmaFingerprint], max_postings_per_hash: u32, ) -> Vec<usize>

Append many fingerprints, returning their stable ref_ids in order. Reserves LUT capacity once for the whole batch; per-fingerprint semantics identical to HaitsmaIndex::insert.

Source

pub fn live_count(&self) -> usize

Live reference count (excludes ids erased by HaitsmaIndex::remove). O(1) — maintained incrementally.

Source

pub fn calibrated_score(&self, r: &MatchResult) -> f32

Estimated P(same recording | evidence) for a HaitsmaIndex::query result: same haitsma-v1 map as HaitsmaMatcher::calibrated_score.

Both paths report score = 1 − BER; their prominence formulas differ (0.5/BER here vs median_BER/(ber+ε) in the matcher) — see calibrated_haitsma for the bounded-error discussion. Raw fields untouched.

Source

pub fn estimated_bytes(&self) -> usize

Measured heap footprint in bytes: LUT posting lists (capacity × 8 bytes per (u32, u32) posting) + map slots + per-reference frame vectors (capacity × 4 bytes — the dominant term) + fps / live / vacant / touched storage. O(map size) — call rarely, not per query. Documented approximation (±table overhead, allocator rounding); for alerting and sharding, not billing.

Source

pub fn query( &self, query: &HaitsmaFingerprint, cfg: &HaitsmaMatchConfig, ) -> Option<(usize, MatchResult)>

Query the index, returning the best-matching (ref_id, result).

For each query frame, probes the LUT to gather candidate (ref_id, delta) pairs. Each candidate reference is then verified with the exact-BER path at up to the 8 most-hit candidate offsets (a repeated motif can concentrate hits at a wrong offset while the true alignment has the better BER).

Only exact sub-fingerprint matches are probed (no bit-flips in the index path — use match_ranked with explicit probe_bit_flips when recall under codec distortion matters).

Source

pub fn len(&self) -> usize

Return the number of unique sub-fingerprints (LUT keys) in the index.

Key-space, not catalog size — keys emptied by HaitsmaIndex::remove are retained. Use HaitsmaIndex::live_count for the reference count.

Source

pub fn is_empty(&self) -> bool

Return true if the index holds no LUT keys at all.

Stays false after every reference has been removed (removal leaves empty keys behind); use HaitsmaIndex::is_empty_catalog for the liveness test.

Source

pub fn is_empty_catalog(&self) -> bool

Return true if the catalog holds no live references.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send> ⓘ

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.