a `Å]_¦Xã@s$ddlmZmZddlmZmZddlmZddlZddl m Z dd„Z dZ e d Z e e ƒZd d „Zd d „ZGdd„deƒZGdd„ded�ZedeggƒZe e¡e e¡e e¡ddd„Zz:ddlZej d¡ràeZnddlmZe eeƒƒ¡Wne�yeZYn0dd„ZdS)é)ÚabstractmethodÚABCMeta)ÚSequenceÚHashable)ÚIntegralN©Ú transformcCst|ƒ d¡S)NÚ1)ÚbinÚcount)Úval©r ú9/usr/lib64/python3.9/site-packages/pyrsistent/_pvector.pyÚ _bitcountsré écCs || ¡t|tƒr| ¡n|ƒS©N)ÚtolistÚ isinstanceÚPVector)ÚvÚotherÚoperatorr r rÚcompare_pvectorsrcCs|dur |St||ƒSr)Úslice)ÚindexÚstopr r rÚ_index_or_slicesrcs^eZdZdZdZ‡fdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„ZeZdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„ZGd-d.„d.eƒZd/d0„Zd1d2„Zd3d4„Ze d5d6„ƒZ!d7d8„Z"d9d:„Z#d;d<„Z$d=d>„Z%d?d@„Z&dAdB„Z'dCdD„Z(dEdF„Z)dGdH„Z*dIdJ„Z+dPdLdM„Z,dNdO„Z-‡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 ÚshiftÚrootÚtailÚself©Ú __class__r rr&!szPythonPVector.__new__cCs|jSr)r©r,r r rÚ__len__,szPythonPVector.__len__cCsdt|tƒr>|jdur,|jdur,|jdur,|St | ¡|¡S|dkrP||j7}t   ||¡|t @S©Nr) rrÚstartrÚstepÚ_EMPTY_PVECTORÚextendrrrÚ _node_forÚBIT_MASK©r,rr r rÚ __getitem__/s  zPythonPVector.__getitem__cCs | |¡Sr)r5©r,rr r rÚ__add__?szPythonPVector.__add__cCsd t| ¡ƒ¡S)Nz pvector({0}))ÚformatÚstrrr/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|dƒo,|jt|ƒko,t||tjƒS)Nr0)Úhasattrrr'rrÚeqr:r r rrBPszPythonPVector.__eq__cCst||tjƒSr)rrÚgtr:r r rÚ__gt__SszPythonPVector.__gt__cCst||tjƒSr)rrÚltr:r r rÚ__lt__VszPythonPVector.__lt__cCst||tjƒSr)rrÚger:r r rÚ__ge__YszPythonPVector.__ge__cCst||tjƒSr)rrÚler:r r rÚ__le__\szPythonPVector.__le__cCs2|dks|turtS|dkr |St || ¡¡S©Nrr)r4r5r©r,Útimesr r rÚ__mul___s zPythonPVector.__mul__cCs4|r&|t8}|D]}| |||¡qn | |¡dSr)ÚSHIFTÚ _fill_listr5)r,Únoder)Úthe_listÚnr 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||ƒSrr©r,Ztransformationsr r rr…szPythonPVector.transformcCst| ¡ffSr)Úpvectorrr/r r rÚ __reduce__ˆszPythonPVector.__reduce__cGsNt|ƒdrtdƒ‚| ¡}tdt|ƒdƒD]}||d|||<q,| ¡S)Néz)mset expected an even number of argumentsrr)r'Ú TypeErrorÚevolverÚrangeÚ persistent)r,Úargsr`Úir r rÚmsetŒs  zPythonPVector.msetc@sxeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„ZdS)zPythonPVector.Evolver) rr r!r"r#Ú _dirty_nodesÚ _extra_tailÚ _cached_leafsÚ _orig_pvectorcCs| |¡dSr)Ú_reset©r,rr r rÚ__init__šszPythonPVector.Evolver.__init__cCs‚t|tƒstdt|ƒjƒ‚|dkr8||jt|jƒ7}|j|krZ|jt|jƒkrnnn|j||jSt  ||¡|t @S)Nú-'%s' object cannot be interpreted as an indexr) rrr_ÚtypeÚ__name__rr'rgrr6r7r8r r rr9�s &z!PythonPVector.Evolver.__getitem__cCsD|j|_|j|_|j|_|j|_|j|_i|_i|_g|_||_dSr) rr r!r"r#rfrhrgrirkr r rrj©szPythonPVector.Evolver._resetcCs|j |¡|Sr)rgÚappend)r,Úelementr r rrp´s zPythonPVector.Evolver.appendcCs|j |¡|Sr)rgr5)r,Úiterabler r rr5¸s zPythonPVector.Evolver.extendcCs |||<|Srr )r,rr r r rÚset¼szPythonPVector.Evolver.setcCsTt|tƒstdt|ƒjƒ‚|dkr8||jt|jƒ7}d|krN|jkrânn�|j  |t ?¡}|rt|||t @<nl||j krÊt |jƒ|jvrºt|jƒ|_d|jt |jƒ<|j|j|t ?<||j|t @<n| |j|j||¡|_nn|j|k�r|jt|jƒk�rnn||j||j<n2||jt|jƒk�rB|j |¡ntd|fƒ‚dS)NrmrTúIndex out of range: %s)rrr_rnrorr'rgrhÚgetrRr7r#Úidr"rfÚlistÚ_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)rvrfrwr7rhrRrx©r,ÚlevelrTrdr ÚretÚ sub_indexr r rrxÚs  zPythonPVector.Evolver._do_setcCs ||=|Srr r8r r rÚdeleteêszPythonPVector.Evolver.deletecCsH|jrkr:|j| |j|j¡g}|t7}n| |j|j|j¡}||fS©Nr)r rrRr!Ú _new_pathr"Ú _push_tail)r,Ú new_shiftÚnew_rootr r rÚ_create_new_root9s  zPythonPVector._create_new_rootcCs\t|jƒtkr:t|jƒ}| |¡t|jd|j|j|ƒS|  ¡\}}t|jd|||gƒSr‹) r'r"Ú BRANCH_FACTORrwrprrr r!r�)r,r rŠr�rŽr r rrpEs    zPythonPVector.appendcCs|dkr |S| |t|¡gSr1)rŒrR)r,r|rTr r rrŒOszPythonPVector._new_pathcCs| ¡\|_|_g|_dSr)r�r!r r"r/r r rÚ_mutating_insert_tailUsz#PythonPVector._mutating_insert_tailcCsHtt|jƒ}||||…}|j |¡t|ƒ}|j|7_||Sr)r‘r'r"r5r)r,ÚoffsetÚsequenceZ max_delta_lenZdeltaZ delta_lenr r rÚ_mutating_fill_tailYs  z!PythonPVector._mutating_fill_tailcCsNd}t|ƒ}||kr8| ||¡}t|jƒtkr | ¡q |jt|jƒ|_dSr1)r'r•r"r‘r’rr#)r,r”r“Z sequence_lenr r rÚ_mutating_extendas  zPythonPVector._mutating_extendcCsFt|tƒr| ¡nt|ƒ}|rB| |d¡}| |dd…¡|S|SrN)rrrrwrpr–)r,Úobjr�Z 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'r�rŒ)r,r|ÚparentZ tail_noder}r~r r rr�vs   zPythonPVector._push_tailcOs| ¡j|g|¢Ri|¤ŽSr)rr©r,ÚvaluercÚkwargsr r rr�szPythonPVector.indexcCs| ¡ |¡Sr)rr ©r,ršr r rr �szPythonPVector.countNcCs| ¡}|t||ƒ=t |¡Sr)rrr4r5)r,rrr�r r rr“s zPythonPVector.deletecCs| ¡}| |¡t |¡Sr)rÚremover4r5)r,ršr�r r rr�˜s zPythonPVector.remove)N)/ror†r‡Ú__doc__rˆr&r0r9r;r>r?rArCrBrGrIrKrMrQÚ__rmul__rSrrXrZrr]reÚobjectr‰r`rsrxÚ staticmethodr6r�rprŒr’r•r–r5r�rr rr�Ú __classcell__r r r-rrsT    p       rc@sÆeZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZed d „ƒZ ed d „ƒZ edd„ƒZ edd„ƒZ edd„ƒZ edd„ƒZedd„ƒZedd„ƒZedd„ƒZed!dd„ƒZedd „ƒ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 rr0ÇszPVector.__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 rr9ÎszPVector.__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 rrQãszPVector.__mul__cCsdS)zo >>> v1 = v(1, 2, 3) >>> v2 = v(1, 2, 3) >>> hash(v1) == hash(v2) True Nr r/r r rrZëszPVector.__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,r—r 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 r™r 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 rœr 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 rrŠszPVector.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 rœr r rr�–szPVector.remove)N)ror†r‡ržrr0r9r;rQrZr`rersrpr5rr rrr�r r r rr�s>)      /        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 rÚpython_pvector©sr¤ZPYRSISTENT_NO_C_EXTENSION©r\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 rr¿sr)r )ÚabcrrÚcollections.abcrrZnumbersrrZpyrsistent._transformationsrrr‘r7rRrrr rrr4Úregisterr¤ÚosÚenvironrur\ZpvectorcrnÚ ImportErrorrr r r rÚs>