a `]_X@s$ddlmZmZddlmZmZddlmZddlZddl m Z ddZ dZ e d Z e e Zd d Zd d ZGdddeZGdddedZedeggZeeeeeedddZz:ddlZejdreZnddlmZeeeWneyeZYn0ddZdS))abstractmethodABCMeta)SequenceHashable)IntegralN transformcCst|dS)N1)bincount)valr 9/usr/lib64/python3.9/site-packages/pyrsistent/_pvector.py _bitcountsr cCs ||t|tr|n|SN)tolist isinstancePVector)votheroperatorr r rcompare_pvectorsrcCs|dur |St||Sr)slice)indexstopr r r_index_or_slicesrcs^eZdZdZdZfddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZddZddZddZddZeZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,ZGd-d.d.eZd/d0Zd1d2Zd3d4Ze d5d6Z!d7d8Z"d9d:Z#d;d<Z$d=d>Z%d?d@Z&dAdBZ'dCdDZ(dEdFZ)dGdHZ*dIdJZ+dPdLdMZ,dNdOZ-Z.S)Q PythonPVectorzd Support structure for PVector that implements structural sharing for vectors using a trie. )_count_shift_root_tail _tail_offset __weakref__cs>tt||}||_||_||_||_|jt|j|_|Sr) superr__new__rr r!r"lenr#)clsr shiftroottailself __class__r rr&!szPythonPVector.__new__cCs|jSr)rr,r r r__len__,szPythonPVector.__len__cCsdt|tr>|jdur,|jdur,|jdur,|St||S|dkrP||j7}t |||t @SNr) rrstartrstep_EMPTY_PVECTORextendrrr _node_forBIT_MASKr,rr r r __getitem__/s  zPythonPVector.__getitem__cCs ||Sr)r5r,rr r r__add__?szPythonPVector.__add__cCsdt|S)Nz pvector({0}))formatstrrr/r r r__repr__BszPythonPVector.__repr__cCs|Sr)r>r/r r r__str__EszPythonPVector.__str__cCs t|Sr)iterrr/r r r__iter__HszPythonPVector.__iter__cCs || Sr)__eq__r:r r r__ne__MszPythonPVector.__ne__cCs.||up,t|do,|jt|ko,t||tjS)Nr0)hasattrrr'rreqr:r r rrBPszPythonPVector.__eq__cCst||tjSr)rrgtr:r r r__gt__SszPythonPVector.__gt__cCst||tjSr)rrltr:r r r__lt__VszPythonPVector.__lt__cCst||tjSr)rrger:r r r__ge__YszPythonPVector.__ge__cCst||tjSr)rrler:r r r__le__\szPythonPVector.__le__cCs2|dks|turtS|dkr |St||SNrr)r4r5rr,timesr r r__mul___s zPythonPVector.__mul__cCs4|r&|t8}|D]}||||qn ||dSr)SHIFT _fill_listr5)r,noder)the_listnr r rrSjs zPythonPVector._fill_listcCs&g}||j|j|||j|S)zK The fastest way to convert the vector into a python list. )rSr!r r5r")r,rUr r rrrs zPythonPVector.tolistcCs t|S)z8 Returns the content as a python tuple. )tuplerr/r r r_totuple{szPythonPVector._totuplecCs t|Sr)hashrXr/r r r__hash__szPythonPVector.__hash__cGs t||Srrr,Ztransformationsr r rrszPythonPVector.transformcCst|ffSr)pvectorrr/r r r __reduce__szPythonPVector.__reduce__cGsNt|drtd|}tdt|dD]}||d|||<q,|S)Nz)mset expected an even number of argumentsrr)r' TypeErrorevolverrange persistent)r,argsr`ir r rmsets  zPythonPVector.msetc@sxeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZdS)zPythonPVector.Evolver) rr r!r"r# _dirty_nodes _extra_tail _cached_leafs _orig_pvectorcCs||dSr)_resetr,rr r r__init__szPythonPVector.Evolver.__init__cCst|tstdt|j|dkr8||jt|j7}|j|krZ|jt|jkrnnn|j||jSt |||t @S)N-'%s' object cannot be interpreted as an indexr) rrr_type__name__rr'rgrr6r7r8r r rr9s &z!PythonPVector.Evolver.__getitem__cCsD|j|_|j|_|j|_|j|_|j|_i|_i|_g|_||_dSr) rr r!r"r#rfrhrgrirkr r rrjszPythonPVector.Evolver._resetcCs|j||Sr)rgappend)r,elementr r rrps zPythonPVector.Evolver.appendcCs|j||Sr)rgr5)r,iterabler r rr5s zPythonPVector.Evolver.extendcCs |||<|Srr )r,rr r r rsetszPythonPVector.Evolver.setcCsTt|tstdt|j|dkr8||jt|j7}d|krN|jkrnn|j |t ?}|rt|||t @<nl||j krt |j|jvrt|j|_d|jt |j<|j|j|t ?<||j|t @<n||j|j|||_nn|j|kr|jt|jkrnn||j||j<n2||jt|jkrB|j|ntd|fdS)NrmrTIndex out of range: %s)rrr_rnrorr'rgrhgetrRr7r#idr"rflist_do_setr r!rp IndexError)r,rr rTr r r __setitem__s(   *z!PythonPVector.Evolver.__setitem__cCszt||jvr|}nt|}d|jt|<|dkrN|||t@<||j|t?<n(||?t@}||t||||||<|S)NTr)rvrfrwr7rhrRrxr,levelrTrdr ret sub_indexr r rrxs  zPythonPVector.Evolver._do_setcCs ||=|Srr r8r r rdeleteszPythonPVector.Evolver.deletecCsH|jrkr:|j||j|jg}|t7}n||j|j|j}||fSNr)r rrRr! _new_pathr" _push_tail)r, new_shiftnew_rootr r r_create_new_root9s  zPythonPVector._create_new_rootcCs\t|jtkr:t|j}||t|jd|j|j|S| \}}t|jd|||gSr) r'r" BRANCH_FACTORrwrprrr r!r)r,r rrrr r rrpEs    zPythonPVector.appendcCs|dkr |S||t|gSr1)rrR)r,r|rTr r rrOszPythonPVector._new_pathcCs|\|_|_g|_dSr)rr!r r"r/r r r_mutating_insert_tailUsz#PythonPVector._mutating_insert_tailcCsHtt|j}||||}|j|t|}|j|7_||Sr)rr'r"r5r)r,offsetsequenceZ max_delta_lenZdeltaZ delta_lenr r r_mutating_fill_tailYs  z!PythonPVector._mutating_fill_tailcCsNd}t|}||kr8|||}t|jtkr |q |jt|j|_dSr1)r'rr"rrrr#)r,rrZ sequence_lenr r r_mutating_extendas  zPythonPVector._mutating_extendcCsFt|tr|nt|}|rB||d}||dd|S|SrN)rrrrwrpr)r,objrZ new_vectorr r rr5ks zPythonPVector.extendcCstt|}|tkr|||S|jd|?t@}t||krZ||t|||||<|S|||t||S)z if parent is leaf, insert node, else does it map to an existing child? -> node_to_insert = push node one more level else alloc new path return node_to_insert placed in copy of parent r)rwrRrprr7r'rr)r,r|parentZ tail_noder}r~r r rrvs   zPythonPVector._push_tailcOs|j|g|Ri|Sr)rrr,valuerckwargsr r rrszPythonPVector.indexcCs||Sr)rr r,rr r rr szPythonPVector.countNcCs|}|t||=t|Sr)rrr4r5)r,rrrr r rrs zPythonPVector.deletecCs|}||t|Sr)rremover4r5)r,rrr r rrs zPythonPVector.remove)N)/rorr__doc__rr&r0r9r;r>r?rArCrBrGrIrKrMrQ__rmul__rSrrXrZrr]reobjectrr`rsrx staticmethodr6rrprrrrr5rrr rr __classcell__r r r-rrsT    p       rc@seZdZdZeddZeddZeddZedd Zed d Z ed d Z eddZ eddZ eddZ eddZeddZeddZeddZed!ddZedd ZdS)"ra Persistent vector implementation. Meant as a replacement for the cases where you would normally use a Python list. Do not instantiate directly, instead use the factory functions :py:func:`v` and :py:func:`pvector` to create an instance. Heavily influenced by the persistent vector available in Clojure. Initially this was more or less just a port of the Java code for the Clojure vector. It has since been modified and to some extent optimized for usage in Python. The vector is organized as a trie, any mutating method will return a new vector that contains the changes. No updates are done to the original vector. Structural sharing between vectors are applied where possible to save space and to avoid making complete copies. This structure corresponds most closely to the built in list type and is intended as a replacement. Where the semantics are the same (more or less) the same function names have been used but for some cases it is not possible, for example assignments. The PVector implements the Sequence protocol and is Hashable. Inserts are amortized O(1). Random access is log32(n) where n is the size of the vector. The following are examples of some common operations on persistent vectors: >>> p = v(1, 2, 3) >>> p2 = p.append(4) >>> p3 = p2.extend([5, 6, 7]) >>> p pvector([1, 2, 3]) >>> p2 pvector([1, 2, 3, 4]) >>> p3 pvector([1, 2, 3, 4, 5, 6, 7]) >>> p3[5] 6 >>> p.set(1, 99) pvector([1, 99, 3]) >>> cCsdS)z/ >>> len(v(1, 2, 3)) 3 Nr r/r r rr0szPVector.__len__cCsdS)z Get value at index. Full slicing support. >>> v1 = v(5, 6, 7, 8) >>> v1[2] 7 >>> v1[1:3] pvector([6, 7]) Nr r8r r rr9szPVector.__getitem__cCsdS)zm >>> v1 = v(1, 2) >>> v2 = v(3, 4) >>> v1 + v2 pvector([1, 2, 3, 4]) Nr r:r r rr;szPVector.__add__cCsdS)zY >>> v1 = v(1, 2) >>> 3 * v1 pvector([1, 2, 1, 2, 1, 2]) Nr rOr r rrQszPVector.__mul__cCsdS)zo >>> v1 = v(1, 2, 3) >>> v2 = v(1, 2, 3) >>> hash(v1) == hash(v2) True Nr r/r r rrZszPVector.__hash__cCsdS)a~ Create a new evolver for this pvector. The evolver acts as a mutable view of the vector with "transaction like" semantics. No part of the underlying vector i updated, it is still fully immutable. Furthermore multiple evolvers created from the same pvector do not interfere with each other. You may want to use an evolver instead of working directly with the pvector in the following cases: * Multiple updates are done to the same vector and the intermediate results are of no interest. In this case using an evolver may be a more efficient and easier to work with. * You need to pass a vector into a legacy function or a function that you have no control over which performs in place mutations of lists. In this case pass an evolver instance instead and then create a new pvector from the evolver once the function returns. The following example illustrates a typical workflow when working with evolvers. It also displays most of the API (which i kept small by design, you should not be tempted to use evolvers in excess ;-)). Create the evolver and perform various mutating updates to it: >>> v1 = v(1, 2, 3, 4, 5) >>> e = v1.evolver() >>> e[1] = 22 >>> _ = e.append(6) >>> _ = e.extend([7, 8, 9]) >>> e[8] += 1 >>> len(e) 9 The underlying pvector remains the same: >>> v1 pvector([1, 2, 3, 4, 5]) The changes are kept in the evolver. An updated pvector can be created using the persistent() function on the evolver. >>> v2 = e.persistent() >>> v2 pvector([1, 22, 3, 4, 5, 6, 7, 8, 10]) The new pvector will share data with the original pvector in the same way that would have been done if only using operations on the pvector. Nr r/r r rr`szPVector.evolvercGsdS)aY Return a new vector with elements in specified positions replaced by values (multi set). Elements on even positions in the argument list are interpreted as indexes while elements on odd positions are considered values. >>> v1 = v(1, 2, 3) >>> v1.mset(0, 11, 2, 33) pvector([11, 2, 33]) Nr )r,rcr r rre$sz PVector.msetcCsdS)a Return a new vector with element at position i replaced with val. The original vector remains unchanged. Setting a value one step beyond the end of the vector is equal to appending. Setting beyond that will result in an IndexError. >>> v1 = v(1, 2, 3) >>> v1.set(1, 4) pvector([1, 4, 3]) >>> v1.set(3, 4) pvector([1, 2, 3, 4]) >>> v1.set(-1, 4) pvector([1, 2, 4]) Nr )r,rdr r r rrs1sz PVector.setcCsdS)z Return a new vector with val appended. >>> v1 = v(1, 2) >>> v1.append(3) pvector([1, 2, 3]) Nr )r,r r r rrpBszPVector.appendcCsdS)z Return a new vector with all values in obj appended to it. Obj may be another PVector or any other Iterable. >>> v1 = v(1, 2, 3) >>> v1.extend([4, 5]) pvector([1, 2, 3, 4, 5]) Nr )r,rr r rr5LszPVector.extendcOsdS)z Return first index of value. Additional indexes may be supplied to limit the search to a sub range of the vector. >>> v1 = v(1, 2, 3, 4, 3) >>> v1.index(3) 2 >>> v1.index(3, 3, 5) 4 Nr rr r rrWsz PVector.indexcCsdS)z Return the number of times that value appears in the vector. >>> v1 = v(1, 4, 3, 4) >>> v1.count(4) 2 Nr rr r rr dsz PVector.countcGsdS)a Transform arbitrarily complex combinations of PVectors and PMaps. A transformation consists of two parts. One match expression that specifies which elements to transform and one transformation function that performs the actual transformation. >>> from pyrsistent import freeze, ny >>> news_paper = freeze({'articles': [{'author': 'Sara', 'content': 'A short article'}, ... {'author': 'Steve', 'content': 'A slightly longer article'}], ... 'weather': {'temperature': '11C', 'wind': '5m/s'}}) >>> short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:25] + '...' if len(c) > 25 else c) >>> very_short_news = news_paper.transform(['articles', ny, 'content'], lambda c: c[:15] + '...' if len(c) > 15 else c) >>> very_short_news.articles[0].content 'A short article' >>> very_short_news.articles[1].content 'A slightly long...' When nothing has been transformed the original data structure is kept >>> short_news is news_paper True >>> very_short_news is news_paper False >>> very_short_news.articles[0] is news_paper.articles[0] True Nr r[r r rrnszPVector.transformNcCsdS)z Delete a portion of the vector by index or range. >>> v1 = v(1, 2, 3, 4, 5) >>> v1.delete(1) pvector([1, 3, 4, 5]) >>> v1.delete(1, 3) pvector([1, 4, 5]) Nr )r,rrr r rrszPVector.deletecCsdS)z Remove the first occurrence of a value from the vector. >>> v1 = v(1, 2, 3, 2, 1) >>> v2 = v1.remove(1) >>> v2 pvector([2, 3, 2, 1]) >>> v2.remove(1) pvector([2, 3, 2]) Nr rr r rrszPVector.remove)N)rorrrrr0r9r;rQrZr`rersrpr5rr rrrr r r rrs>)      /        r) metaclassr cCs t|S)z Create a new persistent vector containing the elements in iterable. >>> v1 = pvector([1, 2, 3]) >>> v1 pvector([1, 2, 3]) )r4r5)rrr r rpython_pvectorsrZPYRSISTENT_NO_C_EXTENSIONr\cGst|S)z Create a new persistent vector containing all parameters to this function. >>> v1 = v(1, 2, 3) >>> v1 pvector([1, 2, 3]) r)elementsr r rrsr)r )abcrrcollections.abcrrZnumbersrrZpyrsistent._transformationsrrrr7rRrrrrrr4registerrosenvironrur\Zpvectorcrn ImportErrorrr r r rs>