Check if two indexes are equal, possibly with cached results. If the two indexes are not of the same type or they do not implement equality, fallback to coordinate labels equality check.
(
index: Index,
other_index: Index,
variable: Variable,
other_variable: Variable,
cache: dict[tuple[int, int], bool | None] | None = None,
)
| 2014 | |
| 2015 | |
| 2016 | def indexes_equal( |
| 2017 | index: Index, |
| 2018 | other_index: Index, |
| 2019 | variable: Variable, |
| 2020 | other_variable: Variable, |
| 2021 | cache: dict[tuple[int, int], bool | None] | None = None, |
| 2022 | ) -> bool: |
| 2023 | """Check if two indexes are equal, possibly with cached results. |
| 2024 | |
| 2025 | If the two indexes are not of the same type or they do not implement |
| 2026 | equality, fallback to coordinate labels equality check. |
| 2027 | |
| 2028 | """ |
| 2029 | if cache is None: |
| 2030 | # dummy cache |
| 2031 | cache = {} |
| 2032 | |
| 2033 | key = (id(index), id(other_index)) |
| 2034 | equal: bool | None = None |
| 2035 | |
| 2036 | if key not in cache: |
| 2037 | if type(index) is type(other_index): |
| 2038 | try: |
| 2039 | equal = index.equals(other_index) |
| 2040 | except NotImplementedError: |
| 2041 | equal = None |
| 2042 | else: |
| 2043 | cache[key] = equal |
| 2044 | else: |
| 2045 | equal = None |
| 2046 | else: |
| 2047 | equal = cache[key] |
| 2048 | |
| 2049 | if equal is None: |
| 2050 | equal = variable.equals(other_variable) |
| 2051 | |
| 2052 | return cast(bool, equal) |
| 2053 | |
| 2054 | |
| 2055 | def indexes_identical( |
no test coverage detected
searching dependent graphs…