1use std::ops::RangeBounds;
2
3use crate::level::Level;
4use num::cast::AsPrimitive;
5use u24::u24;
6
7pub trait PartitionRead<L: Level> {
8 fn cardinality(&self) -> usize;
10
11 fn is_empty(&self) -> bool;
13
14 fn contains(&self, value: L::Value) -> bool;
16
17 fn rank(&self, value: L::Value) -> usize;
20
21 fn select(&self, idx: usize) -> Option<L::Value>;
23
24 fn last(&self) -> Option<L::Value>;
26
27 fn iter(&self) -> impl Iterator<Item = L::Value>;
29
30 fn range<R>(&self, range: R) -> impl Iterator<Item = L::Value>
32 where
33 R: RangeBounds<L::Value> + Clone,
34 {
35 let r2 = range.clone();
36 self.iter()
37 .skip_while(move |s| !range.contains(s))
38 .take_while(move |s| r2.contains(s))
39 }
40}
41
42pub trait PartitionWrite<L: Level> {
43 fn insert(&mut self, value: L::Value) -> bool;
46
47 fn remove(&mut self, value: L::Value) -> bool;
50}
51
52#[doc(hidden)]
53pub trait TruncateFrom<T> {
54 fn truncate_from(other: T) -> Self;
55}
56
57macro_rules! impl_truncate_from_usize {
58 ($($ty:ty),*) => {
59 $(
60 impl TruncateFrom<usize> for $ty {
61 #[inline(always)]
62 fn truncate_from(other: usize) -> Self {
63 other.as_()
64 }
65 }
66 )*
67 };
68}
69impl_truncate_from_usize!(u32, u24, u16, u8);
70
71pub trait Optimizable {
72 fn optimize(&mut self);
74}
75
76pub trait Merge<Rhs = Self> {
77 fn merge(&mut self, rhs: &Rhs);
79}
80
81pub trait Cut<Rhs = Self> {
82 type Out;
83
84 fn cut(&mut self, rhs: &Rhs) -> Self::Out;
87}