
    :Qg2                     H   d dl Z d dl Zd dlmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZ  ed      Z ed      Zeee   ee   f   Zeee   ee   ee   f   ZdedefdZ G d de	e   ee         Z G d	 d
ee         Z G d dee         Z G d dee         Z G d d      Zy)    N)AbstractSetAnyDictIterableIteratorList
MutableSetOptionalSequenceSetTypeVarUnionoverloadTobjreturnc                 .    t        | t        t        f      S )ay  
    Returns True for objects which are iterable but should not be iterated in
    the context of indexing a StableSet or an OrderedSet.

    When we index by an iterable, usually that means we're being asked to look
    up a list of things.

    However, in the case of the .index() method, we shouldn't handle strings
    and tuples like other iterables. They're not sequences of things to look
    up, they're the single, atomic thing we're trying to find.

    As an example, oset.index('hello') should give the index of 'hello' in an
    StableSet of strings. It shouldn't give the indexes of each individual
    character.
    )
isinstancestrtuple)r   s    M/var/www/html/answerous/venv/lib/python3.12/site-packages/orderly_set/sets.py
_is_atomicr      s      cC<((    c                      e Zd ZU dZdZeeef   ed<   d;de	e
e      fdZdefdZed	edd
fd       Zed	ee   dee   fd       Zed	edefd       Zd Zd Zd ZdedefdZdee   fdZdee   fdZdefdZeZdee   dd
fdZd Zd Z d Z!d Z"d Z#d Z$d Z%d<dZ&d=d Z'dedefd!Z(e(Z)d"ee   defd#Z*edee   dee   fd$       Z+ededefd%       Z+d& Z+e+Z,e+Z-d>d	edefd'Z.d?d(efd)Z/d<d*Z0deddfd+Z1d,ee   dd
fd-Z2d,ee   dd
fd.Z3d,ee   dd
fd/Z4dee   dd
fd0Z5d,ee   ddfd1Z6dee   ddfd2Z7dee   ddfd3Z8dee   defd4Z9dee   defd5Z:d@d6eded7efd8Z;d@ded7efd9Z<d: Z=y)A	StableSeta  
    A StableSet is a custom MutableSet that remembers its insertion order.
    Featuring: Fast O(1) insertion, deletion, iteration and membership testing.
    But slow O(N) Index Lookup.

    StableSet is meant to be a drop-in replacement for `set` when iteration in insertion order
    is the only additional requirement over the built-in `set`.

    Equality: StableSet, like `set` and `dict_keys` [dict.keys()], and unlike OrderdSet,
    disregards the items order when checking equality.
    Like `set` it may be equal only to other instances of AbstractSet
    (like `set`, `dict_keys` or StableSet).

    This implementation of StableSet is based on the built-in dict type.
    In Python 3.6 and later, the built-in dict type is inherently ordered.
    If you ignore the dictionary values, that also gives you a simple ordered set,
    with fast O(1) insertion, deletion, iteration and membership testing.
    However, dict does not provide the list-like random access features of StableSet.
    So we have to convert it to a list in O(N) to look up the index of an entry
    or look up an entry by its index.

    Example:
        >>> StableSet([1, 1, 2, 3, 2])
        StableSet([1, 2, 3])
    )_mapr   Ninitialc                 L    |rt         j                  |      | _        y i | _        y N)dictfromkeysr   selfr   s     r   __init__zStableSet.__init__O   s    .5DMM'*	2	r   r   c                 6    | j                   j                         S )z
        Returns the number of unique elements in the ordered set

        Example:
            >>> len(StableSet([]))
            0
            >>> len(StableSet([1, 2]))
            2
        )r   __len__r#   s    r   r&   zStableSet.__len__R   s     yy  ""r   indexStableSet[T]c                      y r    r#   r(   s     r   __getitem__zStableSet.__getitem__^       r   c                      y r   r+   r,   s     r   r-   zStableSet.__getitem__b   r.   r   c                      y r   r+   r,   s     r   r-   zStableSet.__getitem__f   r.   r   c                    t        |t              rY|dk  rt        | j                        |z   }	 t	        t        j                  | j                  j                         ||dz               S t        |t              r|t        k(  r| j                         S t        | j                  j                               }t        |t              r|D cg c]  }||   	 c}S t        |t              st        |d      r(||   }t        |t              r| j!                  |      S |S t#        d|       # t        $ r t        d| d      w xY wc c}w )aM  
        Get the item at a given index.

        If `index` is a slice, you will get back that slice of items, as a
        new StableSet.

        If `index` is a list or a similar iterable, you'll get a list of
        items corresponding to those indices. This is similar to NumPy's
        "fancy indexing". The result is not a StableSet because you may ask
        for duplicate indices, and the number of elements returned should be
        the number of elements asked for.

        Example:
            >>> oset = StableSet([1, 2, 3])
            >>> oset[1]
            2
        r      zindex z out of range	__index__z'Don't know how to index a StableSet by )r   intlenr   next	itertoolsislicekeysStopIteration
