safegraph 0.1.0

A type-safe, scope-aware graph library that leverages Rust's type system to prevent common graph-related bugs at compile time
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
//! Undirected hypergraph backend.
//!
//! A hypergraph generalizes ordinary graphs by allowing each edge ("hyper-
//! edge") to connect an arbitrary number of nodes, not just two. This
//! backend maintains a dual index so both directions of lookup are
//! O(degree) rather than O(V + E):
//!
//! ```text
//! nodes: NC                              // outer node collection
//!   - Index   = VIx
//!   - Value   = V
//!   - Storage = IS                       // set of incident EIx
//!
//! edges: EC                              // outer edge collection
//!   - Index   = EIx
//!   - Value   = E
//!   - Storage = ES                       // set of endpoint VIx (impl `Endpoints`)
//! ```
//!
//! The invariant `eix ∈ nodes[vix].Storage  ⇔  vix ∈ edges[eix].Storage`
//! is maintained by every insert / remove path.

use core::marker::PhantomData;
use std::collections::{btree_set, hash_set, BTreeSet, HashMap, HashSet};
use std::fmt::{Debug, Display};
use std::hash::Hash;

use crate::collection::{
    Collection, InsertableCollection, RandomAccess, RandomAccessRef, RemovableRandomAccess,
    StableCollection, UpdatableRandomAccess,
};
use crate::graph::capability::{
    InsertEdge, InsertNode, RemoveEdge, RemoveNode, StableEdge, StableNode, UniqueEdge,
    UniqueNode, UpdateEdge, UpdateNode,
};
use crate::graph::edge::Endpoints;
use crate::graph::operation::GraphOperation;
use crate::graph::walk_item::{WalkItem, WalkItemMut};
use crate::graph::GraphProperty;

/// Per-node container of incident edge indices. Used as the `Storage` type
/// on the outer node collection.
pub trait IncidenceSet<I>: Default {
    fn insert(&mut self, item: I);
    fn remove(&mut self, item: &I);
    fn contains(&self, item: &I) -> bool;
    fn len(&self) -> usize;
    /// Returns `true` if the incidence set is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Iteration access for an [`IncidenceSet`], parameterized by borrow lifetime
/// to sidestep GATs (matches the HRTB pattern used elsewhere in this crate).
pub trait IncidenceSetRef<'a, I>: IncidenceSet<I>
where
    Self: 'a,
{
    type Iter: Iterator<Item = I>;
    fn iter(&'a self) -> Self::Iter;
}

// --- HashSet<I> ---

impl<I: Copy + Eq + Hash> IncidenceSet<I> for HashSet<I> {
    #[inline]
    fn insert(&mut self, item: I) {
        HashSet::insert(self, item);
    }
    #[inline]
    fn remove(&mut self, item: &I) {
        HashSet::remove(self, item);
    }
    #[inline]
    fn contains(&self, item: &I) -> bool {
        HashSet::contains(self, item)
    }
    #[inline]
    fn len(&self) -> usize {
        HashSet::len(self)
    }
}

impl<'a, I: Copy + Eq + Hash + 'a> IncidenceSetRef<'a, I> for HashSet<I> {
    type Iter = std::iter::Copied<hash_set::Iter<'a, I>>;
    #[inline]
    fn iter(&'a self) -> Self::Iter {
        HashSet::iter(self).copied()
    }
}

// --- BTreeSet<I> ---

impl<I: Copy + Eq + Ord> IncidenceSet<I> for BTreeSet<I> {
    #[inline]
    fn insert(&mut self, item: I) {
        BTreeSet::insert(self, item);
    }
    #[inline]
    fn remove(&mut self, item: &I) {
        BTreeSet::remove(self, item);
    }
    #[inline]
    fn contains(&self, item: &I) -> bool {
        BTreeSet::contains(self, item)
    }
    #[inline]
    fn len(&self) -> usize {
        BTreeSet::len(self)
    }
}

impl<'a, I: Copy + Eq + Ord + 'a> IncidenceSetRef<'a, I> for BTreeSet<I> {
    type Iter = std::iter::Copied<btree_set::Iter<'a, I>>;
    #[inline]
    fn iter(&'a self) -> Self::Iter {
        BTreeSet::iter(self).copied()
    }
}

// --- Vec<I> (multiset semantics; `remove` deletes one occurrence) ---

