splinter_rs/
traits.rs

1use std::ops::RangeBounds;
2
3use crate::level::Level;
4use num::cast::AsPrimitive;
5use u24::u24;
6
7pub trait PartitionRead<L: Level> {
8    /// the total number of values accessible via this partition.
9    fn cardinality(&self) -> usize;
10
11    /// returns true if this partition is empty
12    fn is_empty(&self) -> bool;
13
14    /// returns true if this partition contains the given value
15    fn contains(&self, value: L::Value) -> bool;
16
17    /// returns the 0-based position of the value in the partition if it exists,
18    /// otherwise returns None.
19    fn position(&self, value: L::Value) -> Option<usize>;
20
21    /// returns the number of values contained in this partition up to and
22    /// including the value.
23    fn rank(&self, value: L::Value) -> usize;
24
25    /// returns the value at position `idx`.
26    fn select(&self, idx: usize) -> Option<L::Value>;
27
28    /// returns the last value in the partition
29    fn last(&self) -> Option<L::Value>;
30
31    /// returns an iterator over all values in this partition
32    fn iter(&self) -> impl Iterator<Item = L::Value>;
33
34    /// returns an iterator over all values in this partition restricted by the provided range.
35    fn range<R>(&self, range: R) -> impl Iterator<Item = L::Value>
36    where
37        R: RangeBounds<L::Value> + Clone,
38    {
39        let r2 = range.clone();
40        self.iter()
41            .skip_while(move |s| !range.contains(s))
42            .take_while(move |s| r2.contains(s))
43    }
44}
45
46pub trait PartitionWrite<L: Level> {
47    /// Inserts the value into the partition unless it already exists.
48    /// Returns `true` if the insertion occurred, `false` otherwise.
49    fn insert(&mut self, value: L::Value) -> bool;
50
51    /// Removes the value from the partition if it exists.
52    /// Returns `true` if the removal occurred, `false` otherwise.
53    fn remove(&mut self, value: L::Value) -> bool;
54
55    /// Removes a range of values from the partition.
56    fn remove_range<R: RangeBounds<L::Value>>(&mut self, values: R);
57}
58
59#[doc(hidden)]
60pub trait TruncateFrom<T> {
61    fn truncate_from(other: T) -> Self;
62}
63
64macro_rules! impl_truncate_from_usize {
65    ($($ty:ty),*) => {
66        $(
67            impl TruncateFrom<usize> for $ty {
68                #[inline(always)]
69                fn truncate_from(other: usize) -> Self {
70                    other.as_()
71                }
72            }
73        )*
74    };
75}
76impl_truncate_from_usize!(u32, u24, u16, u8);
77
78pub trait Optimizable {
79    /// Optimize memory usage. Should be run after batch inserts or before serialization.
80    fn optimize(&mut self);
81}
82
83pub trait Cut<Rhs = Self> {
84    type Out;
85
86    /// Returns the intersection between self and rhs while removing the
87    /// intersection from self
88    fn cut(&mut self, rhs: &Rhs) -> Self::Out;
89}
90
91pub trait DefaultFull {
92    fn full() -> Self;
93}
94
95pub trait Complement {
96    // self = !self
97    fn complement(&mut self);
98}