Skip to content

Commit e8e6913

Browse files
committed
Make get_var_names available more consistently
Closes gh-202
1 parent c132050 commit e8e6913

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

islpy/_monkeypatch.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
SetOrMap: TypeAlias = _isl.BasicSet | _isl.Set | _isl.BasicMap | _isl.Map
6868
SetOrMapT = TypeVar("SetOrMapT", _isl.BasicSet, _isl.Set, _isl.BasicMap, _isl.Map)
6969

70-
HasSpace: TypeAlias = (
70+
HasDimNames: TypeAlias = (
7171
_isl.Space
7272
| _isl.Constraint
7373
| _isl.LocalSpace
@@ -81,6 +81,12 @@
8181
| _isl.Map
8282
)
8383

84+
HasSpace: TypeAlias = (
85+
HasDimNames
86+
| _isl.QPolynomial
87+
| _isl.PwQPolynomial
88+
)
89+
8490

8591
class IslObject(Protocol):
8692
def get_ctx(self) -> _isl.Context:
@@ -608,7 +614,7 @@ def obj_get_var_dict(
608614

609615

610616
def obj_get_var_ids(
611-
self: HasSpace,
617+
self: HasDimNames,
612618
dimtype: _isl.dim_type
613619
) -> Sequence[str | None]:
614620
"""Return a list of :class:`Id` instances for :class:`dim_type` *dimtype*."""
@@ -619,7 +625,7 @@ def obj_get_var_ids(
619625

620626
@_memoize_on_first_arg
621627
def obj_get_var_names_not_none(
622-
self: HasSpace,
628+
self: HasDimNames,
623629
dimtype: _isl.dim_type,
624630
) -> Sequence[str]:
625631
"""Return a list of dim names (in order) for :class:`dim_type` *dimtype*.
@@ -639,7 +645,7 @@ def obj_get_var_names_not_none(
639645

640646
@_memoize_on_first_arg
641647
def obj_get_var_names(
642-
self: HasSpace,
648+
self: HasDimNames,
643649
dimtype: _isl.dim_type,
644650
) -> Sequence[str | None]:
645651
"""Return a list of dim names (in order) for :class:`dim_type` *dimtype*.
@@ -648,6 +654,39 @@ def obj_get_var_names(
648654
for i in range(self.dim(dimtype))]
649655

650656

657+
@_memoize_on_first_arg
658+
def obj_get_var_names_not_none_via_space(
659+
self: HasSpace,
660+
dimtype: _isl.dim_type,
661+
) -> Sequence[str]:
662+
"""Return a list of dim names (in order) for :class:`dim_type` *dimtype*.
663+
664+
Raise :exc:`ValueError` if any of the names is *None*.
665+
666+
.. versionadded:: 2025.2.5
667+
"""
668+
space = self.get_space()
669+
ndim = space.dim(dimtype)
670+
res = [n
671+
for i in range(ndim)
672+
if (n := space.get_dim_name(dimtype, i)) is not None]
673+
if len(res) != ndim:
674+
raise ValueError("None encountered in dim names")
675+
return res
676+
677+
678+
@_memoize_on_first_arg
679+
def obj_get_var_names_via_space(
680+
self: HasSpace,
681+
dimtype: _isl.dim_type,
682+
) -> Sequence[str | None]:
683+
"""Return a list of dim names (in order) for :class:`dim_type` *dimtype*.
684+
"""
685+
space = self.get_space()
686+
return [space.get_dim_name(dimtype, i)
687+
for i in range(space.dim(dimtype))]
688+
689+
651690
def pwaff_get_pieces(self: _isl.PwAff | _isl.Aff) -> list[tuple[_isl.Set, _isl.Aff]]:
652691
if isinstance(self, _isl.Aff):
653692
self = self.to_pw_aff()
@@ -1058,12 +1097,18 @@ def _add_functionality() -> None:
10581097
# {{{ common functionality
10591098

10601099
for cls in ALL_CLASSES:
1061-
if hasattr(cls, "get_space") and cls is not _isl.Space:
1100+
if hasattr(cls, "get_space"):
10621101
cls.get_id_dict = obj_get_id_dict
10631102
cls.get_var_dict = obj_get_var_dict
10641103
cls.get_var_ids = obj_get_var_ids
1104+
1105+
# NB: This will include 'Space'
1106+
if hasattr(cls, "dim") and hasattr(cls, "get_dim_name"):
10651107
cls.get_var_names = obj_get_var_names
10661108
cls.get_var_names_not_none = obj_get_var_names_not_none
1109+
elif hasattr(cls, "get_space"):
1110+
cls.get_var_names = obj_get_var_names_via_space
1111+
cls.get_var_names_not_none = obj_get_var_names_not_none_via_space
10671112

10681113
# }}}
10691114

test/test_isl.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ def test_sched_constraints_set_validity():
485485
assert str(validity) == str(validity2)
486486

487487

488+
def test_polynomial_var_names():
489+
pw_qpoly = isl.PwQPolynomial("[n] -> { [i] -> 2 * i + n }")
490+
(_s, qpoly), = pw_qpoly.get_pieces()
491+
492+
for poly in [pw_qpoly, qpoly]:
493+
poly.get_var_names(isl.dim_type.param)
494+
495+
488496
if __name__ == "__main__":
489497
import sys
490498
if len(sys.argv) > 1:

0 commit comments

Comments
 (0)