IndexErrorslice	SLICE_ALLcopylistr   hasattr	__class__	TypeErrorr#   r(   itemsiresults        r   r-   zStableSet.__getitem__k   s!   $ eS!qyDII.@I,,TYY^^-=ueaiPQQ u%%9*<99;TYY^^%&eX&&+,E!H,,u%)D5\F&$'~~f--EeWMNN ! @ 6%!>??@ -s   :D+ 
E+Ec                 6    t        |       dk(  ryt        |       S )Nr   r   )r5   r?   r'   s    r   __getstate__zStableSet.__getstate__   s    t9> :r   c                 T    |dk(  r| j                  g        y | j                  |       y )Nr   )r$   )r#   states     r   __setstate__zStableSet.__setstate__   s"    GMM"MM% r   keyc                 8    | j                   j                  |      S )z
        Test if the item is in this ordered set.

        Example:
            >>> 1 in StableSet([1, 3, 2])
            True
            >>> 5 in StableSet([1, 3, 2])
            False
        )r   __contains__r#   rL   s     r   rN   zStableSet.__contains__   s     yy%%c**r   c                 R    | j                   j                         j                         S )za
        Example:
            >>> list(iter(StableSet([1, 2, 3])))
            [1, 2, 3]
        )r   r9   __iter__r'   s    r   rQ   zStableSet.__iter__   s     yy~~((**r   c                 H    t        | j                  j                               S )z
        Supported from Python >= 3.8
        Example:
            >>> list(reversed(StableSet([1, 2, 3])))
            [3, 2, 1]
        )reversedr   r9   r'   s    r   __reversed__zStableSet.__reversed__   s     		())r   c                     | s| j                   j                   dS | j                   j                   dt        |       dS Nz()())rA   __name__r?   r'   s    r   __repr__zStableSet.__repr__   s?    nn--.b11..))*!DJ>;;r   otherc                 $    | j                  |      S r   )intersectionr#   r[   s     r   __and__zStableSet.__and__   s      ''r   c                     t        t        | t              r| nt        t              rnt              }t        t              st        t              st
        S  |       |fd| D              S )Nc              3   ,   K   | ]  }|vs|  y wr   r+   ).0valuer[   s     r   	<genexpr>z$StableSet.__sub__.<locals>.<genexpr>   s     AUe5.@5A   	typer   r   r   r   NotImplementedr#   r[   clss    ` r   __sub__zStableSet.__sub__   sf    $	*  %+ 
 %%eX.%%JEAdAAAr   c                      t        t         t              r nt        |t              r|nt              }t        |t              st        |t              st
        S  ||      } | fd|D              S )Nc              3   ,   K   | ]  }|vs|  y wr   r+   )rb   rc   r#   s     r   rd   z%StableSet.__rsub__.<locals>.<genexpr>   s     AUuD/@5Are   rf   ri   s   `  r   __rsub__zStableSet.__rsub__   sf    $	*  %+ 
 %%eX.%%JEAeAAAr   c                     t        t        | t              r| nt        |t              r|nt              }t        |t              st        S d | |fD        } ||      S )Nc              3   .   K   | ]  }|D ]  }|   y wr   r+   rb   ses      r   rd   z#StableSet.__or__.<locals>.<genexpr>        5q15a55   rg   r   r   r   rh   r#   r[   rj   chains       r   __or__zStableSet.__or__   sY    $	*  %+ 
 %*!!5T5M55zr   c                     t        t        | t              r| nt        |t              r|nt              }t        |t              st        S d || fD        } ||      S )Nc              3   .   K   | ]  }|D ]  }|   y wr   r+   rq   s      r   rd   z$StableSet.__ror__.<locals>.<genexpr>	  rt   ru   rv   rw   s       r   __ror__zStableSet.__ror__   sY    $	*  %+ 
 %*!!5UDM55zr   c                 D    t        |t              st        S | |z
  || z
  z  S r   r   r   rh   r^   s     r   __xor__zStableSet.__xor__  s%    %*!!u..r   c                 D    t        |t              st        S || z
  | |z
  z  S r   r~   r^   s     r   __rxor__zStableSet.__rxor__  s%    %*!!..r   c                 <   t        |t              syt        | j                        t        |      k7  ryt        |t              r| j                  |j                  k(  S t        |t
              st        |      }t        | j                  j                               |k(  S NF)r   r   r5   r   r   r?   r9   r^   s     r   __eq__zStableSet.__eq__  sq    %*tyy>SZ'eY'99

**%&KEDIINN$%..r   c                 8    | j                   j                          y)z7
        Remove all items from this StableSet.
        N)r   clearr'   s    r   r   zStableSet.clear!  s     			r   c                 $    | j                  |       S )z
        Return a shallow copy of this object.

        Example:
            >>> this = StableSet([1, 2, 3])
            >>> other = this.copy()
            >>> this == other
            True
            >>> this is other
            False
        )rA   r'   s    r   r>   zStableSet.copy'  s     ~~d##r   c                 P    d| j                   |<   t        | j                         dz
  S )aA  
        Add `key` as an item to this StableSet, then return its index.

        If `key` is already in the StableSet, return the index it already
        had.

        Example:
            >>> oset = StableSet()
            >>> oset.append(3)
            0
            >>> print(oset)
            StableSet([3])
        Nr2   )r   r5   rO   s     r   addzStableSet.add8  s$     		#499~!!r   sequencec                     t         j                  |      }| j                  j                  |       t	        | j                        dz
  S )a:  
        Update the set with the given iterable sequence, then return the index
        of the last element inserted.

        Example:
            >>> oset = StableSet([1, 2, 3])
            >>> oset.update([3, 1, 5, 1, 4])
            4
            >>> print(oset)
            StableSet([1, 2, 3, 5, 4])
        r2   )r    r!   r   updater5   )r#   r   	other_maps      r   r   zStableSet.updateK  s7     MM(+			#499~!!r   c                      y r   r+   rO   s     r   r(   zStableSet.index[  r.   r   c                      y r   r+   rO   s     r   r(   zStableSet.index_  r.   r   c                 0   	 t        |t              r*t        |      s|D cg c]  }| j                  |       c}S t	        | j
                  j                               D ]  \  }}||k(  s|c S  t        |      c c}w # t        $ r t        |      w xY wa>  
        Get the index of a given entry, raising an IndexError if it's not present

        `key` can be an iterable of entries that is not a string, in which case
        this returns a list of indices.

        Example:
            >>> oset = StableSet([1, 2, 3])
            >>> oset.index(2)
            1
        )	r   r   r   r(   	enumerater   r9   KeyError
ValueError)r#   rL   subkeyr(   items        r   r(   zStableSet.indexd  s    		 #x(C9<=v

