a `Å]_e ã@s¬ddlmZmZddlmZddlmZGdd„deƒZGdd„deƒZ Gdd „d e ƒZ e  e ¡e  e ¡Gd d „d e ƒZ e  e ¡e  e ¡e ƒZ ddd„Zdd„ZdS)é)ÚSequenceÚHashable)ÚIntegral)Úreducec@s<eZdZdZdZdd„Zdd„Zdd„Zd d „Zd d „Z d S)Ú _PListBuilderzc Helper class to allow construction of a list without having to reverse it in the end. )Ú_headÚ_tailcCst|_t|_dS©N)Ú _EMPTY_PLISTrr©Úself©r ú7/usr/lib64/python3.9/site-packages/pyrsistent/_plist.pyÚ__init__ sz_PListBuilder.__init__cCs6|js||ƒ|_|j|_n||ƒ|j_|jj|_|jSr )rrÚrest)r ÚelemÚ constructorr r rÚ_appends     z_PListBuilder._appendcCs| |dd„¡S)NcSs t|tƒSr )ÚPListr )Úer r rÚóz+_PListBuilder.append_elem..©r©r rr r rÚ append_elemsz_PListBuilder.append_elemcCs| |dd„¡S)NcSs|Sr r )Úlr r rrrz,_PListBuilder.append_plist..r)r Úplr r rÚ append_plistsz_PListBuilder.append_plistcCs|jSr )rr r r rÚbuild!sz_PListBuilder.buildN) Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú __slots__rrrrrr r r rrs rc@s”eZdZdZejZejZdd„Zdd„Zdd„Z e Z dd „Z d d „Z d d „Z e Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„ZdS)Ú _PListBase)Ú __weakref__cCstt|ƒffSr )ÚplistÚlistr r r rÚ __reduce__.sz_PListBase.__reduce__cCstdd„|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. css|] }dVqdS)éNr )Ú.0Ú_r r rÚ :rz%_PListBase.__len__..)Úsumr r r rÚ__len__2sz_PListBase.__len__cCsd t|ƒ¡S)Nz plist({0}))Úformatr'r r r rÚ__repr__<sz_PListBase.__repr__cCs t||ƒS)z Return a new list with elem inserted as new head. >>> plist([1, 2]).cons(3) plist([3, 1, 2]) )rrr r rÚcons@sz_PListBase.conscCs|}|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]) ©r1)r ÚiterableÚheadrr r rÚmconsIs  z_PListBase.mconscCs&tƒ}|}|r"| |j¡}|j}q |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&r1Úfirstr)r Úresultr4r r rÚreverseXs  z_PListBase.reversecCsNtƒ}|}d}|r6||kr6| |j¡|j}|d7}q|sB|tfS| ¡|fS)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])) rr))rrr6rr r)r ÚindexZlbÚ right_listÚir r rÚsplitms   z_PListBase.splitccs|}|r|jV|j}qdSr ©r6r)r Zlir r rÚ__iter__ƒsz_PListBase.__iter__cCst|tƒstSt|ƒt|ƒkSr )Ú isinstancer$ÚNotImplementedÚtuple)r Úotherr r rÚ__lt__‰s z_PListBase.__lt__cCsHt|tƒstS|}|}|r<|r<|j|jks.dS|j}|j}q| oF| S)z  Traverses the lists, checking equality of elements. This is an O(n) operation, but preserves the standard semantics of list equality. F)r?r$r@r6r)r rBZ self_headZ other_headr r rÚ__eq__s  z_PListBase.__eq__c CsÀt|tƒrN|jdur>|jdur>|jdus2|jdkr>| |j¡Stt|ƒ|ƒSt|tƒsjt dt |ƒj ƒ‚|dkr~|t |ƒ7}z| |¡j WStyº}ztdƒ|‚WYd}~n d}~00dS)Nr)z-'%s' object cannot be interpreted as an indexrúPList index out of range)r?ÚsliceÚstartÚstopÚstepÚ_dropr&rArÚ TypeErrorÚtyperÚlenr6ÚAttributeErrorÚ IndexError)r r9rr r rÚ __getitem__¢s (   z_PListBase.__getitem__cCs0|dkrtdƒ‚|}|dkr,|j}|d8}q|S)NrrEr))rOr)r Úcountr4r r rrJ¹s z_PListBase._dropcCs tt|ƒƒSr )ÚhashrAr r r rÚ__hash__Äsz_PListBase.__hash__cCsJtƒ}|}|r8|j|kr$| |j¡S| |j¡|j}q td |¡ƒ‚dS)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 PListN)rr6rrrÚ ValueErrorr/)r rZbuilderr4r r rÚremoveÇs    z_PListBase.removeN)rr r!r#rrQr9r(r.r0Ú__str__r1r5r8Ú __reversed__r<r>rCrDrPrJrSrUr r r rr$%s&   r$cs0eZdZdZdZ‡fdd„Zdd„ZeZ‡ZS)raÉ 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]) r=cs tt|ƒ |¡}||_||_|Sr )ÚsuperrÚ__new__r6r)Úclsr6rÚinstance©Ú __class__r rrYûsz PList.__new__cCsdS)NTr r r r rÚ__bool__szPList.__bool__) rr r!r"r#rYr^Ú __nonzero__Ú __classcell__r r r\rrÞs  rc@s4eZdZdZdd„ZeZedd„ƒZedd„ƒZdS) Ú _EmptyPListr cCsdS)NFr r r r rr^ sz_EmptyPList.__bool__cCs tdƒ‚dS)NzEmpty PList has no first)rNr r r rr6sz_EmptyPList.firstcCs|Sr r r r r rrsz_EmptyPList.restN) rr r!r#r^r_Úpropertyr6rr r r rra s rar FcCs$|st|ƒ}| ¡tdd„|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]) cSs | |¡Sr r2)rrr r rr/rzplist..)r'r8rr )r3r8r r rr& s r&cGst|ƒS)zj Creates a new persistent list containing all arguments. >>> l(1, 2, 3) plist([1, 2, 3]) )r&)Úelementsr r rr2srN)r F)Úcollections.abcrrZnumbersrÚ functoolsrÚobjectrr$rÚregisterrar r&rr r r rÚs  :(