Skip to content

feat: Add map_owned method for Graph#794

Closed
itpetey wants to merge 2 commits intopetgraph:masterfrom
itpetey:feat/graph-map-owned
Closed

feat: Add map_owned method for Graph#794
itpetey wants to merge 2 commits intopetgraph:masterfrom
itpetey:feat/graph-map-owned

Conversation

@itpetey
Copy link
Contributor

@itpetey itpetey commented May 20, 2025

Closes #542

Copy link
Member

@RaoulLuque RaoulLuque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, first off, thanks for the PR 🦕

Seems like a good addition, I just have a few notes :)

One thing I didn't include in the code review:
Is there any reason why this wouldn't be implemented for StableGraphs while we're at it?

Additionally, in the original issue it was suggested to add similar functionality for filter_map, so maybe add that as well?

/// The resulting graph has the same structure and the same graph indices
/// as `self`.
///
/// If you want a non-destructive version of this function, see `map`.
Copy link
Member

@RaoulLuque RaoulLuque May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// If you want a non-destructive version of this function, see `map`.
/// If you want a non-consuming version of this function, see
/// [`map`](struct.Graph.html#method.map).

I think, I would prefer to write non-consuming instead of destructive, since destruction might imply more than just giving up ownership of the value.

Also, we should reference the actual alternative map method.

/// as `self`.
///
/// If you want a non-destructive version of this function, see `map`.
pub fn map_owned<F, G, N2, E2>(self, mut node_map: F, mut edge_map: G) -> Graph<N2, E2, Ty, Ix>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible instead of map_owned we could call this method into_map aligning with naming conventions from std, such as into_iter or into_bytes. On the other hand, we could also go for map_into which is also used in itertools for example:
https://github.com/rust-itertools/itertools/blob/b0942d6b84d47dec75c19e541859211423d857d9/src/adaptors/map.rs#L125

map_into would make sense since we don't actually convert into a map (into an interator) but instead convert to a graph immediately (similar to applying collect to an iterator in the iterator analogy). Therefore we 'map' 'into' another graph. into_map on the other hand would suggest that we would return a map of some sort and consume the provided graph.

Comment on lines +1599 to +1619
#[test]
fn map_map_owned() {
let mut g = Graph::new_undirected();
let a = g.add_node("A");
let b = g.add_node("B");
let c = g.add_node("C");
let ab = g.add_edge(a, b, 7);
let bc = g.add_edge(b, c, 14);
let ca = g.add_edge(c, a, 9);

let g2 = g.map_owned(|_, name| format!("map-{name}"), |_, weight| weight * 2);
assert_eq!(g2.node_count(), 3);
assert_eq!(g2.node_weight(a).map(|s| &**s), Some("map-A"));
assert_eq!(g2.node_weight(b).map(|s| &**s), Some("map-B"));
assert_eq!(g2.node_weight(c).map(|s| &**s), Some("map-C"));
assert_eq!(g2.edge_count(), 3);
assert_eq!(g2.edge_weight(ab), Some(&14));
assert_eq!(g2.edge_weight(bc), Some(&28));
assert_eq!(g2.edge_weight(ca), Some(&18));
}

Copy link
Member

@RaoulLuque RaoulLuque May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily your concern but I am just noticing that we don't have any (normal) map tests for graph. So maybe add one for that as well while we are at it?

@starovoid
Copy link
Collaborator

starovoid commented May 20, 2025

I suggest also implement filter_map_owned, and then I'll include it all in 0.8.3. Then both of these methods will also be added to StableGraph. Well, there remains the naming problem too.

@RaoulLuque
Copy link
Member

Referenced this in the marked PRs by accident, sry ^^

@starovoid starovoid added C-feature-accepted Category: A feature request that has been accepted pending implementation P-easy Call for participation: Experience needed to fix: Easy / not much labels Jun 13, 2025
@RaoulLuque RaoulLuque added the S-waiting-on-author Status: Awaiting some action by the PR/Issue author label Jun 21, 2025
@RaoulLuque
Copy link
Member

Since this PR seems stale, I was wondering if it would be alright if I would pick it up. That is, fork your branch and finish it such that your work gets merged : )

@itpetey
Copy link
Contributor Author

itpetey commented Jul 28, 2025

@RaoulLuque Hey mate sorry. Been really busy over the last...yeesh, over a month. I'd really appreciate you picking it up :) Thanks!!

@RaoulLuque
Copy link
Member

@RaoulLuque Hey mate sorry. Been really busy over the last...yeesh, over a month. I'd really appreciate you picking it up :) Thanks!!

Hey, no worries at all :) Just wanted to make sure I'd ask in order not step on anyone's toes 🌞 Appreciate your quick response :)

@RaoulLuque
Copy link
Member

Closed in favor of #863.

@RaoulLuque RaoulLuque closed this Jul 31, 2025
github-merge-queue bot pushed a commit that referenced this pull request Sep 3, 2025
…raph` (#863)

This PR continues the work done in PR #794. Thus, it resolves #542.

It adds `map_owned` and `filter_map_owned` methods for both `Graph` and
`StableGraph` which take ownership of the respective graphs and their
respective associated data in comparison to the `map` and `filter_map`
counterparts which only take `&` references.

Appropriate tests for this have been added. If desired, I can also add
quickchecks, but this did not seem necessary.

---------

Co-authored-by: Pete Hayes <[email protected]>
RaoulLuque added a commit to RaoulLuque/petgraph that referenced this pull request Sep 21, 2025
…raph` (petgraph#863)

This PR continues the work done in PR petgraph#794. Thus, it resolves petgraph#542.

It adds `map_owned` and `filter_map_owned` methods for both `Graph` and
`StableGraph` which take ownership of the respective graphs and their
respective associated data in comparison to the `map` and `filter_map`
counterparts which only take `&` references.

Appropriate tests for this have been added. If desired, I can also add
quickchecks, but this did not seem necessary.

---------

Co-authored-by: Pete Hayes <[email protected]>
RaoulLuque added a commit to cactusdualcore/petgraph that referenced this pull request Sep 21, 2025
…raph` (petgraph#863)

This PR continues the work done in PR petgraph#794. Thus, it resolves petgraph#542.

It adds `map_owned` and `filter_map_owned` methods for both `Graph` and
`StableGraph` which take ownership of the respective graphs and their
respective associated data in comparison to the `map` and `filter_map`
counterparts which only take `&` references.

Appropriate tests for this have been added. If desired, I can also add
quickchecks, but this did not seem necessary.

---------

Co-authored-by: Pete Hayes <[email protected]>
RaoulLuque added a commit to RaoulLuque/petgraph that referenced this pull request Sep 21, 2025
…raph` (petgraph#863)

This PR continues the work done in PR petgraph#794. Thus, it resolves petgraph#542.

It adds `map_owned` and `filter_map_owned` methods for both `Graph` and
`StableGraph` which take ownership of the respective graphs and their
respective associated data in comparison to the `map` and `filter_map`
counterparts which only take `&` references.

Appropriate tests for this have been added. If desired, I can also add
quickchecks, but this did not seem necessary.

---------

Co-authored-by: Pete Hayes <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-feature-accepted Category: A feature request that has been accepted pending implementation P-easy Call for participation: Experience needed to fix: Easy / not much S-waiting-on-author Status: Awaiting some action by the PR/Issue author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add another map method taking self instead of &self

3 participants