o
    ae                      @   s   d dl mZmZ d dlmZ d dlmZ G dd deZG dd deZ	G dd	 d	e	Z
ee
 ee
 G d
d de	Zee ee e ZdddZdd ZdS )    )SequenceHashable)Integral)reducec                   @   s<   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dS )_PListBuilderzc
    Helper class to allow construction of a list without
    having to reverse it in the end.
    )_head_tailc                 C   s   t | _t | _d S N)_EMPTY_PLISTr   r   self r   3/usr/lib/python3/dist-packages/pyrsistent/_plist.py__init__   s   
z_PListBuilder.__init__c                 C   s:   | j s||| _| j| _ | jS ||| j _| j j| _ | jS r	   )r   r   rest)r   elemconstructorr   r   r   _append   s   

z_PListBuilder._appendc                 C      |  |dd S )Nc                 S   s
   t | tS r	   )PListr
   )er   r   r   <lambda>      
 z+_PListBuilder.append_elem.<locals>.<lambda>r   r   r   r   r   r   append_elem      z_PListBuilder.append_elemc                 C   r   )Nc                 S      | S r	   r   )lr   r   r   r      s    z,_PListBuilder.append_plist.<locals>.<lambda>r   )r   plr   r   r   append_plist   r   z_PListBuilder.append_plistc                 C   s   | j S r	   )r   r   r   r   r   build!   s   z_PListBuilder.buildN)
__name__
__module____qualname____doc__	__slots__r   r   r   r    r!   r   r   r   r   r      s    
r   c                   @   s   e Zd ZdZejZejZdd Zdd Zdd Z	e	Z
dd	 Zd
d Zdd ZeZ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 )
_PListBase)__weakref__c                 C   s   t t| ffS r	   )plistlistr   r   r   r   
__reduce__.   s   z_PListBase.__reduce__c                 C   s   t dd | D S )a  
        Return the length of the list, computed by traversing it.

        This is obviously O(n) but with the current implementation
        where a list is also a node the overhead of storing the length
        in every node would be quite significant.
        c                 s   s    | ]}d V  qdS )   Nr   ).0_r   r   r   	<genexpr>:   s    z%_PListBase.__len__.<locals>.<genexpr>)sumr   r   r   r   __len__2   s   z_PListBase.__len__c                 C   s   d t| S )Nz
plist({0}))formatr*   r   r   r   r   __repr__<   s   z_PListBase.__repr__c                 C   s
   t || S )z
        Return a new list with elem inserted as new head.

        >>> plist([1, 2]).cons(3)
        plist([3, 1, 2])
        )r   r   r   r   r   cons@   s   
z_PListBase.consc                 C   s   | }|D ]}| |}q|S )a   
        Return a new list with all elements of iterable repeatedly cons:ed to the current list.
        NB! The elements will be inserted in the reverse order of the iterable.
        Runs in O(len(iterable)).

        >>> plist([1, 2]).mcons([3, 4])
        plist([4, 3, 1, 2])
        r4   )r   iterableheadr   r   r   r   mconsI   s   	z_PListBase.mconsc                 C   s(   t  }| }|r||j}|j}|s|S )a  
        Return a reversed version of list. Runs in O(n) where n is the length of the list.

        >>> plist([1, 2, 3]).reverse()
        plist([3, 2, 1])

        Also supports the standard reversed function.

        >>> reversed(plist([1, 2, 3]))
        plist([3, 2, 1])
        )r)   r4   firstr   )r   resultr7   r   r   r   reverseX   s   z_PListBase.reversec                 C   sX   t  }| }d}|r ||k r ||j |j}|d7 }|r ||k s|s&| tfS | |fS )z
        Spilt the list at position specified by index. Returns a tuple containing the
        list up until index and the list after the index. Runs in O(index).

        >>> plist([1, 2, 3, 4]).split(2)
        (plist([1, 2]), plist([3, 4]))
        r   r,   )r   r   r9   r   r
   r!   )r   indexlb
right_listir   r   r   splitm   s   z_PListBase.splitc                 c   s$    | }|r|j V  |j}|sd S d S r	   r9   r   )r   lir   r   r   __iter__   s   z_PListBase.__iter__c                 C   s   t |tstS t| t|k S r	   )
isinstancer'   NotImplementedtuple)r   otherr   r   r   __lt__   s   
z_PListBase.__lt__c                 C   sN   t |tstS | }|}|r!|r!|j|jksdS |j}|j}|r!|s| o&| S )z
        Traverses the lists, checking equality of elements.

        This is an O(n) operation, but preserves the standard semantics of list equality.
        F)rD   r'   rE   r9   r   )r   rG   	self_head
