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

Commit

Permalink
Clean up in coerce_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Nov 2, 2017
1 parent bc45dab commit 87a0193
Show file tree
Hide file tree
Showing 8 changed files with 716 additions and 628 deletions.
2 changes: 1 addition & 1 deletion src/sage/categories/homset.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
# trac ticket #14159

from sage.structure.coerce_dict import TripleDict
_cache = TripleDict(53, weak_values=True)
_cache = TripleDict(weak_values=True)

def Hom(X, Y, category=None, check=True):
"""
Expand Down
42 changes: 22 additions & 20 deletions src/sage/structure/coerce.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -406,33 +406,28 @@ cdef class CoercionModel_cache_maps(CoercionModel):
- Robert Bradshaw
"""

def __init__(self, lookup_dict_size=127, lookup_dict_threshold=.75):
def __init__(self, *args, **kwds):
"""
INPUT:
- ``lookup_dict_size`` - initial size of the coercion hashtables
- ``lookup_dict_threshold`` - maximal density of the coercion
hashtables before forcing a re-hash
EXAMPLES::
sage: from sage.structure.coerce import CoercionModel_cache_maps
sage: cm = CoercionModel_cache_maps(4, .95)
sage: cm = CoercionModel_cache_maps()
sage: K = NumberField(x^2-2, 'a')
sage: A = cm.get_action(ZZ, K, operator.mul)
sage: f, g = cm.coercion_maps(QQ, int)
sage: f, g = cm.coercion_maps(ZZ, int)
.. NOTE::
TESTS::
In practice 4 would be a really bad number to choose, but
it makes the hashing deterministic.
sage: cm = CoercionModel_cache_maps(4, .95)
doctest:...: DeprecationWarning: the 'lookup_dict_size' argument is deprecated
See http://trac.sagemath.org/24135 for details.
doctest:...: DeprecationWarning: the 'lookup_dict_threshold' argument is deprecated
See http://trac.sagemath.org/24135 for details.
"""
self.reset_cache(lookup_dict_size, lookup_dict_threshold)
self.reset_cache(*args, **kwds)

def reset_cache(self, lookup_dict_size=127, lookup_dict_threshold=.75):
def reset_cache(self, lookup_dict_size=None, lookup_dict_threshold=None):
"""
Clear the coercion cache.
Expand All @@ -450,14 +445,20 @@ cdef class CoercionModel_cache_maps(CoercionModel):
sage: cm.get_cache()
({}, {})
"""
if lookup_dict_size is not None:
from sage.misc.superseded import deprecation
deprecation(24135, "the 'lookup_dict_size' argument is deprecated")
if lookup_dict_threshold is not None:
from sage.misc.superseded import deprecation
deprecation(24135, "the 'lookup_dict_threshold' argument is deprecated")
# This MUST be a mapping of tuples, where each
# tuple contains at least two elements that are either
# None or of type Map.
self._coercion_maps = TripleDict(lookup_dict_size, threshold=lookup_dict_threshold)
self._coercion_maps = TripleDict()
# This MUST be a mapping to actions.
self._action_maps = TripleDict(lookup_dict_size, threshold=lookup_dict_threshold)
self._action_maps = TripleDict()
# This is a mapping from Parents to Parents, storing the result of division in the given parent.
self._division_parents = TripleDict(lookup_dict_size, threshold=lookup_dict_threshold)
self._division_parents = TripleDict()

def get_cache(self):
"""
Expand Down Expand Up @@ -521,8 +522,9 @@ cdef class CoercionModel_cache_maps(CoercionModel):
sage: act(1/5, x+10)
1/5*x + 2
"""
return dict([((S, R), mors) for (S, R, op), mors in self._coercion_maps.iteritems()]), \
dict(self._action_maps.iteritems())
d1 = {(S, R): mors for (S, R, op), mors in self._coercion_maps.items()}
d2 = self._action_maps.copy()
return d1, d2

def record_exceptions(self, bint value=True):
r"""
Expand Down
28 changes: 21 additions & 7 deletions src/sage/structure/coerce_dict.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
cimport cython
from cpython cimport PyObject

cdef struct mono_cell

cdef struct mono_cell:
PyObject* key_id
PyObject* key_weakref
PyObject* value


@cython.final
cdef class MonoDict:
Expand All @@ -12,12 +17,21 @@ cdef class MonoDict:
cdef mono_cell* table
cdef bint weak_values
cdef eraser
cdef mono_cell* lookup(self,PyObject* key)
cdef get(self, object k)
cdef set(self, object k, value)
cdef mono_cell* lookup(self, PyObject* key)
cdef get(self, k)
cdef int set(self, k, value) except -1
cdef int resize(self) except -1

cdef struct triple_cell

cdef struct triple_cell:
PyObject* key_id1
PyObject* key_id2
PyObject* key_id3
PyObject* key_weakref1
PyObject* key_weakref2
PyObject* key_weakref3
PyObject* value


@cython.final
cdef class TripleDict:
Expand All @@ -29,6 +43,6 @@ cdef class TripleDict:
cdef bint weak_values
cdef eraser
cdef triple_cell* lookup(self, PyObject* key1, PyObject* key2, PyObject* key3)
cdef get(self, object k1, object k2, object k3)
cdef set(self, object k1, object k2, object k3, value)
cdef get(self, k1, k2, k3)
cdef int set(self, k1, k2, k3, value) except -1
cdef int resize(self) except -1
Loading

0 comments on commit 87a0193

Please sign in to comment.