mesh-sieve 4.0.1

Modular, high-performance Rust library for mesh and data management, designed for scientific computing and PDE codes.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
//! In-memory oriented Sieve: stores (dst, payload, orientation) per arrow.
//! Implements both `Sieve` (payload-only view) and `OrientedSieve`.

use once_cell::sync::OnceCell;
use std::collections::HashMap;
use std::sync::Arc;

use super::build_ext::SieveBuildExt;
use super::mutable::MutableSieve;
use super::oriented::{Orientation, OrientedSieve};
use super::query_ext::SieveQueryExt;
use super::sieve_trait::Sieve;
use crate::mesh_error::MeshSieveError;
use crate::topology::_debug_invariants::debug_invariants;
use crate::topology::bounds::{PayloadLike, PointLike};
use crate::topology::cache::InvalidateCache;
use crate::topology::orientation::Sign;
use crate::topology::sieve::strata::{StrataCache, compute_strata};

#[derive(Clone, Debug)]
pub struct InMemoryOrientedSieve<P, T = (), O = Sign>
where
    P: PointLike,
    O: Orientation,
{
    pub adjacency_out: HashMap<P, Vec<(P, T, O)>>,
    pub adjacency_in: HashMap<P, Vec<(P, T, O)>>,
    pub strata: OnceCell<StrataCache<P>>,
}

impl<P, T, O> InMemoryOrientedSieve<P, Arc<T>, O>
where
    P: PointLike,
    O: super::oriented::Orientation + PartialEq + std::fmt::Debug,
{
    /// Insert by value; wraps once into `Arc<T>`.
    #[inline]
    pub fn add_arrow_val(&mut self, src: P, dst: P, payload: T) {
        self.add_arrow(src, dst, Arc::new(payload));
    }

    /// Oriented insert by value; wraps once into `Arc<T>`.
    #[inline]
    pub fn add_arrow_o_val(&mut self, src: P, dst: P, payload: T, orient: O) {
        self.add_arrow_o(src, dst, Arc::new(payload), orient);
    }
}

impl<P, T, O> Default for InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    O: Orientation + PartialEq + std::fmt::Debug,
{
    fn default() -> Self {
        Self {
            adjacency_out: HashMap::new(),
            adjacency_in: HashMap::new(),
            strata: OnceCell::new(),
        }
    }
}

