Class: Permutation

Inherits:
Object
  • Object
show all
Defined in:
lib/intperm.rb

Overview

Main permutation class.

Instance Method Summary collapse

Constructor Details

#initialize(seed = nil, bit_length = 64) ⇒ Permutation

Returns a new instance of Permutation.



5
6
7
8
9
10
11
12
13
# File 'lib/intperm.rb', line 5

def initialize(seed = nil, bit_length = 64)
  @mask = (1 << bit_length) - 1
  seed = Random.rand @mask if seed.nil?
  xorshift = XORShift.new seed, @mask
  @masks = (0.upto bit_length * 2).map do |i|
    xorshift.next & ((1 << (i >> 1)) ^ @mask)
  end
  @bit_length = bit_length
end

Instance Method Details

#map_from(num) ⇒ Object



19
20
21
# File 'lib/intperm.rb', line 19

def map_from(num)
  permutate num, ((@bit_length - 1).downto 0)
end

#map_to(num) ⇒ Object



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

def map_to(num)
  permutate num, (0.upto @bit_length - 1)
end