impl<I: Copy + Eq> IncidenceSet<I> for Vec<I> {
    #[inline]
    fn insert(&mut self, item: I) {
        Vec::push(self, item);
    }
    fn remove(&mut self, item: &I) {
        if let Some(pos) = <[I]>::iter(self).position(|x| x == item) {
            // swap_remove keeps O(1); order of iteration doesn't matter
            // for incidence semantics.
            self.swap_remove(pos);
        }
    }
    fn contains(&self, item: &I) -> bool {
        <[I]>::iter(self).any(|x| x == item)
    }
    #[inline]
    fn len(&self) -> usize {
        Vec::len(self)
    }
}

impl<'a, I: Copy + Eq + 'a> IncidenceSetRef<'a, I> for Vec<I> {
    type Iter = std::iter::Copied<std::slice::Iter<'a, I>>;
    #[inline]
    fn iter(&'a self) -> Self::Iter {
        <[I]>::iter(self).copied()
    }
}

/// Undirected hypergraph backed by two cross-referenced [`RandomAccess`]
/// collections.
///
/// See the module-level docs for the storage layout.
#[derive(Clone, Debug)]
pub struct HyperGraph<NC, EC> {
    pub(crate) nodes: NC,
    pub(crate) edges: EC,
}

impl<NC: Default, EC: Default> Default for HyperGraph<NC, EC> {
    fn default() -> Self {
        Self {
            nodes: NC::default(),
            edges: EC::default(),
        }
    }
}

impl<NC: Default, EC: Default> HyperGraph<NC, EC> {
    /// Equivalent to [`Default::default`].
    pub fn new() -> Self {
        Self::default()
    }
}

/// `BTreeMap`-backed hypergraph (Key=Value pattern). Indices stable;
/// implements [`StableNode`] + [`StableEdge`].
pub type StableHyperGraph<N, E> = HyperGraph<
    std::collections::BTreeMap<N, BTreeSet<E>>,
    std::collections::BTreeMap<E, BTreeSet<N>>,
>;

/// `HashMap`-backed hypergraph (Key=Value pattern). Same stability as
/// [`StableHyperGraph`], hash-based iteration order.
pub type HashHyperGraph<N, E> =
    HyperGraph<HashMap<N, HashSet<E>>, HashMap<E, HashSet<N>>>;

impl<NC, EC, V, E, VIx, EIx, IS, ES> GraphProperty for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS>,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES>,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
    type Node = V;
    type Edge = E;
    type NodeIx = VIx;
    type EdgeIx = EIx;
    type Endpoints = ES;
    // Hyperedges are undirected (no source/target distinction).
    const DIRECTED: bool = false;
}

/// `edge_indices_from_unchecked` result: thin wrapper around the
/// incidence-set iterator.
pub struct EdgeIndicesFromIter<I> {
    inner: I,
}

impl<EIx, I> Iterator for EdgeIndicesFromIter<I>
where
    I: Iterator<Item = EIx>,
{
    type Item = EIx;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

/// `walks_from_unchecked` / `walks_of_unchecked` iterator.
///
/// Flat-maps over incident hyperedges and yields `(EIx, &Edge, neighbor VIx)`
/// for every endpoint of each edge except the origin node. Same edge
/// appears multiple times when its arity is > 2.
pub struct Walks<'r, NC, EC, VIx, EIx, IS, ES>
where
    NC: RandomAccess<Index = VIx, Storage = IS>,
    EC: RandomAccess<Index = EIx, Storage = ES>,
    IS: IncidenceSetRef<'r, EIx>,
    ES: IntoIterator,
{
    origin: VIx,
    edges: &'r EC,
    incidence_iter: IS::Iter,
    cur_edge: Option<(EIx, ES::IntoIter)>,
    _marker: PhantomData<&'r NC>,
}

impl<'r, NC, EC, V, E, VIx, EIx, IS, ES> Iterator
    for Walks<'r, NC, EC, VIx, EIx, IS, ES>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS>,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES>,
    IS: IncidenceSet<EIx> + IncidenceSetRef<'r, EIx>,
    ES: Endpoints<NodeIx = VIx> + 'r,
    VIx: Copy + Eq + 'r,
    EIx: Copy + 'r,
    V: 'r,
    E: 'r,
{
    type Item = WalkItem<'r, EIx, E, VIx>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some((eix, ep_iter)) = self.cur_edge.as_mut() {
                let eix = *eix;
                for vix in ep_iter.by_ref() {
                    if vix == self.origin {
                        continue;
                    }
                    // SAFETY: eix is from a maintained incidence set.
                    let edge_value = unsafe { self.edges.get_value_unchecked(&eix) };
                    return Some(WalkItem::new(eix, edge_value, vix));
                }
                self.cur_edge = None;
            }
            let eix = self.incidence_iter.next()?;
            let endpoints =
                unsafe { self.edges.get_storage_unchecked(&eix) }.clone();
            self.cur_edge = Some((eix, endpoints.into_iter()));
        }
    }
}

