ordered_tree: remove subs when all children are detached.
[nit.git] / lib / ordered_tree.nit
index 32cea45..d1dde15 100644 (file)
@@ -128,7 +128,15 @@ class OrderedTree[E: Object]
        do
                var old_parent = parents.get_or_null(e)
                if old_parent != null then
-                       sub[old_parent].remove(e)
+                       var subs = sub[old_parent]
+                       subs.remove(e)
+                       if subs.is_empty then
+                               # remove the sub when all children are detached
+                               # so that `==` and `hash` are sane
+                               # Otherwise an empty array will be considered
+                               # differently than no array.
+                               sub.keys.remove(old_parent)
+                       end
                else if roots.has(e) then
                        roots.remove(e)
                end