Class: Knapsacker::Node
- Inherits:
-
Object
- Object
- Knapsacker::Node
- Defined in:
- lib/knapsacker/node.rb
Instance Attribute Summary collapse
-
#item_index ⇒ Object
readonly
Returns the value of attribute item_index.
-
#negative_child ⇒ Object
Returns the value of attribute negative_child.
-
#positive_child ⇒ Object
Returns the value of attribute positive_child.
-
#upper_bound ⇒ Object
readonly
Returns the value of attribute upper_bound.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #cap_negative_child! ⇒ Object
- #cap_positive_child! ⇒ Object
- #cost ⇒ Object
-
#initialize(item_index, item, upper_bound, parent) ⇒ Node
constructor
A new instance of Node.
- #items ⇒ Object
- #leaf? ⇒ Boolean
- #negative_child_growable? ⇒ Boolean
- #positive_child_growable? ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(item_index, item, upper_bound, parent) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 |
# File 'lib/knapsacker/node.rb', line 8 def initialize(item_index, item, upper_bound, parent) @item_index = item_index @item = item @upper_bound = upper_bound @parent = parent end |
Instance Attribute Details
#item_index ⇒ Object (readonly)
Returns the value of attribute item_index.
5 6 7 |
# File 'lib/knapsacker/node.rb', line 5 def item_index @item_index end |
#negative_child ⇒ Object
Returns the value of attribute negative_child.
3 4 5 |
# File 'lib/knapsacker/node.rb', line 3 def negative_child @negative_child end |
#positive_child ⇒ Object
Returns the value of attribute positive_child.
4 5 6 |
# File 'lib/knapsacker/node.rb', line 4 def positive_child @positive_child end |
#upper_bound ⇒ Object (readonly)
Returns the value of attribute upper_bound.
6 7 8 |
# File 'lib/knapsacker/node.rb', line 6 def upper_bound @upper_bound end |
Instance Method Details
#<=>(other) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/knapsacker/node.rb', line 15 def <=>(other) if other.upper_bound == upper_bound other.item_index <=> item_index else other.upper_bound <=> upper_bound end end |
#cap_negative_child! ⇒ Object
23 24 25 |
# File 'lib/knapsacker/node.rb', line 23 def cap_negative_child! @negative_child = :cap end |
#cap_positive_child! ⇒ Object
27 28 29 |
# File 'lib/knapsacker/node.rb', line 27 def cap_positive_child! @positive_child = :cap end |
#cost ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/knapsacker/node.rb', line 31 def cost if @parent @parent.cost + (@item ? @item.cost : 0) else 0 end end |
#items ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/knapsacker/node.rb', line 39 def items if @parent if @item @parent.items << @item else @parent.items end else [] end end |
#leaf? ⇒ Boolean
51 52 53 |
# File 'lib/knapsacker/node.rb', line 51 def leaf? @negative_child == :cap && @positive_child == :cap end |
#negative_child_growable? ⇒ Boolean
55 56 57 |
# File 'lib/knapsacker/node.rb', line 55 def negative_child_growable? @negative_child.nil? end |
#positive_child_growable? ⇒ Boolean
59 60 61 |
# File 'lib/knapsacker/node.rb', line 59 def positive_child_growable? @positive_child.nil? end |
#value ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/knapsacker/node.rb', line 63 def value if @parent @parent.value + (@item ? @item.value : 0) else 0 end end |