Class: BinPacking::Score

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bin_packing/score.rb

Constant Summary collapse

MAX_INT =
(2**(0.size * 8 -2) -1)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(score_1 = nil, score_2 = nil) ⇒ Score

Returns a new instance of Score.



13
14
15
16
# File 'lib/bin_packing/score.rb', line 13

def initialize(score_1 = nil, score_2 = nil)
  @score_1 = score_1 || MAX_INT
  @score_2 = score_2 || MAX_INT
end

Instance Attribute Details

#score_1Object (readonly)

Returns the value of attribute score_1.



7
8
9
# File 'lib/bin_packing/score.rb', line 7

def score_1
  @score_1
end

#score_2Object (readonly)

Returns the value of attribute score_2.



7
8
9
# File 'lib/bin_packing/score.rb', line 7

def score_2
  @score_2
end

Class Method Details

.new_blankObject



9
10
11
# File 'lib/bin_packing/score.rb', line 9

def self.new_blank
  new
end

Instance Method Details

#<=>(other) ⇒ Object

Smaller number is greater (used by original algorithm).



19
20
21
22
23
24
25
26
27
# File 'lib/bin_packing/score.rb', line 19

def <=>(other)
  if self.score_1 > other.score_1 || (self.score_1 == other.score_1 && self.score_2 > other.score_2)
    -1
  elsif self.score_1 < other.score_1 || (self.score_1 == other.score_1 && self.score_2 < other.score_2)
    1
  else
    0
  end
end

#assign(other) ⇒ Object



29
30
31
32
# File 'lib/bin_packing/score.rb', line 29

def assign(other)
  @score_1 = other.score_1
  @score_2 = other.score_2
end

#decrease_by(delta) ⇒ Object



38
39
40
41
# File 'lib/bin_packing/score.rb', line 38

def decrease_by(delta)
  @score_1 += delta
  @score_2 += delta
end

#is_blank?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bin_packing/score.rb', line 34

def is_blank?
  @score_1 == MAX_INT
end