impl<P, T, O> InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    T: PayloadLike,
    O: Orientation + PartialEq + std::fmt::Debug,
{
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_arrows<I: IntoIterator<Item = (P, P, T, O)>>(arrows: I) -> Self {
        let mut s = Self::default();
        for (src, dst, pay, ori) in arrows {
            s.add_arrow_o(src, dst, pay, ori);
        }
        s
    }

    fn rebuild_support_from_out(&mut self) {
        self.adjacency_in.clear();
        for (&src, outs) in &self.adjacency_out {
            for &(dst, ref pay, ori) in outs {
                self.adjacency_in
                    .entry(dst)
                    .or_default()
                    .push((src, pay.clone(), ori));
            }
        }
    }

    #[inline]
    pub fn strata_cache(&self) -> Result<&StrataCache<P>, MeshSieveError> {
        self.strata.get_or_try_init(|| compute_strata(self))
    }

    #[inline]
    pub fn invalidate_strata(&mut self) {
        self.strata.take();
    }

    /// Point exists if it has an entry in either role (even with zero degree).
    #[inline]
    pub fn contains_point(&self, p: P) -> bool {
        self.adjacency_out.contains_key(&p) || self.adjacency_in.contains_key(&p)
    }

    #[inline]
    fn scrub_outgoing_only(&mut self, src: P) {
        let old: Vec<(P, T, O)> = std::mem::take(self.adjacency_out.entry(src).or_default());
        for (dst, _, _) in old {
            if let Some(ins) = self.adjacency_in.get_mut(&dst) {
                ins.retain(|(s, _, _)| *s != src);
            }
        }
    }

    #[inline]
    fn scrub_incoming_only(&mut self, dst: P) {
        let old: Vec<(P, T, O)> = std::mem::take(self.adjacency_in.entry(dst).or_default());
        for (src, _, _) in old {
            if let Some(outs) = self.adjacency_out.get_mut(&src) {
                outs.retain(|(d, _, _)| *d != dst);
            }
        }
    }

    #[cfg(debug_assertions)]
    pub fn debug_assert_consistent(&self) {
        for (src, outs) in &self.adjacency_out {
            for (dst, _, _) in outs {
                let ok = self
                    .adjacency_in
                    .get(dst)
                    .is_some_and(|ins| ins.iter().any(|(s, _, _)| s == src));
                debug_assert!(
                    ok,
                    "Missing mirror in[{dst:?}] for out edge ({src:?} -> {dst:?})"
                );
            }
        }
        for (dst, ins) in &self.adjacency_in {
            for (src, _, _) in ins {
                let ok = self
                    .adjacency_out
                    .get(src)
                    .is_some_and(|outs| outs.iter().any(|(d, _, _)| d == dst));
                debug_assert!(
                    ok,
                    "Missing mirror out[{src:?}] for in edge ({src:?} -> {dst:?})"
                );
            }
        }
    }

    #[inline]
    pub fn has_arrow(&self, src: P, dst: P) -> bool {
        self.adjacency_out
            .get(&src)
            .is_some_and(|v| v.iter().any(|(d, _, _)| *d == dst))
    }

    #[cfg(debug_assertions)]
    pub fn debug_assert_no_parallel_edges_src(&self, src: P) {
        if let Some(v) = self.adjacency_out.get(&src) {
            use std::collections::HashSet;
            let mut seen = HashSet::new();
            for (dst, _, _) in v {
                assert!(
                    seen.insert(*dst),
                    "duplicate edges out of {src:?} to {dst:?}"
                );
            }
        }
    }

    #[cfg(debug_assertions)]
    pub fn debug_assert_no_parallel_edges_dst(&self, dst: P) {
        if let Some(v) = self.adjacency_in.get(&dst) {
            use std::collections::HashSet;
            let mut seen = HashSet::new();
            for (src, _, _) in v {
                assert!(
                    seen.insert(*src),
                    "duplicate edges into {dst:?} from {src:?}"
                );
            }
        }
    }

    #[cfg(any(debug_assertions, feature = "strict-invariants"))]
    pub(crate) fn debug_assert_invariants(&self)
    where
        O: super::oriented::Orientation + PartialEq + std::fmt::Debug,
    {
        use crate::topology::_debug_invariants as dbg;

        let out_view: std::collections::HashMap<P, Vec<(P, ())>> = self
            .adjacency_out
            .iter()
            .map(|(&src, v)| (src, v.iter().map(|(dst, _, _)| (*dst, ())).collect()))
            .collect();
        dbg::assert_no_dups_per_src(&out_view);

        let out_pairs = dbg::count_pairs(
            self.adjacency_out
                .iter()
                .flat_map(|(&src, vec)| vec.iter().map(move |(dst, _, _)| (src, *dst))),
        );
        let in_pairs = dbg::count_pairs(
            self.adjacency_in
                .iter()
                .flat_map(|(&dst, vec)| vec.iter().map(move |(src, _, _)| (*src, dst))),
        );
        dbg::counts_equal(&out_pairs, &in_pairs, "adjacency_out", "adjacency_in");

        fn check_orient<P, T, O>(s: &InMemoryOrientedSieve<P, T, O>)
        where
            P: PointLike,
            O: super::oriented::Orientation + PartialEq + std::fmt::Debug,
        {
            use std::collections::HashMap;
            let mut out_map: HashMap<(P, P), O> = HashMap::new();
            for (&src, v) in &s.adjacency_out {
                for (dst, _, o) in v {
                    let prev = out_map.insert((src, *dst), *o);
                    crate::topology::_debug_invariants::inv_assert!(
                        prev.is_none(),
                        "duplicate (src,dst) unexpectedly seen"
                    );
                }
            }
            for (&dst, v) in &s.adjacency_in {
                for (src, _, o_in) in v {
                    let Some(o_out) = out_map.get(&(*src, dst)) else {
                        crate::topology::_debug_invariants::inv_assert!(
                            false,
                            "in mirror without out entry: ({src:?}->{dst:?})"
                        );
                        continue;
                    };
                    crate::topology::_debug_invariants::inv_assert_eq!(
                        o_in,
                        o_out,
                        "orientation mismatch for ({src:?}->{dst:?}): in={o_in:?}, out={o_out:?}"
                    );
                }
            }
        }

        check_orient(self);
    }

    /// Pre-size cone/support capacities from `(src, dst, count)` tuples.
    pub fn reserve_from_edge_counts(&mut self, counts: impl IntoIterator<Item = (P, P, usize)>) {
        use std::collections::HashMap;
        let mut by_src: HashMap<P, usize> = HashMap::new();
        let mut by_dst: HashMap<P, usize> = HashMap::new();
        for (src, dst, k) in counts {
            *by_src.entry(src).or_default() += k;
            *by_dst.entry(dst).or_default() += k;
        }
        for (src, k) in by_src {
            MutableSieve::reserve_cone(self, src, k);
        }
        for (dst, k) in by_dst {
            MutableSieve::reserve_support(self, dst, k);
        }
    }

    /// Convenience helper to preallocate from a raw edge list.
    pub fn reserve_from_edges(&mut self, edges: impl IntoIterator<Item = (P, P)>) {
        use std::collections::HashMap;
        let mut by_src: HashMap<P, usize> = HashMap::new();
        let mut by_dst: HashMap<P, usize> = HashMap::new();
        for (s, d) in edges {
            *by_src.entry(s).or_default() += 1;
            *by_dst.entry(d).or_default() += 1;
        }
        for (s, k) in by_src {
            MutableSieve::reserve_cone(self, s, k);
        }
        for (d, k) in by_dst {
            MutableSieve::reserve_support(self, d, k);
        }
    }

    /// Optional: compact any excess capacity after bulk construction.
    pub fn shrink_to_fit(&mut self) {
        for v in self.adjacency_out.values_mut() {
            v.shrink_to_fit();
        }
        for v in self.adjacency_in.values_mut() {
            v.shrink_to_fit();
        }
    }
}

