class Mementus::Structure::AdjacencyList
Public Class Methods
new(is_directed=true)
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 6 def initialize(is_directed=true) @index = {} @is_directed = is_directed end
Public Instance Methods
adjacent(id)
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 61 def adjacent(id) @index[id].to_a end
directed?()
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 21 def directed? @is_directed end
each_edge() { |edge_proxy(edge(from: from, to: to), self)| ... }
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 65 def each_edge(&blk) if directed? nodes.each do |from| adjacent(from.id).each do |to| yield EdgeProxy.new(Edge.new(from: from, to: to), self) end end else raise 'Edge traversal unsupported for undirected graphs' end end
edges_count()
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 15 def edges_count c = 0 each_edge { c += 1 } c end
has_edge?(edge)
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 45 def has_edge?(edge) if edge.is_a?(Mementus::Edge) || edge.is_a?(Mementus::EdgeProxy) has_node?(edge.from.id) && @index[edge.from.id].include?(edge.to.id) else raise 'Edge IDs are not supported by this data structure' end end
has_node?(node)
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 37 def has_node?(node) if node.is_a?(Mementus::Node) || node.is_a?(Mementus::NodeProxy) @index.key?(node.id) else @index.key?(node) end end
node(id)
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 53 def node(id) NodeProxy.new(Node.new(id: id), self) end
nodes()
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 57 def nodes @index.keys.map { |id| NodeProxy.new(Node.new(id: id), self) } end
nodes_count()
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 11 def nodes_count @index.size end
set_edge(edge)
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 29 def set_edge(edge) set_node(edge.from) unless has_node?(edge.from.id) set_node(edge.to) unless has_node?(edge.to.id) @index[edge.from.id].add(edge.to.id) @index[edge.to.id].add(edge.from.id) unless directed? end
set_node(node)
click to toggle source
# File lib/mementus/structure/adjacency_list.rb, line 25 def set_node(node) @index[node.id] ||= Set.new end