Sha256: 2c48503804adcfbcc735ea71b0505513f8e58c289a81b7551d96abc61a655558

Contents?: true

Size: 1.36 KB

Versions: 10

Compression:

Stored size: 1.36 KB

Contents

# = FILE
#
#   combine.rb
#
# = DESCRIPTION
#
#   Range extensions for combination.
#
# = AUTHORS
#
#   CREDIT Daniel Schierbeck
#   CREDIT Brandon Keepers
#
# = NOTES
#
#   TODO Incorporate end sentinal inclusion vs. exclusion in #combine.

#
class Range

  # Combine intervals.

  def self.combine(*intervals)
    intype = intervals.first.class
    result = []

    intervals = intervals.collect do |i|
      [i.first, i.last]
    end

    intervals.sort.inject([]) do |result, (from, to)|
      if result.empty? or from > result.last[1]
        result << [from, to]
      elsif to > result.last[1]
        result.last[1] = to
      end
      result
    end

    if intype <= Range
      result.collect{ |i| ((i.first)..(i.last)) }
    else
      result
    end
  end

  # Combine intervals.

  def combine(*intervals)
    Range.combine(self, *intervals)
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TestRangeCombine < Test::Unit::TestCase

    def test_combine_ranges
      r = Range.combine(0..4, 2..6, 6..10, 13..17, 12..19)
      x = [0..10, 12..19]
      assert_equal(x, r)
    end

    def test_combine_arrays_as_intervals
      r = Range.combine([0, 4], [2, 6], [6, 10], [13, 17], [12, 19])
      x = [[0, 10], [12, 19]]
      assert_equal(x, r)
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.1 lib/core/facets/range/combine.rb
facets-2.0.0 lib/core/facets/range/combine.rb
facets-2.0.2 lib/core/facets/range/combine.rb
facets-2.0.5 lib/core/facets/range/combine.rb
facets-2.1.1 lib/core/facets/range/combine.rb
facets-2.1.2 lib/core/facets/range/combine.rb
facets-2.0.4 lib/core/facets/range/combine.rb
facets-2.1.0 lib/core/facets/range/combine.rb
facets-2.0.3 lib/core/facets/range/combine.rb
facets-2.1.3 lib/core/facets/range/combine.rb