Skip to content

Retain for stable graph #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 11, 2017
Merged

Conversation

Rupsbant
Copy link
Contributor

Lets try this again.

@bluss
Copy link
Member

bluss commented Feb 25, 2017

Hi, to be frank with you here I'd expect the PR to explain the thoughts behind the change.

I disagree with adding a method that does not have the same signature as the corresponding one on Graph.

Ruben Lapauw added 2 commits February 27, 2017 00:57
Now actually consistent with Graph::retain_nodes
@Rupsbant
Copy link
Contributor Author

It's my first public pull-request so I might do some things the wrong way. As for actual content:

I see your point. And considering this signature corresponds to the std::vec::Vec signature this should probably be reverted. I'll remove the output vector.

if you're curious for my thoughts:
First off, consider a non-clone node type. You can move them in the graph-node and when you remove them you get them back. If you don't use them they get dropped. However if you want to remove a lot at the same time. They either get destroyed or you need to remove them one-by-one (which is quadratic since you don't have an unsafe NodeIndex iterator.

I'd consider the Vec and Graph signature insufficient. However this needs to be measured against other concerns (which I did not do!):

  • What happens if the allocation is not needed, can this be optimized away?
  • How consistent is this signature?
  • What is the cost of working around it? (Fixed by Rc or Option + take)

@bluss
Copy link
Member

bluss commented Feb 27, 2017

StableGraph and Graph should have the same interface where it's possible. It may be insufficient but having the interfaces mismatch only means it's a halfway done API transition: one or the other will need to change to match, otherwise the library is a mess. There is no trade off here IMO; it's about fixing the signature or starting to plan the name and signature of the new API.

From your message it sounds like there is no pressing use case behind the choice to return a Vec, and then I think the choice of design is simple (don't return the Vec).

(We have the benefit that removing nodes in sequence is not quadratic with the stable graph, since nodes don't shift. Regular Graph can achieve this too by removing them in reverse order, which is how Graph::retain_nodes is implemented.)

@Rupsbant
Copy link
Contributor Author

Rupsbant commented Mar 3, 2017

To be sure: the tests fail because of an update to nightly. I think this is a compiler bug regarding scope. I opened an issue rust-lang/rust#40235

use petgraph::graph::node_index as n;
...
#[test]
fn filtered_post_order() {
    ...
    let mut dfs = DfsPostOrder::new(&gr, n(0));
    let f = NodeFiltered(&gr, map);
    while let Some(n) = dfs.next(&f) {
        po.push(n);
    }
    assert!(!po.contains(&n(1)));
}

pub fn retain_nodes<F>(&mut self, mut f: F) where F: FnMut(Frozen<Self>, NodeIndex<Ix>) -> bool {
for i in 0..self.g.node_count() {
let ix = node_index(i);
if !f(Frozen(self), ix) {
Copy link
Member

Choose a reason for hiding this comment

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

Ah this should check first if ix is a node in the graph.

Copy link
Member

Choose a reason for hiding this comment

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

Oh and the upper bound is not node_count but the upper bound count.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The node count is correct: the count of the unstable subgraph is used.
Added the check. The users should indeed not do this manually.

@bluss
Copy link
Member

bluss commented Mar 11, 2017

Thank you! I'm sorry for the delay. I'll try out the squash merge button, then I don't have to ask you to do the squash.. See you on the other side..

@bluss bluss merged commit 73fa90c into petgraph:master Mar 11, 2017
@bluss
Copy link
Member

bluss commented Mar 11, 2017

Looks like it worked!

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