class Node

Represents to a node in the graph

Author: Johnny Lee Othon

Attributes

id[RW]
name[RW]

Public Class Methods

new(id, name=nil) click to toggle source
# File lib/usearchtree/node.rb, line 10
def initialize id, name=nil
    @name = name
    @id = id
    @edges = Array.new
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/usearchtree/node.rb, line 26
def <=> other
    return @id <=> other.id
end
add_edge(node, cost) click to toggle source
# File lib/usearchtree/node.rb, line 45
def add_edge node, cost
    @edges << Edge.new(node, cost)
end
cost(node) click to toggle source
# File lib/usearchtree/node.rb, line 49
def cost node
    edge = @edges.find{|e| e.node == node}
    if edge
        edge.cost
    end
end
edges() click to toggle source

Gets an enumerator of the edges

# File lib/usearchtree/node.rb, line 62
def edges
    return @edges.each
end
inspect() click to toggle source

Returns the name of the node or its id as a string.

# File lib/usearchtree/node.rb, line 22
def inspect
    self.to_s
end
key() click to toggle source

Gets the name if exists or the id

# File lib/usearchtree/node.rb, line 57
def key
    @name or @id
end
to_adjacency_list() click to toggle source

Returns the node in the format <id:name>.

# File lib/usearchtree/node.rb, line 31
def to_adjacency_list
    s = "#{name}"
    unless @name.nil? or @name.empty?
        s += "(#{@id})"
    end
    unless @edges.empty?
        s += ": "
    end
    nodes = @edges.map do |edge|
        "#{edge.node}=>#{edge.cost}"
    end.join(", ")
    s += nodes
end
to_s() click to toggle source

Returns the name of the node or its id as a string.

# File lib/usearchtree/node.rb, line 17
def to_s
    @name or "#{@id}"
end