B +[X@s6ddlmZmZddlmZmZddlmZddlZddl Z ddl m Z ddZ d Z e dZe eZd d Zd d ZGdddeZe eGdddeZedeggZeeeeeedddZy:ddlZejdreZnddlmZeeeWnek r(eZYnXddZ dS))abstractmethodABCMeta)SequenceHashable)IntegralN) transformcCst|dS)N1)bincount)valr F/opt/alt/python37/lib64/python3.7/site-packages/pyrsistent/_pvector.py _bitcount sr cCs ||t|tr|n|S)N)tolist isinstancePVector)votheroperatorr r rcompare_pvectorsrcCs|dkr |St||S)N)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|_|S)N) superr__new__rrrr lenr!)clsr shiftroottailself) __class__r rr$"szPythonPVector.__new__cCs|jS)N)r)r*r r r__len__-szPythonPVector.__len__cCsdt|tr>|jdkr,|jdkr,|jdkr,|St||S|dkrP||j7}t |||t @S)Nr) rrstartrstep_EMPTY_PVECTORextendrrr _node_forBIT_MASK)r*rr r r __getitem__0s  zPythonPVector.__getitem__cCs ||S)N)r0)r*rr r r__add__@szPythonPVector.__add__cCsdt|S)Nz pvector({0}))formatstrr)r*r r r__repr__CszPythonPVector.__repr__cCs|S)N)r7)r*r r r__str__FszPythonPVector.__str__cCs t|S)N)iterr)r*r r r__iter__IszPythonPVector.__iter__cCs|jt|kpt||tjS)N)rr%rrne)r*rr r r__ne__NszPythonPVector.__ne__cCs$||kp"|jt|ko"t||tjS)N)rr%rreq)r*rr r r__eq__QszPythonPVector.__eq__cCst||tjS)N)rrgt)r*rr r r__gt__TszPythonPVector.__gt__cCst||tjS)N)rrlt)r*rr r r__lt__WszPythonPVector.__lt__cCst||tjS)N)rrge)r*rr r r__ge__ZszPythonPVector.__ge__cCst||tjS)N)rrle)r*rr r r__le__]szPythonPVector.__le__cCs2|dks|tkrtS|dkr |St||S)Nrr)r/r0r)r*timesr r r__mul__`s zPythonPVector.__mul__cCs8|r*|t8}x&|D]}||||qWn ||dS)N)SHIFT _fill_listr0)r*noder'the_listnr r rrJks  zPythonPVector._fill_listcCs&g}||j|j|||j|S)zK The fastest way to convert the vector into a python list. )rJrrr0r )r*rLr r rrss zPythonPVector.tolistcCs t|S)z8 Returns the content as a python tuple. )tupler)r*r r r_totuple|szPythonPVector._totuplecCs t|S)N)hashrO)r*r r r__hash__szPythonPVector.__hash__cGs t||S)N)r)r*transformationsr r rrszPythonPVector.transformcCst|ffS)N)pvectorr)r*r r r __reduce__szPythonPVector.__reduce__cGsRt|drtd|}x,tdt|dD]}||d|||<q.W|S)Nz)mset expected an even number of argumentsrr)r% TypeErrorevolverrange persistent)r*argsrWir r rmsets  zPythonPVector.msetc@sxeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZdS)zPythonPVector.Evolver) rrrr r! _dirty_nodes _extra_tail _cached_leafs _orig_pvectorcCs||dS)N)_reset)r*rr r r__init__szPythonPVector.Evolver.__init__cCst|tstdt|j|dkr8||jt|j7}|j|krZ|jt|jkrnnn|j||jSt |||t @S)Nz-'%s' object cannot be interpreted as an indexr) rrrVtype__name__rr%r^rr1r2)r*rr r rr3s &z!PythonPVector.Evolver.__getitem__cCsD|j|_|j|_|j|_|j|_|j|_i|_i|_g|_||_dS)N) rrrr r!r]r_r^r`)r*rr r rraszPythonPVector.Evolver._resetcCs|j||S)N)r^append)r*Zelementr r rres zPythonPVector.Evolver.appendcCs|j||S)N)r^r0)r*iterabler r rr0s zPythonPVector.Evolver.extendcCs |||<|S)Nr )r*rr r r rsetszPythonPVector.Evolver.setcCsTt|tstdt|j|dkr8||jt|j7}d|krN|jkrnn|j |t ?}|rt|||t @<nl||j krt |j|jkrt|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)Nz-'%s' object cannot be interpreted as an indexrTzIndex out of range: %s)rrrVrcrdrr%r^r_getrIr2r!idr r]list_do_setrrre IndexError)r*rr rKr r r __setitem__s(   *z!PythonPVector.Evolver.__setitem__cCszt||jkr|}nt|}d|jt|<|dkrN|||t@<||j|t?<n(||?t@}||t||||||<|S)NTr)rir]rjr2r_rIrk)r*levelrKr[r ret sub_indexr r rrks  zPythonPVector.Evolver._do_setcCs ||=|S)Nr )r*rr r rdeleteszPythonPVector.Evolver.deletecCsH|jrkr:|j||j|jg}|t7}n||j|j|j}||fS)Nr)rrrIr _new_pathr _push_tail)r* new_shiftnew_rootr r r_create_new_root:s  zPythonPVector._create_new_rootcCs\t|jtkr:t|j}||t|jd|j|j|S| \}}t|jd|||gS)Nr) r%r BRANCH_FACTORrjrerrrrr)r*r r|rrr r rreFs    zPythonPVector.appendcCs|dkr |S||t|gS)Nr)r}rI)r*rnrKr r rr}PszPythonPVector._new_pathcCs|\|_|_g|_dS)N)rrrr )r*r r r_mutating_insert_tailVsz#PythonPVector._mutating_insert_tailcCsHtt|j}||||}|j|t|}|j|7_||S)N)rr%r r0r)r*offsetsequenceZ max_delta_lenZdeltaZ delta_lenr r r_mutating_fill_tailZs  z!PythonPVector._mutating_fill_tailcCsRd}t|}x.||kr:|||}t|jtkr|qW|jt|j|_dS)Nr)r%rr rrrr!)r*rrZ sequence_lenr r r_mutating_extendbs   zPythonPVector._mutating_extendcCsFt|tr|nt|}|rB||d}||dd|S|S)Nrr)rrrrjrer)r*objrsZ new_vectorr r rr0ls 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)rjrIrerr2r%r~r})r*rnparentZ tail_noderorpr r rr~ws   zPythonPVector._push_tailcOs|j|f||S)N)rr)r*valuerZkwargsr r rrszPythonPVector.indexcCs||S)N)rr )r*rr r rr szPythonPVector.countNcCs|}|t||=t|S)N)rrr/r0)r*rrrsr r rrqs zPythonPVector.deletecCs|}||t|S)N)rremover/r0)r*rrsr r rrs zPythonPVector.remove)N)/rdrxry__doc__rzr$r,r3r4r7r8r:r<r>r@rBrDrFrH__rmul__rJrrOrQrrTr\objectr{rWrgrk staticmethodr1rrer}rrrr0r~rr rqr __classcell__r r )r+rrsR    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 rr,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 )r*rr r rr3s zPVector.__getitem__cCsdS)zm >>> v1 = v(1, 2) >>> v2 = v(3, 4) >>> v1 + v2 pvector([1, 2, 3, 4]) Nr )r*rr r rr4szPVector.__add__cCsdS)zY >>> v1 = v(1, 2) >>> 3 * v1 pvector([1, 2, 1, 2, 1, 2]) Nr )r*rGr r rrHszPVector.__mul__cCsdS)zo >>> v1 = v(1, 2, 3) >>> v2 = v(1, 2, 3) >>> hash(v1) == hash(v2) True Nr )r*r r rrQszPVector.__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 rrWs.zPVector.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*rZr r rr\&s z 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*r[r r r rrg3sz 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 rreDszPVector.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 rr0Ns zPVector.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*rrZrr r rrYs z 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*rr r rr fsz 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*rRr r rrpszPVector.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 rrqs zPVector.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*rr r rrs zPVector.remove)N)rdrxryrrr,r3r4rHrQrWr\rgrer0rr rrqrr r r rrs")   0   rr cCs t|S)z Create a new persistent vector containing the elements in iterable. >>> v1 = pvector([1, 2, 3]) >>> v1 pvector([1, 2, 3]) )r/r0)rfr r rpython_pvectorsrZPYRSISTENT_NO_C_EXTENSION)rScGst|S)z Create a new persistent vector containing all parameters to this function. >>> v1 = v(1, 2, 3) >>> v1 pvector([1, 2, 3]) )rS)elementsr r rrsr)r )!abcrrZ_compatrrZnumbersrrZsixZpyrsistent._transformationsrrrr2rIrrrrZ add_metaclassrr/registerrosenvironrhrSZpvectorcrc ImportErrorrr r r rsB