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
786
787
788
789
790
791
792
793
//! Storage-agnostic traits for discrete topology views.
//!
//! `oxgraph-topology` defines the minimal vocabulary shared by graph,
//! hypergraph, snapshot, and layout crates. It does not define concrete node,
//! edge, vertex, hyperedge, incidence, storage, or role types. Implementations
//! provide those through associated types.
//!
//! This crate defines read-view capabilities. Mutation belongs in explicit
//! capability traits that define identity stability, deletion, compaction, and
//! stale-handle semantics.
//!
//! A topology view is any value that exposes topology through these traits. The
//! view decides its own boundary: an entire snapshot, one layout section, a
//! generated projection, a page-sized window, or an overlay can all be views if
//! they provide the requested capabilities.
extern crate kani;
/// Marker trait for compact topology identity handles.
///
/// IDs are handles into topology storage, not the storage itself. They are
/// passed by value so traversal APIs can remain simple and static-dispatch
/// friendly. A logical element, relation, or incidence can have different ID
/// representations at different layers. Implementations should document which
/// identity layer each ID type represents and how it maps to other exposed
/// identity layers.
///
/// # Performance
///
/// Implementations are expected to be small `Copy` handles. Copying,
/// comparing, ordering, hashing, and formatting for debug should be `O(1)`.
/// Blanket implementation for compact ID handle types.
///
/// Any type satisfying the `TopologyId` bounds is a valid topology ID. This
/// keeps the crate trait-based without requiring implementations to write empty
/// marker impls for every local handle type.
///
/// # Performance
///
/// `perf: unspecified`; performance is inherited from the concrete ID type.
/// Common element and relation identity vocabulary for a topology view.
///
/// The associated ID types deliberately avoid graph- or hypergraph-specific
/// names. Graph crates can map elements to nodes and relations to edges;
/// hypergraph crates can map elements to vertices and relations to hyperedges.
///
/// # Performance
///
/// Associated ID values should be compact `O(1)` handles. Arbitrary relation or
/// element payloads should be exposed by separate traits keyed by the associated
/// ID types.
/// Substrate-neutral alias for a topology view's element ID type.
///
/// # Performance
///
/// `perf: unspecified`; performance is inherited from the underlying
/// [`TopologyBase::ElementId`] type.
pub type ElementId<T> = ElementId;
/// Substrate-neutral alias for a topology view's relation ID type.
///
/// # Performance
///
/// `perf: unspecified`; performance is inherited from the underlying
/// [`TopologyBase::RelationId`] type.
pub type RelationId<T> = RelationId;
/// Optional total weight capability for topology elements.
///
/// A view implements this trait only when every visible element has a weight
/// representation. The topology layer does not interpret the value: it is not a
/// probability, cost, distance, count, or property name. Algorithms state their
/// own numeric contracts when they consume a selected weight capability.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Optional total weight capability for topology relations.
///
/// A view implements this trait only when every visible relation has a weight
/// representation. The topology layer does not interpret the value; algorithms
/// define any finite, non-negative, additive, ordered, or normalization
/// requirements separately.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Optional local-to-canonical element identity capability.
///
/// Views implement this trait only when they guarantee a stable canonical ID for
/// every visible element in the view's documented identity scope. The canonical
/// ID is a substrate identity, not a Python label or domain identifier.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Optional canonical-to-local element identity capability.
///
/// This reverse lookup is separate from [`CanonicalElementIdentity`] because it
/// may require extra memory or may be partial for filtered and projected views.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Optional local-to-canonical relation identity capability.
///
/// Views implement this trait only when they guarantee a stable canonical ID for
/// every visible relation in the view's documented identity scope.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Optional canonical-to-local relation identity capability.
///
/// This reverse lookup is separate from [`CanonicalRelationIdentity`] because it
/// may require extra memory or may be partial for filtered and projected views.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Incidence identity and role vocabulary for topology views with incidences.
///
/// Incidence support is separate from [`TopologyBase`] so graph-only views can
/// expose nodes and edges without inventing endpoint identities or roles. Views
/// that support relation-to-element participation records implement this trait.
///
/// # Performance
///
/// Associated ID values should be compact `O(1)` handles. `Role` describes how
/// an element participates in a relation. Arbitrary incidence payloads should be
/// exposed by separate traits keyed by [`Self::IncidenceId`].
/// Optional total weight capability for topology incidences.
///
/// A view implements this trait only when every visible incidence has a weight
/// representation. The topology layer does not interpret the value; algorithms
/// define their own numeric contracts separately.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Optional local-to-canonical incidence identity capability.
///
/// Views implement this trait only when they guarantee a stable canonical ID for
/// every visible incidence in the view's documented identity scope.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Optional canonical-to-local incidence identity capability.
///
/// This reverse lookup is separate from [`CanonicalIncidenceIdentity`] because
/// it may require extra memory or may be partial for filtered and projected
/// views.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Count capability for a topology view.
///
/// This trait is separated from [`TopologyBase`] because counts are a capability.
/// Some views can report global counts cheaply; others expose only local,
/// paged, generated, or filtered topology and should define counts only when the
/// result is meaningful for that view.
///
/// # Performance
///
/// Methods should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Count capability for incidence-capable topology views.
///
/// This trait is separated from [`TopologyCounts`] because not every topology
/// view exposes incidence records. Graph-only views can count nodes and edges
/// without defining endpoint-incidence identities.
///
/// # Performance
///
/// Methods should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Dense element-index capability for topology views.
///
/// This capability maps visible element IDs to compact array indexes usable by
/// algorithms that need visited sets, distance arrays, parent arrays, or other
/// per-element scratch storage. The index is a property of the view, not the ID
/// type itself: a view may expose opaque IDs while also providing a dense index
/// mapping.
///
/// `element_bound` is an allocation bound, not necessarily the exact visible
/// element count. Immutable compact layouts usually have `element_bound() ==
/// element_count()`. Mutable layouts with tombstones, overlays, or stable slots
/// may have `element_bound() >= element_count()`.
///
/// Implementations must ensure every valid visible element maps to an index less
/// than `element_bound`, distinct visible elements map to distinct indexes, and
/// indexes remain stable for the lifetime of the view operation using them.
///
/// # Performance
///
/// Methods should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Dense relation-index capability for topology views.
///
/// This capability maps visible relation IDs to compact array indexes usable by
/// algorithms that need per-relation scratch storage. The index is a property of
/// the view, not the ID type itself.
///
/// `relation_bound` is an allocation bound, not necessarily the exact visible
/// relation count. Immutable compact layouts usually have `relation_bound() ==
/// relation_count()`. Mutable layouts may expose a larger bound.
///
/// Implementations must ensure every valid visible relation maps to an index
/// less than `relation_bound`, distinct visible relations map to distinct
/// indexes, and indexes remain stable for the lifetime of the view operation
/// using them.
///
/// # Performance
///
/// Methods should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Dense incidence-index capability for topology views.
///
/// This capability maps visible incidence IDs to compact array indexes usable by
/// algorithms that need per-incidence scratch storage. Views that do not expose
/// incidences do not implement this trait.
///
/// `incidence_bound` is an allocation bound, not necessarily the exact visible
/// incidence count. Immutable compact layouts usually have `incidence_bound() ==
/// incidence_count()`. Mutable layouts may expose a larger bound.
///
/// Implementations must ensure every valid visible incidence maps to an index
/// less than `incidence_bound`, distinct visible incidences map to distinct
/// indexes, and indexes remain stable for the lifetime of the view operation
/// using them.
///
/// # Performance
///
/// Methods should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Element-ID containment capability for a topology view.
///
/// This capability answers whether an element ID is valid and visible in this
/// view at the time of the call. It is intentionally separate from
/// [`ElementIndex`]: an index bound is an allocation bound, while containment is
/// an ID-validity predicate. Mutable, filtered, tombstoned, or overlay views may
/// have indexes below the bound that are not visible elements.
///
/// # Performance
///
/// Expected `O(1)` unless the implementation documents otherwise.
/// Relation-ID containment capability for a topology view.
///
/// This capability answers whether a relation ID is valid and visible in this
/// view at the time of the call. It does not answer graph-specific questions
/// such as whether an edge between two nodes exists, nor hypergraph-specific
/// questions such as whether a vertex participates in a hyperedge.
///
/// # Performance
///
/// Expected `O(1)` unless the implementation documents otherwise.
/// Incidence-ID containment capability for an incidence-capable topology view.
///
/// This capability answers whether an incidence ID is valid and visible in this
/// view at the time of the call.
///
/// # Performance
///
/// Expected `O(1)` unless the implementation documents otherwise.
/// Capability for traversing incidences attached to a relation.
///
/// The generic associated iterator type lets each backend return its own
/// concrete iterator without allocating or using dynamic dispatch.
///
/// # Performance
///
/// Creating the iterator should be `O(1)` unless an implementation documents a
/// weaker contract. Yielding `k` incidences should be `O(k)` because the output
/// itself has length `k`.
/// Capability for traversing incidences attached to an element.
///
/// This is the topology-general form of asking which relations mention an
/// element.
///
/// # Performance
///
/// Creating the iterator should be `O(1)` unless an implementation documents a
/// weaker contract. Yielding `k` incidences should be `O(k)` because the output
/// itself has length `k`.
/// Capability for resolving the element side of an incidence.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Capability for resolving the relation side of an incidence.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Capability for resolving a role attached to an incidence.
///
/// Roles are implementation-defined topology labels. The core never
/// interprets them.
///
/// # Performance
///
/// Lookup should be `O(1)` unless an implementation documents a weaker
/// contract.
/// Exact relation-incidence count capability.
///
/// This pairs with [`RelationIncidences`] for backends that can report a
/// relation's incidence count without traversal. The trait does not require
/// traversal support by itself.
///
/// # Performance
///
/// Expected `O(1)` unless the implementation documents otherwise.
/// Exact element-incidence count capability.
///
/// This pairs with [`ElementIncidences`] for backends that can report an
/// element's incidence count without traversal. The trait does not require
/// traversal support by itself.
///
/// # Performance
///
/// Expected `O(1)` unless the implementation documents otherwise.
/// Convenience trait for views that can resolve complete incidence records.
///
/// This trait has no methods of its own. It names the common capability bundle
/// needed by generic code that wants to traverse relations and inspect each
/// incidence's element, relation, and role.
///
/// # Performance
///
/// `perf: unspecified`; performance is inherited from the component traits.
/// Blanket implementation for complete incidence views.
///
/// Any type that can traverse relation incidences and resolve each incidence's
/// element, relation, and role automatically implements [`IncidenceView`].
///
/// # Performance
///
/// `perf: unspecified`; performance is inherited from the component traits.
/// Capability for expanding an element to its directed successor elements.
///
/// This is the substrate-neutral form of "follow outgoing connections from
/// this element." For binary graphs the successors are the targets of
/// outgoing edges; for hypergraphs they are the vertices reachable through
/// outgoing hyperedges. Substrate-agnostic algorithms (forward BFS, forward
/// reachability) bind on this trait so the same code drives any topology
/// view that can answer the question.
///
/// Implementations define whether parallel connections produce repeated
/// successor elements. Implementations that preserve multiplicity should
/// document that behavior; consumers that need set semantics should
/// deduplicate at their own level.
///
/// # Performance
///
/// Creating the iterator should be `O(1)` unless an implementation documents a
/// weaker contract. Yielding `k` successors should be `O(k)` and should not
/// allocate unless the implementation documents otherwise.
/// Capability for expanding an element to its directed predecessor elements.
///
/// This is the substrate-neutral form of "follow incoming connections to this
/// element." For binary graphs the predecessors are the sources of incoming
/// edges; for hypergraphs they are the vertices that reach this vertex
/// through outgoing hyperedges. Substrate-agnostic algorithms (reverse BFS,
/// reverse reachability) bind on this trait so the same code drives any
/// topology view that can answer the question.
///
/// Implementations define whether parallel connections produce repeated
/// predecessor elements. Implementations that preserve multiplicity should
/// document that behavior; consumers that need set semantics should
/// deduplicate at their own level.
///
/// # Performance
///
/// Creating the iterator should be `O(1)` unless an implementation documents a
/// weaker contract. Yielding `k` predecessors should be `O(k)` and should not
/// allocate unless the implementation documents otherwise.