Class: TypeBalancer::OrderedCollectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/type_balancer/ordered_collection_manager.rb

Overview

Manages an ordered collection of items, providing methods to place items at specific positions and fill gaps using different strategies. This class acts as a facade for the gap filling functionality.

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ OrderedCollectionManager

Returns a new instance of OrderedCollectionManager.



8
9
10
11
12
# File 'lib/type_balancer/ordered_collection_manager.rb', line 8

def initialize(size)
  @collection = Array.new(size)
  @item_order = [] # Track items in their original order
  @size = size
end

Instance Method Details

#fill_gaps_alternating(primary_items, secondary_items) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/type_balancer/ordered_collection_manager.rb', line 28

def fill_gaps_alternating(primary_items, secondary_items)
  # Add new items to the order list before filling gaps
  @item_order.concat(primary_items)
  @item_order.concat(secondary_items)

  # Find empty positions
  empty_positions = find_empty_positions
  return if empty_positions.empty?

  # Create a copy of the collection for filling
  collection_copy = @collection.dup

  # Use C extension for alternating filling
  result = TypeBalancer::AlternatingFiller.fill(
    collection_copy,
    empty_positions,
    primary_items,
    secondary_items
  )

  # Update collection if we got a valid result
  @collection = result if result.is_a?(Array)
end

#fill_remaining_gaps(items_arrays) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/type_balancer/ordered_collection_manager.rb', line 52

def fill_remaining_gaps(items_arrays)
  # Add all new items to the order list before filling gaps
  items_arrays.each { |items| @item_order.concat(items) }

  # Find empty positions
  empty_positions = find_empty_positions
  return if empty_positions.empty?

  # Create a copy of the collection for filling
  collection_copy = @collection.dup

  # Use C extension for sequential filling
  result = TypeBalancer::SequentialFiller.fill(
    collection_copy,
    empty_positions,
    items_arrays
  )

  # Update collection if we got a valid result
  @collection = result if result.is_a?(Array)
end

#place_at_positions(items, positions) ⇒ Object Also known as: place_items_at_positions



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/type_balancer/ordered_collection_manager.rb', line 14

def place_at_positions(items, positions)
  # Store items in their original order
  @item_order.concat(items)

  # Place items at their positions
  positions.each_with_index do |pos, i|
    break if i >= items.size

    @collection[pos] = items[i] if pos >= 0 && pos < @size
  end
end

#resultObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/type_balancer/ordered_collection_manager.rb', line 74

def result
  # If no items have been placed, return an empty array
  return [] if @item_order.empty?

  # Get all non-nil items from the collection
  non_nil_items = @collection.compact

  # Return empty array if no items were successfully placed
  return [] if non_nil_items.empty?

  # Return all items that were successfully placed, in their original order
  @item_order.select { |item| non_nil_items.include?(item) }
end