Set

Struct Set 

Source
pub struct Set<T: Indexable> { /* private fields */ }
Expand description

An owned set optimized for read-heavy workloads, backed by a PGM-index.

This is a BTreeSet-like container that owns its data and provides efficient lookups using a learned index. Mutations are supported but trigger O(n) index rebuilds; for frequent updates use crate::Dynamic.

Works with any type that implements Indexable:

  • Numeric types (u64, i32, etc.) are indexed directly
  • String/bytes types use prefix extraction

§Example

use pgm_extra::Set;

// Numeric set
let nums: Vec<u64> = (0..10000).collect();
let set = Set::from_sorted_unique(nums, 64, 4).unwrap();
assert!(set.contains(&5000));

// String set
let words = vec!["apple", "banana", "cherry"];
let set = Set::from_sorted_unique(words, 64, 4).unwrap();
assert!(set.contains(&"banana"));

Implementations§

Source§

impl<T: Indexable + Ord> Set<T>
where T::Key: Ord,

Source

pub fn from_sorted_unique( data: Vec<T>, epsilon: usize, epsilon_recursive: usize, ) -> Result<Self, Error>

Create a set from pre-sorted, unique values.

§Panics

Debug builds will panic if values are not sorted or contain duplicates.

Source

pub fn build<I>( iter: I, epsilon: usize, epsilon_recursive: usize, ) -> Result<Self, Error>
where I: IntoIterator<Item = T>,

Create a set from an unsorted iterator.

Values are sorted and deduplicated (like BTreeSet::from_iter).

Source

pub fn empty(epsilon: usize, epsilon_recursive: usize) -> Self

Create an empty set with the given epsilon values.

Source

pub fn new(data: Vec<T>) -> Result<Self, Error>

Create a set with default epsilon values (64, 4).

Source

pub fn contains(&self, value: &T) -> bool

Check if the set contains the value.

Source

pub fn get(&self, value: &T) -> Option<&T>

Get a reference to the value if it exists.

Source

pub fn lower_bound(&self, value: &T) -> usize

Find the index of the first value >= the given value.

Source

pub fn upper_bound(&self, value: &T) -> usize

Find the index of the first value > the given value.

Source

pub fn range<R>(&self, range: R) -> impl DoubleEndedIterator<Item = &T>
where R: RangeBounds<T>,

Returns an iterator over values in the given range.

Source

pub fn first(&self) -> Option<&T>

Get the first (smallest) value.

Source

pub fn last(&self) -> Option<&T>

Get the last (largest) value.

Source

pub fn iter(&self) -> impl ExactSizeIterator<Item = &T> + DoubleEndedIterator

Iterate over all values in sorted order.

Source

pub fn len(&self) -> usize

Get the number of values.

Source

pub fn is_empty(&self) -> bool

Check if the set is empty.

Source

pub fn segments_count(&self) -> usize

Get the number of segments in the underlying index.

Source

pub fn height(&self) -> usize

Get the height of the underlying index.

Source

pub fn epsilon(&self) -> usize

Get the epsilon value.

Source

pub fn epsilon_recursive(&self) -> usize

Get the epsilon_recursive value.

Source

pub fn size_in_bytes(&self) -> usize

Approximate memory usage in bytes.

Source

pub fn as_slice(&self) -> &[T]

Get a reference to the underlying data slice.

Source

pub fn into_vec(self) -> Vec<T>

Consume the set and return the underlying data.

Source

pub fn index(&self) -> Option<&Static<T>>

Get a reference to the underlying index.

Source

pub fn insert(&mut self, value: T) -> bool

Insert a value into the set.

Returns true if the value was newly inserted, false if it already existed.

Note: This rebuilds the entire index, making it O(n) per insertion. For bulk insertions, prefer collecting into a new set or using extend. For frequent mutations, consider using index::owned::Dynamic directly.

Source

pub fn is_disjoint(&self, other: &Set<T>) -> bool

Returns true if self has no elements in common with other.

Source

pub fn is_subset(&self, other: &Set<T>) -> bool

Returns true if self is a subset of other.

Source

pub fn is_superset(&self, other: &Set<T>) -> bool

Returns true if self is a superset of other.

Source

pub fn difference<'a>( &'a self, other: &'a Set<T>, ) -> impl Iterator<Item = &'a T>

Returns an iterator over values in self but not in other.

Source

pub fn symmetric_difference<'a>( &'a self, other: &'a Set<T>, ) -> impl Iterator<Item = &'a T>

Returns an iterator over values in self or other but not both.

Source

pub fn intersection<'a>( &'a self, other: &'a Set<T>, ) -> impl Iterator<Item = &'a T>

Returns an iterator over values in both self and other.

Source

pub fn union<'a>(&'a self, other: &'a Set<T>) -> impl Iterator<Item = &'a T>

Returns an iterator over values in either self or other.

Trait Implementations§

Source§

impl<T: Indexable + Clone> Clone for Set<T>
where T::Key: Clone,

Source§

fn clone(&self) -> Self

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<T: Indexable + Debug> Debug for Set<T>

Source§

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

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

impl<'de, T> Deserialize<'de> for Set<T>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Indexable + Ord> Extend<T> for Set<T>
where T::Key: Ord,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends the set with elements from an iterator.

Note: This rebuilds the entire index, making it O(n) per call. For bulk insertions, prefer collecting into a new set. For frequent mutations, consider using index::owned::Dynamic directly.

Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Indexable + Ord> FromIterator<T> for Set<T>
where T::Key: Ord,

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a Set from an iterator.

Returns an empty set if the iterator is empty.

Source§

impl<T: Indexable + Hash> Hash for Set<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: Indexable> IntoIterator for &'a Set<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Indexable + Ord> IntoIterator for Set<T>
where T::Key: Ord,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Indexable + Ord> Ord for Set<T>
where T::Key: Ord,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: Indexable + Ord + PartialEq> PartialEq for Set<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Indexable + Ord + PartialOrd> PartialOrd for Set<T>
where T::Key: Ord,

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Serialize for Set<T>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Indexable + Ord + Eq> Eq for Set<T>

Auto Trait Implementations§

§

impl<T> Freeze for Set<T>

§

impl<T> RefUnwindSafe for Set<T>

§

impl<T> Send for Set<T>
where T: Send,

§

impl<T> Sync for Set<T>
where T: Sync,

§

impl<T> Unpin for Set<T>
where T: Unpin, <T as Indexable>::Key: Unpin,

§

impl<T> UnwindSafe for Set<T>
where T: UnwindSafe, <T as Indexable>::Key: UnwindSafe,

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,