Class: QuineMc::Pi_List

Inherits:
Array
  • Object
show all
Defined in:
lib/quine_mc/pi_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(m, d) ⇒ Pi_List

Returns a new instance of Pi_List.



6
7
8
9
10
# File 'lib/quine_mc/pi_list.rb', line 6

def initialize(m, d)
	@cube_table = order_terms((m + d).sort)
	#puts @cube_table.inspect
	get_pis()
end

Instance Method Details

#get_pisObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/quine_mc/pi_list.rb', line 33

def get_pis()
	next_table = {}
	while !@cube_table.empty?
		#puts @cube_table.inspect
		@cube_table.each do |k,v|
			if v
				v.each do |c1|
					if @cube_table[k+1]
						@cube_table[k+1].each do |c2|
							if makes_cube?(c1, c2)
								if !next_table[k] then next_table[k] = [] end
								next_table[k].push(Cube.new(c1+c2))
							end
						end
					end
					if c1.any? {|x| !next_table.values.flatten.include?(x)} then push(Cube.new(c1)) end
				end
			end
		end
		@cube_table = next_table
		next_table = {}
	end
end

#makes_cube?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/quine_mc/pi_list.rb', line 28

def makes_cube?(a, b)
	a.zip(b).all? { |x,y| power_of_2?(y-x) } and a.all? {|x| b.all? {|y| y>x} }
end

#order_terms(uo_terms) ⇒ Object

orders terms by number of set bits



13
14
15
16
17
18
19
20
21
22
# File 'lib/quine_mc/pi_list.rb', line 13

def order_terms(uo_terms)
	h = {}
	uo_terms.each do |t|
		t = Cube.new([t])
		b = t.num_ones
		h[b] = h[b] || Array.new
		h[b].push(t)
	end
	return h
end

#power_of_2?(x) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/quine_mc/pi_list.rb', line 24

def power_of_2?(x)
	((x != 0) and (x & (x-1) == 0))
end