universal-weave 0.5.0

General-purpose building blocks for Loom implementations.
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
//! General-purpose building blocks for [Loom](https://generative.ink/posts/loom-interface-to-the-multiverse/) implementations.
//!
//! This library aims to make building Loom implementations easier by providing the following primitives:
//! - [`DependentWeave`](dependent::DependentWeave) - A tree-based [`Weave`] where each [`Node`] depends on the contents of the previous Node.
//!     - [`DependentLoroWeave`](dependent::loro::DependentLoroWeave) - A [`DependentWeave`](dependent::DependentWeave) wrapper which adds collaborative editing using the [`loro`] CRDT library (requires `rkyv` and `loro` features to be enabled).
//! - [`IndependentWeave`](independent::IndependentWeave) - A DAG-based [`Weave`] where each [`Node`] does *not* depend on the contents of the previous Node.
//!
//! Operations on the built-in [`Weave`] implementations always preserve node ordering through the use of [`IndexSet`](indexmap::IndexSet), and (non-tail) insertion and removal operations on ordered sets can have a worst-case time complexity of O(n).
//!
//! Efficient (de)serialization is supported using `rkyv` and `serde`. Basic functionality for versioning serialized data is provided by [`VersionedBytes`](versioning::VersionedBytes) (requires `rkyv` feature to be enabled).

#![no_std]
#![forbid(non_ascii_idents)]
#![warn(missing_docs)]
#![warn(let_underscore)]
#![warn(unsafe_code)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![allow(clippy::multiple_crate_versions, reason = "Unresolvable")]
#![warn(clippy::nursery)]
#![warn(clippy::restriction)]
#![allow(clippy::blanket_clippy_restriction_lints, reason = "Conflicting lint")]
#![allow(clippy::allow_attributes, reason = "Conflicting lint")]
#![allow(clippy::pattern_type_mismatch, reason = "Conflicting lint")]
#![allow(clippy::separated_literal_suffix, reason = "Conflicting lint")]
#![allow(clippy::semicolon_outside_block, reason = "Conflicting lint")]
#![allow(
    clippy::field_scoped_visibility_modifiers,
    reason = "Used by IndependentWeave::from()"
)]
#![allow(
    clippy::missing_inline_in_public_items,
    reason = "Reasonable candidates have already been inlined"
)]
#![allow(clippy::inline_always, reason = "Performance")]
#![allow(clippy::exhaustive_enums, reason = "API")]
#![allow(clippy::exhaustive_structs, reason = "API")]
#![allow(clippy::little_endian_bytes, reason = "API")]
#![allow(clippy::partial_pub_fields, reason = "API")]
#![allow(clippy::pub_use, reason = "API")]
#![allow(clippy::arbitrary_source_item_ordering, reason = "Readability")]
#![allow(clippy::question_mark_used, reason = "Readability")]
#![allow(clippy::single_call_fn, reason = "Readability")]
#![allow(clippy::single_char_lifetime_names, reason = "Readability")]
#![allow(clippy::else_if_without_else, reason = "Style")]
#![allow(clippy::if_then_some_else_none, reason = "Style")]
#![allow(clippy::implicit_return, reason = "Style")]
#![allow(clippy::min_ident_chars, reason = "Style")]
#![allow(clippy::mod_module_files, reason = "Style")]
#![allow(clippy::module_name_repetitions, reason = "Style")]
#![allow(clippy::multiple_inherent_impl, reason = "Style")]
#![allow(clippy::try_err, reason = "Style")]
#![allow(clippy::allow_attributes_without_reason)] // TODO
#![allow(clippy::indexing_slicing)] // TODO
#![allow(clippy::unwrap_in_result)] // TODO
#![allow(clippy::unwrap_used)] // TODO
#![allow(clippy::missing_docs_in_private_items)] // TODO
#![allow(clippy::shadow_unrelated)] // TODO
#![allow(clippy::shadow_reuse)] // TODO
#![allow(clippy::shadow_same)] // TODO

/*

Testing notes:
- When running multiple tests, use `cargo nextest run` instead of `cargo test`
- Test building for no_std using `cargo build --target=aarch64-unknown-none --no-default-features --features serde,rkyv,legacy`
- The following tests continue to function in release mode:
    - layout_reference
    - layout_dependent
    - layout_independent
    - archived_dependent
    - archived_independent
    - dependent_behavior_unchanged
    - independent_behavior_unchanged
    - independent_extends_dependent

*/

mod contract;
pub mod dependent;
pub mod independent;
pub mod wrappers;

#[cfg(all(
    feature = "layout",
    any(target_pointer_width = "32", target_pointer_width = "64")
))]
pub mod layout;

#[cfg(feature = "rkyv")]
pub mod versioning;

pub use contracts;
pub use hashbrown;
pub use indexmap;

#[cfg(feature = "layout")]
pub use glam;

#[cfg(feature = "layout")]
pub use tinyvec;

#[cfg(feature = "rkyv")]
pub use rkyv;

