
    n9e                     0   d dl mZmZmZmZ d dlmZ d dlmZm	Z	 d dl
mZ  e	dd      Zd Z G d	 d
ee         Z ej                  e        ej                  e        ej                  e        ej                  e       d Zd Z e e             Zy)    )	ContainerIterableSizedHashable)reduce)GenericTypeVar)pmapT_coT)	covariantc                 L    | j                  || j                  |d      dz         S )Nr      )setget)counterselements     2/usr/lib/python3/dist-packages/pyrsistent/_pbag.py_add_to_countersr   	   s#    <<gq!9A!=>>    c                       e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd ZeZeZeZd Zd Zd Zd Zd Zy)PBaga  
    A persistent bag/multiset type.

    Requires elements to be hashable, and allows duplicates, but has no
    ordering. Bags are hashable.

    Do not instantiate directly, instead use the factory functions :py:func:`b`
    or :py:func:`pbag` to create an instance.

    Some examples:

    >>> s = pbag([1, 2, 3, 1])
    >>> s2 = s.add(4)
    >>> s3 = s2.remove(1)
    >>> s
    pbag([1, 1, 2, 3])
    >>> s2
    pbag([1, 1, 2, 3, 4])
    >>> s3
    pbag([1, 2, 3, 4])
    )_counts__weakref__c                     || _         y )Nr   )selfcountss     r   __init__zPBag.__init__&   s	    r   c                 @    t        t        | j                  |            S )z
        Add an element to the bag.

        >>> s = pbag([1])
        >>> s2 = s.add(1)
        >>> s3 = s.add(2)
        >>> s2
        pbag([1, 1])
        >>> s3
        pbag([1, 2])
        )r   r   r   r   r   s     r   addzPBag.add)   s     $T\\7;<<r   c                 R    |r$t        t        t        || j                              S | S )z
        Update bag with all elements in iterable.

        >>> s = pbag([1])
        >>> s.update([1, 2])
        pbag([1, 1, 2])
        )r   r   r   r   )r   iterables     r   updatezPBag.update7   s%     /4<<HIIr   c                    || j                   vrt        |      | j                   |   dk(  r&| j                   j                  |      }t	        |      S | j                   j                  || j                   |   dz
        }t	        |      S )z
        Remove an element from the bag.

        >>> s = pbag([1, 1, 2])
        >>> s2 = s.remove(1)
        >>> s3 = s.remove(2)
        >>> s2
        pbag([1, 2])
        >>> s3
        pbag([1, 1])
        r   )r   KeyErrorremover   r   )r   r   newcs      r   r'   zPBag.removeD   s|     $,,&7##\\'"a'<<&&w/D Dz <<##GT\\'-BQ-FGDDzr   c                 :    | j                   j                  |d      S )z
        Return the number of times an element appears.


        >>> pbag([]).count('non-existent')
        0
        >>> pbag([1, 1, 2]).count(1)
        2
        r   )r   r   r    s     r   countz
PBag.countX   s     ||++r   c                 H    t        | j                  j                               S )ze
        Return the length including duplicates.

        >>> len(pbag([1, 1, 2]))
        3
        )sumr   
itervaluesr   s    r   __len__zPBag.__len__d   s     4<<**,--r   c              #   x   K   | j                   j                         D ]  \  }}t        |      D ]  }|   yw)z
        Return an iterator of all elements, including duplicates.

        >>> list(pbag([1, 1, 2]))
        [1, 1, 2]
        >>> list(pbag([1, 2]))
        [1, 2]
        N)r   	iteritemsrange)r   eltr*   is       r   __iter__zPBag.__iter__m   s@      ,,002 	JC5\ 		s   8:c                     || j                   v S )z
        Check if an element is in the bag.

        >>> 1 in pbag([1, 1, 2])
        True
        >>> 0 in pbag([1, 2])
        False
        r   )r   r3   s     r   __contains__zPBag.__contains__z   s     dll""r   c                 6    dj                  t        |             S )Nz	pbag({0}))formatlistr.   s    r   __repr__zPBag.__repr__   s    !!$t*--r   c                 l    t        |      t        urt        d      | j                  |j                  k(  S )z
        Check if two bags are equivalent, honoring the number of duplicates,
        and ignoring insertion order.

        >>> pbag([1, 1, 2]) == pbag([1, 2])
        False
        >>> pbag([2, 1, 0]) == pbag([0, 1, 2])
        True
        z Can only compare PBag with PBags)typer   	TypeErrorr   r   others     r   __eq__zPBag.__eq__   s/     ;d">??||u}},,r   c                     t        d      )NzPBags are not orderable)r>   r?   s     r   __lt__zPBag.__lt__   s    122r   c                    t        |t              st        S | j                  j	                         }|j                  j                         D ]  \  }}| j                  |      |z   ||<    t        |j                               S )z
        Combine elements from two PBags.

        >>> pbag([1, 2, 2]) + pbag([2, 3, 3])
        pbag([1, 2, 2, 2, 3, 3])
        )
