streaming_algorithms/
traits.rs

1/// Union `Self` with `Rhs` in place.
2pub trait UnionAssign<Rhs = Self> {
3	/// Union.
4	fn union_assign(&mut self, rhs: Rhs);
5}
6impl<'a> UnionAssign<&'a u8> for u8 {
7	fn union_assign(&mut self, rhs: &'a Self) {
8		*self = (*self).max(*rhs);
9	}
10}
11impl<'a> UnionAssign<&'a u16> for u16 {
12	fn union_assign(&mut self, rhs: &'a Self) {
13		*self = (*self).max(*rhs);
14	}
15}
16impl<'a> UnionAssign<&'a u32> for u32 {
17	fn union_assign(&mut self, rhs: &'a Self) {
18		*self = (*self).max(*rhs);
19	}
20}
21impl<'a> UnionAssign<&'a u64> for u64 {
22	fn union_assign(&mut self, rhs: &'a Self) {
23		*self = (*self).max(*rhs);
24	}
25}
26impl UnionAssign for usize {
27	fn union_assign(&mut self, rhs: Self) {
28		*self = (*self).max(rhs);
29	}
30}
31impl<'a> UnionAssign<&'a usize> for usize {
32	fn union_assign(&mut self, rhs: &'a Self) {
33		*self = (*self).max(*rhs);
34	}
35}
36
37/// Intersect zero or more `&Self` to create `Option<Self>`.
38pub trait Intersect {
39	/// Intersect.
40	fn intersect<'a>(iter: impl Iterator<Item = &'a Self>) -> Option<Self>
41	where
42		Self: Sized + 'a;
43}
44impl Intersect for u8 {
45	fn intersect<'a>(iter: impl Iterator<Item = &'a Self>) -> Option<Self>
46	where
47		Self: Sized + 'a,
48	{
49		iter.cloned().min()
50	}
51}
52impl Intersect for u16 {
53	fn intersect<'a>(iter: impl Iterator<Item = &'a Self>) -> Option<Self>
54	where
55		Self: Sized + 'a,
56	{
57		iter.cloned().min()
58	}
59}
60impl Intersect for u32 {
61	fn intersect<'a>(iter: impl Iterator<Item = &'a Self>) -> Option<Self>
62	where
63		Self: Sized + 'a,
64	{
65		iter.cloned().min()
66	}
67}
68impl Intersect for u64 {
69	fn intersect<'a>(iter: impl Iterator<Item = &'a Self>) -> Option<Self>
70	where
71		Self: Sized + 'a,
72	{
73		iter.cloned().min()
74	}
75}
76impl Intersect for usize {
77	fn intersect<'a>(iter: impl Iterator<Item = &'a Self>) -> Option<Self>
78	where
79		Self: Sized + 'a,
80	{
81		iter.cloned().min()
82	}
83}
84
85/// New instances are instantiable given a specified input of `<Self as New>::Config`.
86pub trait New {
87	/// The type of data required to instantiate a new `Self`.
88	type Config: Clone;
89	/// Instantiate a new `Self` with the given `<Self as New>::Config`.
90	fn new(config: &Self::Config) -> Self;
91}
92impl New for u8 {
93	type Config = ();
94	fn new(_config: &Self::Config) -> Self {
95		0
96	}
97}
98impl New for u16 {
99	type Config = ();
100	fn new(_config: &Self::Config) -> Self {
101		0
102	}
103}
104impl New for u32 {
105	type Config = ();
106	fn new(_config: &Self::Config) -> Self {
107		0
108	}
109}
110impl New for u64 {
111	type Config = ();
112	fn new(_config: &Self::Config) -> Self {
113		0
114	}
115}
116impl New for usize {
117	type Config = ();
118	fn new(_config: &Self::Config) -> Self {
119		0
120	}
121}
122
123/// An optimisation for cases like putting a HyperLogLog inside a Count–min sketch, where intersecting, adding a val, and then unioning that with counters is the same as simply adding the val to the counters.
124pub trait IntersectPlusUnionIsPlus {
125	/// Apply optimisation or not
126	const VAL: bool;
127}
128
129macro_rules! impl_ipuip {
130	($($t:ty)*) => ($(
131		impl IntersectPlusUnionIsPlus for $t {
132			const VAL: bool = false;
133		}
134	)*)
135}
136
137impl_ipuip!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);