class LogicTools::Implicant

Represents a logic implicant.

Attributes

bits[R]

The bit vector of the implicant.

count[R]

The number of 1 of the implicant.

covers[RW]

The bit values covered by the implicant.

mask[R]

The positions of the X in the implicant.

prime[R]

Tell if the implicant is prime or not.

var[RW]

The variable associated with the implicant Do not interfer at all with the class, so public and fully accessible

Public Class Methods

new(base) click to toggle source

Creates a new implicant from base.

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

# 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

Public Instance Methods

[](i) click to toggle source

Gets the value of bit i.

# File lib/logic_tools/logicsimplify_qm.rb, line 113
def [](i)
    @bits[i]
end
[]=(i,b) click to toggle source

Sets the value of bit i to b.

# 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
each(&blk) click to toggle source

Iterates over the bits of the implicant.

Returns an enumerator if no block given.

# 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
merge(implicant) click to toggle source

Creates a new implicant merging current implicant with imp.

# 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
prime=(st) click to toggle source

Sets the prime status to st (true or false).

# File lib/logic_tools/logicsimplify_qm.rb, line 84
def prime=(st)
    @prime = st ? true : false
end