Skip to content

Commit

Permalink
check that hash and equality works in DecoratedPermutation, restore h…
Browse files Browse the repository at this point in the history
…ash in OrderedMultisetPartitionIntoSets and AbstractSetPartition
  • Loading branch information
mantepse committed Dec 13, 2022
1 parent 32a2bb4 commit 49ff03d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
60 changes: 20 additions & 40 deletions src/sage/combinat/decorated_permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ def __classcall_private__(cls, pi):
sage: DecoratedPermutation([2, 1, -3])
[2, 1, -3]
TESTS:
Check that hashing and comparison works::
sage: S = DecoratedPermutations(3)
sage: elt1 = S([2, 1, -3])
sage: elt2 = DecoratedPermutation([2, 1, -3])
sage: elt1 == elt2
True
sage: elt1 == [2, 1, -3]
False
sage: elt2 = DecoratedPermutation([2, 1, 3])
sage: elt1 != elt2
True
sage: hash(elt1) # random
915443076393556996
"""
pi = list(pi)
return DecoratedPermutations(len(pi))(pi)
Expand Down Expand Up @@ -86,46 +106,6 @@ def check(self):
if self not in self.parent():
raise ValueError("{} is not a decorated permutation".format(self))

def __eq__(self, other):
"""
Check whether ``self`` is equal to ``other``.
INPUT:
- ``other`` -- the element that ``self`` is compared to
OUTPUT: Boolean
EXAMPLES::
sage: S = DecoratedPermutations(3)
sage: elt1 = S([2, 1, -3])
sage: elt2 = DecoratedPermutation([2, 1, -3])
sage: elt1 == elt2
True
"""
return isinstance(other, DecoratedPermutation) and list(self) == list(other)

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

def size(self):
"""
Return the size of the decorated permutation.
Expand Down
16 changes: 16 additions & 0 deletions src/sage/combinat/multiset_partition_into_sets_ordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ def _repr_tight(self):
# eliminate spacing within blocks
return repr.replace(", ", ",").replace("},{", "}, {")

def __hash__(self):
"""
Return the hash of ``self``.
The parent is not included as part of the hash.
EXAMPLES::
sage: OMP = OrderedMultisetPartitionsIntoSets(4)
sage: A = OMP([[1], [1, 2]])
sage: B = OMP([{1}, {1, 2}])
sage: hash(A) == hash(B)
True
"""
return sum(hash(x) for x in self)

def __eq__(self, y):
"""
Check equality of ``self`` and ``y``.
Expand Down
16 changes: 16 additions & 0 deletions src/sage/combinat/set_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ def _repr_(self):
"""
return '{' + ', '.join(('{' + repr(sorted(x))[1:-1] + '}' for x in self)) + '}'

def __hash__(self):
"""
Return the hash of ``self``.
The parent is not included as part of the hash.
EXAMPLES::
sage: P = SetPartitions(4)
sage: A = SetPartition([[1], [2,3], [4]])
sage: B = P([[1], [2,3], [4]])
sage: hash(A) == hash(B)
True
"""
return sum(hash(x) for x in self)

def __eq__(self, y):
"""
Check equality of ``self`` and ``y``.
Expand Down

0 comments on commit 49ff03d

Please sign in to comment.