#[cfg(feature = "serde")]
pub use serde;

#[cfg(feature = "loro")]
pub use loro;

extern crate alloc;

use alloc::vec::Vec;
use core::{
    cmp::{Ordering, Reverse},
    hash::{BuildHasher, Hash},
};

use hashbrown::{HashMap, hash_map::Entry};
use scratchpads::{ScratchpadMap, ScratchpadSet, ScratchpadVec};

#[cfg(feature = "rkyv")]
use rkyv::collections::swiss_table::{ArchivedHashMap, ArchivedIndexSet};

/// An item within a [`Weave`] which can be connected to other items.
#[must_use]
pub trait Node<K, T>
where
    K: Hash + Copy + Eq + Ord,
{
    /// Identifiers corresponding to the node's parents without duplicates.
    type From;
    /// Identifiers corresponding to the node's children without duplicates.
    type To;

    /// Returns the node's unique identifier.
    #[must_use]
    fn id(&self) -> K;
    /// Returns a reference to the identifiers corresponding to the node's parents.
    #[must_use]
    fn from(&self) -> &Self::From;
    /// Returns a reference to the identifiers corresponding to the node's children.
    #[must_use]
    fn to(&self) -> &Self::To;
    /// Returns `true` if the node is considered active.
    ///
    /// The meaning of this value can depend on the underlying [`Weave`] implementation.
    #[must_use]
    fn is_active(&self) -> bool;
    /// Returns a reference to the node's contents.
    #[must_use]
    fn contents(&self) -> &T;
}

/// [`Node`] contents which can be split apart or merged together.
pub trait DiscreteContents: Sized {
    /// Splits the item at specified index.
    ///
    /// If splitting the item fails, the original contents are returned.
    fn split(self, at: usize) -> DiscreteContentResult<Self>;
    /// Merges two items together.
    ///
    /// If merging the two items fails, the original contents are returned in the order they were specified in.
    fn merge(self, value: Self) -> DiscreteContentResult<Self>;
}

/// A type representing the results of an action on a [`DiscreteContents`] item.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[allow(missing_docs, reason = "Enum items are self-explanatory")]
#[must_use]
pub enum DiscreteContentResult<T> {
    One(T),
    Two(T, T),
}

impl DiscreteContents for () {
    fn split(self, _at: usize) -> DiscreteContentResult<Self> {
        DiscreteContentResult::Two((), ())
    }
    fn merge(self, _value: Self) -> DiscreteContentResult<Self> {
        DiscreteContentResult::One(())
    }
}

/// [`Node`] contents which do not depend on the contents of other [`Node`] objects in order to be meaningful.
pub trait IndependentContents {}

impl IndependentContents for () {}

/// [`Node`] contents which can be meaningfully deduplicated.
///
/// Deduplication must be symmetric: `a.is_duplicate_of(b)` implies `b.is_duplicate_of(a)`.
pub trait DeduplicatableContents {
    /// Tests if `self` and `other` should be considered duplicates of each other.
    #[must_use]
    fn is_duplicate_of(&self, other: &Self) -> bool;
}

