Sha256: b57357ca09ba6ee272662465864aad64c48a0d3db81775578707ec626c050a4a

Contents?: true

Size: 1.46 KB

Versions: 25

Compression:

Stored size: 1.46 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief
# @author ongaeshi
# @date   2013/03/15

module Milkode
  MatchLineResult = Struct.new(:index, :match_datas)
  
  class WideMatcher
    attr_reader :num_max

    def self.create(num_max)
      if num_max == 0
        WideMatcherZero.new
      else
        WideMatcher.new(num_max)
      end
    end
    
    def initialize(num_max)
      @num_max   = num_max
      @container = []
    end

    def linenum
      @container.size
    end

    def add_line_matchs(index, matches)
      @last_index = index
      @container.shift if linenum >= @num_max
      @container << matches
      # p @container
    end

    def match?
      @container.reduce(Array.new(@container.first.size)) {|result, matches|
        matches.each_with_index do |m, i|
          result[i] |= m
        end
        result
      }.all?
    end

    def match_lines
      index = @last_index - @container.size + 1
      @container.reduce([]) do |result, matches|
        m = matches.compact
        result << MatchLineResult.new(index, m) unless m.empty?
        index += 1
        result
      end
    end
  end

  class WideMatcherZero
    attr_reader :num_max

    def initialize
      @num_max   = 0
    end

    def linenum
      1
    end

    def add_line_matchs(index, matches)
      @index   = index
      @matches = matches
    end

    def match?
      @matches.any?
    end

    def match_lines
      [MatchLineResult.new(@index, @matches.compact)]
    end
  end
end


Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
milkode-1.8.9.2 lib/milkode/common/wide_matcher.rb
milkode-1.8.9.1 lib/milkode/common/wide_matcher.rb
milkode-1.8.9 lib/milkode/common/wide_matcher.rb
milkode-1.8.8 lib/milkode/common/wide_matcher.rb
milkode-1.8.7 lib/milkode/common/wide_matcher.rb
milkode-1.8.6 lib/milkode/common/wide_matcher.rb
milkode-1.8.5 lib/milkode/common/wide_matcher.rb
milkode-1.8.4 lib/milkode/common/wide_matcher.rb
milkode-1.8.3 lib/milkode/common/wide_matcher.rb
milkode-1.8.2 lib/milkode/common/wide_matcher.rb
milkode-1.8.1 lib/milkode/common/wide_matcher.rb
milkode-1.8.0 lib/milkode/common/wide_matcher.rb
milkode-1.7.1 lib/milkode/common/wide_matcher.rb
milkode-1.7.0 lib/milkode/common/wide_matcher.rb
milkode-1.6.1 lib/milkode/common/wide_matcher.rb
milkode-1.6.0 lib/milkode/common/wide_matcher.rb
milkode-1.5.0 lib/milkode/common/wide_matcher.rb
milkode-1.4.0 lib/milkode/common/wide_matcher.rb
milkode-1.3.0 lib/milkode/common/wide_matcher.rb
milkode-1.2.0 lib/milkode/common/wide_matcher.rb