Skip to content

Commit

Permalink
Make FiniteFamily a Python subclass of FiniteFamily_base - so that En…
Browse files Browse the repository at this point in the history
…umeratedSets mixins work correctly
  • Loading branch information
mkoeppe committed Jan 4, 2023
1 parent d312578 commit 450f9c6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/sage/numerical/mip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cdef extern from *:
cdef int REAL = -1
cdef int INTEGER = 0

from sage.sets.family cimport FiniteFamily
from sage.sets.family cimport FiniteFamily_base
from sage.structure.sage_object cimport SageObject
from sage.numerical.backends.generic_backend cimport GenericBackend

Expand All @@ -29,7 +29,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
cpdef sum(self, L)


cdef class MIPVariable(FiniteFamily):
cdef class MIPVariable(FiniteFamily_base):
cdef MixedIntegerLinearProgram _p
cdef int _vtype
cdef str _name
Expand Down
2 changes: 1 addition & 1 deletion src/sage/numerical/mip.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3131,7 +3131,7 @@ class MIPSolverException(RuntimeError):
pass


cdef class MIPVariable(FiniteFamily):
cdef class MIPVariable(FiniteFamily_base):
r"""
``MIPVariable`` is a variable used by the class
``MixedIntegerLinearProgram``.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/sets/family.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ cdef class AbstractFamily(Parent):
cdef public __custom_name


cdef class FiniteFamily(AbstractFamily):
cdef class FiniteFamily_base(AbstractFamily):
cdef public dict _dictionary
cdef public object _keys
89 changes: 47 additions & 42 deletions src/sage/sets/family.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -536,48 +536,10 @@ cdef class AbstractFamily(Parent):
return Family( dict( (self[k], k) for k in self.keys()) )
cdef class FiniteFamily(AbstractFamily):
cdef class FiniteFamily_base(AbstractFamily):
r"""
A :class:`FiniteFamily` is an associative container which models a finite
family `(f_i)_{i \in I}`. Its elements `f_i` are therefore its
values. Instances should be created via the :func:`Family` factory. See its
documentation for examples and tests.
EXAMPLES:
We define the family `(f_i)_{i \in \{3,4,7\}}` with `f_3=a`,
`f_4=b`, and `f_7=d`::
sage: from sage.sets.family import FiniteFamily
sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'})
Individual elements are accessible as in a usual dictionary::
sage: f[7]
'd'
And the other usual dictionary operations are also available::
sage: len(f)
3
sage: f.keys()
[3, 4, 7]
However f behaves as a container for the `f_i`'s::
sage: list(f)
['a', 'b', 'd']
sage: [ x for x in f ]
['a', 'b', 'd']
The order of the elements can be specified using the ``keys`` optional argument::
sage: f = FiniteFamily({"a": "aa", "b": "bb", "c" : "cc" }, keys = ["c", "a", "b"])
sage: list(f)
['cc', 'aa', 'bb']
Cython base class for :class:`FiniteFamily`.
"""
def __init__(self, dictionary, keys=None):
"""
TESTS::
Expand Down Expand Up @@ -713,8 +675,8 @@ cdef class FiniteFamily(AbstractFamily):
False
"""
return (isinstance(other, self.__class__) and
self._keys == (<FiniteFamily> other)._keys and
self._dictionary == (<FiniteFamily> other)._dictionary)
self._keys == (<FiniteFamily_base> other)._keys and
self._dictionary == (<FiniteFamily_base> other)._dictionary)
def _repr_(self):
"""
Expand Down Expand Up @@ -830,6 +792,49 @@ cdef class FiniteFamily(AbstractFamily):
self.__init__(state['dictionary'], keys=state.get("keys"))
class FiniteFamily(FiniteFamily_base):
r"""
A :class:`FiniteFamily` is an associative container which models a finite
family `(f_i)_{i \in I}`. Its elements `f_i` are therefore its
values. Instances should be created via the :func:`Family` factory. See its
documentation for examples and tests.
EXAMPLES:
We define the family `(f_i)_{i \in \{3,4,7\}}` with `f_3=a`,
`f_4=b`, and `f_7=d`::
sage: from sage.sets.family import FiniteFamily
sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'})
Individual elements are accessible as in a usual dictionary::
sage: f[7]
'd'
And the other usual dictionary operations are also available::
sage: len(f)
3
sage: f.keys()
[3, 4, 7]
However f behaves as a container for the `f_i`'s::
sage: list(f)
['a', 'b', 'd']
sage: [ x for x in f ]
['a', 'b', 'd']
The order of the elements can be specified using the ``keys`` optional argument::
sage: f = FiniteFamily({"a": "aa", "b": "bb", "c" : "cc" }, keys = ["c", "a", "b"])
sage: list(f)
['cc', 'aa', 'bb']
"""
pass
class FiniteFamilyWithHiddenKeys(FiniteFamily):
r"""
A close variant of :class:`FiniteFamily` where the family contains some
Expand Down

0 comments on commit 450f9c6

Please sign in to comment.