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>
impl<T: Indexable + Ord> Set<T>
Sourcepub fn from_sorted_unique(
data: Vec<T>,
epsilon: usize,
epsilon_recursive: usize,
) -> Result<Self, Error>
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.
Sourcepub fn build<I>(
iter: I,
epsilon: usize,
epsilon_recursive: usize,
) -> Result<Self, Error>where
I: IntoIterator<Item = T>,
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).
Sourcepub fn empty(epsilon: usize, epsilon_recursive: usize) -> Self
pub fn empty(epsilon: usize, epsilon_recursive: usize) -> Self
Create an empty set with the given epsilon values.
Sourcepub fn new(data: Vec<T>) -> Result<Self, Error>
pub fn new(data: Vec<T>) -> Result<Self, Error>
Create a set with default epsilon values (64, 4).
Sourcepub fn lower_bound(&self, value: &T) -> usize
pub fn lower_bound(&self, value: &T) -> usize
Find the index of the first value >= the given value.
Sourcepub fn upper_bound(&self, value: &T) -> usize
pub fn upper_bound(&self, value: &T) -> usize
Find the index of the first value > the given value.
Sourcepub fn range<R>(&self, range: R) -> impl DoubleEndedIterator<Item = &T>where
R: RangeBounds<T>,
pub fn range<R>(&self, range: R) -> impl DoubleEndedIterator<Item = &T>where
R: RangeBounds<T>,
Returns an iterator over values in the given range.
Sourcepub fn iter(&self) -> impl ExactSizeIterator<Item = &T> + DoubleEndedIterator
pub fn iter(&self) -> impl ExactSizeIterator<Item = &T> + DoubleEndedIterator
Iterate over all values in sorted order.
Sourcepub fn segments_count(&self) -> usize
pub fn segments_count(&self) -> usize
Get the number of segments in the underlying index.
Sourcepub fn epsilon_recursive(&self) -> usize
pub fn epsilon_recursive(&self) -> usize
Get the epsilon_recursive value.
Sourcepub fn size_in_bytes(&self) -> usize
pub fn size_in_bytes(&self) -> usize
Approximate memory usage in bytes.
Sourcepub fn insert(&mut self, value: T) -> bool
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.
Sourcepub fn is_disjoint(&self, other: &Set<T>) -> bool
pub fn is_disjoint(&self, other: &Set<T>) -> bool
Returns true if self has no elements in common with other.
Sourcepub fn is_superset(&self, other: &Set<T>) -> bool
pub fn is_superset(&self, other: &Set<T>) -> bool
Returns true if self is a superset of other.
Sourcepub fn difference<'a>(
&'a self,
other: &'a Set<T>,
) -> impl Iterator<Item = &'a T>
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.
Sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a Set<T>,
) -> impl Iterator<Item = &'a T>
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.
Sourcepub fn intersection<'a>(
&'a self,
other: &'a Set<T>,
) -> impl Iterator<Item = &'a T>
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.
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Set<T>
impl<'de, T> Deserialize<'de> for Set<T>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: Indexable + Ord> Extend<T> for Set<T>
impl<T: Indexable + Ord> Extend<T> for Set<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
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)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T: Indexable + Ord> FromIterator<T> for Set<T>
impl<T: Indexable + Ord> FromIterator<T> for Set<T>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
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<'a, T: Indexable> IntoIterator for &'a Set<T>
impl<'a, T: Indexable> IntoIterator for &'a Set<T>
Source§impl<T: Indexable + Ord> Ord for Set<T>
impl<T: Indexable + Ord> Ord for Set<T>
Source§impl<T: Indexable + Ord + PartialOrd> PartialOrd for Set<T>
impl<T: Indexable + Ord + PartialOrd> PartialOrd for Set<T>
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>
impl<T> UnwindSafe for Set<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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