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

Commit

Permalink
Trac 18246: use WithEqualityById
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Aug 14, 2015
1 parent 1e59d3a commit e9a8971
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 145 deletions.
27 changes: 12 additions & 15 deletions src/sage/combinat/root_system/hecke_algebra_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import functools
from sage.misc.abstract_method import abstract_method
from sage.misc.cachefunc import cached_method
from sage.misc.fast_method import WithEqualityById
from sage.structure.sage_object import SageObject
from sage.structure.unique_representation import UniqueRepresentation
from sage.sets.family import Family
from sage.combinat.subset import Subsets
from sage.rings.infinity import infinity
from sage.rings.integer_ring import ZZ

class HeckeAlgebraRepresentation(SageObject):
class HeckeAlgebraRepresentation(WithEqualityById, SageObject):
r"""
A representation of an (affine) Hecke algebra given by the action of the `T` generators
Expand Down Expand Up @@ -64,6 +65,16 @@ class HeckeAlgebraRepresentation(SageObject):
sage: H.Y()
Lazy family (...)_{i in Coroot lattice of the Root system of type ['A', 3, 1]}
TESTS::
sage: from sage.combinat.root_system.hecke_algebra_representation import HeckeAlgebraRepresentation
sage: W = SymmetricGroup(3)
sage: domain = W.algebra(QQ)
sage: action = lambda x,i: domain.monomial(x.apply_simple_reflection(i, side="right"))
sage: r = HeckeAlgebraRepresentation(domain, action, CartanType(["A",2]), 1, -1)
sage: hash(r) # random
3
REFERENCES:
.. [HST2008] F. Hivert, A. Schilling, N. Thiery,
Expand All @@ -89,20 +100,6 @@ def __init__(self, domain, on_basis, cartan_type, q1, q2, q=1, side="right"):
self._cartan_type = cartan_type
self._side = side

def __hash__(self):
r"""
TESTS::
sage: from sage.combinat.root_system.hecke_algebra_representation import HeckeAlgebraRepresentation
sage: W = SymmetricGroup(3)
sage: domain = W.algebra(QQ)
sage: action = lambda x,i: domain.monomial(x.apply_simple_reflection(i, side="right"))
sage: r = HeckeAlgebraRepresentation(domain, action, CartanType(["A",2]), 1, -1)
sage: hash(r) # random
3
"""
return id(self)

def _repr_(self):
r"""
EXAMPLES::
Expand Down
8 changes: 3 additions & 5 deletions src/sage/geometry/triangulation/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ AUTHORS:
# http://www.gnu.org/licenses/
########################################################################



from sage.misc.fast_methods cimport hash_by_id
from sage.structure.sage_object cimport SageObject
from sage.structure.parent cimport Parent
from sage.categories.sets_cat import Sets
Expand Down Expand Up @@ -107,7 +106,7 @@ cdef class Point(SageObject):
sage: hash(p[0]) # random
35822008390213632
"""
return id(self._point_configuration) ^ self._index
return hash(self._point_configuration) ^ (<long>self._index)

cpdef point_configuration(self):
r"""
Expand Down Expand Up @@ -466,7 +465,6 @@ cdef class PointConfiguration_base(Parent):
self._pts = tuple([ Point(self, i, proj.column(i), aff.column(i), red.column(i))
for i in range(0,n) ])


def __hash__(self):
r"""
Hash function.
Expand All @@ -477,7 +475,7 @@ cdef class PointConfiguration_base(Parent):
sage: hash(p) # random
8746748042501
"""
return id(self)
return hash_by_id(<void *> self)

