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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#![allow(clippy::type_complexity)]

use amadeus_streaming::{
	HyperLogLogMagnitude, SampleUnstable as SASampleUnstable, Sort as SASort, Top
};
use derive_new::new;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use serde_closure::traits;
use std::{cmp::Ordering, hash::Hash};

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 State = SASampleUnstable<Item>;
	type Done = Self::State;

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

#[derive(new)]
#[must_use]
pub struct Sort<P, F> {
	pipe: P,
	f: F,
	n: usize,
}

impl_par_dist! {
	#[cfg_attr(not(nightly), serde_closure::desugar)]
	impl<P: ParallelPipe<Item>, F, Item> ParallelSink<Item> for Sort<P, F>
	where
		F: traits::Fn(&P::Output, &P::Output) -> Ordering + Clone + Send + 'static,
		P::Output: Clone + Send + 'static,
	{
		folder_par_sink!(
			SortFolder<F>,
			SumZeroFolder<SASort<P::Output, F>>,
			self,
			SortFolder::new(self.f.clone(), self.n),
			SumZeroFolder::new(SASort::new(self.f, self.n))
		);
	}
}

#[derive(Clone, Serialize, Deserialize, new)]
pub struct SortFolder<F> {
	f: F,
	n: usize,
}

#[cfg_attr(not(nightly), serde_closure::desugar)]
impl<Item, F> FolderSync<Item> for SortFolder<F>
where
	F: traits::Fn(&Item, &Item) -> Ordering + Clone,
{
	type State = SASort<Item, F>;
	type Done = Self::State;

	fn zero(&mut self) -> Self::Done {
		SASort::new(self.f.clone(), self.n)
	}
	fn push(&mut self, state: &mut Self::Done, item: Item) {
		state.push(item)
	}
	fn done(&mut self, state: Self::State) -> Self::Done {
		state
	}
}

#[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 State = Top<Item, usize>;
	type Done = Self::State;

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

#[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 State = Top<A, HyperLogLogMagnitude<B>>;
	type Done = Self::State;

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