Skip to main content

solverforge_scoring/stream/joiner/
equal.rs

1// Equal joiner for matching on property equality.
2
3use std::marker::PhantomData;
4
5use super::Joiner;
6
7// Marker for same-source joins where each unordered pair is considered once.
8pub struct Symmetric;
9
10// Marker for left/right joins where pair orientation is part of the semantics.
11pub struct Directed;
12
13/* Creates a joiner that matches when a property is equal on both sides.
14
15This is the primary joiner for self-joins where you're matching
16entities from the same collection on a shared property.
17
18# Example
19
20```
21use solverforge_scoring::stream::joiner::{Joiner, equal};
22
23#[derive(Clone)]
24struct Shift { employee_id: Option<usize>, start: i64 }
25
26// Match shifts with the same employee (self-join)
27let same_employee = equal(|s: &Shift| s.employee_id);
28
29let a = Shift { employee_id: Some(5), start: 0 };
30let b = Shift { employee_id: Some(5), start: 8 };
31let c = Shift { employee_id: Some(3), start: 16 };
32
33assert!(same_employee.matches(&a, &b));
34assert!(!same_employee.matches(&a, &c));
35```
36*/
37pub fn equal<A, T, F>(key: F) -> EqualJoiner<F, F, T, Symmetric>
38where
39    T: PartialEq,
40    F: Fn(&A) -> T + Clone + Send + Sync,
41{
42    EqualJoiner {
43        left: key.clone(),
44        right: key,
45        _phantom: PhantomData,
46    }
47}
48
49/* Creates a joiner that matches when extracted values are equal.
50
51Use this for cross-joins between different entity types.
52
53# Example
54
55```
56use solverforge_scoring::stream::joiner::{Joiner, equal_bi};
57
58#[derive(Clone)]
59struct Employee { id: usize, department: String }
60#[derive(Clone)]
61struct Task { assigned_to: usize, name: String }
62
63// Match employees to their assigned tasks
64let by_id = equal_bi(
65|e: &Employee| e.id,
66|t: &Task| t.assigned_to
67);
68
69let emp = Employee { id: 5, department: "Engineering".into() };
70let task1 = Task { assigned_to: 5, name: "Review".into() };
71let task2 = Task { assigned_to: 3, name: "Test".into() };
72
73assert!(by_id.matches(&emp, &task1));
74assert!(!by_id.matches(&emp, &task2));
75```
76*/
77pub fn equal_bi<A, B, T, Fa, Fb>(left: Fa, right: Fb) -> EqualJoiner<Fa, Fb, T, Directed>
78where
79    T: PartialEq,
80    Fa: Fn(&A) -> T + Send + Sync,
81    Fb: Fn(&B) -> T + Send + Sync,
82{
83    EqualJoiner {
84        left,
85        right,
86        _phantom: PhantomData,
87    }
88}
89
90/* A joiner that matches when extracted values are equal.
91
92Created by the [`equal()`] or [`equal_bi()`] functions.
93*/
94pub struct EqualJoiner<Fa, Fb, T, Mode = Directed> {
95    left: Fa,
96    right: Fb,
97    _phantom: PhantomData<(fn() -> T, fn() -> Mode)>,
98}
99
100impl<Fa, Fb, T, Mode> EqualJoiner<Fa, Fb, T, Mode> {
101    // Extracts the join key from an A entity.
102    #[inline]
103    pub fn key_a<A>(&self, a: &A) -> T
104    where
105        Fa: Fn(&A) -> T,
106    {
107        (self.left)(a)
108    }
109
110    // Extracts the join key from a B entity.
111    #[inline]
112    pub fn key_b<B>(&self, b: &B) -> T
113    where
114        Fb: Fn(&B) -> T,
115    {
116        (self.right)(b)
117    }
118
119    /* Consumes the joiner and returns the key extractors.
120
121    This is useful for zero-erasure constraint creation where
122    the key extractors need to be stored as concrete types.
123    */
124    #[inline]
125    pub fn into_keys(self) -> (Fa, Fb) {
126        (self.left, self.right)
127    }
128
129    // Returns references to the key extractors.
130    #[inline]
131    pub fn key_extractors(&self) -> (&Fa, &Fb) {
132        (&self.left, &self.right)
133    }
134}
135
136impl<A, B, T, Fa, Fb, Mode> Joiner<A, B> for EqualJoiner<Fa, Fb, T, Mode>
137where
138    T: PartialEq,
139    Fa: Fn(&A) -> T + Send + Sync,
140    Fb: Fn(&B) -> T + Send + Sync,
141{
142    #[inline]
143    fn matches(&self, a: &A, b: &B) -> bool {
144        (self.left)(a) == (self.right)(b)
145    }
146}