Class: Lm::ImplicantChart

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

Instance Method Summary collapse

Constructor Details

#initialize(chart, length:, step: 0) ⇒ ImplicantChart

Returns a new instance of ImplicantChart.



5
6
7
8
9
# File 'lib/lm/implicant_chart.rb', line 5

def initialize(chart, length:, step: 0)
  @chart = chart
  @step = step
  @length = length
end

Instance Method Details

#bucketsObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lm/implicant_chart.rb', line 11

def buckets
  @buckets ||= begin
    rr = {}
    @chart.each do |k, v|
      ones = v.count("1")
      rr[ones] ||= {}
      rr[ones][k] = v
    end
    # p rr
    rr
  end
end

#keysObject



24
25
26
# File 'lib/lm/implicant_chart.rb', line 24

def keys
  Set.new(@chart.keys)
end

#reducedObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lm/implicant_chart.rb', line 28

def reduced
  rr = {}
  touched = Set.new
  @length.times do |x|
    next if buckets[x].nil?
    next if buckets[x + 1].nil?

    buckets[x].each do |num, pattern|
      buckets[x + 1].each do |num2, pattern2|
        diffcount = 0
        last = 0
        @length.times do |col|
          if pattern[col] == "-" && pattern2[col] != "-" ||
             pattern2[col] == "-" && pattern[col] != "-"
            diffcount += 2
            last = col
          end
          next unless pattern[col] == "1" && pattern2[col] == "0" ||
                      pattern2[col] == "1" && pattern[col] == "0"

          diffcount += 1
          last = col
        end
        next unless diffcount == 1

        str = pattern.dup
        str[last] = "-"
        rr["#{num},#{num2}".split(",").sort.join(",").to_str] = str
        touched << num.to_s
        touched << num2.to_s
      end
    end
  end

  @chart.each do |k, v|
    next if touched.include? k

    rr[k] = v
  end
  ImplicantChart.new(rr, step: @step + 1, length: @length)
end

#soplistObject



70
71
72
73
74
75
76
# File 'lib/lm/implicant_chart.rb', line 70

def soplist
  rr = {}
  @chart.each do |k, v|
    rr[k] = Product.new(v)
  end
  rr
end

#variablesObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lm/implicant_chart.rb', line 78

def variables
  @variables ||= begin
    vars = Set.new
    @chart.each_key do |x|
      x.split(",").each do |name|
        vars << name
      end
    end
    vars
  end
end