[librsvg] Node: new function Node::is_ancestor()
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] Node: new function Node::is_ancestor()
- Date: Thu, 30 Mar 2017 16:49:40 +0000 (UTC)
commit fdbbca254e260d9089ed63336f6617c666ea14fb
Author: Federico Mena Quintero <federico gnome org>
Date: Thu Mar 23 18:11:47 2017 -0600
Node: new function Node::is_ancestor()
rust/src/node.rs | 42 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 42 insertions(+), 0 deletions(-)
---
diff --git a/rust/src/node.rs b/rust/src/node.rs
index 36a426d..5ae0fb3 100644
--- a/rust/src/node.rs
+++ b/rust/src/node.rs
@@ -159,6 +159,20 @@ impl Node {
}
}
+ pub fn is_ancestor (ancestor: Rc<Node>, descendant: Rc<Node>) -> bool {
+ let mut desc = Some (descendant.clone ());
+
+ while let Some (ref d) = desc.clone () {
+ if rc_node_ptr_eq (&ancestor, d) {
+ return true;
+ }
+
+ desc = d.get_parent ();
+ }
+
+ false
+ }
+
pub fn add_child (&self, child: &Rc<Node>) {
self.children.borrow_mut ().push (child.clone ());
}
@@ -474,4 +488,32 @@ mod tests {
rsvg_node_unref (ref1);
rsvg_node_unref (ref2);
}
+
+ #[test]
+ fn node_is_its_own_ancestor () {
+ let node = Rc::new (Node::new (NodeType::Path,
+ None,
+ ptr::null_mut (),
+ Box::new (TestNodeImpl {})));
+
+ assert! (Node::is_ancestor (node.clone (), node.clone ()));
+ }
+
+ #[test]
+ fn node_is_ancestor_of_child () {
+ let node = Rc::new (Node::new (NodeType::Path,
+ None,
+ ptr::null_mut (),
+ Box::new (TestNodeImpl {})));
+
+ let child = Rc::new (Node::new (NodeType::Path,
+ Some (Rc::downgrade (&node)),
+ ptr::null_mut (),
+ Box::new (TestNodeImpl {})));
+
+ node.add_child (&child);
+
+ assert! (Node::is_ancestor (node.clone (), child.clone ()));
+ assert! (!Node::is_ancestor (child.clone (), node.clone ()));
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]