Ticket #14054: trac_14054_cythoned_unique_representation.patch

File trac_14054_cythoned_unique_representation.patch, 9.1 KB (added by SimonKing, 7 years ago)
  • module_list.py

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1359929327 -3600
    # Node ID b30fc04e2d67f4a5f5bec15aff54e470b20d2950
    # Parent  bf17d205055ad5d2bd2352ebf2b86bea907d70c5
    #14054: Cythoned UniqueRepresentation
    
    diff --git a/module_list.py b/module_list.py
    a b  
    18511851    Extension('sage.structure.sage_object',
    18521852              sources = ['sage/structure/sage_object.pyx']),
    18531853
     1854    Extension('sage.structure.unique_representation',
     1855              sources = ['sage/structure/unique_representation.pyx']),
     1856
    18541857    Extension('sage.structure.wrapper_parent',
    18551858              sources = ['sage/structure/wrapper_parent.pyx']),
    18561859
  • sage/categories/primer.py

    diff --git a/sage/categories/primer.py b/sage/categories/primer.py
    a b  
    427427    [<class 'sage.categories.examples.finite_semigroups.LeftRegularBand_with_category'>,
    428428     <class 'sage.categories.examples.finite_semigroups.LeftRegularBand'>,
    429429     <class 'sage.structure.unique_representation.UniqueRepresentation'>,
     430     <type 'sage.structure.unique_representation.UniqueRepresentation_c'>,
    430431     <type 'sage.structure.parent.Parent'>,
    431432     <type 'sage.structure.category_object.CategoryObject'>,
    432433     <type 'sage.structure.sage_object.SageObject'>,
  • sage/categories/sets_cat.py

    diff --git a/sage/categories/sets_cat.py b/sage/categories/sets_cat.py
    a b  
    8888        <class 'sage.categories.examples.sets_cat.PrimeNumbers_Inherits'>
    8989        <class 'sage.categories.examples.sets_cat.PrimeNumbers_Abstract'>
    9090        <class 'sage.structure.unique_representation.UniqueRepresentation'>
     91        <type 'sage.structure.unique_representation.UniqueRepresentation_c'>
    9192        <type 'sage.structure.parent.Parent'>
    9293        <type 'sage.structure.category_object.CategoryObject'>
    9394        <type 'sage.structure.sage_object.SageObject'>
     
    9697        <class 'sage.categories.objects.Objects.parent_class'>
    9798        <type 'object'>
    9899
     100
    99101    We run some generic checks on P::
    100102
    101103        sage: TestSuite(P).run(verbose=True)
  • sage/combinat/root_system/cartan_type.py

    diff --git a/sage/combinat/root_system/cartan_type.py b/sage/combinat/root_system/cartan_type.py
    a b  
    20882088
    20892089##############################################################################
    20902090# For backward compatibility
    2091 class CartanType_simple_finite(object):
     2091class CartanType_simple_finite(UniqueRepresentation):
    20922092    def __setstate__(self, dict):
    20932093        """
    20942094        Implements the unpickling of Cartan types pickled by Sage <= 4.0.
  • (a) a/sage/structure/unique_representation.py vs. (b) b/sage/structure/unique_representation.pyx

    diff --git a/sage/structure/unique_representation.py b/sage/structure/unique_representation.pyx
    rename from sage/structure/unique_representation.py
    rename to sage/structure/unique_representation.pyx
    a b  
    2525from sage.misc.cachefunc import cached_function
    2626from sage.misc.classcall_metaclass import ClasscallMetaclass, typecall
    2727
    28 class UniqueRepresentation:
     28cdef class UniqueRepresentation_c:
     29    """
     30    Auxiliary class, providing fast cythoned hash and comparison
     31    methods for :class:`UniqueRepresentation`.
     32    """
     33    def __richcmp__(self, other, int op):
     34        """
     35        Rich comparison of ``self`` and ``other`
     36       
     37        Comparison is done by identity.
     38
     39        See also :class:`UniqueRepresentation` for a discussion.
     40
     41        EXAMPLES::
     42
     43            sage: x = UniqueRepresentation()
     44            sage: y = UniqueRepresentation()
     45            sage: x == y
     46            True
     47            sage: x is y
     48            True
     49            sage: x == 3
     50            False
     51            sage: x != y
     52            False
     53
     54        TESTS::
     55
     56            sage: class bla(UniqueRepresentation, SageObject):
     57            ...        def __init__(self, i): pass
     58            sage: b1 = bla(1); b2 = bla(2)
     59            sage: b1 == b1
     60            True
     61            sage: b1 == b2
     62            False
     63            sage: b1 != b1
     64            False
     65            sage: b1 != b2
     66            True
     67
     68        """
     69        if self is other:
     70            if op is 0:  # <
     71                return False
     72            if op is 1:  # <=
     73                return True
     74            if op is 2:  # ==
     75                return True
     76            if op is 3:  # !=
     77                return False
     78            if op is 4:  # >
     79                return False
     80            if op is 5:  # >=
     81                return True
     82        else:
     83            if op is 2:
     84                return False
     85            if op is 3:
     86                return True
     87        return NotImplemented
     88
     89    def __hash__(self):
     90        """
     91        Returns the hash value of ``self``.
     92
     93        See also :class:`UniqueRepresentation` for a discussion.
     94
     95        EXAMPLES::
     96
     97            sage: x = UniqueRepresentation()
     98            sage: y = UniqueRepresentation()
     99            sage: hash(x) # random
     100            74153040
     101            sage: type(hash(x))
     102            <type 'int'>
     103            sage: hash(x) == hash(y)
     104            True
     105            sage: class bla(UniqueRepresentation, SageObject): pass
     106            sage: x = bla(); hx = hash(x)
     107            sage: x.rename("toto")
     108            sage: hx == hash(x)
     109            True
     110        """
     111        return id(self)
     112
     113class UniqueRepresentation(UniqueRepresentation_c):
    29114    """
    30115    Classes derived from UniqueRepresentation inherit a unique
    31116    representation behavior for their instances.
     
    86171    subclasses. Its implementation is also more technical, which leads
    87172    to some subtleties.
    88173
     174    Note that :class:`UniqueRepresentation` has to be a Python class, because
     175    it relies on a metaclass. For improved performance, the methods for hash
     176    (returning the id of the object) and comparison (using comparison by
     177    identity when possible) are cythoned. This was done in
     178    :class:`UniqueRepresentation_c`, see :trac:`14054`.
     179
    89180    EXAMPLES:
    90181
    91182    We start with a simple class whose constructor takes a single
     
    283374
    284375        sage: x = MyClass(value = 1)
    285376        sage: x.__reduce__()
    286         (<function unreduce at ...>, (<class '__main__.MyClass'>, (), {'value': 1}))
     377        (<built-in function unreduce>, (<class '__main__.MyClass'>, (), {'value': 1}))
    287378        sage: x is loads(dumps(x))
    288379        True
    289380
     
    466557            instance._reduction = (cls, args, options)
    467558        return instance
    468559
    469     # Should be cythoned
    470     def __eq__(self, other):
    471         """
    472         Test if ``self`` and ``other` are equal by comparing their
    473         identity.
    474 
    475         See also :class:`UniqueRepresentation` for a discussion.
    476 
    477         EXAMPLES::
    478 
    479             sage: x = UniqueRepresentation()
    480             sage: y = UniqueRepresentation()
    481             sage: x == y
    482             True
    483             sage: x is y
    484             True
    485             sage: x == 3
    486             False
    487 
    488         TESTS::
    489 
    490             sage: class bla(UniqueRepresentation, SageObject):
    491             ...        def __init__(self, i): pass
    492             sage: b1 = bla(1); b2 = bla(2)
    493             sage: b1 == b1
    494             True
    495             sage: b1 == b2
    496             False
    497         """
    498         return self is other
    499 
    500     # Should be cythoned
    501     def __ne__(self, other):
    502         """
    503         Test if ``self`` and ``other` are different by comparing their
    504         identity.
    505 
    506         See also :class:`UniqueRepresentation` for a discussion.
    507 
    508         EXAMPLES::
    509 
    510             sage: x = UniqueRepresentation()
    511             sage: y = UniqueRepresentation()
    512             sage: x != y
    513             False
    514 
    515         TESTS::
    516 
    517             sage: class bla(UniqueRepresentation, SageObject):
    518             ...        def __init__(self, i): pass
    519             sage: b1 = bla(1); b2 = bla(2)
    520             sage: b1 != b1
    521             False
    522             sage: b1 != b2
    523             True
    524         """
    525         return self is not other
    526 
    527     # Should be cythoned
    528     def __hash__(self):
    529         """
    530         Returns the hash value of ``self``.
    531 
    532         See also :class:`UniqueRepresentation` for a discussion.
    533 
    534         EXAMPLES::
    535 
    536             sage: x = UniqueRepresentation()
    537             sage: y = UniqueRepresentation()
    538             sage: hash(x) # random
    539             74153040
    540             sage: type(hash(x))
    541             <type 'int'>
    542             sage: hash(x) == hash(y)
    543             True
    544             sage: class bla(UniqueRepresentation, SageObject): pass
    545             sage: x = bla(); hx = hash(x)
    546             sage: x.rename("toto")
    547             sage: hx == hash(x)
    548             True
    549         """
    550         return id(self)
    551 
    552560    def __reduce__(self):
    553561        """
    554562        Returns the argument that have been passed to :meth:`__new__<object.__new__>`
     
    560568
    561569            sage: x = UniqueRepresentation()
    562570            sage: x.__reduce__()
    563             (<function unreduce at ...>, (<class 'sage.structure.unique_representation.UniqueRepresentation'>, (), {}))
     571            (<built-in function unreduce>, (<class 'sage.structure.unique_representation.UniqueRepresentation'>, (), {}))
    564572        """
    565573        return (unreduce, self._reduction)
    566574