neo4j/graph: Implement optimization services of `SequentialNodeCollection`.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Sat, 20 Dec 2014 19:24:57 +0000 (14:24 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Mon, 29 Dec 2014 20:53:47 +0000 (15:53 -0500)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

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