Module: MoreMath::RankingCommon

Included in:
Permutation, Subset
Defined in:
lib/more_math/ranking_common.rb

Overview

Common functionality for ranking systems. This module provides basic ranking operations that are shared across different ranking implementations in the MoreMath library, including permutations and subsets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject? (readonly)

Returns the collection the #rank applies to if any was set, otherwise returns nil.

Returns:

  • (Object, nil)

    the associated collection or nil



26
27
28
# File 'lib/more_math/ranking_common.rb', line 26

def collection
  @collection
end

#lastInteger (readonly)

Returns the rank of the last ranked instance.

Returns:

  • (Integer)

    the maximum rank value for this size



20
21
22
# File 'lib/more_math/ranking_common.rb', line 20

def last
  @last
end

#rankInteger (readonly)

Returns the rank of this instance, a Fixnum in the range of 0 and #last.

Returns:

  • (Integer)

    the current rank



15
16
17
# File 'lib/more_math/ranking_common.rb', line 15

def rank
  @rank
end

#sizeInteger (readonly)

Returns the size of this instance’s collection, a Fixnum.

Returns:

  • (Integer)

    the size of the collection



9
10
11
# File 'lib/more_math/ranking_common.rb', line 9

def size
  @size
end

Instance Method Details

#each {|instance| ... } ⇒ self

Iterates over all instances starting with the first (rank == 0) ranked instance and ending with the last (rank == #last) ranked instance while yielding to a freshly created instance for every iteration step.

The mixed in methods from the Enumerable module rely on this method.

Yields:

  • (instance)

    yields each instance in sequence

Returns:

  • (self)

    returns self after iteration



92
93
94
95
96
97
98
99
# File 'lib/more_math/ranking_common.rb', line 92

def each
  0.upto(last) do |r|
    klon = clone
    klon.rank = r
    yield klon
  end
  self
end

#each! {|instance| ... } ⇒ self

Does something similar to #each. It doesn’t create new instances (less overhead) for every iteration step, but yields to a modified self instead. This is useful if one only wants to call a method on the yielded value and work with the result of this call. It’s not a good idea to put the yielded values in a data structure because all of them will reference the same (this!) instance. If you want to do this use #each.

Yields:

  • (instance)

    yields the current instance

Returns:

  • (self)

    returns self after iteration



110
111
112
113
114
115
116
117
118
119
# File 'lib/more_math/ranking_common.rb', line 110

def each!
  old_rank = rank
  0.upto(last) do |r|
    self.rank = r
    yield self
  end
  self
ensure
  self.rank = old_rank
end

#nextself Also known as: succ

Returns the next ranked instance. If this instance is the #last instance it returns the first (rank == 0) instance again.

Returns:

  • (self)

    a new instance with incremented rank



43
44
45
# File 'lib/more_math/ranking_common.rb', line 43

def next
  clone.next!
end

#next!self Also known as: succ!

Switches this instance to the next ranked instance. If this was the #last instance it wraps around to the first (rank == 0) instance.

Returns:

  • (self)

    returns self after incrementing rank



32
33
34
35
# File 'lib/more_math/ranking_common.rb', line 32

def next!
  self.rank += 1
  self
end

#predself

Returns the previously ranked instance. If this was the first instance it returns the last (rank == #last) instance.

Returns:

  • (self)

    a new instance with decremented rank



62
63
64
# File 'lib/more_math/ranking_common.rb', line 62

def pred
  clone.pred!
end

#pred!self

Switches this instance to the previously ranked instance. If this was the first instance it returns the last (rank == #last) instance.

Returns:

  • (self)

    returns self after decrementing rank



53
54
55
56
# File 'lib/more_math/ranking_common.rb', line 53

def pred!
  self.rank -= 1
  self
end

#randomself

Returns a randomly ranked instance.

Returns:

  • (self)

    a new instance with random rank



78
79
80
# File 'lib/more_math/ranking_common.rb', line 78

def random
  clone.random!
end

#random!self

Switches this instance to a randomly ranked instance.

Returns:

  • (self)

    returns self after setting random rank



69
70
71
72
73
# File 'lib/more_math/ranking_common.rb', line 69

def random!
  new_rank = rand(last + 1).to_i
  self.rank = new_rank
  self
end