Manuel Bustillo
91bbae1c63
All checks were successful
Check usage of free licenses / check-licenses (pull_request) Successful in 59s
Add copyright notice / copyright_notice (pull_request) Successful in 2m21s
Run unit tests / unit_tests (pull_request) Successful in 3m2s
Build Nginx-based docker image / build-static-assets (pull_request) Successful in 25m17s
41 lines
999 B
Ruby
41 lines
999 B
Ruby
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
# 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)|
|
|
original_discomfort_a = table_a.reset
|
|
original_discomfort_b = table_b.reset
|
|
|
|
table_a.delete(person_a)
|
|
table_b.delete(person_b)
|
|
|
|
table_a << person_b
|
|
table_b << person_a
|
|
|
|
yield(@initial_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
|