Class: LogicTools::Implicant

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/logic_tools/logicsimplify_qm.rb

Overview

Represents a logic implicant.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Implicant

Creates a new implicant from base.

Argument +base+ can be either another implicant or a bit string.


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/logic_tools/logicsimplify_qm.rb', line 56

def initialize(base)
    if base.is_a?(Implicant)
        @covers = base.covers.dup
        @bits = base.bits.dup
        @mask = base.mask.dup
        @count = base.count
    else
        @bits = base.to_s
        unless @bits.match(/^[01]*$/)
            raise "Invalid bit string for an initial implicant: "+ @bits
        end
        @mask = " " * @bits.size
        @count = @bits.count("1")
        @covers = [ @bits ]
    end
    @prime = true # By default assumed prime
end

Instance Attribute Details

#bitsObject (readonly)

The bit vector of the implicant.



37
38
39
# File 'lib/logic_tools/logicsimplify_qm.rb', line 37

def bits
  @bits
end

#countObject (readonly)

The number of 1 of the implicant.



39
40
41
# File 'lib/logic_tools/logicsimplify_qm.rb', line 39

def count
  @count
end

#coversObject

The bit values covered by the implicant.



41
42
43
# File 'lib/logic_tools/logicsimplify_qm.rb', line 41

def covers
  @covers
end

#maskObject (readonly)

The positions of the X in the implicant.



35
36
37
# File 'lib/logic_tools/logicsimplify_qm.rb', line 35

def mask
  @mask
end

#primeObject

Tell if the implicant is prime or not.



43
44
45
# File 'lib/logic_tools/logicsimplify_qm.rb', line 43

def prime
  @prime
end

#varObject

The variable associated with the implicant

Do not interfer at all with the class, so
public and fully accessible


47
48
49
# File 'lib/logic_tools/logicsimplify_qm.rb', line 47

def var
  @var
end

Instance Method Details

#<=>(implicant) ⇒ Object

:nodoc:



103
104
105
# File 'lib/logic_tools/logicsimplify_qm.rb', line 103

def <=>(implicant) #:nodoc:
    @bits <=> implicant.to_s
end

#==(implicant) ⇒ Object

Compares with implicant



100
101
102
# File 'lib/logic_tools/logicsimplify_qm.rb', line 100

def ==(implicant) # :nodoc:
    @bits == implicant.to_s
end

#[](i) ⇒ Object

Gets the value of bit i.



113
114
115
# File 'lib/logic_tools/logicsimplify_qm.rb', line 113

def [](i)
    @bits[i]
end

#[]=(i, b) ⇒ Object

Sets the value of bit i to b.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/logic_tools/logicsimplify_qm.rb', line 118

def []=(i,b)
    raise "Invalid bit value: #{b}" unless ["0","1","x"].include?(b)
    return if @bits[i] == b # Already set
    # Update count and mask
    @count -= 1 if @bits[i] == "1"    # One 1 less
    @count += 1 if b == "1"           # One 1 more
    @mask[i] = " " if @bits[i] == "x" # One x less
    @mask[i] = "x" if b == "x"        # One x more
    # Update the bit string
    @bits[i] = b 
end

#dupObject

duplicates the implicant.



108
109
110
# File 'lib/logic_tools/logicsimplify_qm.rb', line 108

def dup # :nodoc:
    Implicant.new(self)
end

#each(&blk) ⇒ Object

Iterates over the bits of the implicant.

Returns an enumerator if no block given.


91
92
93
94
95
96
97
# File 'lib/logic_tools/logicsimplify_qm.rb', line 91

def each(&blk)
    # No block given? Returns an enumerator
    return to_enum(:each) unless block_given?
    
    # Block given? Applies it on each bit.
    @bits.each_char(&blk)
end

#inspectObject

:nodoc:



79
80
81
# File 'lib/logic_tools/logicsimplify_qm.rb', line 79

def inspect # :nodoc:
    @bits.dup
end

#merge(implicant) ⇒ Object

Creates a new implicant merging current implicant with imp.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/logic_tools/logicsimplify_qm.rb', line 132

def merge(implicant)
    # Has implicant the same mask?
    return nil unless implicant.mask == @mask
    # First look for a 1-0 or 0-1 difference
    found = nil
    @bits.each_char.with_index do |b0,i|
        b1 = implicant.bits[i]
        # Bits are different
        if (b0 != b1) then
            # Stop if there where already a difference
            if (found)
                found = nil
                break
            end
            # A 0-1 or a 1-0 difference is found
            found = i
        end
    end
    # Can merge at bit found
    if found then
        # print "merge!\n"
        # Duplicate current implicant
        merged = self.dup
        # And update its x
        merged[found] = "x"
        # Finally update its covers
        merged.covers = @covers | implicant.covers
        return merged
    end
    # No merge
    return nil
end

#to_sObject

Converts to a string.



75
76
77
# File 'lib/logic_tools/logicsimplify_qm.rb', line 75

def to_s # :nodoc:
    @bits
end