ó
G8C\c           @   s  d  Z  d d l m Z d d l m Z d d l m Z m Z m Z d d l	 m
 Z
 y d d l m Z Wn e k
 r… d d l Z n Xd e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j e j f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s-   Sorted collections recipes implementations.

iÿÿÿÿ(   t   deepcopy(   t   count(   t   SortedKeyListt
   SortedDictt	   SortedSet(   t   recursive_repr(   t   abcNt   IndexableDictc           B   s   e  Z d  Z d „  Z RS(   sa  Dictionary that supports numerical indexing.

    Keys are numerically indexable using dict views. For example::

        >>> indexable_dict = IndexableDict.fromkeys('abcde')
        >>> keys = indexable_dict.keys()
        >>> sorted(keys[:]) == ['a', 'b', 'c', 'd', 'e']
        True

    The dict views support the sequence abstract base class.

    c         O   s    t  t |  ƒ j t | | Ž d  S(   N(   t   superR   t   __init__t   hash(   t   selft   argst   kwargs(    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR	   %   s    (   t   __name__t
   __module__t   __doc__R	   (    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR      s   t   IndexableSetc           B   s   e  Z d  Z d „  Z RS(   s#  Set that supports numerical indexing.

    Values are numerically indexable. For example::

        >>> indexable_set = IndexableSet('abcde')
        >>> sorted(indexable_set[:]) == ['a', 'b', 'c', 'd', 'e']
        True

    `IndexableSet` implements the sequence abstract base class.

    c         O   s#   t  t |  ƒ j d t | | Ž d  S(   Nt   key(   R   R   R	   R
   (   R   R   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR	   6   s    (   R   R   R   R	   (    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR   )   s   t   ItemSortedDictc           B   sG   e  Z d  Z d „  Z d „  Z d „  Z e Z d „  Z e Z d „  Z	 RS(   s1  Sorted dictionary with key-function support for item pairs.

    Requires key function callable specified as the first argument. The
    callable must accept two arguments, key and value, and return a value used
    to determine the sort order. For example::

        def multiply(key, value):
            return key * value
        mapping = ItemSortedDict(multiply, [(3, 2), (4, 1), (2, 5)])
        list(mapping) == [4, 3, 2]

    Above, the key/value item pairs are ordered by ``key * value`` according to
    the callable given as the first argument.

    c            sr   | r t  | d ƒ s t ‚ t | ƒ } | d ‰  ˆ _ ‡  ‡ f d †  } | | d <t t ˆ ƒ j | | Ž  d  S(   Ni    c            s   ˆ  |  ˆ |  ƒ S(   s-   Apply key function to (key, value) item pair.(    (   R   (   t   funcR   (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   key_funcN   s    (   t   callablet   AssertionErrort   listt   _funcR   R   R	   (   R   R   R   R   (    (   R   R   s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR	   J   s    
c         C   s9   | |  k r t  | ƒ ‚ n  |  j | ƒ |  j | ƒ d S(   s   ``del mapping[key]``N(   t   KeyErrort   _list_removet   _dict_delitem(   R   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __delitem__T   s    c         C   sJ   | |  k r) |  j  | ƒ |  j | ƒ n  |  j | | ƒ |  j | ƒ d S(   s   ``mapping[key] = value``N(   R   R   t   _dict_setitemt	   _list_add(   R   R   t   value(    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __setitem__[   s
    c         C   s   |  j  |  j t |  j ƒ  ƒ ƒ S(   s#   Return shallow copy of the mapping.(   t	   __class__R   t   itert   items(   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   copye   s    c            s/   ‡  f d †  |  j  ƒ  Dƒ } |  j |  j | ƒ S(   Nc         3   s   |  ] } t  | ˆ  ƒ Vq d  S(   N(   R    (   t   .0t   item(   t   memo(    s8   lib/python2.7/site-packages/sortedcollections/recipes.pys	   <genexpr>l   s    (   R$   R"   R   (   R   R(   R$   (    (   R(   s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __deepcopy__k   s    (
   R   R   R   R	   R   R!   t   _setitemR%   t   __copy__R)   (    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR   :   s   	
			t   ValueSortedDictc           B   sY   e  Z d  Z d „  Z d „  Z d „  Z e Z d „  Z e Z d „  Z	 e
 ƒ  d „  ƒ Z RS(   sJ  Sorted dictionary that maintains (key, value) item pairs sorted by value.

    - ``ValueSortedDict()`` -> new empty dictionary.

    - ``ValueSortedDict(mapping)`` -> new dictionary initialized from a mapping
      object's (key, value) pairs.

    - ``ValueSortedDict(iterable)`` -> new dictionary initialized as if via::

        d = ValueSortedDict()
        for k, v in iterable:
            d[k] = v

    - ``ValueSortedDict(**kwargs)`` -> new dictionary initialized with the
      name=value pairs in the keyword argument list.  For example::

        ValueSortedDict(one=1, two=2)

    An optional key function callable may be specified as the first
    argument. When so, the callable will be applied to the value of each item
    pair to determine the comparable for sort order as with Python's builtin
    ``sorted`` function.

    c            sº   t  | ƒ } | rR t | d ƒ rR | d ‰  ˆ _ ‡  ‡ f d †  } | | d <nK d  ˆ _ ‡ f d †  } | r | d d  k r | | d <n | j d | ƒ t t ˆ ƒ j | | Ž  d  S(   Ni    c            s   ˆ  ˆ |  ƒ S(   s)   Apply key function to ``mapping[value]``.(    (   R   (   R   R   (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR      s    c            s   ˆ  |  S(   s   Return mapping value for key.(    (   R   (   R   (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR   “   s    (   R   R   R   t   Nonet   insertR   R,   R	   (   R   R   R   R   (    (   R   R   s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR	   ‰   s    	c         C   s9   | |  k r t  | ƒ ‚ n  |  j | ƒ |  j | ƒ d S(   s   ``del mapping[key]``N(   R   R   R   (   R   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR   œ   s    c         C   sJ   | |  k r) |  j  | ƒ |  j | ƒ n  |  j | | ƒ |  j | ƒ d S(   s   ``mapping[key] = value``N(   R   R   R   R   (   R   R   R    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR!   £   s
    c         C   s   |  j  |  j t |  j ƒ  ƒ ƒ S(   s#   Return shallow copy of the mapping.(   R"   R   R#   R$   (   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR%   ­   s    c         C   sB   g  |  j  D] } | |  | f ^ q
 } |  j | f } |  j | f S(   N(   t   _listR   R"   (   R   R   R$   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt
   __reduce__³   s    &c            sJ   d } d j  ‡  f d †  ˆ  j Dƒ ƒ } | j ˆ  j j t ˆ  j ƒ | ƒ S(   Ns   {0}({1}, {{{2}}})s   , c         3   s1   |  ]' } d  j  t | ƒ t ˆ  | ƒ ƒ Vq d S(   s   {0}: {1}N(   t   formatt   repr(   R&   R   (   R   (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pys	   <genexpr>»   s   (   t   joinR/   R1   R"   R   R2   R   (   R   t   tempR$   (    (   R   s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __repr__¸   s    	(   R   R   R   R	   R   R!   R*   R%   R+   R0   R   R5   (    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR,   p   s   					t
   OrderedSetc           B   sw   e  Z d  Z d d „ Z d „  Z e Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z e Z RS(   sy  Like OrderedDict, OrderedSet maintains the insertion order of elements.

    For example::

        >>> ordered_set = OrderedSet('abcde')
        >>> list(ordered_set) == list('abcde')
        True
        >>> ordered_set = OrderedSet('edcba')
        >>> list(ordered_set) == list('edcba')
        True

    OrderedSet also implements the collections.Sequence interface.

    c         C   sA   i  |  _  t ƒ  |  _ |  j j ƒ  |  _ t ƒ  |  _ |  | O}  d  S(   N(   t   _keysR   t   _numst   keyst
   _keys_viewR   t   _count(   R   t   iterable(    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR	   Ô   s
    	c         C   s   | |  j  k S(   s   ``key in ordered_set``(   R7   (   R   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __contains__Ü   s    c         C   s   t  |  j j ƒ  ƒ S(   s   ``iter(ordered_set)``(   R#   R8   t   values(   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __iter__â   s    c         c   s-   |  j  } x t | ƒ D] } | | Vq Wd S(   s   ``reversed(ordered_set)``N(   R8   t   reversed(   R   R8   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __reversed__æ   s    	c         C   s   |  j  | } |  j | S(   s;   ``ordered_set[index]`` -> element; lookup element at index.(   R:   R8   (   R   t   indext   num(    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __getitem__ì   s    c         C   s   t  |  j ƒ S(   s   ``len(ordered_set)``(   t   lenR7   (   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   __len__ñ   s    c         C   sF   y |  j  | SWn0 t k
 rA t d | t |  ƒ j f ƒ ‚ n Xd S(   s   Return index of value.s   %r is not in %sN(   R7   R   t
   ValueErrort   typeR   (   R   R    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyRB   õ   s    c         C   s?   | |  j  k r; t |  j ƒ } | |  j  | <| |  j | <n  d S(   s   Add element, value, to set.N(   R7   t   nextR;   R8   (   R   R    RC   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   addý   s    c         C   s2   |  j  j | d ƒ } | d k	 r. |  j | =n  d S(   s2   Remove element, value, from set if it is a member.N(   R7   t   popR-   R8   (   R   R    RC   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   discard  s    c         C   s   d t  |  ƒ j t |  ƒ f S(   s   Text representation of set.s   %s(%r)(   RH   R   R   (   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR5   
  s    (    (   R   R   R   R	   R=   R   R?   RA   RD   RF   RB   RJ   RL   R5   t   __str__(    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR6   Ä   s   									t   SegmentListc           B   sª   e  Z d  Z d
 d „ Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d e d „ Z d	 „  Z e Z e Z e Z e Z e Z e Z e Z e Z e Z e Z RS(   s  List that supports fast random insertion and deletion of elements.

    SegmentList is a special case of a SortedList initialized with a key
    function that always returns 0. As such, several SortedList methods are not
    implemented for SegmentList.

    c         C   s    t  t |  ƒ j | |  j ƒ d  S(   N(   R   RN   R	   t   zero(   R   R<   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR	     s    c         C   s   d S(   s	   Return 0.i    (    (   t   _(    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyRO     s    c         C   sB   t  | t ƒ r t ‚ n  |  j | ƒ \ } } | |  j | | <d  S(   N(   t
   isinstancet   slicet   NotImplementedErrort   _post   _lists(   R   RB   R    t   post   idx(    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR!   "  s    	c         C   s   |  j  rT t |  j ƒ d } |  j | j | ƒ |  j | j d ƒ |  j | ƒ n6 |  j j | g ƒ |  j j d g ƒ |  j j d ƒ |  j  d 7_  d  S(   Ni   i    (   t   _lenRE   RU   t   appendR7   t   _expandt   _maxes(   R   R    RV   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyRY   (  s    	c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   RY   (   R   R>   R    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   extend4  s    c         C   sƒ   | |  j  k r  |  j | ƒ d  S|  j | ƒ \ } } |  j | j | | ƒ |  j | j | d ƒ |  j | ƒ |  j  d 7_  d  S(   Ni    i   (   RX   RY   RT   RU   R.   R7   RZ   (   R   RB   R    RV   RW   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR.   8  s    c         C   s1   t  |  ƒ } | j ƒ  |  j ƒ  |  j | ƒ d  S(   N(   R   t   reverset   clearR\   (   R   R>   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyR]   B  s    

c         C   s3   t  |  d | d | ƒ} |  j ƒ  |  j | ƒ d S(   s   Stable sort in place.R   R]   N(   t   sortedR^   R\   (   R   R   R]   R>   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   sortH  s    
c         O   s
   t  ‚ d S(   s   Not implemented.N(   RS   (   R   R   R   (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   _not_implementedN  s    (    N(   R   R   R   R	   t   staticmethodRO   R!   RY   R\   R.   R]   R-   t   FalseR`   Ra   RJ   t   bisectt   bisect_leftt   bisect_rightt
   bisect_keyt   bisect_key_leftt   bisect_key_rightt   iranget
   irange_keyt   update(    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyRN     s(   				
		(   R   R%   R    t	   itertoolsR   t   sortedcontainersR   R   R   t   sortedcontainers.sortedlistR   t   collectionsR   t   ImportErrorR   R   R   R,   t
   MutableSett   SequenceR6   RN   (    (    (    s8   lib/python2.7/site-packages/sortedcollections/recipes.pyt   <module>   s   6TM