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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![allow(clippy::type_complexity)]

use derive_new::new;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use std::hash::Hash;
use streaming_algorithms::{HyperLogLogMagnitude, SampleUnstable as SASampleUnstable, Top};

use super::{
	folder_par_sink, FolderSync, FolderSyncReducer, ParallelPipe, ParallelSink, SumFolder, SumZeroFolder
};

#[derive(new)]
#[must_use]
pub struct SampleUnstable<P> {
	pipe: P,
	samples: usize,
}

impl_par_dist! {
	impl<P: ParallelPipe<Item>, Item> ParallelSink<Item> for SampleUnstable<P>
	where
		P::Output: Send + 'static,
	{
		folder_par_sink!(
			SampleUnstableFolder,
			SumFolder<SASampleUnstable<P::Output>>,
			self,
			SampleUnstableFolder::new(self.samples),
			SumFolder::new()
		);
	}
}

#[derive(Clone, Serialize, Deserialize, new)]
pub struct SampleUnstableFolder {
	samples: usize,
}

impl<Item> FolderSync<Item> for SampleUnstableFolder {
	type Done = SASampleUnstable<Item>;

	fn zero(&mut self) -> Self::Done {
		SASampleUnstable::new(self.samples)
	}
	fn push(&mut self, state: &mut Self::Done, item: Item) {
		state.push(item, &mut thread_rng())
	}
}

#[derive(new)]
#[must_use]
pub struct MostFrequent<P> {
	pipe: P,
	n: usize,
	probability: f64,
	tolerance: f64,
}

impl_par_dist! {
	impl<P: ParallelPipe<Item>, Item> ParallelSink<Item> for MostFrequent<P>
	where
		P::Output: Clone + Hash + Eq + Send + 'static,
	{
		folder_par_sink!(
			MostFrequentFolder,
			SumZeroFolder<Top<P::Output, usize>>,
			self,
			MostFrequentFolder::new(self.n, self.probability, self.tolerance),
			SumZeroFolder::new(Top::new(self.n, self.probability, self.tolerance, ()))
		);
	}
}

#[derive(Clone, Serialize, Deserialize, new)]
pub struct MostFrequentFolder {
	n: usize,
	probability: f64,
	tolerance: f64,
}

impl<Item> FolderSync<Item> for MostFrequentFolder
where
	Item: Clone + Hash + Eq + Send + 'static,
{
	type Done = Top<Item, usize>;

	fn zero(&mut self) -> Self::Done {
		Top::new(self.n, self.probability, self.tolerance, ())
	}
	fn push(&mut self, state: &mut Self::Done, item: Item) {
		state.push(item, &1)
	}
}

#[derive(new)]
#[must_use]
pub struct MostDistinct<P> {
	pipe: P,
	n: usize,
	probability: f64,
	tolerance: f64,
	error_rate: f64,
}

impl_par_dist! {
	impl<P: ParallelPipe<Item, Output = (A, B)>, Item, A, B> ParallelSink<Item> for MostDistinct<P>
	where
		A: Clone + Hash + Eq + Send + 'static,
		B: Hash + 'static,
	{
		folder_par_sink!(
			MostDistinctFolder,
			SumZeroFolder<Top<A, HyperLogLogMagnitude<B>>>,
			self,
			MostDistinctFolder::new(self.n, self.probability, self.tolerance, self.error_rate),
			SumZeroFolder::new(Top::new(
				self.n,
				self.probability,
				self.tolerance,
				self.error_rate
			))
		);
	}
}

#[derive(Clone, Serialize, Deserialize, new)]
pub struct MostDistinctFolder {
	n: usize,
	probability: f64,
	tolerance: f64,
	error_rate: f64,
}

impl<A, B> FolderSync<(A, B)> for MostDistinctFolder
where
	A: Clone + Hash + Eq + Send + 'static,
	B: Hash + 'static,
{
	type Done = Top<A, HyperLogLogMagnitude<B>>;

	fn zero(&mut self) -> Self::Done {
		Top::new(self.n, self.probability, self.tolerance, self.error_rate)
	}
	fn push(&mut self, state: &mut Self::Done, item: (A, B)) {
		state.push(item.0, &item.1)
	}
}