// ----------- Sieve (payload-only view) -----------
type MapOut<'a, P, T, O> =
    std::iter::Map<std::slice::Iter<'a, (P, T, O)>, fn(&'a (P, T, O)) -> (P, T)>;

type MapOOut<'a, P, T, O> =
    std::iter::Map<std::slice::Iter<'a, (P, T, O)>, fn(&'a (P, T, O)) -> (P, O)>;

impl<P, T, O> Sieve for InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    T: PayloadLike,
    O: Orientation + PartialEq + std::fmt::Debug,
{
    type Point = P;
    type Payload = T;

    type ConeIter<'a>
        = MapOut<'a, P, T, O>
    where
        Self: 'a;
    type SupportIter<'a>
        = MapOut<'a, P, T, O>
    where
        Self: 'a;

    fn cone<'a>(&'a self, p: P) -> Self::ConeIter<'a> {
        fn map_fn<P: Copy, T: Clone, O: Copy>((dst, pay, _): &(P, T, O)) -> (P, T) {
            (*dst, pay.clone())
        }
        let f: fn(&(P, T, O)) -> (P, T) = map_fn::<P, T, O>;
        self.adjacency_out
            .get(&p)
            .map(|v| v.iter().map(f))
            .unwrap_or_else(|| [].iter().map(f))
    }

    fn support<'a>(&'a self, p: P) -> Self::SupportIter<'a> {
        fn map_fn<P: Copy, T: Clone, O: Copy>((src, pay, _): &(P, T, O)) -> (P, T) {
            (*src, pay.clone())
        }
        let f: fn(&(P, T, O)) -> (P, T) = map_fn::<P, T, O>;
        self.adjacency_in
            .get(&p)
            .map(|v| v.iter().map(f))
            .unwrap_or_else(|| [].iter().map(f))
    }

    fn add_arrow(&mut self, src: P, dst: P, payload: T) {
        self.add_arrow_o(src, dst, payload, O::default());
    }

    fn remove_arrow(&mut self, src: P, dst: P) -> Option<T> {
        let mut removed = None;
        if let Some(v) = self.adjacency_out.get_mut(&src)
            && let Some(pos) = v.iter().position(|(d, _, _)| *d == dst)
        {
            removed = Some(v.remove(pos).1);
        }
        if let Some(v) = self.adjacency_in.get_mut(&dst)
            && let Some(pos) = v.iter().position(|(s, _, _)| *s == src)
        {
            v.remove(pos);
        }
        self.invalidate_cache();
        debug_invariants!(self);
        removed
    }

    fn base_points<'a>(&'a self) -> Box<dyn Iterator<Item = P> + 'a> {
        Box::new(self.adjacency_out.keys().copied())
    }

    fn cap_points<'a>(&'a self) -> Box<dyn Iterator<Item = P> + 'a> {
        Box::new(self.adjacency_in.keys().copied())
    }

    fn height(&mut self, p: P) -> Result<u32, MeshSieveError> {
        let cache = self.strata_cache()?;
        Ok(cache.height.get(&p).copied().unwrap_or(0))
    }

    fn depth(&mut self, p: P) -> Result<u32, MeshSieveError> {
        let cache = self.strata_cache()?;
        Ok(cache.depth.get(&p).copied().unwrap_or(0))
    }

    fn diameter(&mut self) -> Result<u32, MeshSieveError> {
        Ok(self.strata_cache()?.diameter)
    }

    fn height_stratum<'a>(
        &'a mut self,
        k: u32,
    ) -> Result<Box<dyn Iterator<Item = P> + 'a>, MeshSieveError> {
        let items = self
            .strata_cache()?
            .strata
            .get(k as usize)
            .cloned()
            .unwrap_or_default();
        Ok(Box::new(items.into_iter()))
    }

    fn depth_stratum<'a>(
        &'a mut self,
        k: u32,
    ) -> Result<Box<dyn Iterator<Item = P> + 'a>, MeshSieveError> {
        let cache = self.strata_cache()?;
        let pts: Vec<_> = cache
            .depth
            .iter()
            .filter_map(|(&p, &d)| if d == k { Some(p) } else { None })
            .collect();
        Ok(Box::new(pts.into_iter()))
    }

    fn chart_index(&mut self, p: P) -> Result<Option<usize>, MeshSieveError> {
        Ok(self.strata_cache()?.index_of(p))
    }

    fn chart_points(&mut self) -> Result<Vec<P>, MeshSieveError> {
        Ok(self.strata_cache()?.chart_points.clone())
    }
}

