Skip to main content

universal_weave/
lib.rs

1//! General-purpose building blocks for [Loom](https://generative.ink/posts/loom-interface-to-the-multiverse/) implementations.
2//!
3//! This library aims to make building Loom implementations easier by providing the following primitives:
4//! - [`dependent::DependentWeave`] - A tree-based [`Weave`] where each [`Node`] depends on the contents of the previous Node.
5//!     - [`dependent::loro::DependentLoroWeave`] - A [`dependent::DependentWeave`] wrapper which adds collaborative editing using the [`loro`] CRDT library (requires `rkyv` and `loro` features to be enabled).
6//! - [`independent::IndependentWeave`] - A DAG-based [`Weave`] where each [`Node`] does *not* depend on the contents of the previous Node.
7//!
8//! Efficient (de)serialization is supported using `rkyv` and `serde`. Basic functionality for versioning serialized data is provided by [`versioning::VersionedBytes`] (requires `rkyv` feature to be enabled).
9//!
10
11#![no_std]
12#![forbid(non_ascii_idents)]
13#![warn(missing_docs)]
14#![warn(let_underscore)]
15#![warn(clippy::pedantic)]
16#![warn(clippy::cargo)]
17#![allow(clippy::multiple_crate_versions, reason = "Unresolvable")]
18#![warn(clippy::nursery)]
19#![warn(clippy::restriction)]
20#![allow(clippy::blanket_clippy_restriction_lints, reason = "Conflicting lint")]
21#![allow(clippy::allow_attributes, reason = "Conflicting lint")]
22#![allow(clippy::pattern_type_mismatch, reason = "Conflicting lint")]
23#![allow(clippy::separated_literal_suffix, reason = "Conflicting lint")]
24#![allow(
25    clippy::field_scoped_visibility_modifiers,
26    reason = "Used by IndependentWeave::from()"
27)]
28#![allow(
29    clippy::missing_inline_in_public_items,
30    reason = "Reasonable candidates have already been inlined"
31)]
32#![allow(clippy::exhaustive_enums, reason = "API")]
33#![allow(clippy::exhaustive_structs, reason = "API")]
34#![allow(clippy::little_endian_bytes, reason = "API")]
35#![allow(clippy::partial_pub_fields, reason = "API")]
36#![allow(clippy::pub_use, reason = "API")]
37#![allow(clippy::arbitrary_source_item_ordering, reason = "Readability")]
38#![allow(clippy::question_mark_used, reason = "Readability")]
39#![allow(clippy::single_call_fn, reason = "Readability")]
40#![allow(clippy::single_char_lifetime_names, reason = "Readability")]
41#![allow(clippy::else_if_without_else, reason = "Style")]
42#![allow(clippy::if_then_some_else_none, reason = "Style")]
43#![allow(clippy::implicit_return, reason = "Style")]
44#![allow(clippy::min_ident_chars, reason = "Style")]
45#![allow(clippy::mod_module_files, reason = "Style")]
46#![allow(clippy::module_name_repetitions, reason = "Style")]
47#![allow(clippy::multiple_inherent_impl, reason = "Style")]
48#![allow(clippy::try_err, reason = "Style")]
49#![allow(clippy::allow_attributes_without_reason)] // TODO
50#![allow(clippy::indexing_slicing)] // TODO
51#![allow(clippy::unwrap_in_result)] // TODO
52#![allow(clippy::unwrap_used)] // TODO
53#![allow(clippy::missing_docs_in_private_items)] // TODO
54#![allow(clippy::shadow_unrelated)] // TODO
55#![allow(clippy::shadow_reuse)] // TODO
56
57mod contract;
58pub mod dependent;
59pub mod independent;
60pub mod wrappers;
61
62#[cfg(feature = "rkyv")]
63pub mod versioning;
64
65pub use contracts;
66pub use hashbrown;
67pub use indexmap;
68
69#[cfg(feature = "rkyv")]
70pub use rkyv;
71
72#[cfg(feature = "loro")]
73pub use loro;
74
75extern crate alloc;
76
77use alloc::{collections::vec_deque::VecDeque, vec::Vec};
78use core::{
79    cmp::{Ordering, Reverse},
80    hash::{BuildHasher, Hash},
81};
82
83use hashbrown::{HashMap, HashSet, hash_map::Entry};
84
85#[cfg(feature = "serde")]
86pub use serde;
87
88/// An item within a [`Weave`] which can be connected to other items.
89#[must_use]
90pub trait Node<K, T>
91where
92    K: Hash + Copy + Eq + Ord,
93{
94    /// Identifiers corresponding to the node's parents.
95    type From;
96    /// Identifiers corresponding to the node's children.
97    type To;
98
99    /// Returns the node's unique identifier.
100    #[must_use]
101    fn id(&self) -> K;
102    /// Returns a reference to the identifiers corresponding to the node's parents.
103    #[must_use]
104    fn from(&self) -> &Self::From;
105    /// Returns a reference to the identifiers corresponding to the node's children.
106    #[must_use]
107    fn to(&self) -> &Self::To;
108    /// Returns `true` if the node is considered active.
109    ///
110    /// The meaning of this value can depend on the underlying [`Weave`] implementation.
111    #[must_use]
112    fn is_active(&self) -> bool;
113    /// Returns a reference to the node's contents.
114    #[must_use]
115    fn contents(&self) -> &T;
116}
117
118/// [`Node`] contents which can be split apart or merged together.
119pub trait DiscreteContents: Sized {
120    /// Splits the item at specified index.
121    ///
122    /// If splitting the item fails, the original contents are returned.
123    fn split(self, at: usize) -> DiscreteContentResult<Self>;
124    /// Merges two items together.
125    ///
126    /// If merging the two items fails, the original contents are returned in the order they were specified in.
127    fn merge(self, value: Self) -> DiscreteContentResult<Self>;
128}
129
130/// A type representing the results of an action on a [`DiscreteContents`] item.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
132#[allow(missing_docs, reason = "Enum items are self-explanatory")]
133#[must_use]
134pub enum DiscreteContentResult<T> {
135    One(T),
136    Two(T, T),
137}
138
139/// [`Node`] contents which do not depend on the contents of other [`Node`] objects in order to be meaningful.
140pub trait IndependentContents {}
141
142/// [`Node`] contents which can be meaningfully deduplicated.
143///
144/// Deduplication must be symmetric: `a.is_duplicate_of(b)` implies `b.is_duplicate_of(a)`.
145pub trait DeduplicatableContents {
146    /// Tests if `self` and `other` should be considered duplicates of each other.
147    #[must_use]
148    fn is_duplicate_of(&self, other: &Self) -> bool;
149}
150
151/// A document linking together multiple [`Node`] objects without cyclical links.
152///
153/// # Deserialization
154///
155/// If a Weave implementation supports deserialization, it must validate internal consistency during the deserialization process in a way which is robust to untrusted inputs.
156///
157/// # Panics
158///
159/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
160#[must_use]
161pub trait Weave<K, N, T>
162where
163    K: Hash + Copy + Eq + Ord,
164    N: Node<K, T>,
165{
166    /// Mapping between identifiers and nodes.
167    type Nodes;
168    /// Identifiers of root nodes (nodes which do not have any parents).
169    type Roots;
170
171    /// Returns the number of nodes stored within the Weave.
172    #[must_use]
173    fn len(&self) -> usize;
174    /// Returns `true` if the Weave does not contain any nodes.
175    #[must_use]
176    fn is_empty(&self) -> bool;
177    /// Returns a reference to the identifier:node mapping.
178    #[must_use]
179    fn nodes(&self) -> &Self::Nodes;
180    /// Returns a reference to the identifiers of root nodes (nodes which do not have any parents).
181    #[must_use]
182    fn roots(&self) -> &Self::Roots;
183    /// Returns `true` if the Weave contains a node with the specified identifier.
184    #[must_use]
185    fn contains(&self, id: &K) -> bool;
186    /// Returns `true` if the Weave contains an active node (`node.is_active() == true`) with the specified identifier.
187    ///
188    /// The meaning of this value can depend on the underlying Weave implementation.
189    #[must_use]
190    fn contains_active(&self, id: &K) -> bool;
191    /// Returns a reference to the node corresponding to the identifier.
192    #[must_use]
193    fn get(&self, id: &K) -> Option<&N>;
194    /// Convenience method for `self.get(id).map(Node::from)`.
195    #[must_use]
196    fn get_parents(&self, id: &K) -> Option<&N::From>;
197    /// Convenience method for `self.get(id).map(Node::to)`.
198    #[must_use]
199    fn get_children(&self, id: &K) -> Option<&N::To>;
200    /// Convenience method for `self.get(id).map(Node::contents)`.
201    #[must_use]
202    fn get_contents(&self, id: &K) -> Option<&T>;
203    /// Builds a list of all node identifiers ordered by their positions in the Weave.
204    fn get_ordered_identifiers(&mut self, output: &mut Vec<K>);
205    /// Recursively builds a list of all children of the specified node ordered by their positions in the Weave.
206    fn get_ordered_identifiers_from(&mut self, id: &K, output: &mut Vec<K>);
207    /// Builds a path through the Weave starting at the deepest active node and ending at a root node.
208    ///
209    /// In an [`ActivePathWeave`], this path will be the longest contiguous path of active nodes.
210    fn get_active_path(&mut self, output: &mut Vec<K>);
211    /// Builds a path through the Weave starting at the specified node and ending at a root node.
212    ///
213    /// In an [`ActivePathWeave`], this path will preferentially route through the active path.
214    fn get_path_from(&mut self, id: &K, output: &mut Vec<K>);
215    /// Inserts a node into the Weave, returning `true` if the insertion was successful.
216    ///
217    /// This function may change the active status of nodes if it is necessary to preserve internal consistency.
218    fn insert(&mut self, node: N) -> bool;
219    /// Sets the active status of a node with the specified identifier.
220    ///
221    /// This function may change the active status of other nodes in an implementation-specific manner if it is necessary to preserve internal consistency.
222    fn set_active(&mut self, id: &K, value: bool) -> bool;
223    /// Removes a node with the specified identifier, returning its value if it was present within the Weave.
224    ///
225    /// This function may remove or update other nodes if it is necessary to preserve internal consistency.
226    ///
227    /// This function uses the same removal logic as [`Weave::remove_tracked`].
228    fn remove(&mut self, id: &K) -> Option<N>;
229    /// Removes a node with the specified identifier, returning `true` if it was present within the Weave.
230    ///
231    /// 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.
232    ///
233    /// # Panics
234    ///
235    /// May panic if `on_removal` panics.
236    fn remove_tracked(&mut self, id: &K, on_removal: impl FnMut(N)) -> bool;
237    /// Removes all nodes from the Weave.
238    ///
239    /// In a [`MetadataWeave`], the associated metadata is left unchanged.
240    fn clear(&mut self);
241}
242
243/// A [`Weave`] containing document-wide metadata.
244///
245/// # Panics
246///
247/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
248pub trait MetadataWeave<K, N, T, M>: Weave<K, N, T>
249where
250    K: Hash + Copy + Eq + Ord,
251    N: Node<K, T>,
252{
253    /// Returns a reference to the Weave's associated metadata.
254    #[must_use]
255    fn metadata(&self) -> &M;
256    /// Mutable access to the Weave's associated metadata.
257    ///
258    /// # Panics
259    ///
260    /// May panic if `callback` panics.
261    fn metadata_mut<O>(&mut self, callback: impl FnOnce(&mut M) -> O) -> O;
262}
263
264/// A [`Weave`] where nodes can be bookmarked.
265pub trait BookmarkableWeave<K, N, T>: Weave<K, N, T>
266where
267    K: Hash + Copy + Eq + Ord,
268    N: Node<K, T>,
269{
270    /// Identifiers of bookmarked nodes.
271    type Bookmarks;
272
273    /// Returns a reference to the identifiers of bookmarked nodes.
274    #[must_use]
275    fn bookmarks(&self) -> &Self::Bookmarks;
276    /// Returns `true` if the Weave contains a bookmarked node with the specified identifier.
277    #[must_use]
278    fn contains_bookmark(&self, id: &K) -> bool;
279    /// Sets the bookmarked status of a node with the specified identifier.
280    fn set_bookmarked(&mut self, id: &K, value: bool) -> bool;
281}
282
283/// A [`Weave`] where the ordering of nodes is stable and can be user-defined.
284///
285/// # Panics
286///
287/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
288pub trait SortableWeave<K, N, T>: Weave<K, N, T>
289where
290    K: Hash + Copy + Eq + Ord,
291    N: Node<K, T>,
292{
293    /// Sorts the child nodes of a parent node with the specified identifier using the comparison function `cmp`.
294    ///
295    /// # Panics
296    ///
297    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
298    fn sort_children_by(&mut self, id: &K, cmp: impl FnMut(&N, &N) -> Ordering) -> bool;
299    /// Sorts the identifiers of a parent node's children with the specified identifier using the comparison function `cmp`.
300    ///
301    /// # Panics
302    ///
303    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
304    fn sort_children_by_id(&mut self, id: &K, cmp: impl FnMut(&K, &K) -> Ordering) -> bool;
305    /// Sorts root nodes (nodes which do not have any parents) using the comparison function `cmp`.
306    ///
307    /// # Panics
308    ///
309    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
310    fn sort_roots_by(&mut self, cmp: impl FnMut(&N, &N) -> Ordering);
311    /// Sorts the identifiers of root nodes (nodes which do not have any parents) using the comparison function `cmp`.
312    ///
313    /// # Panics
314    ///
315    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
316    fn sort_roots_by_id(&mut self, cmp: impl FnMut(&K, &K) -> Ordering);
317}
318
319/// A [`Weave`] where the ordering of bookmarked nodes is stable and can be user-defined.
320///
321/// # Panics
322///
323/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
324pub trait SortableBookmarkableWeave<K, N, T>:
325    BookmarkableWeave<K, N, T> + SortableWeave<K, N, T>
326where
327    K: Hash + Copy + Eq + Ord,
328    N: Node<K, T>,
329{
330    /// Sorts bookmarked nodes using the comparison function `cmp`.
331    ///
332    /// # Panics
333    ///
334    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
335    fn sort_bookmarks_by(&mut self, cmp: impl FnMut(&N, &N) -> Ordering);
336    /// Sorts the identifiers of bookmarked nodes using the comparison function `cmp`.
337    ///
338    /// # Panics
339    ///
340    /// May panic if `cmp` does not implement a [total order](https://en.wikipedia.org/wiki/Total_order), or if `cmp` itself panics.
341    fn sort_bookmarks_by_id(&mut self, cmp: impl FnMut(&K, &K) -> Ordering);
342}
343
344/// A [`Weave`] where only one [`Node`] can be considered active at a time.
345pub trait ActiveSingularWeave<K, N, T>: Weave<K, N, T>
346where
347    K: Hash + Copy + Eq + Ord,
348    N: Node<K, T>,
349{
350    /// Returns the active node's identifier, if any.
351    #[must_use]
352    fn active(&self) -> Option<K>;
353}
354
355/// A [`Weave`] where every [`Node`] in the active path is always considered active.
356pub trait ActivePathWeave<K, N, T>: Weave<K, N, T>
357where
358    K: Hash + Copy + Eq + Ord,
359    N: Node<K, T>,
360{
361    /// Identifiers of active nodes.
362    type Active;
363
364    /// Returns a reference to the identifiers of active nodes.
365    #[must_use]
366    fn active(&self) -> &Self::Active;
367    /// Replaces the currently active path with the specified set of node IDs.
368    ///
369    /// If the new active path would result in internal inconsistency, this function will correct the path in an implementation-specific manner.
370    fn set_active_path(&mut self, active: impl Iterator<Item = K>);
371}
372
373/// A [`Weave`] where [`Node`] objects do not depend on their parents in order to be meaningful.
374pub trait IndependentWeave<K, N, T>: Weave<K, N, T> + SemiIndependentWeave<K, N, T>
375where
376    K: Hash + Copy + Eq + Ord,
377    N: Node<K, T>,
378    T: IndependentContents,
379{
380    /// Moves a node with the specified identifier to a new set of parent nodes, returning `true` if the move was successful.
381    ///
382    /// This function may change the active status of other nodes if it is necessary to preserve internal consistency.
383    fn move_to(&mut self, id: &K, new_parents: &[K]) -> bool;
384}
385
386/// A [`Weave`] where [`Node`] objects do not depend on the *contents* of their parents in order to be meaningful.
387///
388/// # Panics
389///
390/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
391pub trait SemiIndependentWeave<K, N, T>: Weave<K, N, T>
392where
393    K: Hash + Copy + Eq + Ord,
394    N: Node<K, T>,
395    T: IndependentContents,
396{
397    /// Mutable access to the contents of a node with the specified identifier.
398    ///
399    /// Returns `Some` if the node's contents were successfully updated.
400    ///
401    /// # Panics
402    ///
403    /// May panic if `callback` panics.
404    #[must_use]
405    fn get_contents_mut<O>(&mut self, id: &K, callback: impl FnOnce(&mut T) -> O) -> Option<O>;
406}
407
408/// A [`Weave`] where the contents of [`Node`] objects can be split and merged.
409///
410/// # Panics
411///
412/// All panics should be assumed to leave the Weave in a malformed state unless otherwise specified by the implementation.
413pub trait DiscreteWeave<K, N, T>: Weave<K, N, T>
414where
415    K: Hash + Copy + Eq + Ord,
416    N: Node<K, T>,
417    T: DiscreteContents,
418{
419    /// Splits a node with the specified identifier at the given index, creating a new child node with the identifier `new_id`.
420    ///
421    /// Returns `false` if splitting the node failed.
422    ///
423    /// # Panics
424    ///
425    /// May panic if `T::split` panics.
426    fn split(&mut self, id: &K, at: usize, new_id: K) -> bool;
427    /// Merges a node with the specified identifier with its parent, with the newly merged node inheriting the parent's identifier.
428    ///
429    /// Returns the identifier of the merged node if merging was successful.
430    ///
431    /// # Panics
432    ///
433    /// May panic if `T::merge` panics.
434    fn merge_with_parent(&mut self, id: &K) -> Option<K>;
435}
436
437/// A read-only [`Weave`].
438#[must_use]
439pub trait ImmutableWeave<K, N, T>
440where
441    K: Hash + Copy + Eq + Ord,
442    N: Node<K, T>,
443{
444    /// Mapping between identifiers and nodes.
445    type Nodes;
446    /// Identifiers of root nodes (nodes which do not have any parents).
447    type Roots;
448
449    /// Returns the number of nodes stored within the Weave.
450    #[must_use]
451    fn len(&self) -> usize;
452    /// Returns `true` if the Weave does not contain any nodes.
453    #[must_use]
454    fn is_empty(&self) -> bool;
455    /// Returns a reference to the identifier:node mapping.
456    #[must_use]
457    fn nodes(&self) -> &Self::Nodes;
458    /// Returns a reference to the identifiers of root nodes (nodes which do not have any parents).
459    #[must_use]
460    fn roots(&self) -> &Self::Roots;
461    /// Returns `true` if the Weave contains a node with the specified identifier.
462    #[must_use]
463    fn contains(&self, id: &K) -> bool;
464    /// Returns `true` if the Weave contains an active node (`node.is_active() == true`) with the specified identifier.
465    ///
466    /// The meaning of this value can depend on the underlying Weave implementation.
467    #[must_use]
468    fn contains_active(&self, id: &K) -> bool;
469    /// Returns a reference to the node corresponding to the identifier.
470    #[must_use]
471    fn get(&self, id: &K) -> Option<&N>;
472    /// Convenience method for `self.get(id).map(Node::from)`.
473    #[must_use]
474    fn get_parents(&self, id: &K) -> Option<&N::From>;
475    /// Convenience method for `self.get(id).map(Node::to)`.
476    #[must_use]
477    fn get_children(&self, id: &K) -> Option<&N::To>;
478    /// Convenience method for `self.get(id).map(Node::contents)`.
479    #[must_use]
480    fn get_contents(&self, id: &K) -> Option<&T>;
481    /// Builds a list of all node identifiers ordered by their positions in the Weave.
482    fn get_ordered_identifiers(&self, output: &mut Vec<K>);
483    /// Recursively builds a list of all children of the specified node ordered by their positions in the Weave.
484    fn get_ordered_identifiers_from(&self, id: &K, output: &mut Vec<K>);
485    /// Builds a path through the Weave starting at the deepest active node and ending at a root node.
486    ///
487    /// In an [`ImmutableActivePathWeave`], this path will be the longest contiguous path of active nodes.
488    fn get_active_path(&self, output: &mut Vec<K>);
489    /// Builds a path through the Weave starting at the specified node and ending at a root node.
490    ///
491    /// In an [`ImmutableActivePathWeave`], this path will preferentially route through the active path.
492    fn get_path_from(&self, id: &K, output: &mut Vec<K>);
493}
494
495/// An [`ImmutableWeave`] containing document-wide metadata.
496pub trait ImmutableMetadataWeave<K, N, T, M>: ImmutableWeave<K, N, T>
497where
498    K: Hash + Copy + Eq + Ord,
499    N: Node<K, T>,
500{
501    /// Returns a reference to the Weave's associated metadata.
502    #[must_use]
503    fn metadata(&self) -> &M;
504}
505
506/// An [`ImmutableWeave`] where nodes can be bookmarked.
507pub trait ImmutableBookmarkableWeave<K, N, T>: ImmutableWeave<K, N, T>
508where
509    K: Hash + Copy + Eq + Ord,
510    N: Node<K, T>,
511{
512    /// Identifiers of bookmarked nodes.
513    type Bookmarks;
514
515    /// Returns a reference to the identifiers of bookmarked nodes.
516    #[must_use]
517    fn bookmarks(&self) -> &Self::Bookmarks;
518    /// Returns `true` if the Weave contains a bookmarked node with the specified identifier.
519    #[must_use]
520    fn contains_bookmark(&self, id: &K) -> bool;
521}
522
523/// An [`ImmutableWeave`] where only one [`Node`] can be considered active at a time.
524pub trait ImmutableActiveSingularWeave<K, N, T>: ImmutableWeave<K, N, T>
525where
526    K: Hash + Copy + Eq + Ord,
527    N: Node<K, T>,
528{
529    /// Returns the active node's identifier, if any.
530    #[must_use]
531    fn active(&self) -> Option<K>;
532}
533
534/// An [`ImmutableWeave`] where every [`Node`] in the active path is always considered active.
535pub trait ImmutableActivePathWeave<K, N, T>: ImmutableWeave<K, N, T>
536where
537    K: Hash + Copy + Eq + Ord,
538    N: Node<K, T>,
539{
540    /// Identifiers of active nodes.
541    type Active;
542
543    /// Returns a reference to the identifiers of active nodes.
544    #[must_use]
545    fn active(&self) -> &Self::Active;
546}
547
548#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
549enum Step<A, B> {
550    Enter(A),
551    Exit(B),
552}
553
554fn topological_sort<'a, K, N, T, S>(
555    nodes: &'a HashMap<K, N, S>,
556    id: &'a K,
557    scratchpad: &mut Vec<K>,
558    identifiers: &mut Vec<K>,
559    identifier_set: &mut HashSet<K, S>,
560    identifier_map: &mut HashMap<K, usize, S>,
561) where
562    K: Hash + Copy + Eq + Ord + 'a,
563    N: Node<K, T> + 'a,
564    <N as Node<K, T>>::From: 'a,
565    <N as Node<K, T>>::To: 'a,
566    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator + ExactSizeIterator>,
567    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
568    S: BuildHasher + Default + Clone,
569{
570    scratchpad.push(*id);
571
572    while let Some(id) = scratchpad.pop() {
573        let node = &nodes[&id];
574
575        if identifier_set.contains(&id)
576            || identifier_map
577                .get(&id)
578                .copied()
579                .unwrap_or_else(|| node.from().into_iter().len())
580                != 0
581        {
582            continue;
583        }
584
585        identifiers.push(id);
586        identifier_set.insert(id);
587
588        for child in node.to().into_iter().rev().copied() {
589            let remaining = identifier_map
590                .entry(child)
591                .or_insert_with(|| nodes[&child].from().into_iter().len());
592            *remaining = remaining.strict_sub(1);
593
594            scratchpad.push(child);
595        }
596    }
597}
598
599fn topological_sort_subgraph<'a, K, N, T, S>(
600    nodes: &'a HashMap<K, N, S>,
601    filter: &impl Fn(&K) -> bool,
602    id: &'a K,
603    scratchpad: &mut Vec<K>,
604    identifiers: &mut Vec<K>,
605    identifier_set: &mut HashSet<K, S>,
606    identifier_map: &mut HashMap<K, usize, S>,
607) where
608    K: Hash + Copy + Eq + Ord + 'a,
609    N: Node<K, T> + 'a,
610    <N as Node<K, T>>::From: 'a,
611    <N as Node<K, T>>::To: 'a,
612    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
613    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
614    S: BuildHasher + Default + Clone,
615{
616    scratchpad.push(*id);
617
618    while let Some(id) = scratchpad.pop() {
619        let node = &nodes[&id];
620
621        if !filter(&id)
622            || identifier_set.contains(&id)
623            || identifier_map.get(&id).copied().unwrap_or_else(|| {
624                node.from()
625                    .into_iter()
626                    .filter(|&parent| filter(parent))
627                    .count()
628            }) != 0
629        {
630            continue;
631        }
632
633        identifiers.push(id);
634        identifier_set.insert(id);
635
636        for child in node.to().into_iter().rev().copied() {
637            let remaining = identifier_map.entry(child).or_insert_with(|| {
638                nodes[&child]
639                    .from()
640                    .into_iter()
641                    .filter(|&parent| filter(parent))
642                    .count()
643            });
644            *remaining = remaining.strict_sub(1);
645
646            scratchpad.push(child);
647        }
648    }
649}
650
651fn detect_cycles<'a, K, N, T, S>(
652    nodes: &'a HashMap<K, N, S>,
653    roots: impl Iterator<Item = K>,
654    scratchpad: &mut Vec<Step<K, K>>,
655    scratchpad_map: &mut HashMap<K, bool, S>,
656) -> bool
657where
658    K: Hash + Copy + Eq + Ord + 'a,
659    N: Node<K, T> + 'a,
660    <N as Node<K, T>>::From: 'a,
661    <N as Node<K, T>>::To: 'a,
662    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
663    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator + ExactSizeIterator>,
664    S: BuildHasher + Default + Clone,
665{
666    for root in roots {
667        if scratchpad_map.contains_key(&root) {
668            continue;
669        }
670
671        scratchpad.push(Step::Enter(root));
672
673        while let Some(step) = scratchpad.pop() {
674            match step {
675                Step::Enter(id) => {
676                    scratchpad.push(Step::Exit(id));
677
678                    match scratchpad_map.entry(id) {
679                        Entry::Occupied(entry) => {
680                            if !entry.get() {
681                                return true;
682                            }
683                        }
684                        Entry::Vacant(entry) => {
685                            entry.insert_entry(false);
686
687                            scratchpad.extend(
688                                nodes[&id].to().into_iter().rev().copied().map(Step::Enter),
689                            );
690                        }
691                    }
692                }
693                Step::Exit(id) => {
694                    scratchpad_map.insert(id, true);
695                }
696            }
697        }
698    }
699
700    scratchpad_map.len() != nodes.len()
701}
702
703fn shortest_path_to_ancestor<'a, K, N, T, S>(
704    nodes: &'a HashMap<K, N, S>,
705    id: &'a K,
706    target: &impl Fn(&'a N) -> bool,
707    scratchpad: &mut VecDeque<K>,
708    scratchpad_map: &mut HashMap<K, K, S>,
709    scratchpad_set: &mut HashSet<K, S>,
710    path: &mut Vec<K>,
711) where
712    K: Hash + Copy + Eq + Ord + 'a,
713    N: Node<K, T> + 'a,
714    <N as Node<K, T>>::From: 'a,
715    <N as Node<K, T>>::To: 'a,
716    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
717    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
718    S: BuildHasher + Default + Clone,
719{
720    scratchpad.push_front(*id);
721    scratchpad_set.insert(*id);
722
723    while let Some(id) = scratchpad.pop_back() {
724        let node = &nodes[&id];
725
726        if target(node) {
727            scratchpad.clear();
728
729            path.push(id);
730
731            while let Some(child) = scratchpad_map.remove(path.last().unwrap()) {
732                path.push(child);
733            }
734
735            return;
736        }
737
738        for parent in node.from().into_iter().copied() {
739            if scratchpad_set.insert(parent) {
740                scratchpad.push_front(parent);
741                scratchpad_map.insert(parent, id);
742            }
743        }
744    }
745}
746
747fn longest_candidate_path_to_root<'a, K, N, T, S>(
748    nodes: &'a HashMap<K, N, S>,
749    topological_order: &[K],
750    is_candidate: &impl Fn(&K) -> bool,
751    scratchpad_map: &mut HashMap<K, usize, S>,
752    reversed_path: &mut Vec<K>,
753) where
754    K: Hash + Copy + Eq + Ord + 'a,
755    N: Node<K, T> + 'a,
756    <N as Node<K, T>>::From: 'a,
757    <N as Node<K, T>>::To: 'a,
758    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
759    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
760    S: BuildHasher + Default + Clone,
761{
762    let mut longest_distance = None;
763
764    for id in topological_order {
765        if !is_candidate(id) {
766            continue;
767        }
768
769        let node = &nodes[id];
770        let distance = if node.from().into_iter().next().is_none() {
771            Some(0)
772        } else {
773            node.from()
774                .into_iter()
775                .filter_map(|parent| scratchpad_map.get(parent).copied())
776                .max()
777                .map(|l| l.strict_add(1))
778        };
779
780        if let Some(distance) = distance {
781            scratchpad_map.insert(*id, distance);
782
783            if longest_distance.is_none_or(|(value, _)| distance > value) {
784                longest_distance = Some((distance, id));
785            }
786        }
787    }
788
789    let mut current = longest_distance.map(|(_, id)| id);
790
791    while let Some(id) = current {
792        reversed_path.push(*id);
793
794        current = nodes[id]
795            .from()
796            .into_iter()
797            .filter(|id| scratchpad_map.contains_key(*id))
798            .min_by_key(|id| Reverse(scratchpad_map[*id]));
799    }
800}
801
802fn ancestor_subgraph<'a, K, N, T, S>(
803    nodes: &'a HashMap<K, N, S>,
804    id: K,
805    scratchpad: &mut Vec<K>,
806    identifiers: &mut HashSet<K, S>,
807) where
808    K: Hash + Copy + Eq + Ord + 'a,
809    N: Node<K, T>,
810    <N as Node<K, T>>::From: 'a,
811    <N as Node<K, T>>::To: 'a,
812    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
813    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
814    S: BuildHasher + Default + Clone,
815{
816    scratchpad.push(id);
817
818    while let Some(id) = scratchpad.pop() {
819        if identifiers.insert(id) {
820            scratchpad.extend(nodes[&id].from().into_iter().rev().copied());
821        }
822    }
823}
824
825fn descendant_subgraph<'a, K, N, T, S>(
826    nodes: &'a HashMap<K, N, S>,
827    id: K,
828    scratchpad: &mut Vec<K>,
829    identifiers: &mut HashSet<K, S>,
830) where
831    K: Hash + Copy + Eq + Ord + 'a,
832    N: Node<K, T>,
833    <N as Node<K, T>>::From: 'a,
834    <N as Node<K, T>>::To: 'a,
835    &'a N::From: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
836    &'a N::To: IntoIterator<Item = &'a K, IntoIter: DoubleEndedIterator>,
837    S: BuildHasher + Default + Clone,
838{
839    scratchpad.push(id);
840
841    while let Some(id) = scratchpad.pop() {
842        if identifiers.insert(id) {
843            scratchpad.extend(nodes[&id].to().into_iter().rev().copied());
844        }
845    }
846}