Sha256: 7dd60ade5e6b8a72cf6ac47a2fb3443da04a9ef28e06fed2a2ac5c0010cc60ea

Contents?: true

Size: 565 Bytes

Versions: 7

Compression:

Stored size: 565 Bytes

Contents

module Performant
  # This class will be enriched with c-methods
  #
  class Array

    # Chooses a good algorithm for intersecting arrays.
    #
    # Note: The sort order will be changed.
    #
    def self.intersect array_of_arrays
      array_of_arrays.sort! { |a, b| a.size <=> b.size }

      if (array_of_arrays.sum(&:size) < 20_000)
        Performant::Array.brute_force_intersect array_of_arrays
      else
        array_of_arrays.inject([]) do |total, elements|
          total.empty? ? elements : elements & total
        end
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
picky-0.0.8 lib/picky/performant/array.rb
picky-0.0.7 lib/picky/performant/array.rb
picky-0.0.6 lib/picky/performant/array.rb
picky-0.0.5 lib/picky/performant/array.rb
picky-0.0.4 lib/picky/performant/array.rb
picky-0.0.3 lib/picky/performant/array.rb
picky-0.0.2 lib/picky/performant/array.rb