From: Jean-Christophe Beaupré Date: Sat, 20 Dec 2014 19:35:33 +0000 (-0500) Subject: neo4j/graph: Optimize some services of `SequentialNodeCollection`. X-Git-Tag: v0.7.1~38^2~3 X-Git-Url: http://nitlanguage.org neo4j/graph: Optimize some services of `SequentialNodeCollection`. Signed-off-by: Jean-Christophe Beaupré --- diff --git a/lib/neo4j/graph/sequential_id.nit b/lib/neo4j/graph/sequential_id.nit index 3b3559a..0bcaa6c 100644 --- a/lib/neo4j/graph/sequential_id.nit +++ b/lib/neo4j/graph/sequential_id.nit @@ -36,6 +36,7 @@ private import pipeline # assert b["id"] == 2 # assert c["id"] == 4 # assert nodes.to_a == [a, b, c] +# assert nodes.length == 3 # ~~~ class SequentialNodeCollection super NeoNodeCollection @@ -44,16 +45,25 @@ class SequentialNodeCollection private var nodes = new Array[nullable NeoNode] + redef var length = 0 + redef fun iterator do return new NullSkipper[NeoNode](self.nodes.iterator) + redef fun [](id) do return nodes[id].as(NeoNode) + redef fun get_or_null(id) do if id < 0 or id > nodes.length then return null return nodes[id] end + redef fun has_id(id: Int): Bool do + return id >= 0 and id < nodes.length and nodes[id] isa NeoNode + end + redef fun register(node) do nodes.add node id_of(node) = nodes.length + length += 1 end redef fun add(node) do @@ -65,15 +75,23 @@ class SequentialNodeCollection sys.stderr.write "The local ID must be greater or equal to 0. Got {id}.\n" end # Pad with nulls. + nodes.enlarge(id) var delta = id - nodes.length while delta > 0 do nodes.add null delta -= 1 end nodes[id] = node + length += 1 end redef fun remove_at(id) do nodes[id] = null + length -= 1 + end + + redef fun clear do + nodes.clear + length = 0 end end