pub struct Set64<T: Fits64>(_, _);
Expand description
A set type that can store any type that fits in a u64
. This set
type is very space-efficient in storing small or closely spaced
integers, while not being bad at storing large integers.
Major caveat The Set64
type defines iterators (drain()
and
iter()
) that iterate over T
rather than &T
. This is a break
with standard libray convention, and can be annoying if you are
translating code from HashSet
to Set64
. The motivation for
this is several-fold:
-
Set64
does not storeT
directly in its data structures (which would waste space), so there is no reference to the data to take. This does not make it impossible, but does mean we would have to fabricate aT
and return a reference to it, which is awkward and ugly. -
There is no inefficiency involved in returning
T
, since it is necessarily no larger than a pointer.
Examples
use tinyset::Set64;
let a: Set64<char> = "Hello world".chars().collect();
for x in "Hello world".chars() {
assert!(a.contains(&x));
}
for x in a {
assert!("Hello world".contains(x));
}
Storage details
Internally a Set64
is identical to a SetU64, so read there for
details. In short, small sets are the size of a pointer with no
heap storage. Densely packed sets are around a bit per member.
Intermediate sets have intermediate storage. The worst case
scenario is large integers widely spaced apart, in which case the
storage is similar to a std::collections::HashSet
.
Implementations
sourceimpl<T: Fits64> Set64<T>
impl<T: Fits64> Set64<T>
sourcepub fn with_capacity(_cap: usize) -> Self
pub fn with_capacity(_cap: usize) -> Self
Creates an empty set with the specified capacity.
sourcepub fn insert(&mut self, elem: T) -> bool
pub fn insert(&mut self, elem: T) -> bool
Adds a value to the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
sourcepub fn contains<R: Borrow<T>>(&self, value: R) -> bool
pub fn contains<R: Borrow<T>>(&self, value: R) -> bool
Returns true if the set contains a value.
Trait Implementations
sourceimpl<'a, 'b, T: Fits64> BitOr<&'b Set64<T>> for &'a Set64<T>
impl<'a, 'b, T: Fits64> BitOr<&'b Set64<T>> for &'a Set64<T>
sourcefn bitor(self, rhs: &Set64<T>) -> Set64<T>
fn bitor(self, rhs: &Set64<T>) -> Set64<T>
Returns the union of self
and rhs
as a new Set64<T>
.
Examples
use tinyset::Set64;
let a: Set64<u32> = vec![1, 2, 3].into_iter().collect();
let b: Set64<u32> = vec![3, 4, 5].into_iter().collect();
let set = &a | &b;
let mut i = 0;
let expected = [1, 2, 3, 4, 5];
for x in set {
assert!(expected.contains(&x));
i += 1;
}
assert_eq!(i, expected.len());
sourceimpl<T: Fits64> Extend<T> for Set64<T>
impl<T: Fits64> Extend<T> for Set64<T>
sourcefn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Adds a bunch of elements to the set
Examples
use tinyset::Set64;
let mut a: Set64<u32> = vec![1, 2, 3].into_iter().collect();
a.extend(vec![3, 4, 5]);
let mut i = 0;
let expected = [1, 2, 3, 4, 5];
for x in a {
assert!(expected.contains(&x));
i += 1;
}
assert_eq!(i, expected.len());
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T: Fits64> FromIterator<T> for Set64<T>
impl<T: Fits64> FromIterator<T> for Set64<T>
sourcefn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates a value from an iterator. Read more
sourceimpl<T: Fits64> IntoIterator for Set64<T>
impl<T: Fits64> IntoIterator for Set64<T>
type Item = T
type Item = T
The type of the elements being iterated over.
sourceimpl<'a, 'b, T: Fits64> Sub<&'b Set64<T>> for &'a Set64<T>
impl<'a, 'b, T: Fits64> Sub<&'b Set64<T>> for &'a Set64<T>
sourcefn sub(self, rhs: &Set64<T>) -> Set64<T>
fn sub(self, rhs: &Set64<T>) -> Set64<T>
Returns the difference of self
and rhs
as a new Set64<T>
.
Examples
use tinyset::Set64;
let a: Set64<u32> = vec![1, 2, 3].into_iter().collect();
let b: Set64<u32> = vec![3, 4, 5].into_iter().collect();
let set = &a - &b;
let mut i = 0;
let expected = [1, 2];
for x in set {
assert!(expected.contains(&x));
i += 1;
}
assert_eq!(i, expected.len());
impl<T: Fits64> Eq for Set64<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Set64<T> where
T: RefUnwindSafe,
impl<T> Send for Set64<T> where
T: Send,
impl<T> Sync for Set64<T> where
T: Sync,
impl<T> Unpin for Set64<T> where
T: Unpin,
impl<T> UnwindSafe for Set64<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more