solverforge-scoring 0.19.14

Incremental constraint scoring for SolverForge
Documentation
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use super::group_view::GroupView;
use super::{Operator, PairDelta};
use crate::stream::collection_extract::FlattenExtract;
use crate::stream::collector::Accumulator;
use crate::stream::relational::count_state::KeyState;
use crate::stream::relational::view_plan::ViewKey;
use crate::stream::relational::{FastMap, HandleMap, Leaf, RowHandle};
use std::hash::Hash;
use std::marker::PhantomData;

/* Membership key source for one right-side row.

Plain rows contribute their single equality key; a parent row contributes the
multiset of its children's keys, read straight from the solution. Either way
the contribution is pushed into the node's reused scratch buffer — right-side
children never become rows and never receive handles.
*/
pub trait RightKeySet<V>: Send + Sync {
    type Key;

    fn push_keys(&self, view: &V, out: &mut Vec<Self::Key>);
}

/* One row, one key. */
pub struct UnitKeys<VK>(VK);
impl<VK> UnitKeys<VK> {
    pub fn new(inner: VK) -> Self {
        Self(inner)
    }
}
impl<V, VK, K> RightKeySet<V> for UnitKeys<VK>
where
    VK: ViewKey<V, Key = K> + Send + Sync,
    K: Clone,
{
    type Key = K;

    fn push_keys(&self, view: &V, out: &mut Vec<K>) {
        out.push(self.0.key(view));
    }
}

/* One parent row, its children's keys. The children are read through the
solution-borrowed flatten extractor, so a changed parent costs one pass over
its own elements and nothing is materialized. */
pub struct ParentChildKeys<KB, F> {
    key_b: KB,
    flatten: F,
}
impl<KB, F> ParentChildKeys<KB, F> {
    pub fn new(key_b: KB, flatten: F) -> Self {
        Self { key_b, flatten }
    }
}
impl<P, B, KB, F, K> RightKeySet<Leaf<'_, P>> for ParentChildKeys<KB, F>
where
    KB: Fn(&B) -> K + Send + Sync,
    F: FlattenExtract<P, Item = B> + Send + Sync,
    K: Clone,
{
    type Key = K;

    fn push_keys(&self, view: &Leaf<'_, P>, out: &mut Vec<K>) {
        for child in self.flatten.extract(view.entity) {
            out.push((self.key_b)(child));
        }
    }
}

/* Pushes one key per grouped row: the group's aggregated result, read through
the accumulator so grouped-existence keys off the aggregate like any other view
key. */
pub struct GroupRightKeys<RK>(RK);

impl<RK> GroupRightKeys<RK> {
    pub fn new(inner: RK) -> Self {
        Self(inner)
    }
}

impl<'a, S: 'static, O: Operator<S>, K, A: Accumulator<V, R>, V, R, RK>
    RightKeySet<GroupView<'a, S, O, K, A, V, R>> for GroupRightKeys<RK>
