gen/fhdl/namer: Add update_hierarchy_node function to reduce build_hierarchy_tree complexity.
This commit is contained in:
parent
19a3ab2614
commit
16804acaa8
|
@ -27,6 +27,34 @@ class HierarchyNode:
|
|||
self.use_number = False
|
||||
self.children = {}
|
||||
|
||||
def update_hierarchy_node(current, name, number, use_number, current_base):
|
||||
"""
|
||||
Updates or creates a hierarchy node based on the current position, name, and number.
|
||||
If numbering is used, sorts and stores all numbers associated with the base node.
|
||||
|
||||
Parameters:
|
||||
curren t (HierarchyNode): The current node in the hierarchy.
|
||||
name (str): The name of the current hierarchy level.
|
||||
number (int): The number associated with the current hierarchy level.
|
||||
use_number (bool): Flag indicating whether to use the number in the hierarchy.
|
||||
current_base (HierarchyNode): The base node for number usage information.
|
||||
|
||||
Returns:
|
||||
HierarchyNode: The updated or created node.
|
||||
"""
|
||||
# Create the appropriate key for the node.
|
||||
key = (name, number) if use_number else name
|
||||
# Use setdefault to either get the existing child node or create a new one.
|
||||
current = current.children.setdefault(key, HierarchyNode())
|
||||
# Add the number to the set of numbers associated with this node.
|
||||
current.numbers.add(number)
|
||||
# Increment the count of signals that have traversed this node.
|
||||
current.signal_count += 1
|
||||
# If numbering is used, sort and store all numbers associated with the base node.
|
||||
if use_number and current_base:
|
||||
current.all_numbers = sorted(current_base.numbers)
|
||||
return current
|
||||
|
||||
def build_hierarchy_tree(signals, base_tree=None):
|
||||
"""
|
||||
Constructs a hierarchical tree from signals, where each signal's backtrace contributes to the tree structure.
|
||||
|
@ -54,17 +82,8 @@ def build_hierarchy_tree(signals, base_tree=None):
|
|||
current_base = current_base.children.get(name)
|
||||
use_number = current_base.use_number if current_base else False
|
||||
|
||||
# Create the appropriate key for the node.
|
||||
key = (name, number) if use_number else name
|
||||
# Use setdefault to either get the existing child node or create a new one.
|
||||
current = current.children.setdefault(key, HierarchyNode())
|
||||
# Add the number to the set of numbers associated with this node.
|
||||
current.numbers.add(number)
|
||||
# Increment the count of signals that have traversed this node.
|
||||
current.signal_count += 1
|
||||
# If numbering is used, sort and store all numbers associated with the base node.
|
||||
if use_number:
|
||||
current.all_numbers = sorted(current_base.numbers)
|
||||
# Update the current node in the hierarchy.
|
||||
current = update_hierarchy_node(current, name, number, use_number, current_base)
|
||||
|
||||
return root
|
||||
|
||||
|
|
Loading…
Reference in New Issue