Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
refactor to use ClonableArray
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Sep 14, 2020
1 parent 4a8cb1f commit e236ef0
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions src/sage/combinat/decorated_permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@
from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.parent import Parent
from sage.structure.element import Element
from sage.structure.richcmp import richcmp
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
from sage.arith.all import factorial
from sage.rings.integer import Integer
from .permutation import Permutations
from .subset import Subsets
from .colored_permutations import SignedPermutations
from sage.combinat.permutation import Permutations
from sage.combinat.subset import Subsets
from sage.combinat.colored_permutations import SignedPermutations
from sage.structure.list_clone import ClonableArray

class DecoratedPermutation(Element,
class DecoratedPermutation(ClonableArray,
metaclass=InheritComparisonClasscallMetaclass):
r"""
A decorated permutation.
Expand All @@ -41,7 +40,7 @@ class DecoratedPermutation(Element,
non-fixed points have positive sign.
"""
@staticmethod
def __classcall_private__(cls, pi, check=True):
def __classcall_private__(cls, pi):
"""
Create a decorated permutation.
Expand All @@ -55,9 +54,9 @@ def __classcall_private__(cls, pi, check=True):
"""
pi = list(pi)
return DecoratedPermutations(len(pi))(pi, check=check)
return DecoratedPermutations(len(pi))(pi)

def __init__(self, parent, pi):
def __init__(self, parent, pi, check=True):
"""
Initialize ``self``.
Expand All @@ -67,34 +66,34 @@ def __init__(self, parent, pi):
sage: elt = S([2, 1, -3])
sage: TestSuite(elt).run()
"""
self._pi = pi
Element.__init__(self, parent)
ClonableArray.__init__(self, parent, pi, check=check)

def __hash__(self):
r"""
TESTS::
def check(self):
"""
Check that ``self`` is a valid decorated permutation.
EXAMPLES::
sage: S = DecoratedPermutations(3)
sage: elt = S([2, 1, -3])
sage: hash(elt) # random
915443076393556996
sage: elt.check()
sage: elt = S([2, -1, 3])
Traceback (most recent call last):
...
ValueError: invalid decorated permutation
"""
return hash(self._pi)
if self not in self.parent():
raise ValueError("{} is not a decorated permutation".format(self))

def _repr_(self):
def __eq__(self, other):
"""
Return a string representation of ``self``.
Check whether ``self`` is equal to ``other``.
EXAMPLES::
INPUT:
sage: DecoratedPermutation([2, 1, -3])
[2, 1, -3]
"""
return repr(list(self._pi))
- ``other`` -- the element that ``self`` is compared to
def _richcmp_(self, other, op):
"""
Do the comparison.
OUTPUT: Boolean
EXAMPLES::
Expand All @@ -104,30 +103,39 @@ def _richcmp_(self, other, op):
sage: elt1 == elt2
True
"""
return richcmp(self._pi, other._pi, op)
return isinstance(other, DecoratedPermutation) and list(self) == list(other)

def size(self):
def __ne__(self, other):
"""
Return the size of the decorated permutation.
Check whether ``self`` is not equal to ``other``.
INPUT:
- ``other`` -- the element that ``self`` is compared to
OUTPUT: Boolean
EXAMPLES::
sage: DecoratedPermutation([2, 1, -3]).size()
3
sage: S = DecoratedPermutations(3)
sage: elt1 = S([2, 1, -3])
sage: elt2 = DecoratedPermutation([2, 1, 3])
sage: elt1 != elt2
True
"""
return len(self._pi)
return not (self == other)

def __iter__(self):
def size(self):
"""
Iterate over the values of the decorated permutation.
Return the size of the decorated permutation.
EXAMPLES::
sage: list(DecoratedPermutation([2, 1, -3]))
[2, 1, -3]
sage: DecoratedPermutation([2, 1, -3]).size()
3
"""
return iter(self._pi)
return len(self)

def to_signed_permutation(self):
"""
Return ``self`` as a signed permutation.
Expand All @@ -137,7 +145,7 @@ def to_signed_permutation(self):
sage: DecoratedPermutation([2, 1, -3]).to_signed_permutation()
[2, 1, -3]
"""
return SignedPermutations(len(self._pi))(list(self._pi))
return SignedPermutations(len(self))(list(self))

class DecoratedPermutations(UniqueRepresentation, Parent):
r"""
Expand Down Expand Up @@ -197,7 +205,7 @@ def __contains__(self, pi):
False
"""
if isinstance(pi, DecoratedPermutation):
return len(pi._pi) == self._n
return len(pi) == self._n

values = [v for v in pi]
if len(values) != self._n:
Expand Down

0 comments on commit e236ef0

Please sign in to comment.