Class: LogicTools::Implicant

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/logic_tools/logicsimplify.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.


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

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.



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

def bits
  @bits
end

#countObject (readonly)

The number of 1 of the implicant.



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

def count
  @count
end

#coversObject

The bit values covered by the implicant.



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

def covers
  @covers
end

#maskObject (readonly)

The positions of the X in the implicant.



33
34
35
# File 'lib/logic_tools/logicsimplify.rb', line 33

def mask
  @mask
end

#primeObject

Tell if the implicant is prime or not.



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

def prime
  @prime
end

#varObject

The variable associated with the implicant

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


45
46
47
# File 'lib/logic_tools/logicsimplify.rb', line 45

def var
  @var
end

Instance Method Details

#<=>(implicant) ⇒ Object

:nodoc:



95
96
97
# File 'lib/logic_tools/logicsimplify.rb', line 95

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

#==(implicant) ⇒ Object

Compares with implicant



92
93
94
# File 'lib/logic_tools/logicsimplify.rb', line 92

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

#[](i) ⇒ Object

Gets the value of bit i.



105
106
107
# File 'lib/logic_tools/logicsimplify.rb', line 105

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

#[]=(i, b) ⇒ Object

Sets the value of bit i to b.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/logic_tools/logicsimplify.rb', line 110

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.



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

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

#each(&blk) ⇒ Object

Iterates over the bits of the implicant.



87
88
89
# File 'lib/logic_tools/logicsimplify.rb', line 87

def each(&blk)
    @bits.each_char(&blk)
end

#inspectObject

:nodoc:



77
78
79
# File 'lib/logic_tools/logicsimplify.rb', line 77

def inspect #:nodoc:
    @bits.dup
end

#merge(implicant) ⇒ Object

Creates a new implicant merging current implicant with imp.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/logic_tools/logicsimplify.rb', line 124

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



73
74
75
# File 'lib/logic_tools/logicsimplify.rb', line 73

def to_s # :nodoc:
    @bits
end