Sha256: 26c951f97c8c66bd51a46145a8ddd46e3ba8584de27b933b84c2f7a6fb149659

Contents?: true

Size: 1.06 KB

Versions: 31

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EhbrsRubyUtils
  class CircularListSpreader
    class List
      require_sub __FILE__, require_dependency: true

      class << self
        def empty
          new([], 0)
        end
      end

      enable_simple_cache
      common_constructor :items, :inserted_at
      compare_by :spreadness, :inserted_at
      delegate :count, to: :items

      def insert(position, item_level)
        dup_items = items.dup
        dup_items.insert(position, item_level)
        self.class.new(dup_items, position)
      end

      def items_pair_enumerator
        ::Enumerator.new do |enum|
          (count - 1).times do |li|
            ((li + 1)..(count - 1)).each do |ri|
              enum.yield(li, ri)
            end
          end
        end
      end

      def to_s
        items.map { |i| i.item.to_s }.join(', ')
      end

      private

      # @return [Integer]
      def spreadness_uncached
        items_pair_enumerator.inject(0) { |a, e| a + item_pair_spreadness(*e) }
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ehbrs_ruby_utils-0.44.2 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.44.1 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.44.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.41.1 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.41.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.40.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.39.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.38.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.37.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.36.1 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.36.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.35.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.33.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.32.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.31.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.30.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.29.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.28.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.27.1 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb
ehbrs_ruby_utils-0.27.0 lib/ehbrs_ruby_utils/circular_list_spreader/list.rb