Skip to content

Commit 1f671d6

Browse files
authored
[AIG] Add slice indexing support to LongestPathCollection in AIG python on bindings (#8709)
This commit implements slice indexing functionality for the LongestPathCollection class, allowing users to access multiple timing paths using Python's standard slice notation. The implementation handles all slice types including negative indices, step values, and empty slices through Python's built-in slice.indices() method.
1 parent 0ee3284 commit 1f671d6

File tree

2 files changed

+21
-6
lines changed
  • integration_test/Bindings/Python/dialects
  • lib/Bindings/Python/dialects

2 files changed

+21
-6
lines changed

integration_test/Bindings/Python/dialects/aig.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ def build_module(module):
7575
# CHECK-NEXT: sum: 128
7676
print("sum: ", sum(p.delay for p in collection))
7777

78-
for p in list(collection)[:2]:
78+
for p in collection[:2]:
7979
# CHECK-NEXT: delay 2 : out2[{{[0-9]+}}]
8080
# CHECK-NEXT: delay 2 : out2[{{[0-9]+}}]
8181
print("delay", p.delay, ":", p.fan_out)
8282

83+
# CHECK-NEXT: minus index slice: True
84+
print("minus index slice:", len(collection[:-2]) == len(collection) - 2)
85+
8386
# Test framegraph emission.
8487
# CHECK: top:test_aig;a[7] 0
8588
# CHECK-NEXT: top:test_aig;out2[7] 2

lib/Bindings/Python/dialects/aig.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import json
1010
from dataclasses import dataclass
11-
from typing import Any, Dict, List
11+
from typing import Any, Dict, List, Union
1212

1313
# ============================================================================
1414
# Core Data Structures for AIG Path Analysis
@@ -290,14 +290,26 @@ def __len__(self) -> int:
290290
"""Get the number of paths in the collection."""
291291
return self.length
292292

293-
def __getitem__(self, index: int) -> DataflowPath:
293+
def __getitem__(
294+
self, index: Union[slice,
295+
int]) -> Union[DataflowPath, List[DataflowPath]]:
294296
"""
295297
Get a specific path from the collection by index.
296-
Supports negative indexing. Results are cached to avoid expensive
297-
JSON parsing on repeated access.
298+
Supports both integer and slice indexing. Integer indices can be negative.
299+
Results are cached to avoid expensive JSON parsing on repeated access.
300+
298301
Args:
299-
index: Index of the path to retrieve (0 = longest path)
302+
index: Integer index or slice object to access paths
303+
304+
Returns:
305+
DataflowPath or list of DataflowPaths for slice access
306+
307+
Raises:
308+
IndexError: If index is out of range
300309
"""
310+
if isinstance(index, slice):
311+
return [self[i] for i in range(*index.indices(len(self)))]
312+
301313
# Handle negative indexing
302314
if index < 0:
303315
index += self.length

0 commit comments

Comments
 (0)