Class: DAG::Vertex
- Inherits:
-
Object
- Object
- DAG::Vertex
- Defined in:
- lib/dag/vertex.rb
Instance Attribute Summary collapse
-
#dag ⇒ Object
readonly
Returns the value of attribute dag.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve a value from the vertex’s payload.
- #ancestors(result_set = Set.new) ⇒ Object
- #descendants(result_set = Set.new) ⇒ Object
-
#has_ancestor?(other) ⇒ Boolean
(also: #is_reachable_from?)
Is there a path from
other
to here following edges in the DAG?. -
#has_path_to?(other) ⇒ Boolean
(also: #has_descendant?, #has_descendent?)
Is there a path from here to
other
following edges in the DAG?. - #incoming_edges ⇒ Object
- #inspect ⇒ Object
- #outgoing_edges ⇒ Object
- #predecessors ⇒ Object
- #successors ⇒ Object
Instance Attribute Details
#dag ⇒ Object (readonly)
Returns the value of attribute dag.
6 7 8 |
# File 'lib/dag/vertex.rb', line 6 def dag @dag end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
6 7 8 |
# File 'lib/dag/vertex.rb', line 6 def payload @payload end |
Instance Method Details
#[](key) ⇒ Object
Retrieve a value from the vertex’s payload. This is a shortcut for vertex.payload.
73 74 75 |
# File 'lib/dag/vertex.rb', line 73 def [](key) @payload[key] end |
#ancestors(result_set = Set.new) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/dag/vertex.rb', line 77 def ancestors(result_set = Set.new) predecessors.each do |v| unless result_set.include? v result_set.add(v) v.ancestors(result_set) end end return result_set end |
#descendants(result_set = Set.new) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/dag/vertex.rb', line 87 def descendants(result_set = Set.new) successors.each do |v| unless result_set.include? v result_set.add(v) v.descendants(result_set) end end return result_set end |
#has_ancestor?(other) ⇒ Boolean Also known as: is_reachable_from?
Is there a path from other
to here following edges in the DAG?
58 59 60 61 62 |
# File 'lib/dag/vertex.rb', line 58 def has_ancestor?(other) raise ArgumentError.new('You must supply a vertex in this DAG') unless is_vertex_in_my_dag?(other) predecessors.include?(other) || predecessors.any? {|v| v.has_ancestor?(other) } end |
#has_path_to?(other) ⇒ Boolean Also known as: has_descendant?, has_descendent?
Is there a path from here to other
following edges in the DAG?
42 43 44 45 46 |
# File 'lib/dag/vertex.rb', line 42 def has_path_to?(other) raise ArgumentError.new('You must supply a vertex in this DAG') unless is_vertex_in_my_dag?(other) successors.include?(other) || successors.any? {|v| v.has_path_to?(other) } end |
#incoming_edges ⇒ Object
19 20 21 |
# File 'lib/dag/vertex.rb', line 19 def incoming_edges @dag.edges.select {|e| e.destination == self} end |
#inspect ⇒ Object
31 32 33 |
# File 'lib/dag/vertex.rb', line 31 def inspect "DAG::Vertex:#{@payload.inspect}" end |
#outgoing_edges ⇒ Object
15 16 17 |
# File 'lib/dag/vertex.rb', line 15 def outgoing_edges @dag.edges.select {|e| e.origin == self} end |
#predecessors ⇒ Object
23 24 25 |
# File 'lib/dag/vertex.rb', line 23 def predecessors incoming_edges.map(&:origin).uniq end |
#successors ⇒ Object
27 28 29 |
# File 'lib/dag/vertex.rb', line 27 def successors outgoing_edges.map(&:destination).uniq end |