neo4j/graph: Implement optimization services of `SequentialNodeCollection`.
[nit.git] / lib / neo4j / graph / sequential_id.nit
index 0bcaa6c..fb6129f 100644 (file)
@@ -37,6 +37,13 @@ private import pipeline
 # assert c["id"] == 4
 # assert nodes.to_a == [a, b, c]
 # assert nodes.length == 3
+#
+# nodes.compact
+# assert a["id"] == 1
+# assert b["id"] == 2
+# assert c["id"] == 3
+# assert nodes.to_a == [a, b, c]
+# assert nodes.length == 3
 # ~~~
 class SequentialNodeCollection
        super NeoNodeCollection
@@ -60,6 +67,8 @@ class SequentialNodeCollection
                return id >= 0 and id < nodes.length and nodes[id] isa NeoNode
        end
 
+       redef fun enlarge(cap) do nodes.enlarge(cap)
+
        redef fun register(node) do
                nodes.add node
                id_of(node) = nodes.length
@@ -94,4 +103,14 @@ class SequentialNodeCollection
                nodes.clear
                length = 0
        end
+
+       redef fun compact do
+               var i = iterator
+
+               nodes = new Array[nullable NeoNode]
+               for n in i do
+                       nodes.add n
+                       id_of(n) = nodes.length
+               end
+       end
 end