6*==()9: !t3; L! 3-	 >  	 3-	 s&   B  A;B  .B  +B  /B   Bc                    | j                   st        d      |dk(  r| j                   j                         \  }}|S |dk(  r-t        t	        | j                   j                                     }n;t        t        j                  | j                   j                         ||dz               }| j                   j                  |       |S )a
  
        Remove and return item at index (default last).

        Raises KeyError if the set is empty.
        Raises IndexError if index is out of range.

        Example:
            >>> oset = StableSet([1, 2, 3])
            >>> oset.pop()
            3
        Set is emptyr   r2   )	r   r   popitemr6   iterr9   r7   r8   pop)r#   r(   elem_s       r   r   zStableSet.pop  s     yy>**B;ii'')GD!KaZTYY^^-./D	(()95%!)LMD		dr   lastc                    | j                   st        d      |r| j                   j                         \  }}|S t        t	        | j                   j                                     }| j                   j                  |       |S )zRemove and return an item from the set.
        Items are returned in LIFO order if last is true or FIFO order if false.
        r   )r   r   r   r6   r   r9   r   )r#   r   r   r   s       r   r   zStableSet.popitem  sc     yy>**ii'')GD!KD)*+		dr   c                 X    | j                   j                  |       d| j                   |<   y)zcMove an existing element to the end.
        Raise KeyError if the element does not exist.
        Nr   r   rO   s     r   move_to_endzStableSet.move_to_end  s!     			c		#r   c                 <    | j                   j                  |d       y)a  
        Remove an element.  Do not raise an exception if absent.

        The MutableSet mixin uses this to implement the .remove() method, which
        *does* raise an error when asked to remove a non-existent item.

        Example:
            >>> oset = StableSet([1, 2, 3])
            >>> oset.discard(2)
            >>> print(oset)
            StableSet([1, 3])
            >>> oset.discard(2)
            >>> print(oset)
            StableSet([1, 3])
        Nr   rO   s     r   discardzStableSet.discard  s    $ 			c4 r   setsc                     t        t        | t              r| nt              }t        t        t        j                  | g|            }t
        j                  j                  |      } ||      S )a  
        Combines all unique items.
        Each item order is defined by its first appearance.

        Example:
            >>> oset = StableSet.union(StableSet([3, 1, 4, 1, 5]), [1, 3], [2, 0])
            >>> print(oset)
            StableSet([3, 1, 4, 5, 2, 0])
            >>> oset.union([8, 9])
            StableSet([3, 1, 4, 5, 2, 0, 8, 9])
            >>> oset | {10}
            StableSet([3, 1, 4, 5, 2, 0, 10])
        )rg   r   r   mapr?   itrx   from_iterable)r#   r   rj   
containersrD   s        r   unionzStableSet.union  sP     :dI64IFrxx56
&&z25zr   c                     t        t        | t              r| nt              }| }|r+t        j                  t        t        |       fd| D        } ||      S )a  
        Returns elements in common between all sets. Order is defined only
        by the first set.

        Example:
            >>> oset = StableSet.intersection(StableSet([0, 1, 2, 3]), [1, 2, 3])
            >>> print(oset)
            StableSet([1, 2, 3])
            >>> oset.intersection([2, 4, 5], [1, 2, 3, 4])
            StableSet([2])
            >>> oset.intersection()
            StableSet([1, 2, 3])
        c              3   ,   K   | ]  }|v s|  y wr   r+   )rb   r   commons     r   rd   z)StableSet.intersection.<locals>.<genexpr>  s     =ddfnT=re   )rg   r   r   setr]   r   )r#   r   rj   rD   r   s       @r   r]   zStableSet.intersection  sL     :dI64IF#'%%s3~6F=d=E5zr   c                     t        t        | t              r| nt              }| }|r+t        j                  t        t        |       fd| D        } ||      S )a  
        Returns all elements that are in this set but not the others.

        Example:
            >>> StableSet([1, 2, 3]).difference(StableSet([2]))
            StableSet([1, 3])
            >>> StableSet([1, 2, 3]).difference(StableSet([2]), StableSet([3]))
            StableSet([1])
            >>> StableSet([1, 2, 3]) - StableSet([2])
            StableSet([1, 3])
            >>> StableSet([1, 2, 3]).difference()
            StableSet([1, 2, 3])
        c              3   ,   K   | ]  }|vs|  y wr   r+   rb   r   r[   s     r   rd   z'StableSet.difference.<locals>.<genexpr>  s     @dd%.?T@re   )rg   r   r   r   r   r   )r#   r   rj   rD   r[   s       @r   
differencezStableSet.difference  sJ     :dI64IF#'IIs3~.E@d@E5zr   c                     t        t        | t              r| nt        |t              r|nt              } ||       j                  |      } ||      j                  |       }|j	                  |      S )a  
        Return the symmetric difference of two StableSets as a new set.
        That is, the new set will contain all elements that are in exactly
        one of the sets.

        Their order will be preserved, with elements from `self` preceding
        elements from `other`.

        Example:
            >>> this = StableSet([1, 4, 3, 5, 7])
            >>> other = StableSet([9, 7, 1, 3, 2])
            >>> this.symmetric_difference(other)
            StableSet([4, 5, 9, 2])
        )rg   r   r   r   r   )r#   r[   rj   diff1diff2s        r   symmetric_differencezStableSet.symmetric_difference  sj     $	*  %+ 
 D	$$U+E
