pypy / lib_pypy / identity_dict.py

try:
    from __pypy__ import identity_dict as idict
except ImportError:
    idict = None

from UserDict import DictMixin


class IdentityDictPurePython(object, DictMixin):
    __slots__ = "_dict _keys".split()

    def __init__(self):
        self._dict = {}
        self._keys = {} # id(obj) -> obj

    def __getitem__(self, arg):
        return self._dict[id(arg)]

    def __setitem__(self, arg, val):
        self._keys[id(arg)] = arg
        self._dict[id(arg)] = val

    def __delitem__(self, arg):
        del self._keys[id(arg)]
        del self._dict[id(arg)]

    def keys(self):
        return self._keys.values()

    def __contains__(self, arg):
        return id(arg) in self._dict

    def copy(self):
        d = type(self)()
        d.update(self.iteritems())
        assert len(d) == len(self)
        return d


class IdentityDictPyPy(object, DictMixin):
    __slots__ = ["_dict"]

    def __init__(self):
        self._dict = idict()

    def __getitem__(self, arg):
        return self._dict[arg]

    def __setitem__(self, arg, val):
        self._dict[arg] = val

    def __delitem__(self, arg):
        del self._dict[arg]

    def keys(self):
        return self._dict.keys()

    def __contains__(self, arg):
        return arg in self._dict

    def copy(self):
        d = type(self)()
        d.update(self.iteritems())
        assert len(d) == len(self)
        return d

if idict is None:
    identity_dict = IdentityDictPurePython
else:
    identity_dict = IdentityDictPyPy
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.