cpdef reduced_affine_vector_space(self):
"""
Expand Down
17 changes: 17 additions & 0 deletions src/sage/misc/fast_methods.pxd
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
cdef extern from "Python.h":
cdef size_t SIZEOF_VOID_P

cdef class FastHashable_class:
cdef Py_ssize_t _hash

cdef inline long hash_by_id(void * p):
r"""
This function is a copy paste from the default Python hash function.
"""
cdef long x
cdef size_t y = <size_t>p
# bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
# excessive hash collisions for dicts and sets
y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4))
x = <long>y
if x == -1:
x = -2
return x
16 changes: 8 additions & 8 deletions src/sage/misc/fast_methods.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ from sage.misc.lazy_attribute import lazy_class_attribute
from cpython.bool cimport *
from cpython.ref cimport *

cdef int SIZEOF_VOID_P_SHIFT = 8*sizeof(void *) - 4
cdef extern from "Python.h":
cdef size_t SIZEOF_VOID_P

cdef class WithEqualityById:
"""
Expand Down Expand Up @@ -150,15 +151,14 @@ cdef class WithEqualityById:
sage: hash(a) == object.__hash__(a)
True
sage: from sage.misc.fast_methods import WithEqualityById
sage: o1 = WithEqualityById()
sage: o2 = WithEqualityById()
sage: hash(o1) == hash(o2)
False
"""
# This is the default hash function in Python's object.c:
cdef long x
cdef size_t y = <size_t><void *>self
y = (y >> 4) | (y << SIZEOF_VOID_P_SHIFT)
x = <long>y
if x==-1:
x = -2
return x
return hash_by_id(<void *>self)

def __richcmp__(self, other, int m):
"""
Expand Down
6 changes: 2 additions & 4 deletions src/sage/numerical/linear_functions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ See :trac:`12091` ::
include "sage/ext/interrupt.pxi"
from cpython.object cimport *

cdef extern from "limits.h":
long LONG_MAX

from sage.misc.fast_methods cimport hash_by_id
from sage.structure.parent cimport Parent
from sage.structure.element cimport ModuleElement, Element
from sage.misc.cachefunc import cached_function
Expand Down Expand Up @@ -941,7 +939,7 @@ cdef class LinearFunction(ModuleElement):
sage: d[f] = 3
"""
# see _cmp_() if you want to change the hash function
return id(self) % LONG_MAX
return hash_by_id(<void *> self)

def __cmp__(left, right):
"""
Expand Down
6 changes: 2 additions & 4 deletions src/sage/numerical/linear_tensor_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ Here is an example of a linear function tensored with a vector space::

from cpython.object cimport *

cdef extern from "limits.h":
long LONG_MAX

from sage.misc.fast_methods cimport hash_by_id
from sage.structure.element cimport ModuleElement, RingElement
from sage.numerical.linear_functions cimport LinearFunction, is_LinearFunction

Expand Down Expand Up @@ -460,7 +458,7 @@ cdef class LinearTensor(ModuleElement):
sage: d[f] = 3
"""
# see _cmp_() if you want to change the hash function
return id(self) % LONG_MAX
return hash_by_id(<void *> self)

def __cmp__(left, right):
"""
Expand Down
18 changes: 7 additions & 11 deletions src/sage/plot/animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import struct
import zlib

from sage.misc.fast_methods import WithEqualityById
from sage.structure.sage_object import SageObject
from sage.misc.temporary_file import tmp_dir, tmp_filename, graphics_filename
import plot
Expand All @@ -139,7 +140,7 @@ def animate(frames, **kwds):
"""
return Animation(frames, **kwds)

class Animation(SageObject):
class Animation(WithEqualityById, SageObject):
r"""
Return an animation of a sequence of plots of objects.
Expand Down Expand Up @@ -215,6 +216,11 @@ class Animation(SageObject):
sage: a._frames
<generator object ...
TESTS::
sage: from sage.plot.animate import Animation
sage: hash(Animation()) # random
140658972348064
"""
def __init__(self, v=None, **kwds):
r"""
Expand Down Expand Up @@ -266,16 +272,6 @@ def _combine_kwds(self, *kwds_tuple):
new_kwds[name] = getattr(__builtin__, name[1:])(values)
return new_kwds

def __hash__(self):
r"""
TESTS::
sage: from sage.plot.animate import Animation
sage: hash(Animation()) # random
140658972348064
"""
return id(self)

def __getitem__(self, i):
"""
Get a frame from an animation or
Expand Down
33 changes: 11 additions & 22 deletions src/sage/plot/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import sage.misc.misc
from sage.misc.html import html
from sage.misc.temporary_file import tmp_filename
from sage.misc.fast_methods import WithEqualityById
from sage.structure.sage_object import SageObject
from sage.misc.decorators import suboptions
from colors import rgbcolor
Expand Down Expand Up @@ -84,7 +85,7 @@ def is_Graphics(x):
"""
return isinstance(x, Graphics)

