Class: CompTree::Node
- Inherits:
-
Object
- Object
- CompTree::Node
- Defined in:
- lib/comp_tree/node.rb
Overview
Base class for nodes in the computation tree.
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#computed ⇒ Object
Returns the value of attribute computed.
-
#function ⇒ Object
Returns the value of attribute function.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parents ⇒ Object
Returns the value of attribute parents.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
-
#children_results ⇒ Object
If all children have been computed, return their results; otherwise return nil.
-
#compute ⇒ Object
Compute this node; children must be computed and lock must be already acquired.
-
#compute_now ⇒ Object
Force all children and self to be computed; no locking required.
- #free? ⇒ Boolean
-
#initialize(name) ⇒ Node
constructor
Create a node.
- #lock ⇒ Object
-
#reset ⇒ Object
Reset the computation for this node and all children.
-
#reset_self ⇒ Object
Reset the computation for this node.
- #unlock ⇒ Object
Constructor Details
#initialize(name) ⇒ Node
Create a node
20 21 22 23 24 25 26 |
# File 'lib/comp_tree/node.rb', line 20 def initialize(name) @name = name @parents = [] @children = [] @function = nil reset_self end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def children @children end |
#computed ⇒ Object
Returns the value of attribute computed.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def computed @computed end |
#function ⇒ Object
Returns the value of attribute function.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def function @function end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/comp_tree/node.rb', line 7 def name @name end |
#parents ⇒ Object
Returns the value of attribute parents.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def parents @parents end |
#result ⇒ Object
Returns the value of attribute result.
9 10 11 |
# File 'lib/comp_tree/node.rb', line 9 def result @result end |
Instance Method Details
#children_results ⇒ Object
If all children have been computed, return their results; otherwise return nil.
71 72 73 74 75 76 77 78 |
# File 'lib/comp_tree/node.rb', line 71 def children_results @children_results ||= @children.map { |child| unless child.computed return nil end child.result } end |
#compute ⇒ Object
Compute this node; children must be computed and lock must be already acquired.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/comp_tree/node.rb', line 84 def compute begin unless @function raise NoFunctionError.new(@name) end @result = @function.call(*@children_results) @computed = true rescue Exception => exception @computed = exception end @result end |
#compute_now ⇒ Object
Force all children and self to be computed; no locking required. Intended to be used outside of parallel computations.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/comp_tree/node.rb', line 52 def compute_now unless @computed unless @children_results @children_results = @children.map { |child| child.compute_now } end compute if @computed.is_a? Exception raise @computed end end @result end |
#free? ⇒ Boolean
97 98 99 |
# File 'lib/comp_tree/node.rb', line 97 def free? @lock_level.zero? end |
#lock ⇒ Object
101 102 103 104 105 106 |
# File 'lib/comp_tree/node.rb', line 101 def lock @lock_level = @lock_level.succ @parents.each { |parent| parent.lock } end |
#reset ⇒ Object
Reset the computation for this node and all children.
41 42 43 44 45 46 |
# File 'lib/comp_tree/node.rb', line 41 def reset reset_self @children.each { |child| child.reset } end |
#reset_self ⇒ Object
Reset the computation for this node.
31 32 33 34 35 36 |
# File 'lib/comp_tree/node.rb', line 31 def reset_self @result = nil @computed = nil @lock_level = 0 @children_results = nil end |
#unlock ⇒ Object
108 109 110 111 112 113 |
# File 'lib/comp_tree/node.rb', line 108 def unlock @lock_level -= 1 # Fixnum#pred not in 1.8.6 @parents.each { |parent| parent.unlock } end |