where
    RK: Fn(&GroupView<'a, S, O, K, A, V, R>) -> K + Send + Sync,
    K: Clone + 'static,
{
    type Key = K;

    fn push_keys(&self, view: &GroupView<'a, S, O, K, A, V, R>, out: &mut Vec<K>) {
        out.push((self.0)(view));
    }
}

/* Semi/anti join over concrete row producers, answered from per-key counts.

The node retains one occurrence count per right-side key, each right row's
stored key contribution, and one key bucket of left rows per key. A key whose
count crosses the alive/dead threshold publishes or retracts every left row
carrying it; nothing else changes. Left rows are re-keyed from the live
solution on insert, so a changed key moves the row between key domains without
pair bookkeeping. Only left rows are published, never scored pair rows.
*/
pub struct ExistenceNode<S, L, R, K, LK, RK> {
    left: L,
    right: R,
    left_key: LK,
    right_keys: RK,
    /// Per-key occurrence counts, left-row buckets and the current event's
    /// touched keys. The `usize`-keyed case stores dense arrays instead of
    /// hash tables, dispatched once at construction.
    keys: KeyState<K>,
    /// Per-event keys whose counts changed, in first-touch order.
    /// Emptied by the membership pass at the end of every event.
    touched: Vec<K>,
    /// Each right row's stored key contribution, for retraction without
    /// re-deriving keys from mutated values.
    right_stored: HandleMap<Vec<K>>,
    /// Each left row's current key.
    left_keys: HandleMap<K>,
    /// Whether each left row is published (its key alive matches `exists`),
    /// mirrored by membership in `accepted`.
    accepted: HandleMap<()>,
    /// Contribution buffers retired by a right retraction, reused by the next
    /// insert so each row keeps one buffer for the life of the search.
    retired: Vec<Vec<K>>,
    /// Per-event scratch for reading one row's key contribution.
    key_scratch: Vec<K>,
    /// Reused per-event notification buffers (left/right, removed/inserted)
    /// plus the published result buffers, so no event allocates.
    left_removed: Vec<RowHandle>,
    left_inserted: Vec<RowHandle>,
    right_removed: Vec<RowHandle>,
    right_inserted: Vec<RowHandle>,
    removed: Vec<RowHandle>,
    inserted: Vec<RowHandle>,
    /// Per-event scratch for membership-flip handles by polarity, so settled
    /// keys never allocate.
    flip_ins: Vec<RowHandle>,
    flip_rem: Vec<RowHandle>,
    exists: bool,
    marker: PhantomData<fn() -> S>,
}
impl<S: 'static, L: Operator<S>, R: Operator<S>, K, LK, RK> ExistenceNode<S, L, R, K, LK, RK>
where
    K: Eq + Hash + Clone + 'static,
    for<'a> LK: ViewKey<L::View<'a>, Key = K>,
    for<'a> RK: RightKeySet<R::View<'a>, Key = K>,
{
    pub fn new(left: L, right: R, left_key: LK, right_keys: RK, exists: bool) -> Self {
        Self {
            left,
            right,
            left_key,
            right_keys,
            keys: KeyState::new(),
            touched: Vec::new(),
            right_stored: HandleMap::new(),
            left_keys: HandleMap::new(),
            accepted: HandleMap::new(),
            retired: Vec::new(),
            key_scratch: Vec::new(),
            left_removed: Vec::new(),
            left_inserted: Vec::new(),
            right_removed: Vec::new(),
            right_inserted: Vec::new(),
            removed: Vec::new(),
            inserted: Vec::new(),
            flip_ins: Vec::new(),
            flip_rem: Vec::new(),
            exists,
            marker: PhantomData,
        }
    }
    fn notify(
        &mut self,
        solution: &S,
        descriptor: usize,
        index: usize,
        retract: bool,
        removed: &mut Vec<RowHandle>,
        inserted: &mut Vec<RowHandle>,
    ) {
        let mut left_removed = std::mem::take(&mut self.left_removed);
        let mut left_inserted = std::mem::take(&mut self.left_inserted);
        left_removed.clear();
        left_inserted.clear();
        let mut right_removed = std::mem::take(&mut self.right_removed);
        let mut right_inserted = std::mem::take(&mut self.right_inserted);
        right_removed.clear();
        right_inserted.clear();
        if retract {
            self.left.retract(
                solution,
                descriptor,
                index,
                &mut left_removed,
                &mut left_inserted,
            );
            self.right.retract(
                solution,
                descriptor,
                index,
                &mut right_removed,
                &mut right_inserted,
            );
        } else {
            self.left.insert(
                solution,
                descriptor,
                index,
                &mut left_removed,
                &mut left_inserted,
            );
            self.right.insert(
                solution,
                descriptor,
                index,
                &mut right_removed,
                &mut right_inserted,
            );
        }
        self.apply_changes(
            solution,
            PairDelta {
                left_removed: &left_removed,
                left_inserted: &left_inserted,
                right_removed: &right_removed,
                right_inserted: &right_inserted,
            },
            removed,
            inserted,
        );
        self.left_removed = left_removed;
        self.left_inserted = left_inserted;
        self.right_removed = right_removed;
        self.right_inserted = right_inserted;
    }
    /// Re-evaluate one touched key's left rows against the key's current
    /// membership. Idempotent per key, so an event may list a key once.
    /// Reads the bucket through a raw borrow and copies only the handles
    /// that flip, so an unchanged key's bucket is never copied.
    /// Applies one key's membership deltas to `self.accepted` and appends the
    /// flipped handles to the polarity scratch (`flip_ins` / `flip_rem`).
    fn settle_key(&mut self, key: &K) {
        let should = self.keys.alive(key) == self.exists;
        let bucket: &[RowHandle] = self.keys.left_bucket(key).unwrap_or(&[]);
        for &h in bucket.iter() {
            let settled = self.accepted.get(h).is_some() == should;
            if !settled {
                if should {
                    self.accepted.insert(h, ());
                    self.flip_ins.push(h);
                } else {
                    self.accepted.remove(h);
                    self.flip_rem.push(h);
                }
            }
        }
    }
    fn apply_changes(
        &mut self,
        solution: &S,
        delta: PairDelta<'_>,
        removed: &mut Vec<RowHandle>,
        inserted: &mut Vec<RowHandle>,
    ) {
        let PairDelta {
            left_removed,
            left_inserted,
            right_removed,
            right_inserted,
        } = delta;
        // Retract left rows first so the membership pass only touches live rows.
        for h in left_removed.iter().copied() {
            if let Some(key) = self.left_keys.remove(h) {
                self.keys.left_remove(&key, h);
            }
            if self.accepted.remove(h).is_some() {
                removed.push(h);
            }
        }
        self.keys.begin_event();
        for h in right_removed.iter().copied() {
            if let Some(mut keys) = self.right_stored.remove(h) {
                for k in keys.drain(..) {
                    self.keys.take(&k, 1);
                }
                keys.clear();
                self.retired.push(keys);
            }
        }
        for h in right_inserted.iter().copied() {
            let Some(row) = self.right.resolve(solution, h) else {
                continue;
            };
            let mut slot = self.retired.pop().unwrap_or_default();
            slot.clear();
            self.right_keys.push_keys(&row, &mut slot);
            for k in slot.iter() {
                self.keys.add(k, 1);
            }
            self.right_stored.insert(h, slot);
        }
        // One membership pass per touched key: counts are final here, so a
        // key removed and re-added inside one event settles on its final
        // state with no set bookkeeping.
        let mut touched = std::mem::take(&mut self.touched);
        touched.clear();
        self.keys.drain_touched(&mut touched);
        self.flip_ins.clear();
        self.flip_rem.clear();
        for k in touched.drain(..) {
            self.settle_key(&k);
        }
        self.touched = touched;
        removed.extend_from_slice(&self.flip_rem);
        inserted.extend_from_slice(&self.flip_ins);
        for h in left_inserted.iter().copied() {
            let Some(row) = self.left.resolve(solution, h) else {
                continue;
            };
            let key = self.left_key.key(&row);
            self.keys.left_insert(&key, h);
            self.left_keys.insert(h, key.clone());
            if self.keys.alive(&key) == self.exists {
                self.accepted.insert(h, ());
                inserted.push(h);
            }
        }
    }
}
impl<S: 'static, L: Operator<S>, R: Operator<S>, K, LK, RK> Operator<S>
    for ExistenceNode<S, L, R, K, LK, RK>
where
    K: Eq + Hash + Clone + 'static,
    LK: Send + Sync + 'static,
    RK: Send + Sync + 'static,
    for<'a> LK: ViewKey<L::View<'a>, Key = K>,
    for<'a> RK: RightKeySet<R::View<'a>, Key = K>,
{
    type View<'a> = L::View<'a>;
    type Evaluation = (L::Evaluation, R::Evaluation);
    fn prepare_evaluation(&self, solution: &S) -> Self::Evaluation {
        (
            self.left.prepare_evaluation(solution),
            self.right.prepare_evaluation(solution),
        )
    }
    fn visit_evaluation<'a>(
        &'a self,
        solution: &'a S,
        evaluation: &'a Self::Evaluation,
        visitor: &mut impl FnMut(Self::View<'a>),
    ) {
        let mut counts: FastMap<K, usize> = FastMap::default();
        let mut scratch = Vec::new();
        let right_keys = &self.right_keys;
        self.right
            .visit_evaluation(solution, &evaluation.1, &mut |row| {
                scratch.clear();
                right_keys.push_keys(&row, &mut scratch);
                for k in scratch.iter() {
                    *counts.entry(k.clone()).or_insert(0) += 1;
                }
            });
        let left_key = &self.left_key;
        self.left
            .visit_evaluation(solution, &evaluation.0, &mut |left| {
                if counts.contains_key(&left_key.key(&left)) == self.exists {
                    visitor(left);
                }
            });
    }
    fn clear(&mut self) {
        self.left.clear();
        self.right.clear();
        self.keys.clear();
        self.touched.clear();
        self.right_stored.clear();
        self.left_keys.clear();
        self.accepted.clear();
        self.retired.clear();
        self.key_scratch.clear();
        self.flip_ins.clear();
        self.flip_rem.clear();
        self.left_removed.clear();
        self.left_inserted.clear();
        self.right_removed.clear();
        self.right_inserted.clear();
        self.removed.clear();
        self.inserted.clear();
    }
    fn initialize(&mut self, solution: &S) {
        self.clear();
        self.left.initialize(solution);
        self.right.initialize(solution);
        let left_inserted = self.left.handles();
        let right_inserted = self.right.handles();
        let mut sink_removed = Vec::new();
        let mut sink_inserted = Vec::new();
        self.apply_changes(
            solution,
            PairDelta {
                left_removed: &[],
                left_inserted: &left_inserted,
                right_removed: &[],
                right_inserted: &right_inserted,
            },
            &mut sink_removed,
            &mut sink_inserted,
        );
    }
    fn handles(&self) -> Vec<RowHandle> {
        self.left
            .handles()
            .into_iter()
            .filter(|h| self.accepted.get(*h).is_some())
            .collect()
    }
    fn resolve<'a>(&'a self, solution: &'a S, h: RowHandle) -> Option<Self::View<'a>> {
        self.accepted.get(h)?;
        self.left.resolve(solution, h)
    }
    fn visit_provenance(&self, h: RowHandle, visitor: &mut impl FnMut(u32, usize, usize)) {
        if self.accepted.get(h).is_some() {
            self.left.visit_provenance(h, visitor);
        }
    }
    fn retract(
        &mut self,
        solution: &S,
        descriptor: usize,
        index: usize,
        removed: &mut Vec<RowHandle>,
        inserted: &mut Vec<RowHandle>,
    ) {
        removed.clear();
        inserted.clear();
        self.notify(solution, descriptor, index, true, removed, inserted);
    }
    fn insert(
        &mut self,
        solution: &S,
        descriptor: usize,
        index: usize,
        removed: &mut Vec<RowHandle>,
        inserted: &mut Vec<RowHandle>,
    ) {
        removed.clear();
        inserted.clear();
        self.notify(solution, descriptor, index, false, removed, inserted);
    }
}