/// A document linking together multiple [`Node`] objects without cyclical links.
///
/// # Deserialization
///
/// If a Weave implementation supports deserialization, it must validate internal consistency during the deserialization process in a way which is robust to untrusted inputs.
///
/// # Panics
///
/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
#[must_use]
pub trait Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Mapping between identifiers and nodes.
    type Nodes;
    /// Identifiers of root nodes (nodes which do not have any parents) without duplicates.
    type Roots;

    /// Returns the number of nodes stored within the Weave.
    #[must_use]
    fn len(&self) -> usize;
    /// Returns `true` if the Weave does not contain any nodes.
    #[must_use]
    fn is_empty(&self) -> bool;
    /// Returns a reference to the identifier:node mapping.
    #[must_use]
    fn nodes(&self) -> &Self::Nodes;
    /// Returns a reference to the identifiers of root nodes (nodes which do not have any parents).
    #[must_use]
    fn roots(&self) -> &Self::Roots;
    /// Returns `true` if the Weave contains a node with the specified identifier.
    #[must_use]
    fn contains(&self, id: &K) -> bool;
    /// Returns `true` if the Weave contains an active node (`node.is_active() == true`) with the specified identifier.
    ///
    /// The meaning of this value can depend on the underlying Weave implementation.
    #[must_use]
    fn contains_active(&self, id: &K) -> bool;
    /// Returns a reference to the node corresponding to the identifier.
    #[must_use]
    fn get(&self, id: &K) -> Option<&N>;
    /// Convenience method for `self.get(id).map(Node::from)`.
    #[must_use]
    fn get_parents(&self, id: &K) -> Option<&N::From>;
    /// Convenience method for `self.get(id).map(Node::to)`.
    #[must_use]
    fn get_children(&self, id: &K) -> Option<&N::To>;
    /// Convenience method for `self.get(id).map(Node::contents)`.
    #[must_use]
    fn get_contents(&self, id: &K) -> Option<&T>;
    /// Builds a list of all node identifiers ordered by their positions in the Weave without duplicates.
    fn get_ordered_identifiers(&mut self, output: &mut Vec<K>);
    /// Recursively builds a list of all children of the specified node ordered by their positions in the Weave without duplicates.
    ///
    /// The returned list starts with the identifier of the specified node.
    fn get_ordered_identifiers_from(&mut self, id: &K, output: &mut Vec<K>);
    /// Builds a path through the Weave starting at the deepest active node and ending at a root node.
    ///
    /// In an [`ActivePathWeave`], this path will be the longest contiguous path of active nodes.
    fn get_active_path(&mut self, output: &mut Vec<K>);
    /// Builds a path through the Weave starting at the specified node and ending at a root node.
    ///
    /// In an [`ActivePathWeave`], this path will preferentially route through the active path.
    fn get_path_from(&mut self, id: &K, output: &mut Vec<K>);
    /// Inserts a node into the Weave, returning `true` if the insertion was successful.
    ///
    /// This function may change the active status of nodes if it is necessary to preserve internal consistency.
    fn insert(&mut self, node: N) -> bool;
    /// Sets the active status of a node with the specified identifier.
    ///
    /// This function may change the active status of other nodes in an implementation-specific manner if it is necessary to preserve internal consistency.
    fn set_active(&mut self, id: &K, value: bool) -> bool;
    /// Removes a node with the specified identifier, returning its value if it was present within the Weave.
    ///
    /// This function may remove or update other nodes if it is necessary to preserve internal consistency.
    ///
    /// This function uses the same removal logic as [`Weave::remove_tracked`].
    fn remove(&mut self, id: &K) -> Option<N>;
    /// Removes a node with the specified identifier, returning `true` if it was present within the Weave.
    ///
    /// This function may remove or update other nodes if it is necessary to preserve internal consistency. Every removed node will be returned by the `on_removal` call, with removal ordering being defined by the `Weave` implementation.
    ///
    /// # Panics
    ///
    /// May panic if `on_removal` panics.
    fn remove_tracked(&mut self, id: &K, on_removal: impl FnMut(N)) -> bool;
    /// Removes all nodes from the Weave.
    ///
    /// In a [`MetadataWeave`], the associated metadata is left unchanged.
    fn clear(&mut self);
}

/// A [`Weave`] containing document-wide metadata.
///
/// # Panics
///
/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
pub trait MetadataWeave<K, N, T, M>: Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Returns a reference to the Weave's associated metadata.
    #[must_use]
    fn metadata(&self) -> &M;
    /// Mutable access to the Weave's associated metadata.
    ///
    /// # Panics
    ///
    /// May panic if `callback` panics.
    fn metadata_mut<O>(&mut self, callback: impl FnOnce(&mut M) -> O) -> O;
}

/// A [`Weave`] where nodes can be bookmarked.
pub trait BookmarkableWeave<K, N, T>: Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Identifiers of bookmarked nodes.
    type Bookmarks;

    /// Returns a reference to the identifiers of bookmarked nodes.
    #[must_use]
    fn bookmarks(&self) -> &Self::Bookmarks;
    /// Returns `true` if the Weave contains a bookmarked node with the specified identifier.
    #[must_use]
    fn contains_bookmark(&self, id: &K) -> bool;
    /// Sets the bookmarked status of a node with the specified identifier.
    fn set_bookmarked(&mut self, id: &K, value: bool) -> bool;
}

/// A [`Weave`] where the ordering of nodes is stable and can be user-defined.
///
/// # Panics
///
/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
pub trait SortableWeave<K, N, T>: Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Sorts the child nodes of a parent node with the specified identifier using the comparison function `cmp`.
    ///
    /// # Panics
    ///
    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
    fn sort_children_by(&mut self, id: &K, cmp: impl FnMut(&N, &N) -> Ordering) -> bool;
    /// Sorts the identifiers of a parent node's children with the specified identifier using the comparison function `cmp`.
    ///
    /// # Panics
    ///
    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
    fn sort_children_by_id(&mut self, id: &K, cmp: impl FnMut(&K, &K) -> Ordering) -> bool;
    /// Sorts root nodes (nodes which do not have any parents) using the comparison function `cmp`.
    ///
    /// # Panics
    ///
    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
    fn sort_roots_by(&mut self, cmp: impl FnMut(&N, &N) -> Ordering);
    /// Sorts the identifiers of root nodes (nodes which do not have any parents) using the comparison function `cmp`.
    ///
    /// # Panics
    ///
    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
    fn sort_roots_by_id(&mut self, cmp: impl FnMut(&K, &K) -> Ordering);
}

