Skip to content

Commit

Permalink
Python: make the cofactor methods return Optionals as described in th…
Browse files Browse the repository at this point in the history
…e docstrings

Also don't raise a `MemoryError` for terminals but actually return None
  • Loading branch information
nhusung committed Oct 16, 2024
1 parent 0b141e1 commit 18ceef1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
15 changes: 10 additions & 5 deletions bindings/python/oxidd/bcdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,25 @@ def manager(self) -> BCDDManager:
return BCDDManager._from_raw(_lib.oxidd_bcdd_containing_manager(self._func))

@override
def cofactors(self) -> tuple[Self, Self]:
def cofactors(self) -> Optional[tuple[Self, Self]]:
raw_pair = _lib.oxidd_bcdd_cofactors(self._func)
if raw_pair.first._p == _ffi.NULL:
return None
assert raw_pair.second._p != _ffi.NULL
return (
self.__class__._from_raw(raw_pair.first),
self.__class__._from_raw(raw_pair.second),
)

@override
def cofactor_true(self) -> Self:
return self.__class__._from_raw(_lib.oxidd_bcdd_cofactor_true(self._func))
def cofactor_true(self) -> Optional[Self]:
raw = _lib.oxidd_bcdd_cofactor_true(self._func)
return self.__class__._from_raw(raw) if raw._p != _ffi.NULL else None

@override
def cofactor_false(self) -> Self:
return self.__class__._from_raw(_lib.oxidd_bcdd_cofactor_false(self._func))
def cofactor_false(self) -> Optional[Self]:
raw = _lib.oxidd_bcdd_cofactor_false(self._func)
return self.__class__._from_raw(raw) if raw._p != _ffi.NULL else None

@override
def level(self) -> Optional[int]:
Expand Down
15 changes: 10 additions & 5 deletions bindings/python/oxidd/bdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,25 @@ def manager(self) -> BDDManager:
return BDDManager._from_raw(_lib.oxidd_bdd_containing_manager(self._func))

@override
def cofactors(self) -> tuple[Self, Self]:
def cofactors(self) -> Optional[tuple[Self, Self]]:
raw_pair = _lib.oxidd_bdd_cofactors(self._func)
if raw_pair.first._p == _ffi.NULL:
return None
assert raw_pair.second._p != _ffi.NULL
return (
self.__class__._from_raw(raw_pair.first),
self.__class__._from_raw(raw_pair.second),
)

@override
def cofactor_true(self) -> Self:
return self.__class__._from_raw(_lib.oxidd_bdd_cofactor_true(self._func))
def cofactor_true(self) -> Optional[Self]:
raw = _lib.oxidd_bdd_cofactor_true(self._func)
return self.__class__._from_raw(raw) if raw._p != _ffi.NULL else None

@override
def cofactor_false(self) -> Self:
return self.__class__._from_raw(_lib.oxidd_bdd_cofactor_false(self._func))
def cofactor_false(self) -> Optional[Self]:
raw = _lib.oxidd_bdd_cofactor_false(self._func)
return self.__class__._from_raw(raw) if raw._p != _ffi.NULL else None

@override
def level(self) -> Optional[int]:
Expand Down
6 changes: 3 additions & 3 deletions bindings/python/oxidd/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class BooleanFunction(Function, Protocol):
"""Boolean function represented as decision diagram"""

@abstractmethod
def cofactors(self) -> tuple[Self, Self]:
def cofactors(self) -> Optional[tuple[Self, Self]]:
r"""Get the cofactors ``(f_true, f_false)`` of ``self``
Let f(x₀, …, xₙ) be represented by ``self``, where x₀ is (currently) the
Expand All @@ -157,7 +157,7 @@ def cofactors(self) -> tuple[Self, Self]:
raise NotImplementedError

@abstractmethod
def cofactor_true(self) -> Self:
def cofactor_true(self) -> Optional[Self]:
"""Get the cofactor ``f_true`` of ``self``
This method is slightly more efficient than :meth:`Self::cofactors` in
Expand All @@ -172,7 +172,7 @@ def cofactor_true(self) -> Self:
raise NotImplementedError

@abstractmethod
def cofactor_false(self) -> Self:
def cofactor_false(self) -> Optional[Self]:
"""Get the cofactor ``f_true`` of ``self``
This method is slightly more efficient than :meth:`Self::cofactors` in
Expand Down
15 changes: 10 additions & 5 deletions bindings/python/oxidd/zbdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,25 @@ def manager(self) -> ZBDDManager:
return ZBDDManager._from_raw(_lib.oxidd_zbdd_containing_manager(self._func))

@override
def cofactors(self) -> tuple[Self, Self]:
def cofactors(self) -> Optional[tuple[Self, Self]]:
raw_pair = _lib.oxidd_zbdd_cofactors(self._func)
if raw_pair.first._p == _ffi.NULL:
return None
assert raw_pair.second._p != _ffi.NULL
return (
self.__class__._from_raw(raw_pair.first),
self.__class__._from_raw(raw_pair.second),
)

@override
def cofactor_true(self) -> Self:
return self.__class__._from_raw(_lib.oxidd_zbdd_cofactor_true(self._func))
def cofactor_true(self) -> Optional[Self]:
raw = _lib.oxidd_zbdd_cofactor_true(self._func)
return self.__class__._from_raw(raw) if raw._p != _ffi.NULL else None

@override
def cofactor_false(self) -> Self:
return self.__class__._from_raw(_lib.oxidd_zbdd_cofactor_false(self._func))
def cofactor_false(self) -> Optional[Self]:
raw = _lib.oxidd_zbdd_cofactor_false(self._func)
return self.__class__._from_raw(raw) if raw._p != _ffi.NULL else None

@override
def level(self) -> Optional[int]:
Expand Down

0 comments on commit 18ceef1

Please sign in to comment.