1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::{
collections::{BTreeMap, HashMap},
hash::Hash,
};
use crate::{core::Reduce, CountMap, IsReduce, Observable, Op, Relation};
impl<C: Op> Relation<C>
where
C::D: Clone + Eq + Hash,
{
pub fn counts(
self,
) -> Relation<impl IsReduce<T = ((C::D, isize), isize), OM = HashMap<C::D, isize>>> {
self.map_h(|x| (x, ()))
.reduce_(|_, &n| n)
.type_named("counts")
}
pub fn distinct(self) -> Relation<impl Op<D = C::D>> {
self.map_h(|x| (x, ()))
.reduce_(|_, _: &isize| ())
.map_h(|(x, ())| x)
.type_named("distinct")
}
}
impl<C: Op<D = (K, V)>, K: Clone + Eq + Hash, V> Relation<C> {
#[allow(clippy::type_complexity)]
pub fn reduce_<M: CountMap<V> + Observable, Y: Clone + Eq, F: Fn(&K, &M) -> Y>(
self,
f: F,
) -> Relation<Reduce<K, V, C, M, Y, HashMap<K, Y>, F>> {
self.reduce_with_output_(f)
}
pub fn reduce<Y: Clone + Eq>(
self,
f: impl Fn(&K, &HashMap<V, isize>) -> Y,
) -> Relation<impl IsReduce<T = ((K, Y), isize), OM = HashMap<K, Y>>>
where
V: Eq + Hash,
{
self.reduce_::<HashMap<V, isize>, _, _>(f)
}
pub fn group_min(self) -> Relation<impl IsReduce<T = ((K, V), isize), OM = HashMap<K, V>>>
where
V: Clone + Ord,
{
self.reduce_(|_, m: &BTreeMap<V, isize>| m.keys().next().unwrap().clone())
.type_named("group_min")
}
pub fn group_max(self) -> Relation<impl IsReduce<T = ((K, V), isize), OM = HashMap<K, V>>>
where
V: Clone + Ord,
{
self.reduce_(|_, m: &BTreeMap<V, isize>| m.keys().next_back().unwrap().clone())
.type_named("group_max")
}
}