2024-10-27 21:42:45 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def find(name)
|
|
|
|
@references[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def <<(name)
|
|
|
|
new_node = Tree::TreeNode.new(name)
|
|
|
|
super(new_node).tap { @references[name] = new_node }
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_child(parent_name, child_name)
|
|
|
|
@references[parent_name] << Tree::TreeNode.new(child_name).tap { |child_node| @references[child_name] = child_node }
|
|
|
|
end
|
|
|
|
|
|
|
|
def distance(name_a, name_b)
|
|
|
|
return nil if @references[name_a].nil? || @references[name_b].nil?
|
|
|
|
|
|
|
|
@references[name_a].distance_to_common_ancestor(@references[name_b])
|
|
|
|
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
|