use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use super::stream::Stream;
use crate::kernel::{Effect, succeed};
struct Side<A, E, R> {
stream: Option<Stream<A, E, R>>,
buf: VecDeque<A>,
}
impl<A, E, R> Side<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
fn new(stream: Stream<A, E, R>) -> Self {
Self {
stream: Some(stream),
buf: VecDeque::new(),
}
}
fn done(&self) -> bool {
self.stream.is_none()
}
fn finished(&self) -> bool {
self.done() && self.buf.is_empty()
}
fn refill(mut self, n: usize) -> Effect<Self, E, R> {
if !self.buf.is_empty() {
return succeed(self);
}
match self.stream.take() {
None => succeed(self),
Some(s) => s.pull_raw(n).map(move |step| {
if let Some((items, rest)) = step {
self.buf.extend(items);
self.stream = Some(rest);
}
self
}),
}
}
}
fn two_sided<A, B, C, S, F, E, R>(
left: Side<A, E, R>,
right: Side<B, E, R>,
state: S,
emit: F,
) -> Stream<C, E, R>
where
A: Send + 'static,
B: Send + 'static,
C: Send + 'static,
S: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
F: FnMut(&mut Side<A, E, R>, &mut Side<B, E, R>, &mut S) -> Vec<C> + Send + 'static,
{
Stream::from_step(move |n| {
left.refill(n).flat_map(move |left| {
right.refill(n).map(move |right| {
let (mut left, mut right, mut state, mut emit) = (left, right, state, emit);
if left.finished() && right.finished() {
return None;
}
let out = emit(&mut left, &mut right, &mut state);
Some((out, two_sided(left, right, state, emit)))
})
})
})
}
impl<A, E, R> Stream<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
#[must_use]
pub fn merge(self, other: Self) -> Self {
two_sided(
Side::new(self),
Side::new(other),
true,
|left, right, turn_left: &mut bool| {
let mut out = Vec::new();
loop {
let (mine, theirs) = if *turn_left {
(&mut *left, &mut *right)
} else {
(&mut *right, &mut *left)
};
if let Some(x) = mine.buf.pop_front() {
out.push(x);
*turn_left = !*turn_left;
} else if mine.done() {
match theirs.buf.pop_front() {
Some(x) => out.push(x),
None => break,
}
} else {
break;
}
}
out
},
)
}
}
pub fn combine_latest<A, B, E, R>(
left: Stream<A, E, R>,
right: Stream<B, E, R>,
) -> Stream<(A, B), E, R>
where
A: Clone + Send + 'static,
B: Clone + Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
two_sided(
Side::new(left),
Side::new(right),
(None::<A>, None::<B>),
|left, right, (latest_a, latest_b)| {
let mut out = Vec::new();
for a in left.buf.drain(..) {
if let Some(b) = latest_b.as_ref() {
out.push((a.clone(), b.clone()));
}
*latest_a = Some(a);
}
for b in right.buf.drain(..) {
if let Some(a) = latest_a.as_ref() {
out.push((a.clone(), b.clone()));
}
*latest_b = Some(b);
}
out
},
)
}
pub fn keyed_join<K, A, B, E, R>(
left: Stream<(K, A), E, R>,
right: Stream<(K, B), E, R>,
) -> Stream<(K, A, B), E, R>
where
K: Clone + Hash + Eq + Send + 'static,
A: Clone + Send + 'static,
B: Clone + Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
two_sided(
Side::new(left),
Side::new(right),
(HashMap::<K, A>::new(), HashMap::<K, B>::new()),
|left, right, (left_map, right_map)| {
let mut out = Vec::new();
for (k, a) in left.buf.drain(..) {
if let Some(b) = right_map.get(&k) {
out.push((k.clone(), a.clone(), b.clone()));
}
left_map.insert(k, a);
}
for (k, b) in right.buf.drain(..) {
if let Some(a) = left_map.get(&k) {
out.push((k.clone(), a.clone(), b.clone()));
}
right_map.insert(k, b);
}
out
},
)
}