Class: Lm::BinFunc

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

Instance Method Summary collapse

Constructor Details

#initialize(products) ⇒ BinFunc

Returns a new instance of BinFunc.



5
6
7
# File 'lib/lm/bin_func.rb', line 5

def initialize(products)
  @products = products.arr
end

Instance Method Details

#apply(product, bitstr) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lm/bin_func.rb', line 17

def apply(product, bitstr)
  raise "len mismatch" if bitstr.length != product.vars.length

  result = 1

  inputlen.times do |x|
    next if product.vars[x].invalid?

    val = bitstr[x].to_i

    val = ~val if product.vars[x].value == "0"

    result &= val
  end

  result
end

#evaluate(num) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lm/bin_func.rb', line 35

def evaluate(num)
  raise "number too large" if num > max

  bitstr = num.to_s(2).rjust(inputlen, "0")

  result = 0
  @products.each do |product|
    result |= apply(product, bitstr)
  end

  result
end

#inputlenObject



9
10
11
# File 'lib/lm/bin_func.rb', line 9

def inputlen
  @products.first.vars.count
end

#maxObject



13
14
15
# File 'lib/lm/bin_func.rb', line 13

def max
  (1 << inputlen) - 1
end