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
use either::Either;
use replace_with::replace_with_or_abort;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

use super::{DistributedIteratorMulti, DistributedReducer, ReduceFactory, Reducer, ReducerA};
use crate::pool::ProcessSend;

#[must_use]
pub struct Fold<I, ID, F, B> {
	i: I,
	identity: ID,
	op: F,
	marker: PhantomData<fn() -> B>,
}
impl<I, ID, F, B> Fold<I, ID, F, B> {
	pub(super) fn new(i: I, identity: ID, op: F) -> Self {
		Self {
			i,
			identity,
			op,
			marker: PhantomData,
		}
	}
}

impl<I: DistributedIteratorMulti<Source>, Source, ID, F, B> DistributedReducer<I, Source, B>
	for Fold<I, ID, F, B>
where
	ID: FnMut() -> B + Clone + ProcessSend,
	F: FnMut(B, Either<I::Item, B>) -> B + Clone + ProcessSend,
	B: ProcessSend,
	I::Item: 'static,
{
	type ReduceAFactory = FoldReducerFactory<I::Item, ID, F, B>;
	type ReduceA = FoldReducerA<I::Item, ID, F, B>;
	type ReduceB = FoldReducerB<I::Item, ID, F, B>;

	fn reducers(self) -> (I, Self::ReduceAFactory, Self::ReduceB) {
		(
			self.i,
			FoldReducerFactory(self.identity.clone(), self.op.clone(), PhantomData),
			FoldReducerB(Either::Left(self.identity), self.op, PhantomData),
		)
	}
}

pub struct FoldReducerFactory<A, ID, F, B>(ID, F, PhantomData<fn(A, B)>);

impl<A, ID, F, B> ReduceFactory for FoldReducerFactory<A, ID, F, B>
where
	ID: FnMut() -> B + Clone,
	F: FnMut(B, Either<A, B>) -> B + Clone,
{
	type Reducer = FoldReducerA<A, ID, F, B>;
	fn make(&self) -> Self::Reducer {
		FoldReducerA(Either::Left(self.0.clone()), self.1.clone(), PhantomData)
	}
}

#[derive(Serialize, Deserialize)]
#[serde(
	bound(serialize = "ID: Serialize, B: Serialize, F: Serialize"),
	bound(deserialize = "ID: Deserialize<'de>, B: Deserialize<'de>, F: Deserialize<'de>")
)]
pub struct FoldReducerA<A, ID, F, B>(Either<ID, B>, F, PhantomData<fn(A)>);

impl<A, ID, F, B> Reducer for FoldReducerA<A, ID, F, B>
where
	ID: FnMut() -> B + Clone,
	F: FnMut(B, Either<A, B>) -> B + Clone,
{
	type Item = A;
	type Output = B;

	#[inline(always)]
	fn push(&mut self, item: Self::Item) -> bool {
		replace_with_or_abort(&mut self.0, |self_0| {
			Either::Right(self_0.map_left(|mut identity| identity()).into_inner())
		});
		let self_1 = &mut self.1;
		replace_with_or_abort(&mut self.0, |self_0| {
			Either::Right((self_1)(self_0.right().unwrap(), Either::Left(item)))
		});
		true
	}
	fn ret(self) -> Self::Output {
		self.0.map_left(|mut identity| identity()).into_inner()
	}
}
impl<A, ID, F, B> ReducerA for FoldReducerA<A, ID, F, B>
where
	A: 'static,
	ID: FnMut() -> B + Clone + ProcessSend,
	F: FnMut(B, Either<A, B>) -> B + Clone + ProcessSend,
	B: ProcessSend,
{
	type Output = B;
}

#[derive(Serialize, Deserialize)]
#[serde(
	bound(serialize = "ID: Serialize, B: Serialize, F: Serialize"),
	bound(deserialize = "ID: Deserialize<'de>, B: Deserialize<'de>, F: Deserialize<'de>")
)]
pub struct FoldReducerB<A, ID, F, B>(Either<ID, B>, F, PhantomData<fn(A)>);

impl<A, ID, F, B> Clone for FoldReducerB<A, ID, F, B>
where
	ID: Clone,
	F: Clone,
{
	fn clone(&self) -> Self {
		Self(
			Either::Left(self.0.as_ref().left().unwrap().clone()),
			self.1.clone(),
			PhantomData,
		)
	}
}

impl<A, ID, F, B> Reducer for FoldReducerB<A, ID, F, B>
where
	ID: FnMut() -> B + Clone,
	F: FnMut(B, Either<A, B>) -> B + Clone,
{
	type Item = B;
	type Output = B;

	#[inline(always)]
	fn push(&mut self, item: Self::Item) -> bool {
		replace_with_or_abort(&mut self.0, |self_0| {
			Either::Right(self_0.map_left(|mut identity| identity()).into_inner())
		});
		let self_1 = &mut self.1;
		replace_with_or_abort(&mut self.0, |self_0| {
			Either::Right((self_1)(self_0.right().unwrap(), Either::Right(item)))
		});
		true
	}
	fn ret(self) -> Self::Output {
		self.0.map_left(|mut identity| identity()).into_inner()
	}
}