/// A [`Weave`] where the ordering of bookmarked nodes is stable and can be user-defined.
///
/// # Panics
///
/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
pub trait SortableBookmarkableWeave<K, N, T>:
    BookmarkableWeave<K, N, T> + SortableWeave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Sorts bookmarked nodes using the comparison function `cmp`.
    ///
    /// # Panics
    ///
    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
    fn sort_bookmarks_by(&mut self, cmp: impl FnMut(&N, &N) -> Ordering);
    /// Sorts the identifiers of bookmarked nodes using the comparison function `cmp`.
    ///
    /// # Panics
    ///
    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
    fn sort_bookmarks_by_id(&mut self, cmp: impl FnMut(&K, &K) -> Ordering);
}

/// A [`Weave`] where only one [`Node`] can be considered active at a time.
pub trait ActiveSingularWeave<K, N, T>: Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Returns the active node's identifier, if any.
    #[must_use]
    fn active(&self) -> Option<K>;
}

/// A [`Weave`] where every [`Node`] in the active path is always considered active.
pub trait ActivePathWeave<K, N, T>: Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Identifiers of active nodes.
    type Active;

    /// Returns a reference to the identifiers of active nodes.
    #[must_use]
    fn active(&self) -> &Self::Active;
    /// Replaces the currently active path with the specified set of node IDs.
    ///
    /// If the new active path would result in internal inconsistency, this function will correct the path in an implementation-specific manner.
    fn set_active_path(&mut self, active: impl Iterator<Item = K>);
}

/// A [`Weave`] where [`Node`] objects do not depend on their parents in order to be meaningful.
pub trait IndependentWeave<K, N, T>: Weave<K, N, T> + SemiIndependentWeave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
    T: IndependentContents,
{
    /// Moves a node with the specified identifier to a new set of parent nodes, returning `true` if the move was successful.
    ///
    /// This function may change the active status of other nodes if it is necessary to preserve internal consistency.
    fn move_to(&mut self, id: &K, new_parents: &[K]) -> bool;
}

/// A [`Weave`] where [`Node`] objects do not depend on the *contents* of their parents in order to be meaningful.
///
/// # Panics
///
/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
pub trait SemiIndependentWeave<K, N, T>: Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
    T: IndependentContents,
{
    /// Mutable access to the contents of a node with the specified identifier.
    ///
    /// Returns `Some` if the node's contents were successfully updated.
    ///
    /// # Panics
    ///
    /// May panic if `callback` panics.
    #[must_use]
    fn get_contents_mut<O>(&mut self, id: &K, callback: impl FnOnce(&mut T) -> O) -> Option<O>;
}

/// A [`Weave`] where the contents of [`Node`] objects can be split and merged.
///
/// # Panics
///
/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
pub trait DiscreteWeave<K, N, T>: Weave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
    T: DiscreteContents,
{
    /// Splits a node with the specified identifier at the given index, creating a new child node with the identifier `new_id`.
    ///
    /// If the target node is at the end of the active path, the right side of the split will be inactive.
    ///
    /// Returns `false` if splitting the node failed.
    ///
    /// # Panics
    ///
    /// May panic if `T::split` panics.
    fn split(&mut self, id: &K, at: usize, new_id: K) -> bool;
    /// Merges a node with the specified identifier with its parent, with the newly merged node inheriting the parent's identifier.
    ///
    /// Returns the identifier of the merged node if merging was successful.
    ///
    /// # Panics
    ///
    /// May panic if `T::merge` panics.
    fn merge_with_parent(&mut self, id: &K) -> Option<K>;
}

/// A geometric item within an arrangement of a Weave's content.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LayoutItem<K, V, P> {
    /// Computed geometry for a [`Node`].
    Node {
        /// Node identifier.
        id: K,
        /// Node center position.
        center: V,
        /// Node size.
        size: V,
    },
    /// Computed geometry for a connection between [`Node`]s.
    Polyline {
        /// Parent node where the connection starts.
        from: K,
        /// Child node where the connection ends.
        to: K,
        /// Points for a polyline routed between the two nodes.
        points: P,
    },
}

/// An algorithm which arranges a [`Weave`]'s content for graphical rendering in an efficiently accessible form.
///
/// # Panics
///
/// All panics should be assumed to leave the Layouter and Weave in a malformed state unless otherwise specified by the implementation.
pub trait Layouter<W, K, N, T, V, P>
where
    W: Weave<K, N, T>,
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Arranges a [`Weave`]'s content for graphical rendering using a closure which maps [`Node`]s to their dimensions.
    ///
    /// # Panics
    ///
    /// Panics if the [`Weave`]'s content could not be arranged due to an unsatisfiable constraint or numerical overflow.
    ///
    /// May panic if `map` panics or if the underlying [`Weave`] is improperly implemented.
    fn layout(&mut self, weave: &mut W, sizes: impl FnMut(&K) -> V);
    /// Returns the size of the bounding box enclosing the arrangement's content.
    fn size(&self) -> V;
    /// Returns [`LayoutItem`]s within the specified bounds in the order that they should be rendered.
    fn view(&mut self, min: V, max: V, callback: impl FnMut(LayoutItem<K, V, P>));
}