%%d+{{5!!r   c                     t               }|D ]  }t        |      }||z  } t        j                  | j                  D cg c]	  }||vs| c}      | _        yc c}w )a  
        Update this StableSet to remove items from one or more other sets.

        Example:
            >>> this = StableSet([1, 2, 3])
            >>> this.difference_update(StableSet([2, 4]))
            >>> print(this)
            StableSet([1, 3])

            >>> this = StableSet([1, 2, 3, 4, 5])
            >>> this.difference_update(StableSet([2, 4]), StableSet([1, 4, 6]))
            >>> print(this)
            StableSet([3, 5])
        Nr   r    r!   r   r#   r   items_to_remover[   items_as_setr   s         r   difference_updatezStableSet.difference_update  s[     % 	,Eu:L|+O	, MM"iiGd4+FTG
	Gs   	A	Ac                     t        |      }t        j                  | j                  D cg c]	  }||v s| c}      | _        yc c}w )aZ  
        Update this StableSet to keep only items in another set, preserving
        their order in this set.

        Example:
            >>> this = StableSet([1, 4, 3, 5, 7])
            >>> other = StableSet([9, 7, 1, 3, 2])
            >>> this.intersection_update(other)
            >>> print(this)
            StableSet([1, 3, 7])
        Nr   r#   r[   r   s      r   intersection_updatezStableSet.intersection_update-  s5     E
MMDII"OD4"OP	"Os
   	AAc                     |D cg c]	  }|| vs| }}t        |      }t        j                  | j                  D cg c]	  }||vs| c}|z         | _        yc c}w c c}w )a  
        Update this StableSet to remove items from another set, then
        add items from the other set that were not present in this set.

        Example:
            >>> this = StableSet([1, 4, 3, 5, 7])
            >>> other = StableSet([9, 7, 1, 3, 2])
            >>> this.symmetric_difference_update(other)
            >>> print(this)
            StableSet([4, 5, 9, 2])
        Nr   r#   r[   r   items_to_addr   s        r   symmetric_difference_updatez%StableSet.symmetric_difference_update<  sa     */C$d2BCCe*MM"iiGd4+FTG,V
	 D Hs   	AA	A A c                 \    t        |       t              kD  ryt        fd| D              S )a4  
        Report whether another set contains this set.

        Example:
            >>> StableSet([1, 2, 3]).issubset({1, 2})
            False
            >>> StableSet([1, 2, 3]).issubset({1, 2, 3, 4})
            True
            >>> StableSet([1, 2, 3]).issubset({1, 4, 3, 5})
            False
        Fc              3   &   K   | ]  }|v  
 y wr   r+   r   s     r   rd   z%StableSet.issubset.<locals>.<genexpr>\  s     2T45=2   r5   allr^   s    `r   issubsetzStableSet.issubsetN  s)     t9s5z!2T222r   c                 \     t               t        |      k  ryt         fd|D              S )a:  
        Report whether this set contains another set.

        Example:
            >>> StableSet([1, 2]).issuperset([1, 2, 3])
            False
            >>> StableSet([1, 2, 3, 4]).issuperset({1, 2, 3})
            True
            >>> StableSet([1, 4, 3, 5]).issuperset({1, 2, 3})
            False
        Fc              3   &   K   | ]  }|v  
 y wr   r+   )rb   r   r#   s     r   rd   z'StableSet.issuperset.<locals>.<genexpr>l  s     2D44<2r   r   r^   s   ` r   
issupersetzStableSet.issuperset^  s)     t9s5z!2E222r   r#   non_consecutivec                     t        |       t        |      kD  ry|r*d}t        |       }|D ]  }|| |   k(  s|dz  }||k(  s y yt        | |      D ]  \  }}||k(  r y yNFr   r2   Tr5   zipr#   r[   r   rE   self_len
other_item	self_items          r   isorderedsubsetzStableSet.isorderedsubsetn      t9s5z!A4yH# $
a(FAH}#	$
 ),T5)9 !%	: J. ! r   c                 0    t         j                  || |      S r   r   r   r#   r[   r   s      r   isorderedsupersetzStableSet.isorderedsuperset      ((oFFr   c                 >    t        t        | j                              S r   )r6   r   r   r'   s    r   getzStableSet.get  s    DO$$r   r   r   N)r   r)   r   TF)>rY   
