pub struct PATCH<const KEY_LEN: usize, O = IdentitySchema, V = ()>where
O: KeySchema<KEY_LEN>,{ /* private fields */ }Expand description
A PATCH is a persistent data structure that stores a set of keys. Each key can be reordered and segmented, based on the provided key ordering and segmentation.
The patch supports efficient set operations, like union, intersection, and difference, because it efficiently maintains a hash for all keys that are part of a sub-tree.
The tree itself is a path- and node-compressed a 256-ary trie. Each nodes stores its children in a byte oriented cuckoo hash table, allowing for O(1) access to children, while keeping the memory overhead low. Table sizes are powers of two, starting at 2.
Having a single node type for all branching factors simplifies the implementation, compared to other adaptive trie implementations, like ARTs or Judy Arrays
The PATCH allows for cheap copy-on-write operations, with clone being O(1).
Implementations§
Source§impl<const KEY_LEN: usize, O, V> PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
impl<const KEY_LEN: usize, O, V> PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
Sourcepub fn insert(&mut self, entry: &Entry<KEY_LEN, V>)
pub fn insert(&mut self, entry: &Entry<KEY_LEN, V>)
Inserts a shared key into the PATCH.
Takes an Entry object that can be created from a key, and inserted into multiple PATCH instances.
If the key is already present, this is a no-op.
Sourcepub fn replace(&mut self, entry: &Entry<KEY_LEN, V>)
pub fn replace(&mut self, entry: &Entry<KEY_LEN, V>)
Inserts a key into the PATCH, replacing the value if it already exists.
Sourcepub fn remove(&mut self, key: &[u8; KEY_LEN])
pub fn remove(&mut self, key: &[u8; KEY_LEN])
Removes a key from the PATCH.
If the key is not present, this is a no-op.
Sourcepub fn get(&self, key: &[u8; KEY_LEN]) -> Option<&V>
pub fn get(&self, key: &[u8; KEY_LEN]) -> Option<&V>
Returns the value associated with key if present.
Sourcepub fn infixes<const PREFIX_LEN: usize, const INFIX_LEN: usize, F>(
&self,
prefix: &[u8; PREFIX_LEN],
for_each: F,
)
pub fn infixes<const PREFIX_LEN: usize, const INFIX_LEN: usize, F>( &self, prefix: &[u8; PREFIX_LEN], for_each: F, )
Allows iteratig over all infixes of a given length with a given prefix. Each infix is passed to the provided closure.
The entire operation is performed over the tree view ordering of the keys.
The length of the prefix and the infix is provided as type parameters, but will usually inferred from the arguments.
The sum of PREFIX_LEN and INFIX_LEN must be less than or equal to KEY_LEN
or a compile-time assertion will fail.
Because all infixes are iterated in one go, less bookkeeping is required, than when using an Iterator, allowing for better performance.
Sourcepub fn has_prefix<const PREFIX_LEN: usize>(
&self,
prefix: &[u8; PREFIX_LEN],
) -> bool
pub fn has_prefix<const PREFIX_LEN: usize>( &self, prefix: &[u8; PREFIX_LEN], ) -> bool
Returns true if the PATCH has a key with the given prefix.
PREFIX_LEN must be less than or equal to KEY_LEN or a compile-time
assertion will fail.
Sourcepub fn segmented_len<const PREFIX_LEN: usize>(
&self,
prefix: &[u8; PREFIX_LEN],
) -> u64
pub fn segmented_len<const PREFIX_LEN: usize>( &self, prefix: &[u8; PREFIX_LEN], ) -> u64
Returns the number of unique segments in keys with the given prefix.
Sourcepub fn iter<'a>(&'a self) -> PATCHIterator<'a, KEY_LEN, O, V> ⓘ
pub fn iter<'a>(&'a self) -> PATCHIterator<'a, KEY_LEN, O, V> ⓘ
Iterates over all keys in the PATCH. The keys are returned in key ordering but random order.
Sourcepub fn iter_ordered<'a>(&'a self) -> PATCHOrderedIterator<'a, KEY_LEN, O, V> ⓘ
pub fn iter_ordered<'a>(&'a self) -> PATCHOrderedIterator<'a, KEY_LEN, O, V> ⓘ
Iterates over all keys in the PATCH in key order.
The traversal visits every key in lexicographic key order, without
accepting a prefix filter. For prefix-aware iteration, see
PATCH::iter_prefix_count.
Sourcepub fn iter_prefix_count<'a, const PREFIX_LEN: usize>(
&'a self,
) -> PATCHPrefixIterator<'a, KEY_LEN, PREFIX_LEN, O, V> ⓘ
pub fn iter_prefix_count<'a, const PREFIX_LEN: usize>( &'a self, ) -> PATCHPrefixIterator<'a, KEY_LEN, PREFIX_LEN, O, V> ⓘ
Iterate over all prefixes of the given length in the PATCH. The prefixes are naturally returned in tree ordering and tree order. A count of the number of elements for the given prefix is also returned.
Sourcepub fn union(&mut self, other: Self)
pub fn union(&mut self, other: Self)
Unions this PATCH with another PATCH.
The other PATCH is consumed, and this PATCH is updated in place.
Sourcepub fn intersect(&self, other: &Self) -> Self
pub fn intersect(&self, other: &Self) -> Self
Intersects this PATCH with another PATCH.
Returns a new PATCH that contains only the keys that are present in both PATCHes.
Sourcepub fn difference(&self, other: &Self) -> Self
pub fn difference(&self, other: &Self) -> Self
Returns the difference between this PATCH and another PATCH.
Returns a new PATCH that contains only the keys that are present in this PATCH, but not in the other PATCH.
Sourcepub fn debug_branch_fill(&self) -> [f32; 8]
pub fn debug_branch_fill(&self) -> [f32; 8]
Calculates the average fill level for branch nodes grouped by their
branching factor. The returned array contains eight entries for branch
sizes 2, 4, 8, 16, 32, 64, 128 and 256 in that order.
Source§impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> PATCH<KEY_LEN, O, V>
impl<const KEY_LEN: usize, O: KeySchema<KEY_LEN>, V> PATCH<KEY_LEN, O, V>
Sourcepub fn into_iter_ordered(self) -> PATCHIntoOrderedIterator<KEY_LEN, O, V> ⓘ
pub fn into_iter_ordered(self) -> PATCHIntoOrderedIterator<KEY_LEN, O, V> ⓘ
Consume and return an iterator that yields keys in key order.
Trait Implementations§
Source§impl<'a, S: ValueSchema> ContainsConstraint<'a, S> for &'a PATCH<VALUE_LEN, IdentitySchema, ()>
impl<'a, S: ValueSchema> ContainsConstraint<'a, S> for &'a PATCH<VALUE_LEN, IdentitySchema, ()>
Source§type Constraint = PatchValueConstraint<'a, S>
type Constraint = PatchValueConstraint<'a, S>
has.Source§impl<'a, S: ValueSchema> ContainsConstraint<'a, S> for PATCH<ID_LEN, IdentitySchema, ()>
impl<'a, S: ValueSchema> ContainsConstraint<'a, S> for PATCH<ID_LEN, IdentitySchema, ()>
Source§type Constraint = PatchIdConstraint<S>
type Constraint = PatchIdConstraint<S>
has.Source§impl<const KEY_LEN: usize, O, V> Default for PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
impl<const KEY_LEN: usize, O, V> Default for PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
Source§impl<'a, const KEY_LEN: usize, O, V> IntoIterator for &'a PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
impl<'a, const KEY_LEN: usize, O, V> IntoIterator for &'a PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
Source§impl<const KEY_LEN: usize, O, V> PartialEq for PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
impl<const KEY_LEN: usize, O, V> PartialEq for PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
impl<const KEY_LEN: usize, O, V> Eq for PATCH<KEY_LEN, O, V>where
O: KeySchema<KEY_LEN>,
Auto Trait Implementations§
impl<const KEY_LEN: usize, O, V> Freeze for PATCH<KEY_LEN, O, V>
impl<const KEY_LEN: usize, O, V> RefUnwindSafe for PATCH<KEY_LEN, O, V>
impl<const KEY_LEN: usize, O, V> Send for PATCH<KEY_LEN, O, V>
impl<const KEY_LEN: usize, O, V> Sync for PATCH<KEY_LEN, O, V>
impl<const KEY_LEN: usize, O, V> Unpin for PATCH<KEY_LEN, O, V>
impl<const KEY_LEN: usize, O, V> UnsafeUnpin for PATCH<KEY_LEN, O, V>
impl<const KEY_LEN: usize, O, V> UnwindSafe for PATCH<KEY_LEN, O, V>
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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