3
dR
  ใ               @   sH   d Z ddlmZ ddlmZ dd ZG dd deZG dd	 d	eZd
S )a=   Variants of weak-key dictionaries that are based on object identity.

They will ignore the ``__hash__`` and ``__eq__`` implementations on the
objects. These are intended for various kinds of caches that map instances of
classes to other things without keeping those instances alive. Note that
iteration is not guarded, so if one were iterating over these dictionaries and
one of the weakrefs got cleaned up, this might modify the structure and break
the iteration. As this is not a common use for such caches, we have not
bothered to make these dicts robust to that case.
้    )ฺMutableMapping)ฺrefc                s   |f fdd	}|S )Nc                s    | }|d k	r|j j d  d S )N)ฺdataฺpop)ฺwrฺid_dict_refZid_dict)ฺkey_idฉ ๚8/tmp/pip-build-7vycvbft/traits/traits/util/weakiddict.pyฺcallback   s    z_remover.<locals>.callbackr	   )r   r   r   r	   )r   r
   ฺ_remover   s    r   c               @   sR   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )ฺ
WeakIDDictzT A weak-key-value dictionary that uses the id() of the key for
    comparisons.
    Nc             C   s   i | _ |d k	r| j| d S )N)r   ฺupdate)ฺselfฺdictr	   r	   r
   ฺ__init__(   s    zWeakIDDict.__init__c             C   s   d| j j dt| ddS )N๚<z at 0xฺx๚>)ฺ	__class__ฺ__name__ฺid)r   r	   r	   r
   ฺ__repr__-   s    zWeakIDDict.__repr__c             C   s   | j t|= d S )N)r   r   )r   ฺkeyr	   r	   r
   ฺ__delitem__0   s    zWeakIDDict.__delitem__c             C   s   | j t| d  S )N้   )r   r   )r   r   r	   r	   r
   ฺ__getitem__3   s    zWeakIDDict.__getitem__c             C   s>   t |tt|t | t |tt|t | f| jt|< d S )N)r   r   r   r   )r   r   ฺvaluer	   r	   r
   ฺ__setitem__6   s    zWeakIDDict.__setitem__c             C   s
   t | jS )N)ฺlenr   )r   r	   r	   r
   ฺ__len__<   s    zWeakIDDict.__len__c             C   s   t || jkS )N)r   r   )r   r   r	   r	   r
   ฺ__contains__?   s    zWeakIDDict.__contains__c             c   s6   x0| j D ]&}| j | d }| }|d k	r|V  qW d S )Nr   )r   )r   Zid_keyZwr_keyr   r	   r	   r
   ฺ__iter__B   s
    zWeakIDDict.__iter__)N)r   ฺ
__module__ฺ__qualname__ฺ__doc__r   r   r   r   r   r    r!   r"   r	   r	   r	   r
   r   #   s   
r   c               @   s    e Zd ZdZdd Zdd ZdS )ฺWeakIDKeyDictzซ A weak-key dictionary that uses the id() of the key for comparisons.

    This differs from `WeakIDDict` in that it does not try to make a weakref to
    the values.
    c             C   s   | j t| d S )Nr   )r   r   )r   r   r	   r	   r
   r   Q   s    zWeakIDKeyDict.__getitem__c             C   s*   t |tt|t | |f| jt|< d S )N)r   r   r   r   )r   r   r   r	   r	   r
   r   T   s    zWeakIDKeyDict.__setitem__N)r   r#   r$   r%   r   r   r	   r	   r	   r
   r&   J   s   r&   N)r%   ฺcollections.abcr   ฺweakrefr   r   r   r&   r	   r	   r	   r
   ฺ<module>   s
   	'