__module____qualname____doc__	__slots__r   r   r   __annotations__r
   SetInitializerr$   r4   r&   r   r<   r-   r   r   rH   rK   boolrN   r   rQ   rT   r   rZ   __str__SetLiker_   rk   rn   ry   r|   r   r   r   r   r>   r   appendr   r(   get_locget_indexerr   r   r   r   r   r]   r   r   r   r   r   r   r   r   r   r   r+   r   r   r   r   0   s   4 I
q#v,>): ; >
# 
#  >   # 47      %OT
!+ + ++(1+ +*hqk *<# <
 G(WQZ (N (BB/
/
	/$""q "S "" F"wqz "c "  ! c    s   0 GK a 0D !1 ! !(71: . &'!*  *
 ~ *"'!* " "4
wqz 
d 
.Q Q Q
 
 
$3gaj 3T 3 3
 3t 3 g g  $Gw G G%r   r   c                   D    e Zd ZdZd Zd Zd ZeZd Zd Z	d Z
d Zd	 Zy
)
OrderlySetz
    OrderlySet keeps the order when adding but if you do difference, subtraction, etc, you lose the order.
    The new results will have a random order but they will keep that order.
    c                 z    t        |t        t        f      r|n
t        |      }t        |       |z
  }t        |      S r   r   r   	frozensetr   r#   r[   rF   s      r   rk   zOrderlySet.__sub__  4    #EC+;<#e*TU"&!!r   c                 z    t        |t        t        f      r|n
t        |      }|t        |       z
  }t        |      S r   r   r   s      r   rn   zOrderlySet.__rsub__  s4    #EC+;<#e*T"&!!r   c                 z    t        |t        t        f      r|n
t        |      }t        |       |z  }t        |      S r   r   r   s      r   r   zOrderlySet.__xor__  r   r   c                 H   t        |t              syt        | j                        t        |      k7  ryt        |t              r| j                  |j                  k(  S t        |t
        t        f      st        |      }t        | j                  j                               |k(  S r   )r   r   r5   r   r   r   r   r9   r^   s     r   r   zOrderlySet.__eq__  sv    %*tyy>SZ'eY'99

**%#y!12JE499>>#$--r   c                     t        |t              syt        | j                        t        |      k  ryt        |t        t
        f      st	        |      }t	        | j                  j                               |k\  S r   r   r   r5   r   r   r   r9   r^   s     r   __ge__zOrderlySet.__ge__  Y    %*tyy>CJ&%#y!12JE499>>#$--r   c                     t        |t              syt        | j                        t        |      k  ryt        |t        t
        f      st	        |      }t	        | j                  j                               |kD  S r   r   r^   s     r   __gt__zOrderlySet.__gt__  Y    %*tyy>SZ'%#y!12JE499>>#$u,,r   c                     t        |t              syt        | j                        t        |      kD  ryt        |t        t
        f      st	        |      }t	        | j                  j                               |k  S r   r   r^   s     r   __le__zOrderlySet.__le__  r   r   c                     t        |t              syt        | j                        t        |      k\  ryt        |t        t
        f      st	        |      }t	        | j                  j                               |k  S r   r   r^   s     r   __lt__zOrderlySet.__lt__  r  r   N)rY   r   r   r   rk   rn   r   r   r   r   r   r  r  r+   r   r   r   r     s6    
"
"
"
 H	..-.-r   r   c                   h    e Zd ZdZdedefdZdee   fdZ	dee   fdZ
dee   fdZdee   fdZy	)
StableSetEqa  
    StableSetEq is a StableSet with a modified quality operator.

    StableSetEq, like `set` and `dict_keys` [dict.keys()], and unlike OrderdSet,
    disregards the items order when checking equality.
    Unlike StableSet, `set`, or `dict_keys` - A StableSetEq can also equal be equal to a Sequence:
    `StableSet([1, 2]) == [1, 2]` and `StableSet([1, 2]) == [2, 1]`; but `set([1, 2]) != [1, 2]`
    r[   r   c                     t        |t              s	 t        |      }| j                  j                         |k(  S # t        $ r Y yw xY w)ap  
        Returns true even if the containers don't have the same items in order.

        Example:
            >>> oset = StableSetEq([1, 3, 2])
            >>> oset == [1, 3, 2]
            True
            >>> oset == [1, 2, 3]
            True
            >>> oset == [2, 3]
            False
            >>> oset == StableSetEq([3, 2, 1])
            True
        F)r   r   r   rB   r   r9   r^   s     r   r   zStableSetEq.__eq__  sH     %-E
 yy~~5((  s   : 	AAc                     t        |       t        |      k  xrS t        |t              r| j                  j	                         |k  S | j                  j	                         t        |      k  S r   r5   r   r   r   r9   r   r^   s     r   r  zStableSetEq.__le__  Y    4yCJ& 
%- IINN%	
 !SZ/	
r   c                     t        |       t        |      k  xrS t        |t              r| j                  j	                         |k  S | j                  j	                         t        |      k  S r   r
  r^   s     r   r  zStableSetEq.__lt__  Y    4y3u:% 
%- IINNu$	
 !CJ.	
r   c                     t        |       t        |      k\  xrS t        |t              r| j                  j	                         |k\  S | j                  j	                         t        |      k\  S r   r
  r^   s     r   r   zStableSetEq.__ge__  r  r   c                     t        |       t        |      kD  xrS t        |t              r| j                  j	                         |kD  S | j                  j	                         t        |      kD  S r   r
  r^   s     r   r   zStableSetEq.__gt__  r  r   N)rY   r   r   r   r   r   r   r   r   r  r  r   r   r+   r   r   r  r    sZ    )C )D ).