impl<'r, NC, EC, V, E, VIx, EIx, IS, ES> GraphOperation<'r> for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS> + 'r,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES> + 'r,
    IS: IncidenceSet<EIx> + for<'a> IncidenceSetRef<'a, EIx> + 'static,
    ES: Endpoints<NodeIx = VIx> + 'r,
    for<'a> NC: RandomAccessRef<'a>,
    for<'a> EC: RandomAccessRef<'a>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    V: 'r,
    E: 'r,
{
    fn contains_node_index(&self, node_ix: Self::NodeIx) -> bool {
        self.nodes.contains_index(&node_ix)
    }

    fn contains_edge_index(&self, edge_ix: Self::EdgeIx) -> bool {
        self.edges.contains_index(&edge_ix)
    }

    fn len_node(&self) -> usize {
        Collection::len(&self.nodes)
    }

    fn len_edge(&self) -> usize {
        Collection::len(&self.edges)
    }

    type NodeIndices = <NC as RandomAccessRef<'r>>::Indices;
    fn node_indices(&'r self) -> Self::NodeIndices {
        self.nodes.indices()
    }

    type EdgeIndices = <EC as RandomAccessRef<'r>>::Indices;
    fn edge_indices(&'r self) -> Self::EdgeIndices {
        self.edges.indices()
    }

    unsafe fn node_unchecked(&self, node_ix: Self::NodeIx) -> &Self::Node {
        unsafe { self.nodes.get_value_unchecked(&node_ix) }
    }

    unsafe fn edge_unchecked(&self, edge_ix: Self::EdgeIx) -> &Self::Edge {
        unsafe { self.edges.get_value_unchecked(&edge_ix) }
    }

    unsafe fn endpoints_unchecked(&self, edge_ix: Self::EdgeIx) -> Self::Endpoints {
        unsafe { self.edges.get_storage_unchecked(&edge_ix) }.clone()
    }

    type EdgeIndicesFrom = EdgeIndicesFromIter<<IS as IncidenceSetRef<'r, EIx>>::Iter>;
    unsafe fn edge_indices_from_unchecked(
        &'r self,
        node_ix: Self::NodeIx,
    ) -> Self::EdgeIndicesFrom {
        let storage = unsafe { self.nodes.get_storage_unchecked(&node_ix) };
        EdgeIndicesFromIter {
            inner: IncidenceSetRef::iter(storage),
        }
    }

    type EdgeIndicesOf = EdgeIndicesFromIter<<IS as IncidenceSetRef<'r, EIx>>::Iter>;
    unsafe fn edge_indices_of_unchecked(
        &'r self,
        node_ix: Self::NodeIx,
    ) -> Self::EdgeIndicesOf {
        // Undirected: same as edge_indices_from.
        unsafe { self.edge_indices_from_unchecked(node_ix) }
    }

    type WalksFrom = Walks<'r, NC, EC, VIx, EIx, IS, ES>;
    unsafe fn walks_from_unchecked(&'r self, node_ix: Self::NodeIx) -> Self::WalksFrom {
        let storage = unsafe { self.nodes.get_storage_unchecked(&node_ix) };
        Walks {
            origin: node_ix,
            edges: &self.edges,
            incidence_iter: IncidenceSetRef::iter(storage),
            cur_edge: None,
            _marker: PhantomData,
        }
    }

    type WalksOf = Walks<'r, NC, EC, VIx, EIx, IS, ES>;
    unsafe fn walks_of_unchecked(&'r self, node_ix: Self::NodeIx) -> Self::WalksOf {
        // Undirected: identical to walks_from.
        unsafe { self.walks_from_unchecked(node_ix) }
    }

    type DrainNode = NC::IntoValues;
    type DrainEdge = EC::IntoValues;
    fn drain(self) -> (Self::DrainNode, Self::DrainEdge) {
        (self.nodes.into_values(), self.edges.into_values())
    }

    fn reverse(&mut self) {
        // Undirected: no-op.
    }
}

impl<NC, EC, V, E, VIx, EIx, IS, ES> InsertNode for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS>
        + InsertableCollection<InsertedIndex = VIx>,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES>,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
    unsafe fn insert_node_unchecked(
        &mut self,
        node: Self::Node,
    ) -> Result<Self::NodeIx, Self::Node> {
        self.nodes.insert(node, IS::default()).map_err(|(v, _)| v)
    }
}

