Class: KCommonVisitor

Inherits:
BaseVisitor show all
Defined in:
lib/visitor/k_common_visitor.rb

Instance Attribute Summary

Attributes inherited from BaseVisitor

#postCounter, #preCounter

Instance Method Summary collapse

Methods inherited from BaseVisitor

#preVisit

Constructor Details

#initialize(dataSource) ⇒ KCommonVisitor

Returns a new instance of KCommonVisitor.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/visitor/k_common_visitor.rb', line 18

def initialize(dataSource)
  @dataSource = dataSource

  #
  # key = common to at least this many (2, 3, ...)
  # value = [ startOffset, endOffset ] of value sequence
  #
  @commonTo = {}

  #
  # set up initial values
  #
  (0..64).each do |value|
    @commonTo[value] = ValueRange.new(0,-1)
  end
  super()
end

Instance Method Details

#countCommon(bits) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/visitor/k_common_visitor.rb', line 59

def countCommon(bits)
  result = 0
  scanner = 1
  bits = bits.to_i
  (1..32).each do
    if ((scanner & bits) != 0) then
      result += 1
    end
    scanner = scanner << 1
  end
  result
end

#longestStringCommonTo(numberInCommon) ⇒ Object



55
56
57
# File 'lib/visitor/k_common_visitor.rb', line 55

def longestStringCommonTo(numberInCommon)
  return @commonTo[numberInCommon].length, @dataSource.valueSequence(@commonTo[numberInCommon].startOffset, @commonTo[numberInCommon].endOffset)
end

#postVisit(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/visitor/k_common_visitor.rb', line 36

def postVisit(node)
  nCommon = self.countCommon(node.dataSourceBit)
  currentCommonLength = @commonTo[nCommon].endOffset - @commonTo[nCommon].startOffset + 1
  if (node.valueDepth > currentCommonLength) then
    @commonTo[nCommon].startOffset = node.incomingEdgeEndOffset - node.valueDepth + 1
    @commonTo[nCommon].endOffset = node.incomingEdgeEndOffset
    if (nCommon > 2) then
      longestLength = node.valueDepth
      (1..(nCommon-1)).each do |offset|
        testLength = @commonTo[offset].endOffset - @commonTo[offset].startOffset + 1
        if (testLength < longestLength) then
          @commonTo[offset].startOffset = @commonTo[nCommon].startOffset
          @commonTo[offset].endOffset = @commonTo[nCommon].endOffset
        end
      end
    end
  end
end