GAJ 

GAJ 

GAJ 

GAJ 
r   r  c                   Z   e Zd ZU dZdZee   ed<   d!dee	e      fdZ
d Zded	efd
Zdee   fdZdee   fdZdee   fdZdee   fdZd"dZded	efdZdee   d	efdZd Zd#ded	efdZd$defdZd Zded	dfdZded	dfdZdee   d	dfdZdee   d	dfdZ dee   d	dfd Z!y)%
OrderedSetaR  
    An OrderedSet is a mutable data structure that is a hybrid of a list and a set.
    It remembers its insertion order so that every entry has an index that can be looked up.
    Featuring: O(1) Index lookup, insertion, iteration and membership testing.
    But slow O(N) Deletion.
    Using OrderedSet over StableSet is advised only if you require fast Index lookup -
    Otherwise using StableSet is advised as it is much faster and has a smaller memory footprint.

    In some aspects OrderedSet behaves like a `set` and in other aspects it behaves like a list.

    Equality: OrderedSet, like `list` and `odict_keys` [OrderdDict.keys()], and unlike OrderdSet,
    regards the items order when checking equality.
    Unlike `set`, An OrderedSet can also equal be equal to a Sequence:
    `StableSet([1, 2]) == [1, 2]` and `StableSet([1, 2]) != [2, 1]`; but `set([1, 2]) != [1, 2]`

    The original implementation of OrderedSet was a recipe posted by Raymond Hettiger,
    https://code.activestate.com/recipes/576694-orderedset/
    Released under the MIT license.
    Hettiger's implementation kept its content in a doubly-linked list referenced by a dict.
    As a result, looking up an item by its index was an O(N) operation, while deletion was O(1).
    This version makes different trade-offs for the sake of efficient lookups.
    Its content is a standard Python list instead of a doubly-linked list.
    This provides O(1) lookups by index at the expense of O(N) deletion,
    as well as slightly faster iteration.

    Example:
        >>> OrderedSet([1, 1, 2, 3, 2])
        OrderedSet([1, 2, 3])
    )_itemsr  Nr   c                 0    g | _         i | _        || |z  } y y r   )r  r   r"   s     r   r$   zOrderedSet.__init__/  s&    	 GOD	 r   c                    t        |t              r| j                  |   S t        |t              r|t        k(  r| j                         S t        |t              r|D cg c]  }| j                  |    c}S t        |t              st        |d      r2| j                  |   }t        |t              r| j                  |      S |S t        d|z        c c}w )Nr3   z+Don't know how to index an OrderedSet by %r)r   r4   r  r<   r=   r>   r   r@   r?   rA   rB   )r#   r(   rE   rF   s       r   r-   zOrderedSet.__getitem__8  s    eS!;;u%%u%%9*<99;x(,12qDKKN22u%)D[['F&$'~~f--IEQRR 3s   Cr[   r   c                     t        |t              r1t        |       t        |      k(  xr | j                  t	        |      k(  S 	 t        |      }| j                  j                         |k(  S # t        $ r Y yw xY w)a  
        Returns true if the containers have the same items.
        If `other` is a Sequence, then order is checked, otherwise it is ignored.

        Example:
            >>> oset = OrderedSet([1, 3, 2])
            >>> oset == [1, 3, 2]
            True
            >>> oset == [1, 2, 3]
            False
            >>> oset == [2, 3]
            False
            >>> oset == OrderedSet([3, 2, 1])
            False
        F)	r   r   r5   r  r?   r   r   r9   rB   )r#   r[   other_as_sets      r   r   zOrderedSet.__eq__H  so      eX& t9E
*It{{d5k/II	4u:L
 99>>#|33	  		s   A+ +	A76A7c                     t        |       t        |      k  xrd t        |t              r| j                  j	                         |k  S t        |t
              r| j                  |k  S | j                  t        |      k  S r   r5   r   r   r   r9   r?   r  r^   s     r   r  zOrderedSet.__le__d  q    4yCJ& 
%- IINN%	
 %& %	

 U+	
r   c                     t        |       t        |      k  xrd t        |t              r| j                  j	                         |k  S t        |t
              r| j                  |k  S | j                  t        |      k  S r   r  r^   s     r   r  zOrderedSet.__lt__m  q    4y3u:% 
%- IINNu$	
 %& u$	

 tE{*	
r   c                     t        |       t        |      k\  xrd t        |t              r| j                  j	                         |k\  S t        |t
              r| j                  |k\  S | j                  t        |      k\  S r   r  r^   s     r   r   zOrderedSet.__ge__v  r  r   c                     t        |       t        |      kD  xrd t        |t              r| j                  j	                         |kD  S t        |t
              r| j                  |kD  S | j                  t        |      kD  S r   r  r^   s     r   r   zOrderedSet.__gt__  r  r   c                 V    | j                   d d = | j                  j                          y r   )r  r   r   r'   s    r   r   zOrderedSet.clear  s    KKN		r   rL   c                     || j                   vr=t        | j                        | j                   |<   | j                  j                  |       | j                   |   S r   )r   r5   r  r   rO   s     r   r   zOrderedSet.add  sD    dii -DIIcNKKs#yy~r   r   c                 :    d}|D ]  }| j                  |      } |S Nr   )r   )r#   r   
item_indexr   s       r   r   zOrderedSet.update  s)    
 	(D$J	(r   c                     t        |t              r*t        |      s|D cg c]  }| j                  |       c}S | j                  |   S c c}w r   )r   r   r   r(   r   )r#   rL   r   s      r   r(   zOrderedSet.index  s?    c8$Z_5896DJJv&99yy~ :s   A