impl<NC, EC, V, E, VIx, EIx, IS, ES> InsertEdge for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS> + UpdatableRandomAccess,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES>
        + InsertableCollection<InsertedIndex = EIx>,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
    unsafe fn insert_edge_unchecked(
        &mut self,
        edge: Self::Edge,
        endpoints: Self::Endpoints,
    ) -> Result<Self::EdgeIx, Self::Edge> {
        let endpoints_clone = endpoints.clone();
        let eix = match self.edges.insert(edge, endpoints) {
            Ok(eix) => eix,
            Err((v, _)) => return Err(v),
        };
        for vix in endpoints_clone.into_iter() {
            let storage = unsafe { self.nodes.get_storage_unchecked_mut(&vix) };
            storage.insert(eix);
        }
        Ok(eix)
    }
}

impl<'r, NC, EC, V, E, VIx, EIx, IS, ES> UpdateNode<'r> for HyperGraph<NC, EC>
where
    NC: UpdatableRandomAccess<Index = VIx, Value = V, Storage = IS> + 'r,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES> + 'r,
    IS: IncidenceSet<EIx> + 'static,
    ES: Endpoints<NodeIx = VIx> + 'r,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    V: 'r,
    E: 'r,
{
    unsafe fn node_unchecked_mut(&mut self, node_ix: Self::NodeIx) -> &mut Self::Node {
        unsafe { self.nodes.get_value_unchecked_mut(&node_ix) }
    }

    type WalksFromMut = std::iter::Empty<WalkItemMut<'r, EIx, E, VIx>>;
    unsafe fn walks_from_unchecked_mut(
        &'r mut self,
        _node_ix: Self::NodeIx,
    ) -> Self::WalksFromMut {
        std::iter::empty()
    }

    type WalksOfMut = std::iter::Empty<WalkItemMut<'r, EIx, E, VIx>>;
    unsafe fn walks_of_unchecked_mut(
        &'r mut self,
        _node_ix: Self::NodeIx,
    ) -> Self::WalksOfMut {
        std::iter::empty()
    }
}

impl<NC, EC, V, E, VIx, EIx, IS, ES> UpdateEdge for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS>,
    EC: UpdatableRandomAccess<Index = EIx, Value = E, Storage = ES>,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
    unsafe fn edge_unchecked_mut(&mut self, edge_ix: Self::EdgeIx) -> &mut Self::Edge {
        unsafe { self.edges.get_value_unchecked_mut(&edge_ix) }
    }
}

impl<NC, EC, V, E, VIx, EIx, IS, ES> RemoveEdge for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS> + UpdatableRandomAccess,
    EC: RemovableRandomAccess<Index = EIx, Value = E, Storage = ES>,
    for<'a> EC: RandomAccessRef<'a>,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
    unsafe fn take_edge_unchecked(&mut self, edge_ix: Self::EdgeIx) -> Self::Edge {
        let (e, endpoints, swapped) = unsafe { self.edges.take_unchecked(&edge_ix) };
        for vix in endpoints.into_iter() {
            let storage = unsafe { self.nodes.get_storage_unchecked_mut(&vix) };
            storage.remove(&edge_ix);
        }
        // If the outer EC swap-removed, rewrite references to old_last.
        if let Some(old_last) = swapped {
            // The relocated edge now sits at `edge_ix`. Its endpoints'
            // incidence sets currently hold `old_last`; they need to point
            // at `edge_ix`.
            let relocated_endpoints =
                unsafe { self.edges.get_storage_unchecked(&edge_ix) }.clone();
            for vix in relocated_endpoints.into_iter() {
                let storage = unsafe { self.nodes.get_storage_unchecked_mut(&vix) };
                storage.remove(&old_last);
                storage.insert(edge_ix);
            }
        }
        e
    }
}

// ---------------------------------------------------------------------------
// RemoveNode — cascade: drop every incident edge, then take the node.
// Handles outer NC swap-remove by rewriting references to old_last.
// ---------------------------------------------------------------------------

