Class: TypeBalancer::Strategies::SlidingWindowStrategy

Inherits:
BaseStrategy
  • Object
show all
Defined in:
lib/type_balancer/strategies/sliding_window_strategy.rb

Overview

Implements an efficient sliding window approach for balancing items This strategy uses array-based indexing and pre-calculated ratios for optimal performance

Defined Under Namespace

Classes: WindowSlotCalculator

Constant Summary collapse

DEFAULT_BATCH_SIZE =
1000

Instance Method Summary collapse

Constructor Details

#initialize(items:, type_field:, types: nil, type_order: nil, window_size: 10, batch_size: DEFAULT_BATCH_SIZE) ⇒ SlidingWindowStrategy

rubocop:disable Metrics/ParameterLists



13
14
15
16
17
18
# File 'lib/type_balancer/strategies/sliding_window_strategy.rb', line 13

def initialize(items:, type_field:, types: nil, type_order: nil, window_size: 10, batch_size: DEFAULT_BATCH_SIZE)
  super(items: items, type_field: type_field, types: types, type_order: type_order)
  @window_size = window_size
  @batch_size  = batch_size
  @types       = types || extract_types
end

Instance Method Details

#balanceObject

rubocop:enable Metrics/ParameterLists



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/type_balancer/strategies/sliding_window_strategy.rb', line 21

def balance
  return [] if @items.empty?

  validate_items!
  return @items.dup if single_type?

  @type_queues = build_type_queues
  @type_ratios = calculate_type_ratios

  if @items.size > @batch_size
    process_large_collection
  else
    process_single_batch
  end
end