/// A read-only [`Weave`].
#[must_use]
pub trait ImmutableWeave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Mapping between identifiers and nodes.
    type Nodes;
    /// Identifiers of root nodes (nodes which do not have any parents) without duplicates.
    type Roots;

    /// Returns the number of nodes stored within the Weave.
    #[must_use]
    fn len(&self) -> usize;
    /// Returns `true` if the Weave does not contain any nodes.
    #[must_use]
    fn is_empty(&self) -> bool;
    /// Returns a reference to the identifier:node mapping.
    #[must_use]
    fn nodes(&self) -> &Self::Nodes;
    /// Returns a reference to the identifiers of root nodes (nodes which do not have any parents).
    #[must_use]
    fn roots(&self) -> &Self::Roots;
    /// Returns `true` if the Weave contains a node with the specified identifier.
    #[must_use]
    fn contains(&self, id: &K) -> bool;
    /// Returns `true` if the Weave contains an active node (`node.is_active() == true`) with the specified identifier.
    ///
    /// The meaning of this value can depend on the underlying Weave implementation.
    #[must_use]
    fn contains_active(&self, id: &K) -> bool;
    /// Returns a reference to the node corresponding to the identifier.
    #[must_use]
    fn get(&self, id: &K) -> Option<&N>;
    /// Convenience method for `self.get(id).map(Node::from)`.
    #[must_use]
    fn get_parents(&self, id: &K) -> Option<&N::From>;
    /// Convenience method for `self.get(id).map(Node::to)`.
    #[must_use]
    fn get_children(&self, id: &K) -> Option<&N::To>;
    /// Convenience method for `self.get(id).map(Node::contents)`.
    #[must_use]
    fn get_contents(&self, id: &K) -> Option<&T>;
    /// Builds a list of all node identifiers ordered by their positions in the Weave without duplicates.
    fn get_ordered_identifiers(&self, output: &mut Vec<K>);
    /// Recursively builds a list of all children of the specified node ordered by their positions in the Weave without duplicates.
    ///
    /// The returned list starts with the identifier of the specified node.
    fn get_ordered_identifiers_from(&self, id: &K, output: &mut Vec<K>);
    /// Builds a path through the Weave starting at the deepest active node and ending at a root node.
    ///
    /// In an [`ImmutableActivePathWeave`], this path will be the longest contiguous path of active nodes.
    fn get_active_path(&self, output: &mut Vec<K>);
    /// Builds a path through the Weave starting at the specified node and ending at a root node.
    ///
    /// In an [`ImmutableActivePathWeave`], this path will preferentially route through the active path.
    fn get_path_from(&self, id: &K, output: &mut Vec<K>);
}

/// An [`ImmutableWeave`] containing document-wide metadata.
pub trait ImmutableMetadataWeave<K, N, T, M>: ImmutableWeave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Returns a reference to the Weave's associated metadata.
    #[must_use]
    fn metadata(&self) -> &M;
}

/// An [`ImmutableWeave`] where nodes can be bookmarked.
pub trait ImmutableBookmarkableWeave<K, N, T>: ImmutableWeave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Identifiers of bookmarked nodes.
    type Bookmarks;

    /// Returns a reference to the identifiers of bookmarked nodes.
    #[must_use]
    fn bookmarks(&self) -> &Self::Bookmarks;
    /// Returns `true` if the Weave contains a bookmarked node with the specified identifier.
    #[must_use]
    fn contains_bookmark(&self, id: &K) -> bool;
}

/// An [`ImmutableWeave`] where only one [`Node`] can be considered active at a time.
pub trait ImmutableActiveSingularWeave<K, N, T>: ImmutableWeave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Returns the active node's identifier, if any.
    #[must_use]
    fn active(&self) -> Option<K>;
}

/// An [`ImmutableWeave`] where every [`Node`] in the active path is always considered active.
pub trait ImmutableActivePathWeave<K, N, T>: ImmutableWeave<K, N, T>
where
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Identifiers of active nodes.
    type Active;

    /// Returns a reference to the identifiers of active nodes.
    #[must_use]
    fn active(&self) -> &Self::Active;
}

