Sha256: ba0fa9b9f96c1bdc9ae60f3321143ffc582aa51a361b19366460cce17cf89f44

Contents?: true

Size: 1.4 KB

Versions: 7

Compression:

Stored size: 1.4 KB

Contents

module Picky

  module Query

    # Combinations are a number of Combination-s.
    #
    # They are the core of an allocation.
    # An allocation consists of a number of combinations.
    #
    module Combinations # :nodoc:all

      # Memory Combinations contain specific methods for
      # calculating score and ids in memory.
      #
      class Memory < Base

        # Returns the result ids for the allocation.
        #
        # Sorts the ids by size and & through them in the following order (sizes):
        # 0. [100_000, 400, 30, 2]
        # 1. [2, 30, 400, 100_000]
        # 2. (100_000 & (400 & (30 & 2))) # => result
        #
        # Note: Uses a C-optimized intersection routine (in performant.c)
        #       for speed and memory efficiency.
        #
        # Note: In the memory based version we ignore the (amount) needed hint.
        #       We cannot use the information to speed up the algorithm, unfortunately.
        #
        def ids _, _
          return [] if @combinations.empty?

          # Get the ids for each combination.
          #
          id_arrays = @combinations.inject([]) do |total, combination|
            total << combination.ids
          end

          # Call the optimized C algorithm.
          #
          # Note: It orders the passed arrays by size.
          #
          Performant::Array.memory_efficient_intersect id_arrays
        end

      end

    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
picky-3.0.1 lib/picky/query/combinations/memory.rb
picky-3.0.0 lib/picky/query/combinations/memory.rb
picky-3.0.0.pre5 lib/picky/query/combinations/memory.rb
picky-3.0.0.pre4 lib/picky/query/combinations/memory.rb
picky-3.0.0.pre3 lib/picky/query/combinations/memory.rb
picky-3.0.0.pre2 lib/picky/query/combinations/memory.rb
picky-3.0.0.pre1 lib/picky/query/combinations/memory.rb