ó
gbc           @   sv   d  d l  m Z d  d l m Z d  d l m Z m Z d e f d „  ƒ  YZ e	 d k rr d  d l
 Z
 e
 j ƒ  GHn  d S(   iÿÿÿÿ(   t
   itemgetter(   t   nlargest(   t   repeatt   ifiltert   Counterc           B   s•   e  Z d  Z d d „ Z d „  Z d d „ Z d „  Z e d d „ ƒ Z	 d d „ Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   sú   Dict subclass for counting hashable objects.  Sometimes called a bag
    or multiset.  Elements are stored as dictionary keys and their counts
    are stored as dictionary values.

    >>> Counter('zyzygy')
    Counter({'y': 3, 'z': 2, 'g': 1})

    c         K   s   |  j  | |  d S(   s	  Create a new, empty Counter object.  And if given, count elements
        from an input iterable.  Or, initialize the count from another mapping
        of elements to their counts.

        >>> c = Counter()                           # a new, empty counter
        >>> c = Counter('gallahad')                 # a new counter from an iterable
        >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
        >>> c = Counter(a=4, b=2)                   # a new counter from keyword args

        N(   t   update(   t   selft   iterablet   kwds(    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   __init__   s    c         C   s   d S(   Ni    (    (   R   t   key(    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   __missing__   s    c         C   sM   | d k r. t |  j ƒ  d t d ƒ d t ƒSt | |  j ƒ  d t d ƒ ƒS(   sì   List the n most common elements and their counts from the most
        common to the least.  If n is None, then list all element counts.

        >>> Counter('abracadabra').most_common(3)
        [('a', 5), ('r', 2), ('b', 2)]

        R
   i   t   reverseN(   t   Nonet   sortedt	   iteritemsR    t   TrueR   (   R   t   n(    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   most_common   s    "c         c   s@   x9 |  j  ƒ  D]+ \ } } x t d | ƒ D] } | Vq) Wq Wd S(   s&  Iterator over elements repeating each as many times as its count.

        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        If an element's count has been set to zero or is a negative number,
        elements() will ignore it.

        N(   R   R   R   (   R   t   elemt   countt   _(    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   elements+   s    c         C   s   t  d ƒ ‚ d  S(   Ns@   Counter.fromkeys() is undefined.  Use Counter(iterable) instead.(   t   NotImplementedError(   t   clsR   t   v(    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   fromkeys<   s    c         K   sÂ   | d k	 r¨ t | d ƒ rt |  ra |  j } xD | j ƒ  D]# \ } } | | d ƒ | |  | <q7 Wq¥ t j |  | ƒ q¨ |  j } x( | D] } | | d ƒ d |  | <q„ Wn  | r¾ |  j | ƒ n  d S(   sÉ  Like dict.update() but add counts instead of replacing them.

        Source can be an iterable, a dictionary, or another Counter instance.

        >>> c = Counter('which')
        >>> c.update('witch')           # add elements from another iterable
        >>> d = Counter('watch')
        >>> c.update(d)                 # add elements from another counter
        >>> c['h']                      # four 'h' in which, witch, and watch
        4

        R   i    i   N(   R   t   hasattrt   getR   t   dictR   (   R   R   R   t   self_getR   R   (    (    s3   lib/python2.7/site-packages/obitools/collections.pyR   A   s    		c         C   s
   t  |  ƒ S(   sB   Like dict.copy() but returns a Counter instance instead of a dict.(   R   (   R   (    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   copy]   s    c         C   s#   | |  k r t  j |  | ƒ n  d S(   sG   Like dict.__delitem__() but does not raise KeyError for missing values.N(   R   t   __delitem__(   R   R   (    (    s3   lib/python2.7/site-packages/obitools/collections.pyR    a   s    c         C   sI   |  s d |  j  j Sd j t d j |  j ƒ  ƒ ƒ } d |  j  j | f S(   Ns   %s()s   , s   %r: %rs   %s({%s})(   t	   __class__t   __name__t   joint   mapt   __mod__R   (   R   t   items(    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   __repr__f   s    !c         C   sl   t  | t ƒ s t St ƒ  } xI t |  ƒ t | ƒ BD]1 } |  | | | } | d k r3 | | | <q3 q3 W| S(   s€   Add counts from two counters.

        >>> Counter('abbb') + Counter('bcc')
        Counter({'b': 4, 'c': 2, 'a': 1})


        i    (   t
   isinstanceR   t   NotImplementedt   set(   R   t   othert   resultR   t   newcount(    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   __add__u   s    	c         C   sl   t  | t ƒ s t St ƒ  } xI t |  ƒ t | ƒ BD]1 } |  | | | } | d k r3 | | | <q3 q3 W| S(   s˜    Subtract count, but keep only results with positive counts.

        >>> Counter('abbbc') - Counter('bccd')
        Counter({'b': 2, 'a': 1})

        i    (   R(   R   R)   R*   (   R   R+   R,   R   R-   (    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   __sub__†   s    	c         C   sw   t  | t ƒ s t St } t ƒ  } xN t |  ƒ t | ƒ BD]6 } | |  | | | ƒ } | d k r9 | | | <q9 q9 W| S(   s    Union is the maximum of value in either of the input counters.

        >>> Counter('abbb') | Counter('bcc')
        Counter({'b': 3, 'c': 2, 'a': 1})

        i    (   R(   R   R)   t   maxR*   (   R   R+   t   _maxR,   R   R-   (    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   __or__–   s    	c         C   s›   t  | t ƒ s t St } t ƒ  } t |  ƒ t | ƒ k  rJ | |  }  } n  xJ t |  j | ƒ D]6 } | |  | | | ƒ } | d k r] | | | <q] q] W| S(   s‡    Intersection is the minimum of corresponding counts.

        >>> Counter('abbb') & Counter('bcc')
        Counter({'b': 1})

        i    (   R(   R   R)   t   mint   lenR   t   __contains__(   R   R+   t   _minR,   R   R-   (    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   __and__§   s    	N(   R"   t
   __module__t   __doc__R   R	   R   R   R   t   classmethodR   R   R   R    R'   R.   R/   R2   R7   (    (    (    s3   lib/python2.7/site-packages/obitools/collections.pyR      s   								t   __main__N(   t   operatorR    t   heapqR   t	   itertoolsR   R   R   R   R"   t   doctestt   testmod(    (    (    s3   lib/python2.7/site-packages/obitools/collections.pyt   <module>   s   ¶