Sha256: 00ab0089c49540a6b42e7114712d5b1e1e5baa9da4d0fb4ed6dc959e415fb6cf

Contents?: true

Size: 1.44 KB

Versions: 19

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'ehbrs_ruby_utils/circular_list_spreader/base_level'
require 'ehbrs_ruby_utils/circular_list_spreader/item_level'

module EhbrsRubyUtils
  class CircularListSpreader
    class GroupLevel
      include ::EhbrsRubyUtils::CircularListSpreader::BaseLevel
      enable_simple_cache

      common_constructor :label

      def push(path, item)
        child_path = path.dup
        current = child_path.shift
        if child_path.any?
          push_group_level(current, child_path, item)
        else
          push_item_level(current, item)
        end
      end

      def pop
        children.values.max.pop
      end

      def pop_all
        r = []
        while remaining?; r << pop; end
        r
      end

      def remaining_i
        children.values.inject(0) { |a, e| a + e.remaining_i }
      end

      def total_i
        children.values.inject(0) { |a, e| a + e.total_i }
      end

      private

      attr_accessor :item

      def push_group_level(current, child_path, item)
        children[current] ||= self.class.new(current)
        children[current].push(child_path, item)
      end

      def children
        @children ||= {}
      end

      def push_item_level(current, item)
        raise "Key \"#{current}\" already used" if children[current].present?

        children[current] = ::EhbrsRubyUtils::CircularListSpreader::ItemLevel.new(item)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ehbrs_ruby_utils-0.33.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.32.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.31.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.30.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.29.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.28.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.27.1 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.27.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.26.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.25.1 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.25.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.24.1 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.24.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.23.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.22.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.21.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.20.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.19.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb
ehbrs_ruby_utils-0.18.0 lib/ehbrs_ruby_utils/circular_list_spreader/group_level.rb