isinstancer   NotImplementedr   evolverr1   r*   
persistent)r   r@   resultelemother_counts        r   __add__zPBag.__add__   sr     %&!!%%'!&!8!8!: 	:D+::d+k9F4L	:F%%'((r   c                 B   t        |t              st        S | j                  j	                         }|j                  j                         D ]:  \  }}| j                  |      |z
  }|dkD  r|||<   %|| v s*|j                  |       < t        |j                               S )z
        Remove elements from one PBag that are present in another.

        >>> pbag([1, 2, 2, 2, 3]) - pbag([2, 3, 3, 4])
        pbag([1, 2, 2])
        r   )	rE   r   rF   r   rG   r1   r*   r'   rH   )r   r@   rI   rJ   rK   newcounts         r   __sub__zPBag.__sub__   s     %&!!%%'!&!8!8!: 	$D+zz$'+5H!|'td#	$ F%%'((r   c                    t        |t              st        S | j                  j	                         }|j                  j                         D ]'  \  }}| j                  |      }t        ||      }|||<   ) t        |j                               S )z
        Union: Keep elements that are present in either of two PBags.

        >>> pbag([1, 2, 2, 2]) | pbag([2, 3, 3])
        pbag([1, 2, 2, 2, 3, 3])
        )	rE   r   rF   r   rG   r1   r*   maxrH   )r   r@   rI   rJ   rK   r*   rN   s          r   __or__zPBag.__or__   s     %&!!%%'!&!8!8!: 	$D+JJt$E5+.H#F4L	$ F%%'((r   c                     t        |t              st        S t               j	                         }| j
                  j                         D ]+  \  }}t        ||j                  |            }|dkD  s'|||<   - t        |j                               S )z
        Intersection: Only keep elements that are present in both PBags.

        >>> pbag([1, 2, 2, 2]) & pbag([2, 3, 3])
        pbag([2])
        r   )
rE   r   rF   r
   rG   r   r1   minr*   rH   )r   r@   rI   rJ   r*   rN   s         r   __and__zPBag.__and__   s}     %&!!!<<113 	(KD%5%++d"34H!|'t	( F%%'((r   c                 ,    t        | j                        S )z
        Hash based on value of elements.

        >>> m = pmap({pbag([1, 2]): "it's here!"})
        >>> m[pbag([2, 1])]
        "it's here!"
        >>> pbag([1, 1, 2]) in m
        False
        )hashr   r.   s    r   __hash__zPBag.__hash__   s     DLL!!r   N)__name__
__module____qualname____doc__	__slots__r   r!   r$   r'   r*   r/   r5   r7   r;   rA   rC   __le____gt____ge__rL   rO   rR   rU   rX    r   r   r   r      so    , +I=(
,.	#.-3 FFF))$) ) 
"r   r   c                      t        |       S )z
    Construct a persistent bag.

    Takes an arbitrary number of arguments to insert into the new persistent
    bag.

    >>> b(1, 2, 3, 2)
    pbag([1, 2, 2, 3])
    )pbagelementss    r   brf      s     >r   c                 V    | st         S t        t        t        | t	                           S )z
    Convert an iterable to a persistent bag.

    Takes an iterable with elements to insert.

    >>> pbag([1, 2, 3, 2])
    pbag([1, 2, 2, 3])
    )_EMPTY_PBAGr   r   r   r
   rd   s    r   rc   rc      s$     '46:;;r   N)collections.abcr   r   r   r   	functoolsr   typingr   r	   pyrsistent._pmapr
   r   r   r   registerrf   rc   rh   ra   r   r   <module>rn      s    @ @  # !v&?\"74= \"~ 	  4    $  t    $ 
< 46lr   