ó
åŽ‹]c           @   sº   d  d l  m Z m Z m Z m Z d d l m Z d d l m Z d „  Z	 d e
 f d „  ƒ  YZ e j e ƒ e j e ƒ e j e ƒ e j e ƒ d „  Z d	 „  Z e e ƒ  ƒ Z d
 S(   i   (   t	   Containert   Iterablet   Sizedt   Hashableiÿÿÿÿ(   t   reduce(   t   pmapc         C   s    |  j  | |  j | d ƒ d ƒ S(   Ni    i   (   t   sett   get(   t   counterst   element(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   _add_to_counters   s    t   PBagc           B   s¶   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 e Z e Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÞ  
    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])
    t   _countst   __weakref__c         C   s   | |  _  d  S(   N(   R   (   t   selft   counts(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __init__#   s    c         C   s   t  t |  j | ƒ ƒ S(   sÃ   
        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/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   add&   s    c         C   s#   | r t  t t | |  j ƒ ƒ S|  S(   s‹   
        Update bag with all elements in iterable.

        >>> s = pbag([1])
        >>> s.update([1, 2])
        pbag([1, 1, 2])
        (   R   R   R
   R   (   R   t   iterable(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   update4   s    c         C   sp   | |  j  k r t | ƒ ‚ nH |  j  | d k rF |  j  j | ƒ } n  |  j  j | |  j  | d ƒ } t | ƒ S(   sÔ   
        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])
        i   (   R   t   KeyErrort   removeR   R   (   R   R	   t   newc(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyR   A   s     c         C   s   |  j  j | d ƒ S(   s¦   
        Return the number of times an element appears.


        >>> pbag([]).count('non-existent')
        0
        >>> pbag([1, 1, 2]).count(1)
        2
        i    (   R   R   (   R   R	   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   countU   s    
c         C   s   t  |  j j ƒ  ƒ S(   se   
        Return the length including duplicates.

        >>> len(pbag([1, 1, 2]))
        3
        (   t   sumR   t
   itervalues(   R   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __len__a   s    c         c   s@   x9 |  j  j ƒ  D]( \ } } x t | ƒ D] } | Vq) Wq Wd S(   s®   
        Return an iterator of all elements, including duplicates.

        >>> list(pbag([1, 1, 2]))
        [1, 1, 2]
        >>> list(pbag([1, 2]))
        [1, 2]
        N(   R   t	   iteritemst   range(   R   t   eltR   t   i(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __iter__j   s    	c         C   s   | |  j  k S(   s   
        Check if an element is in the bag.

        >>> 1 in pbag([1, 1, 2])
        True
        >>> 0 in pbag([1, 2])
        False
        (   R   (   R   R   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __contains__w   s    	c         C   s   d j  t |  ƒ ƒ S(   Ns	   pbag({0})(   t   formatt   list(   R   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __repr__‚   s    c         C   s1   t  | ƒ t k	 r! t d ƒ ‚ n  |  j | j k S(   só   
        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
        s    Can only compare PBag with PBags(   t   typeR   t	   TypeErrorR   (   R   t   other(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __eq__…   s    
c         C   s   t  d ƒ ‚ d  S(   Ns   PBags are not orderable(   R%   (   R   R&   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __lt__“   s    c         C   si   t  | t ƒ s t S|  j j ƒ  } x4 | j j ƒ  D]# \ } } |  j | ƒ | | | <q2 Wt | j ƒ  ƒ S(   sƒ    
        Combine elements from two PBags.

        >>> pbag([1, 2, 2]) + pbag([2, 3, 3])
        pbag([1, 2, 2, 2, 3, 3])
        (   t
   isinstanceR   t   NotImplementedR   t   evolverR   R   t
   persistent(   R   R&   t   resultt   elemt   other_count(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __add__œ   s    c         C   sš   t  | t ƒ s t S|  j j ƒ  } xe | j j ƒ  D]T \ } } |  j | ƒ | } | d k rj | | | <q2 | |  k r2 | j | ƒ q2 q2 Wt | j ƒ  ƒ S(   s    
        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])
        i    (	   R)   R   R*   R   R+   R   R   R   R,   (   R   R&   R-   R.   R/   t   newcount(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __sub__ª   s    c         C   sz   t  | t ƒ s t S|  j j ƒ  } xE | j j ƒ  D]4 \ } } |  j | ƒ } t | | ƒ } | | | <q2 Wt | j ƒ  ƒ S(   s£    
        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])
        (	   R)   R   R*   R   R+   R   R   t   maxR,   (   R   R&   R-   R.   R/   R   R1   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __or__¼   s    c         C   sƒ   t  | t ƒ s t St ƒ  j ƒ  } xN |  j j ƒ  D]= \ } } t | | j | ƒ ƒ } | d k r2 | | | <q2 q2 Wt | j	 ƒ  ƒ S(   sž   
        Intersection: Only keep elements that are present in both PBags.
        
        >>> pbag([1, 2, 2, 2]) & pbag([2, 3, 3])
        pbag([2])
        i    (
   R)   R   R*   R   R+   R   R   t   minR   R,   (   R   R&   R-   R.   R   R1   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __and__Ì   s    c         C   s   t  |  j ƒ S(   sÆ   
        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
        (   t   hashR   (   R   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   __hash__Ü   s    
(   R   R   (   t   __name__t
   __module__t   __doc__t	   __slots__R   R   R   R   R   R   R   R    R#   R'   R(   t   __le__t   __gt__t   __ge__R0   R2   R4   R6   R8   (    (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyR   
   s*   																c          G   s
   t  |  ƒ S(   sª   
    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])
    (   t   pbag(   t   elements(    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   bï   s    
c         C   s#   |  s
 t  St t t |  t ƒ  ƒ ƒ S(   s•   
    Convert an iterable to a persistent bag.

    Takes an iterable with elements to insert.

    >>> pbag([1, 2, 3, 2])
    pbag([1, 2, 2, 3])
    (   t   _EMPTY_PBAGR   R   R
   R   (   RA   (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyR@   ü   s    	N(   t   _compatR    R   R   R   t	   functoolsR   t   pyrsistent._pmapR   R
   t   objectR   t   registerRB   R@   RC   (    (    (    s/   lib/python2.7/site-packages/pyrsistent/_pbag.pyt   <module>   s   "	ß		