r(   c                     | j                   st        d      | j                   |   }| j                   |= | j                  |= |S )Nr   r  r   r   )r#   r(   r   s      r   r   zOrderedSet.pop  s>    {{>**{{5!KKIIdOr   r   c                     | j                   st        d      |rdnd}| j                   |   }| j                   |= | j                  |= |S )Nr   r   r   r%  )r#   r   r(   r   s       r   r   zOrderedSet.popitem  sG    {{>**{{5!KKIIdOr   c                 f    || v r#| j                  |       | j                  |       y t        |      r   )r   r   r   rO   s     r   r   zOrderedSet.move_to_end  s*    $;LLHHSM3-r   c                     || v rd| j                   |   }| j                  |= | j                   |= | j                   j                         D ]  \  }}||k\  s|dz
  | j                   |<    y y )Nr2   )r   r  rD   )r#   rL   rE   kvs        r   r   zOrderedSet.discard  sg    $;		#AA		#		) )16#$q5DIIaL)	 r   rD   c                 f    || _         t        |      D ci c]  \  }}||
 c}}| _        yc c}}w )zu
        Replace the 'items' list of this OrderedSet with a new one, updating
        self._map accordingly.
        N)r  r   r   )r#   rD   idxr   s       r   _update_itemszOrderedSet._update_items  s-    
 2;E2BC;CT3YC	Cs   -r   c                     t               }|D ]  }t        |      }||z  } | j                  | j                  D cg c]	  }||vs| c}       y c c}w r   r   r-  r  r   s         r   r   zOrderedSet.difference_update  sW    % 	,Eu:L|+O	, 	"kkIdT-HTI	
Is   	AAc                     t        |      }| j                  | j                  D cg c]	  }||v s| c}       y c c}w r   r/  r   s      r   r   zOrderedSet.intersection_update  s1    E
T[[JTDEMDJKJs   	;;c                     |D cg c]	  }|| vs| }}t        |      }| j                  | j                  D cg c]	  }||vs| c}|z          y c c}w c c}w r   r/  r   s        r   r   z&OrderedSet.symmetric_difference_update  s]    ).C$d2BCCe*"kkIdT-HTILX	
 D Js   	AA	AAr   r   r   r   )"rY   r   r   r   r   r   r   r   r
   r   r$   r-   r   r   r   r   r  r  r   r   r   r4   r   r   r(   r   r   r   r   r?   r-  r   r   r   r+   r   r   r  r    sO   < IGO): ; S 4C 4D 48
GAJ 

GAJ 

GAJ 

GAJ 
q S wqz c 
 a D  )1 ) )D4 DD D
wqz 
d 
L L L
 
 
r   r  c                   (   e Zd ZdddZd ZdefdZeZd'dZd Z	d	 Z
e
Ze
Zd
 Zd ZexZZd ZeZd Zd Zd ZexZZd Zd Zd Zd Zd Zd Zd ZeZd Z d Z!e!Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d  Z*e*Z+e*Z,d(d!Z-d'd"e.d#e.d$e/fd%Z0d'd#e.d$e/fd&Z1y))	SortedSetNset_c                F    d | _         |r|| _        y t        |i || _        y r   )_sortedr5  r   )r#   r5  argskwargss       r   r$   zSortedSet.__init__  s$    DIT,V,DIr   c              #   @   K   | j                         E d {    y 7 wr   )_get_sortedr'   s    r   rQ   zSortedSet.__iter__  s     ##%%%s   r   c                     | s| j                   j                   dS | j                   j                   d| j                         dS rV   )rA   rY   r;  r'   s    r   r   zSortedSet.__str__  sE    nn--.b11..))*!D,<,<,>+ACCr   c                     | j                   )	 t        | j                  |      | _         | j                   S | j                   S # t        $ r, t        | j                  d |      | _         Y | j                   S w xY w)N)reversec                     t        |       S r   )r   )xs    r   <lambda>z'SortedSet._get_sorted.<locals>.<lambda>  s
    s1v r   )rL   r>  )r7  sortedr5  	Exception)r#   r>  s     r   r;  zSortedSet._get_sorted  sk    <<X%diiA ||t||  X%dii5EwW||Xs   A 'A76A7c                 >    t        t        | j                              S )z&
        Get a random element
        )r6   r   r5  r'   s    r   r   zSortedSet.get  s     DO$$r   c                 8    | j                   |z  }t        |      S Nr4  r5  r3  r   s      r   r_   zSortedSet.__and__      U"f%%r   c                 :    | j                   j                  |       y r   )r5  r   r^   s     r   r   zSortedSet.intersection_update  s    		%%e,r   c                 8    | j                   |z  }t        |      S rF  rG  r   s      r   ry   zSortedSet.__or__	  rH  r   c                 8    | j                   |z
  }t        |      S rF  rG  r   s      r   rk   zSortedSet.__sub__  rH  r   c                 D    d | _          | j                  j                  |  y r   )r7  r5  r   )r#   r   s     r   r   zSortedSet.difference_update  s    #		##T*r   c                 8    | j                   j                  |      S r   )r5  