impl<NC, EC, V, E, VIx, EIx, IS, ES> RemoveNode for HyperGraph<NC, EC>
where
    NC: RemovableRandomAccess<Index = VIx, Value = V, Storage = IS> + UpdatableRandomAccess,
    EC: RemovableRandomAccess<Index = EIx, Value = E, Storage = ES> + UpdatableRandomAccess,
    for<'a> NC: RandomAccessRef<'a>,
    for<'a> EC: RandomAccessRef<'a>,
    IS: IncidenceSet<EIx> + for<'a> IncidenceSetRef<'a, EIx> + 'static,
    ES: Endpoints<NodeIx = VIx> + 'static,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
    unsafe fn take_node_unchecked(&mut self, node_ix: Self::NodeIx) -> Self::Node {
        // Re-reading the incidence set after each removal picks up EIxs
        // that were rewritten by `take_edge_unchecked`'s swap-remove fixup
        // (Vec-backed EC).
        loop {
            let next_eix = {
                let storage = unsafe { self.nodes.get_storage_unchecked(&node_ix) };
                IncidenceSetRef::iter(storage).next()
            };
            let eix = match next_eix {
                Some(eix) => eix,
                None => break,
            };
            let _ = unsafe { <Self as RemoveEdge>::take_edge_unchecked(self, eix) };
        }

        let (data, _storage, swapped) = unsafe { self.nodes.take_unchecked(&node_ix) };

        // On NC swap-remove, rewrite every edge whose endpoints reference
        // `old_last` to reference `node_ix` instead.
        if let Some(old_last) = swapped {
            let all_eixs: Vec<EIx> = self.edges.indices().collect();
            for eix in all_eixs {
                let endpoints_clone =
                    unsafe { self.edges.get_storage_unchecked(&eix) }.clone();
                let mut needs_rewrite = false;
                for vix in endpoints_clone.iter() {
                    if vix == old_last {
                        needs_rewrite = true;
                        break;
                    }
                }
                if !needs_rewrite {
                    continue;
                }
                let rewritten: Vec<VIx> = endpoints_clone
                    .into_iter()
                    .map(|v| if v == old_last { node_ix } else { v })
                    .collect();
                let new_endpoints = match <ES as Endpoints>::try_from_node_indices(rewritten) {
                    Some(ep) => ep,
                    None => continue, // shouldn't happen
                };
                let entry = unsafe { self.edges.get_storage_unchecked_mut(&eix) };
                *entry = new_endpoints;
            }
        }

        data
    }

    unsafe fn take_nodes_edges_unchecked<IN, IE>(
        &mut self,
        node_indices: impl IntoIterator<Item = Self::NodeIx>,
        edge_indices: impl IntoIterator<Item = Self::EdgeIx>,
    ) -> (IN, IE)
    where
        IN: Default + Extend<Self::Node>,
        IE: Default + Extend<Self::Edge>,
    {
        let mut nodes_out = IN::default();
        let mut edges_out = IE::default();
        for eix in edge_indices {
            if !self.edges.contains_index(&eix) {
                continue;
            }
            let e = unsafe { <Self as RemoveEdge>::take_edge_unchecked(self, eix) };
            edges_out.extend(core::iter::once(e));
        }
        for nix in node_indices {
            if !self.nodes.contains_index(&nix) {
                continue;
            }
            let v = unsafe { self.take_node_unchecked(nix) };
            nodes_out.extend(core::iter::once(v));
        }
        (nodes_out, edges_out)
    }
}

unsafe impl<NC, EC, V, E, VIx, EIx, IS, ES> StableNode for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS> + StableCollection,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES>,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
}

unsafe impl<NC, EC, V, E, VIx, EIx, IS, ES> StableEdge for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS>,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES> + StableCollection,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
{
}

impl<NC, EC, V, E, VIx, EIx, IS, ES> UniqueNode for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS>
        + crate::collection::CollectionBiject
        + StableCollection,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES>,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    V: PartialEq,
{
    fn node_index(&self, node: impl core::borrow::Borrow<Self::Node>) -> Option<Self::NodeIx> {
        // SAFETY: CollectionBiject contract guarantees an inverse lookup
        // when the node value is present.
        unsafe { self.nodes.value_to_key_unchecked(node.borrow()) }.copied()
    }
}

impl<NC, EC, V, E, VIx, EIx, IS, ES> UniqueEdge for HyperGraph<NC, EC>
where
    NC: RandomAccess<Index = VIx, Value = V, Storage = IS>,
    EC: RandomAccess<Index = EIx, Value = E, Storage = ES>
        + crate::collection::CollectionBiject
        + StableCollection,
    IS: IncidenceSet<EIx>,
    ES: Endpoints<NodeIx = VIx>,
    VIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    EIx: Copy + Eq + Ord + Hash + Display + Debug + 'static,
    E: PartialEq,
{
    fn edge_index(&self, edge: impl core::borrow::Borrow<Self::Edge>) -> Option<Self::EdgeIx> {
        unsafe { self.edges.value_to_key_unchecked(edge.borrow()) }.copied()
    }
}