Class: Graphsrb::AdjacencyList
- Inherits:
-
Object
- Object
- Graphsrb::AdjacencyList
- Includes:
- Enumerable
- Defined in:
- lib/graphsrb/adjacency_list.rb
Class Method Summary collapse
-
.create_node(vertex_id, args = {}) ⇒ Object
Creates and returns the created node.
Instance Method Summary collapse
-
#<<(node) ⇒ Object
Adds a node to the adjacency list.
-
#add(node) ⇒ Object
Adds a node to the adjacency list.
-
#clear ⇒ Object
Remove all nodes from the list.
-
#delete(node) ⇒ Object
Removes a node from the adjacency list.
- #each(&block) ⇒ Object
-
#find(node) ⇒ Object
Searches for a node in the adjacency list Returns nil if not found.
-
#has_node?(node) ⇒ Boolean
Returns true if the adjacency list contains the node, false otherwise.
-
#increase_weight(node, dw) ⇒ Object
Increses weight by
dw. -
#initialize ⇒ AdjacencyList
constructor
A new instance of AdjacencyList.
-
#nodes ⇒ Object
Returns all nodes.
-
#size ⇒ Object
Returns the number of nodes in the adjacency list.
-
#update_weight(node, w) ⇒ Object
Updates weight.
Constructor Details
#initialize ⇒ AdjacencyList
Returns a new instance of AdjacencyList.
5 6 7 |
# File 'lib/graphsrb/adjacency_list.rb', line 5 def initialize @adj_list = [] end |
Class Method Details
.create_node(vertex_id, args = {}) ⇒ Object
Creates and returns the created node
72 73 74 |
# File 'lib/graphsrb/adjacency_list.rb', line 72 def self.create_node(vertex_id, args={}) Graphsrb::Node.new(vertex_id, args) end |
Instance Method Details
#<<(node) ⇒ Object
Adds a node to the adjacency list
15 16 17 |
# File 'lib/graphsrb/adjacency_list.rb', line 15 def <<(node) add(node) end |
#add(node) ⇒ Object
Adds a node to the adjacency list
10 11 12 |
# File 'lib/graphsrb/adjacency_list.rb', line 10 def add(node) adj_list << node.clone end |
#clear ⇒ Object
Remove all nodes from the list
67 68 69 |
# File 'lib/graphsrb/adjacency_list.rb', line 67 def clear adj_list.clear end |
#delete(node) ⇒ Object
Removes a node from the adjacency list
41 42 43 |
# File 'lib/graphsrb/adjacency_list.rb', line 41 def delete(node) adj_list.delete(node) end |
#each(&block) ⇒ Object
31 32 33 |
# File 'lib/graphsrb/adjacency_list.rb', line 31 def each(&block) @adj_list.each(&block) end |
#find(node) ⇒ Object
Searches for a node in the adjacency list Returns nil if not found
47 48 49 50 51 52 53 54 |
# File 'lib/graphsrb/adjacency_list.rb', line 47 def find(node) index = adj_list.index node if index.nil? return nil else adj_list[index] end end |
#has_node?(node) ⇒ Boolean
Returns true if the adjacency list contains the node, false otherwise
62 63 64 |
# File 'lib/graphsrb/adjacency_list.rb', line 62 def has_node?(node) not find(node).nil? end |
#increase_weight(node, dw) ⇒ Object
Increses weight by dw
26 27 28 29 |
# File 'lib/graphsrb/adjacency_list.rb', line 26 def increase_weight(node, dw) node = find(node) node.update_weight(node.weight + dw) unless node.nil? end |
#nodes ⇒ Object
Returns all nodes
57 58 59 |
# File 'lib/graphsrb/adjacency_list.rb', line 57 def nodes adj_list.clone end |
#size ⇒ Object
Returns the number of nodes in the adjacency list
36 37 38 |
# File 'lib/graphsrb/adjacency_list.rb', line 36 def size adj_list.size end |
#update_weight(node, w) ⇒ Object
Updates weight
20 21 22 23 |
# File 'lib/graphsrb/adjacency_list.rb', line 20 def update_weight(node, w) node = find(node) node.update_weight(w) unless node.nil? end |