1use std::hash::Hash;
12use std::marker::PhantomData;
13
14use solverforge_core::score::Score;
15
16use super::collection_extract::{CollectionExtract, FlattenExtract};
17use super::existence_stream::{ExistenceMode, ExistsConstraintStream};
18use super::filter::UniFilter;
19use super::joiner::EqualJoiner;
20use super::relational::operator::{CollectionNode, ParentChildKeys, UnitKeys};
21use super::relational::view_plan::EntityKey;
22use super::uni_stream::UniConstraintStream;
23
24pub struct FlattenedCollectionTarget<S, P, B, EP, FP, Flatten, Sc>
28where
29 Sc: Score,
30{
31 pub(crate) right_stream: UniConstraintStream<S, P, EP, FP, Sc>,
32 pub(crate) flatten: Flatten,
33 pub(crate) _phantom: PhantomData<(fn() -> B, fn() -> Sc)>,
34}
35
36pub trait ExistenceTarget<S, A, EA, FA, Sc: Score>
39where
40 EA: CollectionExtract<S, Item = A>,
41 FA: UniFilter<S, A>,
42{
43 type Output;
45
46 fn apply(self, mode: ExistenceMode, extractor_a: EA, filter_a: FA) -> Self::Output;
48}
49
50impl<S, A, B, EA, FA, EB, FB, K, KA, KB, Mode, Sc> ExistenceTarget<S, A, EA, FA, Sc>
55 for (
56 UniConstraintStream<S, B, EB, FB, Sc>,
57 EqualJoiner<KA, KB, K, Mode>,
58 )
59where
60 S: Send + Sync + 'static,
61 A: Clone + Send + Sync + 'static,
62 B: Clone + Send + Sync + 'static,
63 EA: CollectionExtract<S, Item = A> + Send + Sync + 'static,
64 FA: UniFilter<S, A> + Send + Sync + 'static,
65 EB: CollectionExtract<S, Item = B> + Send + Sync + 'static,
66 FB: UniFilter<S, B> + Send + Sync + 'static,
67 K: Eq + Hash + Clone + Send + Sync + 'static,
68 KA: Fn(&A) -> K + Send + Sync + 'static,
69 KB: Fn(&B) -> K + Send + Sync + 'static,
70 Sc: Score + 'static,
71{
72 type Output = ExistsConstraintStream<
73 S,
74 A,
75 K,
76 CollectionNode<S, UniConstraintStream<S, A, EA, FA, Sc>>,
77 CollectionNode<S, UniConstraintStream<S, B, EB, FB, Sc>>,
78 EntityKey<KA>,
79 UnitKeys<EntityKey<KB>>,
80 Sc,
81 >;
82
83 fn apply(self, mode: ExistenceMode, extractor_a: EA, filter_a: FA) -> Self::Output {
84 let (right_stream, joiner) = self;
85 let (key_a, key_b) = joiner.into_keys();
86 let left_stream = UniConstraintStream::from_parts(extractor_a, filter_a);
87 let left = CollectionNode::new(left_stream, 0);
88 let right = CollectionNode::new(right_stream, 1);
89 ExistsConstraintStream::new(
90 mode,
91 left,
92 right,
93 EntityKey::new(key_a),
94 UnitKeys::new(EntityKey::new(key_b)),
95 )
96 }
97}
98
99impl<S, A, P, B, EA, FA, EP, FP, K, KA, KB, Mode, Flatten, Sc> ExistenceTarget<S, A, EA, FA, Sc>
104 for (
105 FlattenedCollectionTarget<S, P, B, EP, FP, Flatten, Sc>,
106 EqualJoiner<KA, KB, K, Mode>,
107 )
108where
109 S: Send + Sync + 'static,
110 A: Clone + Send + Sync + 'static,
111 P: Clone + Send + Sync + 'static,
112 B: Clone + Send + Sync + 'static,
113 EA: CollectionExtract<S, Item = A> + Send + Sync + 'static,
114 FA: UniFilter<S, A> + Send + Sync + 'static,
115 EP: CollectionExtract<S, Item = P> + Send + Sync + 'static,
116 FP: UniFilter<S, P> + Send + Sync + 'static,
117 K: Eq + Hash + Clone + Send + Sync + 'static,
118 KA: Fn(&A) -> K + Send + Sync + 'static,
119 KB: Fn(&B) -> K + Send + Sync + 'static,
120 Flatten: FlattenExtract<P, Item = B> + Send + Sync + 'static,
121 Sc: Score + 'static,
122{
123 type Output = ExistsConstraintStream<
124 S,
125 A,
126 K,
127 CollectionNode<S, UniConstraintStream<S, A, EA, FA, Sc>>,
128 CollectionNode<S, UniConstraintStream<S, P, EP, FP, Sc>>,
129 EntityKey<KA>,
130 ParentChildKeys<KB, Flatten>,
131 Sc,
132 >;
133
134 fn apply(self, mode: ExistenceMode, extractor_a: EA, filter_a: FA) -> Self::Output {
135 let (target, joiner) = self;
136 let FlattenedCollectionTarget {
137 right_stream,
138 flatten,
139 ..
140 } = target;
141 let (key_a, key_b) = joiner.into_keys();
142 let left_stream = UniConstraintStream::from_parts(extractor_a, filter_a);
143 let left = CollectionNode::new(left_stream, 0);
144 let right = CollectionNode::new(right_stream, 1);
145 ExistsConstraintStream::new(
146 mode,
147 left,
148 right,
149 EntityKey::new(key_a),
150 ParentChildKeys::new(key_b, flatten),
151 )
152 }
153}