Sha256: ba0ade87b1402ebe085176c5297bad312723586a257c580c95a3ae0ad783df95

Contents?: true

Size: 1.74 KB

Versions: 174

Compression:

Stored size: 1.74 KB

Contents

module Tins
  # This module can be mixed into all classes, whose instances respond to the
  # [] and size-methods, like for example Array. The returned elements from []
  # should respond to the succ method.
  module Minimize
    # Returns a minimized version of this object, that is successive elements
    # are substituted with ranges a..b. In the situation ..., x, y,... and y !=
    # x.succ a range x..x is created, to make it easier to iterate over all the
    # ranges in one run. A small example:
    #  [ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ].minimize # => [ 'A'..'C', 'G'..'G', 'K'..'M' ]
    #
    # If the order of the original elements doesn't matter, it's a good idea to
    # first sort them and then minimize:
    #  [ 5, 1, 4, 2 ].sort.minimize # => [ 1..2, 4..5 ]
    def minimize
      result = []
      last_index = size - 1
      size.times do |i|
        result << [ self[0] ] if i == 0
        if self[i].succ != self[i + 1] or i == last_index
          result[-1] << self[i]
          result << [ self[i + 1] ] unless i == last_index
        end
      end
      result.map! { |a, b| a..b }
    end

    # First minimizes this object, then calls the replace method with the
    # result.
    def minimize!
      replace minimize
    end

    # Invert a minimized version of an object. Some small examples:
    #  [ 'A'..'C', 'G'..'G', 'K'..'M' ].unminimize # => [ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ]
    # and
    #  [ 1..2, 4..5 ].unminimize # => [ 1, 2, 4, 5 ]
    def unminimize
      result = []
      for range in self
        for e in range
          result << e
        end
      end
      result
    end

    # Invert a minimized version of this object in place.
    def unminimize!
      replace unminimize
    end
  end
end

require 'tins/alias'

Version data entries

174 entries across 156 versions & 9 rubygems

Version Path
tins-1.37.0 lib/tins/minimize.rb
tins-1.36.1 lib/tins/minimize.rb
tins-1.36.0 lib/tins/minimize.rb
tins-1.35.0 lib/tins/minimize.rb
tins-1.34.0 lib/tins/minimize.rb
tins-1.33.0 lib/tins/minimize.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/minimize.rb
tins-1.32.1 lib/tins/minimize.rb
tins-1.32.0 lib/tins/minimize.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/minimize.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/minimize.rb
tins-1.31.1 lib/tins/minimize.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/tins-1.31.0/lib/tins/minimize.rb
tins-1.31.0 lib/tins/minimize.rb
tins-1.30.0 lib/tins/minimize.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/minimize.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/minimize.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/minimize.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/minimize.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tins-1.26.0/lib/tins/minimize.rb