From 23b92da86a8ca2397f6247ce6968191cc4e64cb8 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 27 Feb 2014 20:27:57 -0500 Subject: [PATCH] lib: introduce service `depth` in PosetElement Signed-off-by: Alexandre Terrasa --- lib/poset.nit | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/poset.nit b/lib/poset.nit index aea40bb..fc6090f 100644 --- a/lib/poset.nit +++ b/lib/poset.nit @@ -230,4 +230,20 @@ class POSetElement[E: Object] do return t != self.element and self.tos.has(t) end + + # The length of the shortest path to the root of the poset hierarchy + fun depth: Int do + if direct_greaters.is_empty then + return 0 + end + var min = -1 + for p in direct_greaters do + var d = poset[p].depth + 1 + if min == -1 or d < min then + min = d + end + end + return min + + end end -- 1.7.9.5