/// An algorithm which arranges an [`ImmutableWeave`]'s content for graphical rendering in an efficiently accessible form.
///
/// # Panics
///
/// All panics should be assumed to leave the Layouter in a malformed state unless otherwise specified by the implementation.
pub trait ImmutableLayouter<W, K, N, T, V, P>
where
    W: ImmutableWeave<K, N, T>,
    K: Hash + Copy + Eq + Ord,
    N: Node<K, T>,
{
    /// Arranges an [`ImmutableWeave`]'s content for graphical rendering using a closure which maps [`Node`]s to their dimensions.
    ///
    /// # Panics
    ///
    /// Panics if the [`ImmutableWeave`]'s content could not be arranged due to an unsatisfiable constraint or numerical overflow.
    ///
    /// May panic if `map` panics or if the underlying [`ImmutableWeave`] is improperly implemented.
    fn layout(&mut self, weave: &W, sizes: impl FnMut(&K) -> V);
    /// Returns the size of the bounding box enclosing the arrangement's content.
    fn size(&self) -> V;
    /// Returns [`LayoutItem`]s within the specified bounds in the order that they should be rendered.
    fn view(&mut self, min: V, max: V, callback: impl FnMut(LayoutItem<K, V, P>));
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Step<A, B> {
    Enter(A),
    Exit(B),
}

fn topological_sort<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    roots: impl DoubleEndedIterator<Item = K>,
    stack: &mut ScratchpadVec<'_, K>,
    mut identifier_callback: impl FnMut(K),
    identifier_map: &mut ScratchpadMap<'_, K, usize, S>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T> + 'a,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator + ExactSizeIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    identifier_map.extend(nodes.iter().map(|(&k, n)| (k, n.from().into_iter().len())));

    stack.extend(roots.rev());

    while let Some(id) = stack.pop() {
        identifier_callback(id);

        for child in nodes[&id].to().into_iter().rev().copied() {
            let remaining = identifier_map.get_mut(&child).unwrap();
            #[allow(clippy::arithmetic_side_effects, reason = "Can never underflow")]
            {
                *remaining -= 1;
            }

            if *remaining == 0 {
                stack.push(child);
            }
        }
    }
}

#[cfg(feature = "rkyv")]
fn archived_topological_sort<'a, K, N, T, S>(
    nodes: &'a ArchivedHashMap<K, N>,
    roots: &'a ArchivedIndexSet<K>,
    stack: &mut ScratchpadVec<'_, K>,
    mut identifier_callback: impl FnMut(K),
    identifier_map: &mut ScratchpadMap<'_, K, usize, S>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T, From = ArchivedIndexSet<K>, To = ArchivedIndexSet<K>> + 'a,
    S: BuildHasher + Default + Clone,
{
    identifier_map.extend(nodes.iter().map(|(&k, n)| (k, n.from().len())));

    stack.extend(archived_set_reverse_order(roots));

    while let Some(id) = stack.pop() {
        identifier_callback(id);

        for child in archived_set_reverse_order(nodes[&id].to()).copied() {
            let remaining = identifier_map.get_mut(&child).unwrap();
            #[allow(clippy::arithmetic_side_effects, reason = "Can never underflow")]
            {
                *remaining -= 1;
            }

            if *remaining == 0 {
                stack.push(child);
            }
        }
    }
}

fn topological_sort_subgraph<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    filter: impl Fn(&K) -> bool,
    subgraph_root: K,
    stack: &mut ScratchpadVec<'_, K>,
    mut identifier_callback: impl FnMut(K),
    identifier_map: &mut ScratchpadMap<'_, K, usize, S>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T> + 'a,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    /*if filter(id)
        && !identifier_map.contains_key(id)
        && nodes[id]
            .from()
            .into_iter()
            .filter(|&parent| filter(parent))
            .count()
            == 0
    {
        stack.push(*id);
    }*/

    stack.push(subgraph_root);

    while let Some(id) = stack.pop() {
        identifier_callback(id);

        for child in nodes[&id].to().into_iter().rev().copied() {
            if !filter(&child) {
                continue;
            }

            let remaining = identifier_map.entry(child).or_insert_with(|| {
                nodes[&child]
                    .from()
                    .into_iter()
                    .filter(|&parent| filter(parent))
                    .count()
            });
            #[allow(clippy::arithmetic_side_effects, reason = "Can never underflow")]
            {
                *remaining -= 1;
            }

            if *remaining == 0 {
                stack.push(child);
            }
        }
    }
}

#[cfg(feature = "rkyv")]
fn archived_topological_sort_subgraph<'a, K, N, T, S>(
    nodes: &'a ArchivedHashMap<K, N>,
    filter: impl Fn(&K) -> bool,
    subgraph_root: K,
    stack: &mut ScratchpadVec<'_, K>,
    mut identifier_callback: impl FnMut(K),
    identifier_map: &mut ScratchpadMap<'_, K, usize, S>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T, From = ArchivedIndexSet<K>, To = ArchivedIndexSet<K>> + 'a,
    S: BuildHasher + Default + Clone,
{
    /*if filter(id)
        && !identifier_map.contains_key(id)
        && nodes[id]
            .from()
            .into_iter()
            .filter(|&parent| filter(parent))
            .count()
            == 0
    {
        stack.push(*id);
    }*/

    stack.push(subgraph_root);

    while let Some(id) = stack.pop() {
        identifier_callback(id);

        for child in archived_set_reverse_order(nodes[&id].to()).copied() {
            if !filter(&child) {
                continue;
            }

            let remaining = identifier_map.entry(child).or_insert_with(|| {
                nodes[&child]
                    .from()
                    .iter()
                    .filter(|&parent| filter(parent))
                    .count()
            });
            #[allow(clippy::arithmetic_side_effects, reason = "Can never underflow")]
            {
                *remaining -= 1;
            }

            if *remaining == 0 {
                stack.push(child);
            }
        }
    }
}

