Class: ArtDecomp::Blanket

Inherits:
Object
  • Object
show all
Defined in:
lib/art-decomp/blanket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ints) ⇒ Blanket

Returns a new instance of Blanket.



21
22
23
# File 'lib/art-decomp/blanket.rb', line 21

def initialize ints
  @ints = ints.to_set.delete(0).freeze
end

Instance Attribute Details

#intsObject (readonly)

Returns the value of attribute ints.



3
4
5
# File 'lib/art-decomp/blanket.rb', line 3

def ints
  @ints
end

Class Method Details

.[](*ints) ⇒ Object



5
6
7
# File 'lib/art-decomp/blanket.rb', line 5

def self.[] *ints
  new ints
end

.from_array(array) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/art-decomp/blanket.rb', line 9

def self.from_array array
  ints = Hash.new 0
  array.each_with_index do |enc, i|
    ints[enc] |= 1 << i
  end
  ints.each_key do |key|
    ints[key] |= ints[DontCare]
  end unless ints[DontCare].zero?
  ints.delete DontCare unless ints.size <= 1
  new ints.values
end

Instance Method Details

#*(other) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/art-decomp/blanket.rb', line 25

def * other
  ints = []
  @ints.each do |this_int|
    other.ints.each do |other_int|
      ints << (this_int & other_int)
    end
  end
  Blanket.new ints
end

#==(other) ⇒ Object Also known as: eql?



35
36
37
# File 'lib/art-decomp/blanket.rb', line 35

def == other
  @ints == other.ints
end

#encoding(bits) ⇒ Object



39
40
41
42
# File 'lib/art-decomp/blanket.rb', line 39

def encoding bits
  encs = encodings bits
  encs.size == 1 ? encs.first : raise(AmbiguousEncodingQuery, "ambiguous encoding query: block #{bits.bits.join ','}")
end

#encodings(bits) ⇒ Object



44
45
46
47
48
49
# File 'lib/art-decomp/blanket.rb', line 44

def encodings bits
  sorted = @ints.sort
  width = sorted.size.log2_ceil
  encs = @ints.select { |int| int & bits == bits }.map { |int| sorted.index(int) }.map { |i| i.to_s(2).rjust width, '0' }
  encs.size == 0 or encs.size == @ints.size ? [DontCare.to_s * width] : encs
end

#hashObject



53
54
55
# File 'lib/art-decomp/blanket.rb', line 53

def hash
  @ints.hash
end

#inspectObject



57
58
59
60
# File 'lib/art-decomp/blanket.rb', line 57

def inspect
  blocks = @ints.map(&:bits).sort.map { |bl| "B[#{bl.join ','}]" }
  "Blanket[#{blocks.join ', '}]"
end

#pinsObject



62
63
64
# File 'lib/art-decomp/blanket.rb', line 62

def pins
  @ints.size.log2_ceil
end

#sepsObject



66
67
68
69
70
# File 'lib/art-decomp/blanket.rb', line 66

def seps
  potential = @ints.inject(0, :|).bits.pairs.map { |a, b| Sep[a,b] }
  not_separate = @ints.map{ |i| i.bits.pairs.map { |a, b| Sep[a,b] } }.flatten
  (potential - not_separate).to_set
end

#sizeObject



72
73
74
# File 'lib/art-decomp/blanket.rb', line 72

def size
  @ints.size
end