@@ -18,10 +18,10 @@ def _compare_ahb_rows(previous_ahb_row: AhbRow, subsequent_ahb_row: AhbRow) -> A
1818
1919 # consider all AHB properties except `section_name` (Segmentname) and `formatversion`
2020 for entry in AHB_PROPERTIES :
21- previous_ahb_entry = getattr (previous_ahb_row , entry , "" ) or ""
22- subsequent_ahb_entry = getattr (subsequent_ahb_row , entry , "" ) or ""
21+ previous_ahb_entry = normalize_entries ( getattr (previous_ahb_row , entry , "" ) or "" )
22+ subsequent_ahb_entry = normalize_entries ( getattr (subsequent_ahb_row , entry , "" ) or "" )
2323
24- if (previous_ahb_entry . strip () or subsequent_ahb_entry . strip () ) and previous_ahb_entry != subsequent_ahb_entry :
24+ if (previous_ahb_entry or subsequent_ahb_entry ) and previous_ahb_entry != subsequent_ahb_entry :
2525 changed_entries .extend (
2626 [f"{ entry } _{ previous_ahb_row .formatversion } " , f"{ entry } _{ subsequent_ahb_row .formatversion } " ]
2727 )
@@ -50,16 +50,36 @@ def _add_empty_row(formatversion: str) -> AhbRow:
5050
5151
5252def _find_matching_subsequent_row (
53- current_ahb_row : AhbRow , subsequent_ahb_rows : List [AhbRow ], start_idx : int
53+ current_ahb_row : AhbRow , subsequent_ahb_rows : List [AhbRow ], start_idx : int , duplicate_indices : set [ int ]
5454) -> Tuple [int , AhbRow | None ]:
5555 """
56- Find matching row in subsequent version starting from given index.
56+ Find matching row in subsequent version starting from given index by consider all AHB properties
57+ within the same `section_name` group.
5758 """
5859 normalized_current = normalize_entries (current_ahb_row .section_name )
60+ current_key = current_ahb_row .get_key ()
5961
6062 for idx , row in enumerate (subsequent_ahb_rows [start_idx :], start_idx ):
63+ if idx in duplicate_indices :
64+ continue
65+
66+ if (
67+ normalize_entries (row .section_name ) == normalized_current
68+ and row .get_key () == current_key
69+ and row .segment_id == current_ahb_row .segment_id
70+ ):
71+ return idx , row
72+
73+ # in case no match was found, continue by aligning `Segmentname` entries
74+ for idx , row in enumerate (subsequent_ahb_rows [start_idx :], start_idx ):
75+ if idx in duplicate_indices :
76+ continue
77+
6178 if normalize_entries (row .section_name ) == normalized_current :
79+ if row .ahb_expression is not None and current_ahb_row .ahb_expression is None :
80+ continue
6281 return idx , row
82+
6383 return - 1 , None
6484
6585
@@ -70,68 +90,74 @@ def align_ahb_rows(previous_ahb_rows: List[AhbRow], subsequent_ahb_rows: List[Ah
7090 result = []
7191 i = 0
7292 j = 0
93+ duplicate_indices : set [int ] = set ()
7394
7495 while i < len (previous_ahb_rows ) or j < len (subsequent_ahb_rows ):
7596 if i >= len (previous_ahb_rows ):
76- row = subsequent_ahb_rows [j ]
77- result .append (
78- AhbRowComparison (
79- previous_formatversion = _add_empty_row (row .formatversion ),
80- # label remaining rows of subsequent AHB as NEW
81- diff = AhbRowDiff (diff_type = DiffType .ADDED ),
82- subsequent_formatversion = row ,
97+ # add remaining rows as "new" if not already used
98+ if j not in duplicate_indices :
99+ result .append (
100+ AhbRowComparison (
101+ previous_formatversion = _add_empty_row (subsequent_ahb_rows [j ].formatversion ),
102+ diff = AhbRowDiff (diff_type = DiffType .ADDED ),
103+ subsequent_formatversion = subsequent_ahb_rows [j ],
104+ )
83105 )
84- )
85106 j += 1
107+ continue
86108
87- elif j >= len (subsequent_ahb_rows ):
88- row = previous_ahb_rows [i ]
109+ if j >= len (subsequent_ahb_rows ):
89110 result .append (
90111 AhbRowComparison (
91- previous_formatversion = row ,
112+ previous_formatversion = previous_ahb_rows [ i ] ,
92113 # label remaining rows of previous AHB as REMOVED
93114 diff = AhbRowDiff (diff_type = DiffType .REMOVED ),
94- subsequent_formatversion = _add_empty_row (row .formatversion ),
115+ subsequent_formatversion = _add_empty_row (previous_ahb_rows [ i ] .formatversion ),
95116 )
96117 )
97118 i += 1
119+ continue
98120
99- else :
100- current_row = previous_ahb_rows [i ]
101- next_match_idx , matching_row = _find_matching_subsequent_row (current_row , subsequent_ahb_rows , j )
121+ current_row = previous_ahb_rows [i ]
122+ next_match_idx , matching_row = _find_matching_subsequent_row (
123+ current_row , subsequent_ahb_rows , j , duplicate_indices
124+ )
102125
103- if next_match_idx >= 0 and matching_row is not None :
104- # add new rows until `section_name` (Segmentname) matches
105- for k in range ( j , next_match_idx ) :
106- new_row = subsequent_ahb_rows [ k ]
126+ if next_match_idx >= 0 and matching_row is not None :
127+ # add new rows until `section_name` (Segmentname) matches
128+ while j < next_match_idx :
129+ if j not in duplicate_indices :
107130 result .append (
108131 AhbRowComparison (
109- previous_formatversion = _add_empty_row (new_row .formatversion ),
132+ previous_formatversion = _add_empty_row (subsequent_ahb_rows [ j ] .formatversion ),
110133 diff = AhbRowDiff (diff_type = DiffType .ADDED ),
111- subsequent_formatversion = new_row ,
134+ subsequent_formatversion = subsequent_ahb_rows [ j ] ,
112135 )
113136 )
137+ j += 1
114138
115- # add matching rows with comparison
116- diff = _compare_ahb_rows (current_row , matching_row )
117- result .append (
118- AhbRowComparison (
119- previous_formatversion = current_row , diff = diff , subsequent_formatversion = matching_row
120- )
139+ # add matching rows with comparison
140+ diff = _compare_ahb_rows (current_row , matching_row )
141+ result .append (
142+ AhbRowComparison (
143+ previous_formatversion = current_row ,
144+ diff = diff ,
145+ subsequent_formatversion = matching_row ,
121146 )
147+ )
148+ duplicate_indices .add (next_match_idx )
149+ i += 1
150+ j = next_match_idx + 1
122151
123- i += 1
124- j = next_match_idx + 1
125-
126- else :
127- # if no match found - label as REMOVED
128- result .append (
129- AhbRowComparison (
130- previous_formatversion = current_row ,
131- diff = AhbRowDiff (diff_type = DiffType .REMOVED ),
132- subsequent_formatversion = _add_empty_row (current_row .formatversion ),
133- )
152+ else :
153+ # if no match found - label as REMOVED
154+ result .append (
155+ AhbRowComparison (
156+ previous_formatversion = current_row ,
157+ diff = AhbRowDiff (diff_type = DiffType .REMOVED ),
158+ subsequent_formatversion = _add_empty_row (current_row .formatversion ),
134159 )
135- i += 1
160+ )
161+ i += 1
136162
137163 return result
0 commit comments