bustikiller 25bd53600e
Some checks failed
Run unit tests / rubocop (pull_request) Failing after 34s
Run unit tests / copyright_notice (pull_request) Successful in 56s
Run unit tests / check-licenses (pull_request) Successful in 1m12s
Run unit tests / unit_tests (pull_request) Failing after 1m27s
Run unit tests / build-static-assets (pull_request) Has been skipped
Use frozen versions of tables
2025-07-18 18:12:53 +02:00

59 lines
1.6 KiB
Ruby

# Copyright (C) 2024-2025 LibreWeddingPlanner contributors
# frozen_string_literal: true
module Tables
class Swap
private attr_reader :initial_solution
def initialize(initial_solution)
@initial_solution = initial_solution
end
def each
@initial_solution.tables.combination(2) do |table_a, table_b|
table_a.to_a.product(table_b.to_a).each do |(person_a, person_b)|
new_solution = Distribution.new(
min_per_table: @initial_solution.min_per_table,
max_per_table: @initial_solution.max_per_table,
hierarchy: @initial_solution.hierarchy
)
new_solution.tables = @initial_solution.tables.dup
new_solution.tables.delete(table_a)
new_solution.tables.delete(table_b)
modified_table_a = table_a.dup.tap(&:reset)
modified_table_b = table_b.dup.tap(&:reset)
new_solution.tables << modified_table_a
new_solution.tables << modified_table_b
# original_discomfort_a = table_a.reset
# original_discomfort_b = table_b.reset
modified_table_a.delete(person_a)
modified_table_b.delete(person_b)
modified_table_a << person_b
modified_table_b << person_a
yield(new_solution)
# ensure
# table_a.delete(person_b)
# table_b.delete(person_a)
# table_a << person_a
# table_b << person_b
# table_a.discomfort = original_discomfort_a
# table_b.discomfort = original_discomfort_b
end
end
end
end
end