class Graphics(SageObject):
class Graphics(WithEqualityById, SageObject):
"""
The Graphics object is an empty list of graphics objects. It is
useful to use this object when initializing a for loop where
Expand Down Expand Up @@ -134,6 +135,11 @@ class Graphics(SageObject):
sage: isinstance(g2, Graphics)
True
TESTS::
sage: hash(Graphics()) # random
42
.. automethod:: _rich_repr_
"""

Expand All @@ -159,17 +165,6 @@ def __init__(self):
self._show_legend = False
self._tick_label_color = (0, 0, 0)

def __hash__(self):
r"""
Graphics objects are all different!
TESTS::
sage: hash(Graphics()) # random
42
"""
return id(self)

def set_aspect_ratio(self, ratio):
"""
Set the aspect ratio, which is the ratio of height and width
Expand Down Expand Up @@ -3237,7 +3232,7 @@ def description(self):
return '\n'.join(g[1] for g in data)


class GraphicsArray(SageObject):
class GraphicsArray(WithEqualityById, SageObject):
"""
GraphicsArray takes a (`m` x `n`) list of lists of
graphics objects and plots them all on one canvas.
Expand Down Expand Up @@ -3279,6 +3274,9 @@ def __init__(self, array):
Traceback (most recent call last):
...
TypeError: every element of array must be a Graphics object
sage: hash(graphics_array([])) # random
42
"""
if not isinstance(array, (list, tuple)):
raise TypeError("array (=%s) must be a list of lists of Graphics objects"%(array))
Expand All @@ -3302,15 +3300,6 @@ def __init__(self, array):
self._glist.append(g)
self._figsize = None

def __hash__(self):
r"""
TESTS::
sage: hash(graphics_array([])) # random
42
"""
return id(self)

def _repr_(self):
"""
Representation of the graphics array.
Expand Down
6 changes: 4 additions & 2 deletions src/sage/plot/plot3d/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ AUTHORS:
# http://www.gnu.org/licenses/
#*****************************************************************************


from cpython.list cimport *
from cpython.object cimport PyObject

import os
from functools import reduce
Expand All @@ -42,6 +42,8 @@ from sage.misc.misc import sage_makedirs
from sage.env import SAGE_LOCAL
from sage.doctest import DOCTEST_MODE

from sage.misc.fast_methods cimport hash_by_id

from sage.modules.free_module_element import vector

from sage.rings.real_double import RDF
Expand Down Expand Up @@ -87,7 +89,7 @@ cdef class Graphics3d(SageObject):
sage: hash(Graphics3d()) # random
140658972348064
"""
return id(self)
return hash_by_id(<void *> self)

def _repr_(self):
"""
Expand Down
17 changes: 7 additions & 10 deletions src/sage/plot/plot3d/tachyon.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@

from sage.interfaces.tachyon import tachyon_rt

from sage.misc.fast_methods import WithEqualityById
from sage.structure.sage_object import SageObject

from sage.misc.misc import SAGE_TMP
Expand All @@ -153,7 +154,7 @@
from math import sqrt


class Tachyon(SageObject):
class Tachyon(WithEqualityById, SageObject):
r"""
Create a scene the can be rendered using the Tachyon ray tracer.
Expand Down Expand Up @@ -334,6 +335,11 @@ class Tachyon(SageObject):
....: tt = 't1'
....: T.sphere((q, q/3+.3*sin(3*q), .1+.3*cos(3*q)), .1, tt)
sage: T.show()
TESTS::
sage: hash(Tachyon()) # random
140658972348064
"""
def __init__(self,
xres=350, yres=350,
Expand Down Expand Up @@ -380,15 +386,6 @@ def __init__(self,
else:
self._viewdir = viewdir

def __hash__(self):
r"""
TESTS::
sage: hash(Tachyon()) # random
140658972348064
"""
return id(self)

def save_image(self, filename=None, *args, **kwds):
r"""
Save an image representation of ``self``.
Expand Down

0 comments on commit e9a8971

Please sign in to comment.