fn shortest_path_to_ancestor<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    id: &'a K,
    target: impl Fn(&'a N) -> bool,
    scratchpad: &mut ScratchpadVec<'_, K>,
    scratchpad_map: &mut ScratchpadMap<'_, K, K, S>,
    path: &mut Vec<K>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T> + 'a,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    scratchpad.push(*id);
    scratchpad_map.insert(*id, *id);

    let mut head = 0;

    while head < scratchpad.len() {
        let id = scratchpad[head];
        #[allow(clippy::arithmetic_side_effects, reason = "Can never overflow")]
        {
            head += 1;
        }

        let node = &nodes[&id];

        if target(node) {
            path.push(id);
            break;
        }

        for parent in node.from().into_iter().copied() {
            if let Entry::Vacant(entry) = scratchpad_map.entry(parent) {
                entry.insert(id);
                scratchpad.push(parent);
            }
        }
    }

    while let Some(last) = path.last()
        && last != id
    {
        path.push(scratchpad_map[last]);
    }
}

#[cfg(feature = "rkyv")]
fn archived_shortest_path_to_ancestor<'a, K, N, T, S>(
    nodes: &'a ArchivedHashMap<K, N>,
    id: &'a K,
    target: impl Fn(&'a N) -> bool,
    scratchpad: &mut ScratchpadVec<'_, K>,
    scratchpad_map: &mut ScratchpadMap<'_, K, K, S>,
    path: &mut Vec<K>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T, From = ArchivedIndexSet<K>> + 'a,
    S: BuildHasher + Default + Clone,
{
    scratchpad.push(*id);
    scratchpad_map.insert(*id, *id);

    let mut head = 0;

    while head < scratchpad.len() {
        let id = scratchpad[head];
        #[allow(clippy::arithmetic_side_effects, reason = "Can never overflow")]
        {
            head += 1;
        }

        let node = &nodes[&id];

        if target(node) {
            path.push(id);
            break;
        }

        for parent in node.from().iter().copied() {
            if let Entry::Vacant(entry) = scratchpad_map.entry(parent) {
                entry.insert(id);
                scratchpad.push(parent);
            }
        }
    }

    while let Some(last) = path.last()
        && last != id
    {
        path.push(scratchpad_map[last]);
    }
}

fn longest_candidate_path_to_root<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    topological_order: &[K],
    is_candidate: impl Fn(&K) -> bool,
    scratchpad_map: &mut ScratchpadMap<'_, K, (usize, K), S>,
    mut reversed_path_callback: impl FnMut(K),
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T> + 'a,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator + ExactSizeIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    let mut longest_distance = None;

    for id in topological_order {
        if !is_candidate(id) {
            continue;
        }

        let from = nodes[id].from().into_iter();

        let has_parents = from.len() != 0;
        let best_parent = from
            .filter_map(|id| scratchpad_map.get(id).map(|v| (v.0, id)))
            .min_by_key(|&(v, _)| Reverse(v));

        #[allow(clippy::arithmetic_side_effects, reason = "Can never overflow")]
        let distance = match best_parent {
            Some((parent_distance, parent)) => Some((parent_distance + 1, *parent)),
            None => {
                if has_parents {
                    None
                } else {
                    Some((0, *id))
                }
            }
        };

        if let Some((distance, parent)) = distance {
            scratchpad_map.insert(*id, (distance, parent));

            if longest_distance.is_none_or(|(value, _)| distance > value) {
                longest_distance = Some((distance, *id));
            }
        }
    }

    if let Some(mut id) = longest_distance.map(|(_, id)| id) {
        loop {
            reversed_path_callback(id);

            let parent = scratchpad_map[&id].1;
            if parent == id {
                break;
            }
            id = parent;
        }
    }
}

#[cfg(feature = "rkyv")]
fn archived_longest_candidate_path_to_root<'a, K, N, T, S>(
    nodes: &'a ArchivedHashMap<K, N>,
    topological_order: &'a [K],
    is_candidate: impl Fn(&K) -> bool,
    scratchpad_map: &mut ScratchpadMap<'_, K, (usize, K), S>,
    mut reversed_path_callback: impl FnMut(K),
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T, From = ArchivedIndexSet<K>> + 'a,
    S: BuildHasher + Default + Clone,
{
    let mut longest_distance = None;

    for id in topological_order {
        if !is_candidate(id) {
            continue;
        }

        let from = nodes[id].from();

        let has_parents = !from.is_empty();
        let best_parent = from
            .iter()
            .filter_map(|id| scratchpad_map.get(id).map(|v| (v.0, id)))
            .min_by_key(|&(v, _)| Reverse(v));

        #[allow(clippy::arithmetic_side_effects, reason = "Can never overflow")]
        let distance = match best_parent {
            Some((parent_distance, parent)) => Some((parent_distance + 1, *parent)),
            None => {
                if has_parents {
                    None
                } else {
                    Some((0, *id))
                }
            }
        };

        if let Some((distance, parent)) = distance {
            scratchpad_map.insert(*id, (distance, parent));

            if longest_distance.is_none_or(|(value, _)| distance > value) {
                longest_distance = Some((distance, *id));
            }
        }
    }

    if let Some(mut id) = longest_distance.map(|(_, id)| id) {
        loop {
            reversed_path_callback(id);

            let parent = scratchpad_map[&id].1;
            if parent == id {
                break;
            }
            id = parent;
        }
    }
}