other_headr   r   r   __eq__   s   
z_PListBase.__eq__c              
   C   s   t |tr'|jd ur|jd u r|jd u s|jdkr| |jS tt| | S t |ts5t	dt
|j |dk r?|t| 7 }z| |jW S  tyW } ztd|d }~ww )Nr,   z-'%s' object cannot be interpreted as an indexr   PList index out of range)rD   slicestartstopstep_dropr)   rF   r   	TypeErrortyper"   lenr9   AttributeError
IndexError)r   r<   r   r   r   r   __getitem__   s   
(

z_PListBase.__getitem__c                 C   s6   |dk rt d| }|dkr|j}|d8 }|dks|S )Nr   rL   r,   )rV   r   )r   countr7   r   r   r   rQ      s   z_PListBase._dropc                 C   s   t t| S r	   )hashrF   r   r   r   r   __hash__   s   z_PListBase.__hash__c                 C   sH   t  }| }|r|j|kr||jS ||j |j}|std|)a
  
        Return new list with first element equal to elem removed. O(k) where k is the position
        of the element that is removed.

        Raises ValueError if no matching element is found.

        >>> plist([1, 2, 1]).remove(1)
        plist([2, 1])
        z{0} not found in PList)r   r9   r    r   r   
ValueErrorr2   )r   r   builderr7   r   r   r   remove   s   
z_PListBase.removeN)r"   r#   r$   r&   r   rX   r<   r+   r1   r3   __str__r4   r8   r;   __reversed__r@   rC   rH   rK   rW   rQ   rZ   r]   r   r   r   r   r'   %   s(    
	r'   c                       s0   e Zd ZdZdZ fddZdd ZeZ  ZS )r   a  
    Classical Lisp style singly linked list. Adding elements to the head using cons is O(1).
    Element access is O(k) where k is the position of the element in the list. Taking the
    length of the list is O(n).

    Fully supports the Sequence and Hashable protocols including indexing and slicing but
    if you need fast random access go for the PVector instead.

    Do not instantiate directly, instead use the factory functions :py:func:`l` or :py:func:`plist` to
    create an instance.

    Some examples:

    >>> x = plist([1, 2])
    >>> y = x.cons(3)
    >>> x
    plist([1, 2])
    >>> y
    plist([3, 1, 2])
    >>> y.first
    3
    >>> y.rest == x
    True
    >>> y[:2]
    plist([3, 1])
    rA   c                    s    t t| | }||_||_|S r	   )superr   __new__r9   r   )clsr9   r   instance	__class__r   r   ra      s   zPList.__new__c                 C      dS )NTr   r   r   r   r   __bool__     zPList.__bool__)	r"   r#   r$   r%   r&   ra   rg   __nonzero____classcell__r   r   rd   r   r      s    r   c                   @   s4   e Zd ZdZdd ZeZedd Zedd ZdS )	_EmptyPListr   c                 C   rf   )NFr   r   r   r   r   rg     rh   z_EmptyPList.__bool__c                 C   s   t d)NzEmpty PList has no first)rU   r   r   r   r   r9     s   z_EmptyPList.firstc                 C   r   r	   r   r   r   r   r   r     s   z_EmptyPList.restN)	r"   r#   r$   r&   rg   ri   propertyr9   r   r   r   r   r   rk   
  s    
rk   r   Fc                 C   s$   |s
t | } |   tdd | tS )a   
    Creates a new persistent list containing all elements of iterable.
    Optional parameter reverse specifies if the elements should be inserted in
    reverse order or not.

    >>> plist([1, 2, 3])
    plist([1, 2, 3])
    >>> plist([1, 2, 3], reverse=True)
    plist([3, 2, 1])
    c                 S   s
   |  |S r	   r5   )r   r   r   r   r   r   /  r   zplist.<locals>.<lambda>)r*   r;   r   r
   )r6   r;   r   r   r   r)      s   r)   c                  G   s   t | S )zj
    Creates a new persistent list containing all arguments.

    >>> l(1, 2, 3)
    plist([1, 2, 3])
    )r)   )elementsr   r   r   r   2  s   r   N)r   F)collections.abcr   r   numbersr   	functoolsr   objectr   r'   r   registerrk   r
   r)   r   r   r   r   r   <module>   s     :
(