isdisjointr^   s     r   rN  zSortedSet.isdisjoint      yy##E**r   c                 8    | j                   |z  }t        |      S rF  rG  r   s      r   r   zSortedSet.__xor__  rH  r   c                 H    d | _         | j                  j                  |       y r   )r7  r5  r   r^   s     r   r   z%SortedSet.symmetric_difference_update%  s    		--e4r   c                 8    || j                   z
  }t        |      S rF  rG  r   s      r   rn   zSortedSet.__rsub__)  s    "f%%r   c                 H    d | _         | j                  j                  |       y r   )r7  r5  r   )r#   r   s     r   r   zSortedSet.add-  s    		dr   c                 F    d | _         | j                  j                          y r   )r7  r5  r   r'   s    r   r   zSortedSet.clear1  s    		r   c                 H    d | _         | j                  j                  |       y r   )r7  r5  r   rO   s     r   r   zSortedSet.discard5  s    		#r   c                 @    t        t        | j                              S rF  )r3  r   r5  r'   s    r   r>   zSortedSet.copy9  s    c$))n--r   c                 8    | j                   j                  |      S r   )r5  r   r^   s     r   r  zSortedSet.__le__<  s    yy!!%((r   c                      | j                   |k  S r   r4  r^   s     r   r  zSortedSet.__lt__A      yy5  r   c                 8    | j                   j                  |      S r   )r5  r   r^   s     r   r   zSortedSet.__ge__D  rO  r   c                      | j                   |kD  S r   r4  r^   s     r   r   zSortedSet.__gt__I  rY  r   c                 H    d | _         | j                  j                  |       y r   )r7  r5  removerO   s     r   r]  zSortedSet.removeL  s    		r   c                 H    d | _         | j                  j                  |       y r   )r7  r5  r   )r#   r   s     r   r   zSortedSet.updateP  s    		"r   c                 H    | j                   rt        | j                         S dS r!  )r5  r5   r'   s    r   r&   zSortedSet.__len__T  s    !%s499~11r   c                    t        |t              syt        | j                        t        |      k7  ryt        |t              r| j                  |j                  k(  S t        |t
        t        f      st        |      }| j                  |k(  S r   )r   r   r5   r5  r3  r   r   r^   s     r   r   zSortedSet.__eq__W  sj    %*tyy>SZ'eY'99

**%#y!12JEyyE!!r   c                 d    | j                   | j                          t        | j                         S r   )r7  r;  rS   r'   s    r   rT   zSortedSet.__reversed__b  s'    <<%%r   c                    | j                         }t        |t              r||   S t        |t              r|t        k(  r| j                         S t        |t              r|D cg c]  }||   	 c}S t        |t              st        |d      r(||   }t        |t              r| j                  |      S |S t        d|       c c}w )Nr3   z'Don't know how to index a SortedSet by )r;  r   r4   r<   r=   r>   r   r@   r?   rA   rB   rC   s        r   r-   zSortedSet.__getitem__g  s      "eS!<u%%9*<99;eX&&+,E!H,,u%)D5\F&$'~~f--EeWMNN -s   #Cc                     | j                         }	 t        |t              r*t        |      s|D cg c]  }| j	                  |       c}S t        |      D ]  \  }}||k(  s|c S  t        |      c c}w # t        $ r t        |      w xY wr   )r;  r   r   r   r(   r   r   r   )r#   rL   rD   r   r(   r   s         r   r(   zSortedSet.indexy  s       "		 #x(C9<=v

6*==(/ !t3; L! 3-	 >  	 3-	 s(   A8 A3	A8 A8 #A8 'A8 8Bc                     |!d | _         | j                  j                         S | j                         }|j                  |      }| j                  j	                  |       |S r   )r7  r5  r   r;  r]  )r#   r(   rD   rF   s       r   r   zSortedSet.pop  sQ    =DL99==?"  "5!		 r   r#   r[   r   c                     t        |       t        |      kD  ry|r*d}t        |       }|D ]  }|| |   k(  s|dz  }||k(  s y yt        | |      D ]  \  }}||k(  r y yr   r   r   s          r   r   zSortedSet.isorderedsubset  r   r   c                 0    t         j                  || |      S r   r   r   s      r   r   zSortedSet.isorderedsuperset  r   r   r   r   )2rY   r   r   r$   rQ   r   r   rZ   r;  r   r_   r]   __rand__r   ry   r   r|   rk   r   r   rN  r   r   r   r   rn   r   r   r   r>   r  r   r  r   r   r   r]  r   r&   r   rT   r-   r(   r   r   r   r   r   r   r   r+   r   r   r3  r3    s"   #' -&D D H%&
 LH-&
 EG&
 J++&
 '.-85&.) H!+ J!#2	"&
O$ 2 GKg g  $Gw G Gr   r3  )r7   r   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r<   r=   r   r   r   objectr   r   r   r   r  r  r3  r+   r   r   <module>rj     s          $K	CL A+
,{1~x{HQK?@)F )t )&T	%
1x{ T	%nD-1 D-N;
)A, ;
|L
1 L
^VG VGr   