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
use crate::traits::{IndexExcl, IndexLowerBound};
use std::ops::Range;
pub struct Join<S>
where
S: IndexLowerBound<usize>,
{
remaining_range: Range<usize>,
store: S,
}
impl<S> Join<S>
where
S: IndexLowerBound<usize>,
{
pub fn from_parts(remaining_range: Range<usize>, store: S) -> Join<S> {
Join { remaining_range, store }
}
pub fn into_parts(self) -> (Range<usize>, S) {
(self.remaining_range, self.store)
}
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Option<(usize, <S as IndexExcl<usize>>::Item)> {
let next = self.store.lower_bound(self.remaining_range.start);
match next {
Some(idx) => {
self.remaining_range.start = idx + 1;
Some((idx, self.store.index(idx)))
}
None => None,
}
}
pub fn for_each<F>(&mut self, mut f: F)
where
F: FnMut(usize, S::Item),
{
while let Some((id, e)) = self.next() {
f(id, e);
}
}
pub fn until<F>(&mut self, mut f: F)
where
F: FnMut(usize, S::Item) -> bool,
{
while let Some((id, e)) = self.next() {
if !f(id, e) {
break;
}
}
}
}
pub trait IntoJoin {
type Store: IndexLowerBound<usize>;
fn into_join(self) -> Join<<Self as IntoJoin>::Store>;
}
pub trait IntoJoinExt: IntoJoin {
fn join_all<F>(self, f: F)
where
F: FnMut(usize, <Self::Store as IndexExcl<usize>>::Item),
Self: Sized,
{
self.into_join().for_each(f);
}
fn join_until<F>(self, f: F)
where
F: FnMut(usize, <Self::Store as IndexExcl<usize>>::Item) -> bool,
Self: Sized,
{
self.into_join().until(f);
}
}
impl<T: ?Sized> IntoJoinExt for T where T: IntoJoin {}
use shine_graph_macro::impl_intojoin_for_intojoin_tuple;
impl_intojoin_for_intojoin_tuple! {2,3,4,5,6,7,8,9,10}