2024-10-27 21:42:45 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
2024-12-28 18:37:47 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-07-31 20:58:47 +02:00
|
|
|
class AffinityGroupsHierarchy < Array
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
@references = {}
|
2024-11-01 11:55:32 +01:00
|
|
|
|
|
|
|
Group.roots.each do |group|
|
|
|
|
self << group.id
|
|
|
|
|
|
|
|
hydrate(group)
|
|
|
|
end
|
2024-07-31 20:58:47 +02:00
|
|
|
end
|
|
|
|
|
2024-11-01 12:04:15 +01:00
|
|
|
def find(id)
|
|
|
|
@references[id]
|
2024-07-31 20:58:47 +02:00
|
|
|
end
|
|
|
|
|
2024-11-01 12:04:15 +01:00
|
|
|
def <<(id)
|
|
|
|
new_node = Tree::TreeNode.new(id)
|
|
|
|
super(new_node).tap { @references[id] = new_node }
|
2024-07-31 20:58:47 +02:00
|
|
|
end
|
|
|
|
|
2024-11-01 12:04:15 +01:00
|
|
|
def register_child(parent_id, child_id)
|
|
|
|
@references[parent_id] << Tree::TreeNode.new(child_id).tap { |child_node| @references[child_id] = child_node }
|
2024-07-31 20:58:47 +02:00
|
|
|
end
|
|
|
|
|
2024-11-01 12:04:15 +01:00
|
|
|
def distance(id_a, id_b)
|
|
|
|
return nil if @references[id_a].nil? || @references[id_b].nil?
|
2024-07-31 20:58:47 +02:00
|
|
|
|
2024-11-01 12:04:15 +01:00
|
|
|
@references[id_a].distance_to_common_ancestor(@references[id_b])
|
2024-07-31 20:58:47 +02:00
|
|
|
end
|
2024-11-01 11:55:32 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def hydrate(group)
|
|
|
|
group.children.each do |child|
|
|
|
|
register_child(group.id, child.id)
|
|
|
|
|
|
|
|
hydrate(child)
|
|
|
|
end
|
|
|
|
end
|
2024-07-31 20:58:47 +02:00
|
|
|
end
|