Sha256: 770ce6ca1a91174e984e4043b973f9a0613f6330da3ffe1231a59832543352cd

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
procon_bypass_man-0.1.16.1 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.1.16 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.1.15 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.1.14 lib/procon_bypass_man/support/compress_array.rb
procon_bypass_man-0.1.13 lib/procon_bypass_man/support/compress_array.rb