Skip to main content

handlegraph/
mutablehandlegraph.rs

1/*!
2
3Traits for manipulating the nodes and edges of a graph.
4
5*/
6
7use crate::handle::{Edge, Handle, NodeId};
8
9/// Methods for adding handles and edges to a graph.
10pub trait AdditiveHandleGraph {
11    /// Add a node with the provided sequence to the graph, letting
12    /// the graph pick the node ID.
13    fn append_handle(&mut self, sequence: &[u8]) -> Handle;
14
15    /// Add a node with the provided sequence and ID to the graph.
16    fn create_handle<T: Into<NodeId>>(
17        &mut self,
18        sequence: &[u8],
19        node_id: T,
20    ) -> Handle;
21
22    /// Insert an edge into the graph. Implementations may panic if
23    /// both handles of the edge do not already exist.
24    fn create_edge(&mut self, edge: Edge);
25}
26
27/// Methods for removing handles and edges from a graph.
28pub trait SubtractiveHandleGraph {
29    /// Remove a handle from the graph, returning `true` if the handle
30    /// existed.
31    ///
32    /// Implementations may destroy or otherwise modify paths that
33    /// cover the handle.
34    fn remove_handle(&mut self, handle: Handle) -> bool;
35
36    /// Remove an edge from the graph, returning `true` if the edge
37    /// existed.
38    fn remove_edge(&mut self, edge: Edge) -> bool;
39
40    fn clear_graph(&mut self);
41}
42
43/// Methods for manipulating handles that already exist in a graph.
44pub trait MutableHandles: AdditiveHandleGraph {
45    /// Divide the given handle at the provided `offsets`, in terms of
46    /// the node's sequence. Creates `offsets.len() - 1` new handles,
47    /// and updates the edges accordingly.
48    ///
49    /// Implementations should update paths that include a step on
50    /// `handle` by inserting the new handles after that step.
51    fn divide_handle(
52        &mut self,
53        handle: Handle,
54        offsets: Vec<usize>,
55    ) -> Vec<Handle>;
56
57    /// Divide the given handle at the provided offset, creating one
58    /// new handle. Default implementation uses `divide_handle()`, and
59    /// there's probably no need to provide another implementation.
60    ///
61    /// Implementations should update paths that include a step on
62    /// `handle` by inserting the new handles after that step.
63    fn split_handle(
64        &mut self,
65        handle: Handle,
66        offset: usize,
67    ) -> (Handle, Handle) {
68        let handles = self.divide_handle(handle, vec![offset]);
69        (handles[0], handles[1])
70    }
71
72    /// Transform the node that `handle` corresponds to so that the
73    /// orientation of `handle` becomes the node's forward
74    /// orientation. I.e. if `handle` is reverse, the node will be
75    /// reversed. Returns the new handle.
76    fn apply_orientation(&mut self, handle: Handle) -> Handle;
77}
78
79/// Applying transformations to all IDs in a graph, and applying sort orders.
80pub trait TransformNodeIds {
81    /// Reassign all node IDs in the graph using the provided
82    /// `transform` function. `transform` is `Copy + Send + Sync` as
83    /// some implementations may perform part of the work in parallel.
84    fn transform_node_ids<F>(&mut self, transform: F)
85    where
86        F: Fn(NodeId) -> NodeId + Copy + Send + Sync;
87
88    /// Reassign the node IDs using the provided ordering. `order`
89    /// must have one element for each node in the graph, and the node
90    /// IDs will be used to index the slice.
91    fn apply_ordering(&mut self, order: &[Handle]);
92}
93
94/// A graph that supports all forms of handle- and edge-related
95/// mutation.
96///
97/// Has a blanket implementation for all graphs that implement the
98/// other traits in this module.
99pub trait MutableHandleGraph:
100    AdditiveHandleGraph + SubtractiveHandleGraph + MutableHandles + TransformNodeIds
101{
102}
103
104impl<T> MutableHandleGraph for T where
105    T: AdditiveHandleGraph
106        + SubtractiveHandleGraph
107        + MutableHandles
108        + TransformNodeIds
109{
110}