fn ancestor_subgraph<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    id: K,
    stack: &mut ScratchpadVec<'_, K>,
    identifiers: &mut ScratchpadSet<'_, K, S>,
    mut root_callback: impl FnMut(K),
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T>,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator + ExactSizeIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    if identifiers.insert(id) {
        stack.push(id);
    }

    while let Some(id) = stack.pop() {
        let from = nodes[&id].from().into_iter();

        if from.len() == 0 {
            root_callback(id);
        } else {
            for parent in from.rev().copied() {
                if identifiers.insert(parent) {
                    stack.push(parent);
                }
            }
        }
    }
}

fn ancestor_subgraph_reaches<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    ids: impl DoubleEndedIterator<Item = K>,
    target: impl Fn(&K) -> bool,
    stack: &mut ScratchpadVec<'_, K>,
    identifiers: &mut ScratchpadSet<'_, K, S>,
) -> bool
where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T>,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    for id in ids.rev() {
        if identifiers.insert(id) {
            if target(&id) {
                return true;
            }

            stack.push(id);
        }
    }

    while let Some(id) = stack.pop() {
        for parent in nodes[&id].from().into_iter().rev().copied() {
            if identifiers.insert(parent) {
                if target(&parent) {
                    return true;
                }

                stack.push(parent);
            }
        }
    }

    false
}

#[cfg(feature = "rkyv")]
fn archived_ancestor_subgraph<'a, K, N, T, S>(
    nodes: &'a ArchivedHashMap<K, N>,
    id: K,
    stack: &mut ScratchpadVec<'_, K>,
    identifiers: &mut ScratchpadSet<'_, K, S>,
    mut root_callback: impl FnMut(K),
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T, From = ArchivedIndexSet<K>> + 'a,
    S: BuildHasher + Default + Clone,
{
    if identifiers.insert(id) {
        stack.push(id);
    }

    while let Some(id) = stack.pop() {
        let from = nodes[&id].from();

        if from.is_empty() {
            root_callback(id);
        } else {
            for parent in archived_set_reverse_order(from).copied() {
                if identifiers.insert(parent) {
                    stack.push(parent);
                }
            }
        }
    }
}

fn descendant_subgraph<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    id: K,
    stack: &mut ScratchpadVec<'_, K>,
    identifiers: &mut ScratchpadSet<'_, K, S>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T>,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    if identifiers.insert(id) {
        stack.push(id);
    }

    while let Some(id) = stack.pop() {
        for child in nodes[&id].to().into_iter().rev().copied() {
            if identifiers.insert(child) {
                stack.push(child);
            }
        }
    }
}

fn descendant_subgraph_reaches<'a, K, N, T, S>(
    nodes: &'a HashMap<K, N, S>,
    ids: impl DoubleEndedIterator<Item = K>,
    target: impl Fn(&K) -> bool,
    stack: &mut ScratchpadVec<'_, K>,
    identifiers: &mut ScratchpadSet<'_, K, S>,
) -> bool
where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T>,
    <N as Node<K, T>>::From: 'a,
    <N as Node<K, T>>::To: 'a,
    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
    S: BuildHasher + Default + Clone,
{
    for id in ids.rev() {
        if identifiers.insert(id) {
            if target(&id) {
                return true;
            }

            stack.push(id);
        }
    }

    while let Some(id) = stack.pop() {
        for child in nodes[&id].to().into_iter().rev().copied() {
            if identifiers.insert(child) {
                if target(&child) {
                    return true;
                }

                stack.push(child);
            }
        }
    }

    false
}

#[cfg(feature = "rkyv")]
fn archived_descendant_subgraph<'a, K, N, T, S>(
    nodes: &'a ArchivedHashMap<K, N>,
    id: K,
    stack: &mut ScratchpadVec<'_, K>,
    identifiers: &mut ScratchpadSet<'_, K, S>,
) where
    K: Hash + Copy + Eq + Ord + 'a,
    N: Node<K, T, To = ArchivedIndexSet<K>> + 'a,
    S: BuildHasher + Default + Clone,
{
    if identifiers.insert(id) {
        stack.push(id);
    }

    while let Some(id) = stack.pop() {
        for child in archived_set_reverse_order(nodes[&id].to()).copied() {
            if identifiers.insert(child) {
                stack.push(child);
            }
        }
    }
}

#[cfg(feature = "rkyv")]
fn archived_set_reverse_order<T>(set: &ArchivedIndexSet<T>) -> impl Iterator<Item = &T> {
    (0..set.len())
        .rev()
        .filter_map(|index| set.get_index(index))
}