Class: LogicTools::Implicant
- Inherits:
-
Object
- Object
- LogicTools::Implicant
- Includes:
- Enumerable
- Defined in:
- lib/logic_tools/logicsimplify_qm.rb
Overview
Represents a logic implicant
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
The bit vector of the implicant.
-
#count ⇒ Object
readonly
The number of 1 of the implicant.
-
#covers ⇒ Object
readonly
The bit values covered by the implicant.
-
#mask ⇒ Object
readonly
The positions of the X in the implicant.
-
#prime ⇒ Object
Tell if the implicant is prime or not.
-
#var ⇒ Object
The variable associated with the implicant Do not interfer at all with the class, so public and fully accessible.
Instance Method Summary collapse
-
#<=>(implicant) ⇒ Object
:nodoc:.
-
#==(implicant) ⇒ Object
Compares with
implicant. -
#[](i) ⇒ Object
Gets the value of bit
i. -
#[]=(i, b) ⇒ Object
Sets the value of bit
itob. -
#dup ⇒ Object
duplicates the implicant.
-
#each(&blk) ⇒ Object
Iterates over the bits of the implicant.
-
#initialize(base) ⇒ Implicant
constructor
Creates a new implicant from
base. -
#inspect ⇒ Object
:nodoc:.
-
#merge(implicant) ⇒ Object
Creates a new implicant merging current implicant with
imp. -
#to_s ⇒ Object
Converts to a string.
Constructor Details
#initialize(base) ⇒ Implicant
Creates a new implicant from base.
Argument +base+ can be either another implicant or a bit string.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 55 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
#bits ⇒ Object (readonly)
The bit vector of the implicant.
36 37 38 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 36 def bits @bits end |
#count ⇒ Object (readonly)
The number of 1 of the implicant.
38 39 40 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 38 def count @count end |
#covers ⇒ Object
The bit values covered by the implicant.
40 41 42 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 40 def covers @covers end |
#mask ⇒ Object (readonly)
The positions of the X in the implicant.
34 35 36 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 34 def mask @mask end |
#prime ⇒ Object
Tell if the implicant is prime or not.
42 43 44 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 42 def prime @prime end |
#var ⇒ Object
The variable associated with the implicant
Do not interfer at all with the class, so
public and fully accessible
46 47 48 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 46 def var @var end |
Instance Method Details
#<=>(implicant) ⇒ Object
:nodoc:
102 103 104 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 102 def <=>(implicant) #:nodoc: @bits <=> implicant.to_s end |
#==(implicant) ⇒ Object
Compares with implicant
99 100 101 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 99 def ==(implicant) # :nodoc: @bits == implicant.to_s end |
#[](i) ⇒ Object
Gets the value of bit i.
112 113 114 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 112 def [](i) @bits[i] end |
#[]=(i, b) ⇒ Object
Sets the value of bit i to b.
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 117 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 |
#dup ⇒ Object
duplicates the implicant.
107 108 109 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 107 def dup # :nodoc: Implicant.new(self) end |
#each(&blk) ⇒ Object
Iterates over the bits of the implicant.
Returns an enumerator if no block given.
90 91 92 93 94 95 96 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 90 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 |
#inspect ⇒ Object
:nodoc:
78 79 80 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 78 def inspect #:nodoc: @bits.dup end |
#merge(implicant) ⇒ Object
Creates a new implicant merging current implicant with imp.
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 156 157 158 159 160 161 162 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 131 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_s ⇒ Object
Converts to a string.
74 75 76 |
# File 'lib/logic_tools/logicsimplify_qm.rb', line 74 def to_s # :nodoc: @bits end |