Sha256: 636ef673b2d30998108826e7e8936d6f87800ca0ce521f0fdb64867193baacfb

Contents?: true

Size: 1.25 KB

Versions: 29

Compression:

Stored size: 1.25 KB

Contents

module ProconBypassMan
  class CompressArray
    class CompressibleValue
      # @params [String] prev
      # @params [String] current
      def initialize(prev, current)
        @prev = prev
        @current = current
      end

      # @return [Boolean]
      def compress?
        @prev.include?(@current)
      end

      # @return [String]
      def to_s_with_mark
        if /^(.+) \* (\d+)/ =~ @prev
          value = $1
          count = $2
          return "#{value} * #{count.to_i + 1}"
        end
        if /^(.+)/ =~ @prev
          value = $1
          return "#{value} * 1"
        end
      end
    end

    def initialize(array)
      @array = array
    end

    # @return [Array<String>]
    def compress
      previous_value = nil
      @array.reduce([]) do |acc, item|
        if previous_value.nil?
          acc << item
          previous_value = item
          next acc
        end

        if CompressibleValue.new(previous_value, item).compress?
          registered_value = acc.pop
          acc << CompressibleValue.new(registered_value, item).to_s_with_mark
        else
          acc << item
        end

        previous_value = item
        next acc
      end
    end
  end
end


if $0 == __FILE__
  ProconBypassMan::CompressArray.new([''])
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
procon_bypass_man-0.3.12 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.11 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.10 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.9 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.8.1 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.8 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.7 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.6 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.5 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.4 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.3.1 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.3 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.2 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.1 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.3.0 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.2.3 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.2.2 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.2.1 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.2.0 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.1.23 lib/procon_bypass_man/support/compress_array.rb