Skip to content

Conversation

@starovoid
Copy link
Collaborator

Provide methods for checked work with MatrixGraph

PR adds the MatrixGraph methods described in the comment.
The purpose of the proposed changes is to allow potential errors to be handled instead of receiving multiple panics.

To represent possible errors I add the MatrixError enum:

/// The error type for fallible `MatrixGraph` operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatrixError {
    /// The `MatrixGraph` is at the maximum number of nodes for its index.
    NodeIxLimit,

    /// The node with the specified index is missing from the graph.
    NodeMissed(usize),
}
  • Some methods (where appropriate) return Option instead of Result.
  • MatrixGraph::has_edge has been rewritten to avoid panic.

Full list of added methods:

pub fn try_add_node(&mut self, weight: N) -> Result<NodeIndex<Ix>, MatrixError>

pub fn try_update_edge(
        &mut self,
        a: NodeIndex<Ix>,
        b: NodeIndex<Ix>,
        weight: E,
    ) -> Result<Option<E>, MatrixError>

pub fn try_remove_edge(&mut self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> Option<E>

pub fn add_or_update_edge(
        &mut self,
        a: NodeIndex<Ix>,
        b: NodeIndex<Ix>,
        weight: E,
    ) -> Result<Option<E>, MatrixError>

pub fn get_node_weight(&self, a: NodeIndex<Ix>) -> Option<&N>

pub fn get_node_weight_mut(&mut self, a: NodeIndex<Ix>) -> Option<&mut N>

pub fn get_edge_weight(&self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> Option<&E>

pub fn get_edge_weight_mut(&mut self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> Option<&mut E>

@starovoid starovoid self-assigned this Mar 20, 2025
@starovoid starovoid added this to the 0.8.1 milestone Mar 21, 2025
@starovoid starovoid modified the milestones: 0.8.1, 0.8 Mar 28, 2025
@starovoid starovoid mentioned this pull request Mar 28, 2025
8 tasks
@starovoid starovoid changed the title MatrixGraph methods with recoverable errors feat: MatrixGraph methods with recoverable errors Mar 28, 2025
@starovoid starovoid added this pull request to the merge queue Mar 28, 2025
Merged via the queue into petgraph:master with commit cdf7c13 Mar 28, 2025
10 checks passed
@starovoid starovoid deleted the rec-mtrx-errs branch March 28, 2025 18:13
github-merge-queue bot pushed a commit that referenced this pull request Apr 5, 2025
A sufficient number of proposed changes have accumulated to combine them
and publish a new major release numbered `0.8.0`.


BREAKING CHANGE:

This will require the user to provide extra type parameter in some APIs
(Read more in #747).

## List of changes
- [x] #747
The main innovation of the current release, the long-awaited feature
that has become very relevant due to the transition of dependent
projects to support `no_std`.
- [x] #662 
- [x] #611
- [x] #728
- [x] #686
- [x] #737 
- [x] #720 
- [x] #718 

## Note
There are still a large number of PRs that we want to adopt in the near
future, so we should expect at least a release of `0.8.1` soon after the
completion of `0.8.0`.

Thank you all for participating!

---------

Co-authored-by: Agustin Borgna <agustinborgna@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants