B
    ZE                 @   st   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ G dd	 d	eZed
krpddlZee  dS )zCompatibility module for collections.Counter for python < 2.7

Author: Raymond Hettinger
License: MIT License
http://code.activestate.com/recipes/576611/ , downloaded 2013-03-08

    )print_function   )filter	iteritems)
itemgetter)nlargest)repeatc               @   s   e Zd ZdZdddZdd ZdddZd	d
 Zedd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 )!CounterzDict 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})

    Nc             K   s   | j |f| dS )a	  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)update)selfiterablekwds r   9lib/python3.7/site-packages/statsmodels/compat/counter.py__init__   s    zCounter.__init__c             C   s   dS )Nr   r   )r   keyr   r   r   __missing__&   s    zCounter.__missing__c             C   s4   |dkrt t| tdddS t|t| tddS )zList 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)]

        Nr   T)r   reverse)r   )sortedr   r   r   )r   nr   r   r   most_common)   s    zCounter.most_commonc             c   s4   x.t | D ]"\}}xtd|D ]
}|V  qW q
W dS )a&  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   elemcount_r   r   r   elements5   s    zCounter.elementsc             C   s   t dd S )Nz@Counter.fromkeys() is undefined.  Use Counter(iterable) instead.)NotImplementedError)clsr   vr   r   r   fromkeysF   s    zCounter.fromkeysc             K   s   |dk	rzt |drT| rF| j}x4t|D ]\}}||d| | |< q&W qzt| | n&| j}x|D ]}||dd | |< q`W |r| | dS )a  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

        Nr   r   r   )hasattrgetr   dictr
   )r   r   r   self_getr   r   r   r   r   r
   K   s    

zCounter.updatec             C   s   t | S )zBLike dict.copy() but returns a Counter instance instead of a dict.)r	   )r   r   r   r   copyg   s    zCounter.copyc             C   s   || krt | | dS )zGLike dict.__delitem__() but does not raise KeyError for missing values.N)r!   __delitem__)r   r   r   r   r   r$   k   s    zCounter.__delitem__c             C   s6   | sd| j j S dtdj|  }d| j j|f S )Nz%s()z, z%r: %rz%s({%s}))	__class____name__joinmap__mod__r   )r   itemsr   r   r   __repr__p   s    zCounter.__repr__c             C   sR   t |tstS t }x8t| t|B D ]$}| | ||  }|dkr&|||< q&W |S )zAdd counts from two counters.

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


        r   )
isinstancer	   NotImplementedset)r   otherresultr   newcountr   r   r   __add__   s    
zCounter.__add__c             C   sR   t |tstS t }x8t| t|B D ]$}| | ||  }|dkr&|||< q&W |S )z Subtract count, but keep only results with positive counts.

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

        r   )r,   r	   r-   r.   )r   r/   r0   r   r1   r   r   r   __sub__   s    
zCounter.__sub__c             C   sX   t |tstS t}t }x:t| t|B D ]&}|| | || }|dkr*|||< q*W |S )zUnion is the maximum of value in either of the input counters.

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

        r   )r,   r	   r-   maxr.   )r   r/   Z_maxr0   r   r1   r   r   r   __or__   s    
zCounter.__or__c             C   sn   t |tstS t}t }t| t|k r2||  } }x6t| j|D ]&}|| | || }|dkr@|||< q@W |S )z Intersection is the minimum of corresponding counts.

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

        r   )r,   r	   r-   minlenr   __contains__)r   r/   Z_minr0   r   r1   r   r   r   __and__   s    

zCounter.__and__)N)N)N)N)r&   
__module____qualname____doc__r   r   r   r   classmethodr   r
   r#   r$   r+   r2   r3   r5   r9   r   r   r   r   r	      s   


r	   __main__N)r<   Z
__future__r   Zpythonr   r   operatorr   heapqr   	itertoolsr   r!   r	   r&   ZdoctestprintZtestmodr   r   r   r   <module>   s    7