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    /// returns true if this partition contains all values in the given range
46    fn contains_all<R: RangeBounds<L::Value>>(&self, values: R) -> bool;
47
48    /// returns true if this partition has a non-empty intersection with the given range
49    fn contains_any<R: RangeBounds<L::Value>>(&self, values: R) -> bool;
50}
51
52pub trait PartitionWrite<L: Level> {
53    /// Inserts the value into the partition unless it already exists.
54    /// Returns `true` if the insertion occurred, `false` otherwise.
55    fn insert(&mut self, value: L::Value) -> bool;
56
57    /// Removes the value from the partition if it exists.
58    /// Returns `true` if the removal occurred, `false` otherwise.
59    fn remove(&mut self, value: L::Value) -> bool;
60
61    /// Removes a range of values from the partition.
62    fn remove_range<R: RangeBounds<L::Value>>(&mut self, values: R);
63}
64
65#[doc(hidden)]
66pub trait TruncateFrom<T> {
67    fn truncate_from(other: T) -> Self;
68}
69
70macro_rules! impl_truncate_from_usize {
71    ($($ty:ty),*) => {
72        $(
73            impl TruncateFrom<usize> for $ty {
74                #[inline(always)]
75                fn truncate_from(other: usize) -> Self {
76                    other.as_()
77                }
78            }
79        )*
80    };
81}
82impl_truncate_from_usize!(u32, u24, u16, u8);
83
84pub trait Optimizable {
85    /// Optimize memory usage. Should be run after batch inserts or before serialization.
86    fn optimize(&mut self);
87}
88
89pub trait Cut<Rhs = Self> {
90    type Out;
91
92    /// Returns the intersection between self and rhs while removing the
93    /// intersection from self
94    fn cut(&mut self, rhs: &Rhs) -> Self::Out;
95}
96
97pub trait DefaultFull {
98    fn full() -> Self;
99}
100
101pub trait Complement {
102    // self = !self
103    fn complement(&mut self);
104}