impl<P, T, O> MutableSieve for InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    T: PayloadLike,
    O: Orientation + PartialEq + std::fmt::Debug,
{
    fn reserve_cone(&mut self, p: P, additional: usize) {
        self.adjacency_out.entry(p).or_default().reserve(additional);
    }

    fn reserve_support(&mut self, q: P, additional: usize) {
        self.adjacency_in.entry(q).or_default().reserve(additional);
    }

    fn add_point(&mut self, p: P) {
        self.adjacency_out.entry(p).or_default();
        self.adjacency_in.entry(p).or_default();
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn remove_point(&mut self, p: P) {
        if self.adjacency_out.contains_key(&p) {
            self.scrub_outgoing_only(p);
        }
        if self.adjacency_in.contains_key(&p) {
            self.scrub_incoming_only(p);
        }
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn add_base_point(&mut self, p: P) {
        self.adjacency_out.entry(p).or_default();
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn add_cap_point(&mut self, p: P) {
        self.adjacency_in.entry(p).or_default();
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn remove_base_point(&mut self, p: P) {
        if self.adjacency_out.contains_key(&p) {
            self.scrub_outgoing_only(p);
            self.adjacency_out.remove(&p);
        }
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn remove_cap_point(&mut self, p: P) {
        if self.adjacency_in.contains_key(&p) {
            self.scrub_incoming_only(p);
            self.adjacency_in.remove(&p);
        }
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn set_cone(&mut self, p: P, chain: impl IntoIterator<Item = (P, T)>) {
        let mut new_cone: Vec<(P, T)> = chain.into_iter().collect();

        // Dedup by destination, last wins
        {
            use std::collections::HashSet;
            let mut seen = HashSet::new();
            let mut dedup = Vec::with_capacity(new_cone.len());
            for (dst, pay) in new_cone.into_iter().rev() {
                if seen.insert(dst) {
                    dedup.push((dst, pay));
                }
            }
            dedup.reverse();
            new_cone = dedup;
        }

        self.adjacency_out
            .entry(p)
            .or_default()
            .reserve(new_cone.len());
        {
            use std::collections::HashMap;
            let mut dst_counts: HashMap<P, usize> = HashMap::new();
            for (dst, _) in &new_cone {
                *dst_counts.entry(*dst).or_default() += 1;
            }
            for (dst, cnt) in dst_counts {
                MutableSieve::reserve_support(self, dst, cnt);
            }
        }

        let old_cone: Vec<(P, T, O)> = std::mem::take(self.adjacency_out.entry(p).or_default());
        for (dst, _, _) in &old_cone {
            if let Some(ins) = self.adjacency_in.get_mut(dst) {
                ins.retain(|(src, _, _)| *src != p);
            }
        }

        let out = self.adjacency_out.entry(p).or_default();
        *out = new_cone
            .iter()
            .map(|(d, pay)| (*d, pay.clone(), O::default()))
            .collect();
        for (dst, pay) in new_cone {
            self.adjacency_in
                .entry(dst)
                .or_default()
                .push((p, pay, O::default()));
        }

        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn add_cone(&mut self, p: P, chain: impl IntoIterator<Item = (P, T)>) {
        let add: Vec<(P, T)> = chain.into_iter().collect();

        self.adjacency_out.entry(p).or_default().reserve(add.len());
        {
            use std::collections::HashMap;
            let mut dst_counts: HashMap<P, usize> = HashMap::new();
            for (dst, _) in &add {
                *dst_counts.entry(*dst).or_default() += 1;
            }
            for (dst, cnt) in dst_counts {
                MutableSieve::reserve_support(self, dst, cnt);
            }
        }

        let out = self.adjacency_out.entry(p).or_default();
        for (dst, pay) in add {
            if let Some(slot) = out.iter_mut().find(|(d, _, _)| *d == dst) {
                slot.1 = pay.clone();
                slot.2 = O::default();
            } else {
                out.push((dst, pay.clone(), O::default()));
            }
            let ins = self.adjacency_in.entry(dst).or_default();
            if let Some(slot) = ins.iter_mut().find(|(s, _, _)| *s == p) {
                slot.1 = pay.clone();
                slot.2 = O::default();
            } else {
                ins.push((p, pay.clone(), O::default()));
            }
        }
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn set_support(&mut self, q: P, chain: impl IntoIterator<Item = (P, T)>) {
        let mut new_sup: Vec<(P, T)> = chain.into_iter().collect();

        // Dedup by source, last wins
        {
            use std::collections::HashSet;
            let mut seen = HashSet::new();
            let mut dedup = Vec::with_capacity(new_sup.len());
            for (src, pay) in new_sup.into_iter().rev() {
                if seen.insert(src) {
                    dedup.push((src, pay));
                }
            }
            dedup.reverse();
            new_sup = dedup;
        }

        self.adjacency_in
            .entry(q)
            .or_default()
            .reserve(new_sup.len());
        {
            use std::collections::HashMap;
            let mut src_counts: HashMap<P, usize> = HashMap::new();
            for (src, _) in &new_sup {
                *src_counts.entry(*src).or_default() += 1;
            }
            for (src, cnt) in src_counts {
                MutableSieve::reserve_cone(self, src, cnt);
            }
        }

        let old_sup: Vec<(P, T, O)> = std::mem::take(self.adjacency_in.entry(q).or_default());
        for (src, _, _) in &old_sup {
            if let Some(outs) = self.adjacency_out.get_mut(src) {
                outs.retain(|(dst, _, _)| *dst != q);
            }
        }

        let ins = self.adjacency_in.entry(q).or_default();
        *ins = new_sup
            .iter()
            .map(|(s, pay)| (*s, pay.clone(), O::default()))
            .collect();
        for (src, pay) in new_sup {
            self.adjacency_out
                .entry(src)
                .or_default()
                .push((q, pay, O::default()));
        }
        self.invalidate_cache();
        debug_invariants!(self);
    }

    fn add_support(&mut self, q: P, chain: impl IntoIterator<Item = (P, T)>) {
        let add: Vec<(P, T)> = chain.into_iter().collect();

        self.adjacency_in.entry(q).or_default().reserve(add.len());
        {
            use std::collections::HashMap;
            let mut src_counts: HashMap<P, usize> = HashMap::new();
            for (src, _) in &add {
                *src_counts.entry(*src).or_default() += 1;
            }
            for (src, cnt) in src_counts {
                MutableSieve::reserve_cone(self, src, cnt);
            }
        }

        let ins = self.adjacency_in.entry(q).or_default();
        for (src, pay) in add {
            if let Some(slot) = ins.iter_mut().find(|(s, _, _)| *s == src) {
                slot.1 = pay.clone();
                slot.2 = O::default();
            } else {
                ins.push((src, pay.clone(), O::default()));
            }
            let outs = self.adjacency_out.entry(src).or_default();
            if let Some(slot) = outs.iter_mut().find(|(d, _, _)| *d == q) {
                slot.1 = pay.clone();
                slot.2 = O::default();
            } else {
                outs.push((q, pay.clone(), O::default()));
            }
        }
        self.invalidate_cache();
        debug_invariants!(self);
    }
}

impl<P, T, O> OrientedSieve for InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    T: PayloadLike,
    O: Orientation + PartialEq + std::fmt::Debug,
{
    type Orient = O;
    type ConeOIter<'a>
        = MapOOut<'a, P, T, O>
    where
        Self: 'a;
    type SupportOIter<'a>
        = MapOOut<'a, P, T, O>
    where
        Self: 'a;

    fn cone_o<'a>(&'a self, p: P) -> Self::ConeOIter<'a> {
        fn map_fn<P: Copy, T, O: Copy>((dst, _, ori): &(P, T, O)) -> (P, O) {
            (*dst, *ori)
        }
        let f: fn(&(P, T, O)) -> (P, O) = map_fn::<P, T, O>;
        self.adjacency_out
            .get(&p)
            .map(|v| v.iter().map(f))
            .unwrap_or_else(|| [].iter().map(f))
    }

    fn support_o<'a>(&'a self, p: P) -> Self::SupportOIter<'a> {
        fn map_fn<P: Copy, T, O: Copy>((src, _, ori): &(P, T, O)) -> (P, O) {
            (*src, *ori)
        }
        let f: fn(&(P, T, O)) -> (P, O) = map_fn::<P, T, O>;
        self.adjacency_in
            .get(&p)
            .map(|v| v.iter().map(f))
            .unwrap_or_else(|| [].iter().map(f))
    }

    fn add_arrow_o(&mut self, src: P, dst: P, payload: T, orient: O) {
        // Upsert outgoing
        let outs = self.adjacency_out.entry(src).or_default();
        if let Some(slot) = outs.iter_mut().find(|(d, _, _)| *d == dst) {
            slot.1 = payload.clone();
            slot.2 = orient;
        } else {
            outs.push((dst, payload.clone(), orient));
        }

        // Upsert incoming
        let ins = self.adjacency_in.entry(dst).or_default();
        if let Some(slot) = ins.iter_mut().find(|(s, _, _)| *s == src) {
            slot.1 = payload;
            slot.2 = orient;
        } else {
            ins.push((src, payload, orient));
        }

        self.invalidate_cache();
        debug_invariants!(self);
    }
}

impl<P, T, O> InvalidateCache for InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    T: PayloadLike,
    O: Orientation + PartialEq + std::fmt::Debug,
{
    #[inline]
    fn invalidate_cache(&mut self) {
        self.strata.take();
    }
}

impl<P, T, O> SieveQueryExt for InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    T: PayloadLike,
    O: Orientation + PartialEq + std::fmt::Debug,
{
    #[inline]
    fn out_degree(&self, p: P) -> usize {
        self.adjacency_out.get(&p).map_or(0, |v| v.len())
    }
    #[inline]
    fn in_degree(&self, p: P) -> usize {
        self.adjacency_in.get(&p).map_or(0, |v| v.len())
    }
}

impl<P, T, O> SieveBuildExt for InMemoryOrientedSieve<P, T, O>
where
    P: PointLike,
    T: PayloadLike + Clone,
    O: Orientation + PartialEq + std::fmt::Debug + Copy,
{
    fn add_arrows_from<I>(&mut self, edges: I)
    where
        I: IntoIterator<Item = (P, P, T)>,
    {
        for (s, d, pay) in edges {
            self.add_arrow(s, d, pay);
        }
    }

    fn add_arrows_dedup_from<I>(&mut self, edges: I)
    where
        I: IntoIterator<Item = (P, P, T)>,
    {
        use std::collections::HashMap;
        let mut last: HashMap<(P, P), T> = HashMap::new();
        for (s, d, pay) in edges {
            last.insert((s, d), pay);
        }
        for ((s, d), pay) in last {
            self.add_arrow(s, d, pay);
        }
    }
}