| 1 |
try: |
| 2 |
from __pypy__ import identity_dict as idict |
| 3 |
except ImportError: |
| 4 |
idict = None |
| 5 |
|
| 6 |
from UserDict import DictMixin |
| 7 |
|
| 8 |
|
| 9 |
class IdentityDictPurePython(object, DictMixin): |
| 10 |
__slots__ = "_dict _keys".split() |
| 11 |
|
| 12 |
def __init__(self): |
| 13 |
self._dict = {} |
| 14 |
self._keys = {} |
| 15 |
|
| 16 |
def __getitem__(self, arg): |
| 17 |
return self._dict[id(arg)] |
| 18 |
|
| 19 |
def __setitem__(self, arg, val): |
| 20 |
self._keys[id(arg)] = arg |
| 21 |
self._dict[id(arg)] = val |
| 22 |
|
| 23 |
def __delitem__(self, arg): |
| 24 |
del self._keys[id(arg)] |
| 25 |
del self._dict[id(arg)] |
| 26 |
|
| 27 |
def keys(self): |
| 28 |
return self._keys.values() |
| 29 |
|
| 30 |
def __contains__(self, arg): |
| 31 |
return id(arg) in self._dict |
| 32 |
|
| 33 |
def copy(self): |
| 34 |
d = type(self)() |
| 35 |
d.update(self.items()) |
| 36 |
assert len(d) == len(self) |
| 37 |
return d |
| 38 |
|
| 39 |
|
| 40 |
class IdentityDictPyPy(object, DictMixin): |
| 41 |
__slots__ = ["_dict"] |
| 42 |
|
| 43 |
def __init__(self): |
| 44 |
self._dict = idict() |
| 45 |
|
| 46 |
def __getitem__(self, arg): |
| 47 |
return self._dict[arg] |
| 48 |
|
| 49 |
def __setitem__(self, arg, val): |
| 50 |
self._dict[arg] = val |
| 51 |
|
| 52 |
def __delitem__(self, arg): |
| 53 |
del self._dict[arg] |
| 54 |
|
| 55 |
def keys(self): |
| 56 |
return self._dict.keys() |
| 57 |
|
| 58 |
def __contains__(self, arg): |
| 59 |
return arg in self._dict |
| 60 |
|
| 61 |
def copy(self): |
| 62 |
d = type(self)() |
| 63 |
d.update(self.items()) |
| 64 |
assert len(d) == len(self) |
| 65 |
return d |
| 66 |
|
| 67 |
if idict is None: |
| 68 |
identity_dict = IdentityDictPurePython |
| 69 |
else: |
| 70 |
identity_dict = IdentityDictPyPy |
| 71 |
|