a `Å]_Jã@sŽddlmZmZmZmZddlmZddlmZdd„Z Gdd„de ƒZ e  e ¡e  e ¡e  e ¡e  e ¡dd „Z d d „Ze eƒƒZd S) é)Ú ContainerÚIterableÚSizedÚHashable)Úreduce)ÚpmapcCs| || |d¡d¡S)Nré)ÚsetÚget)ZcountersÚelement©r ú6/usr/lib64/python3.9/site-packages/pyrsistent/_pbag.pyÚ_add_to_counterssrc@s eZdZdZdZdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dd„Z dd„Zdd„ZeZeZeZdd„Zdd„Zdd„Zdd „Zd!d"„Zd#S)$ÚPBagaÞ A persistent bag/multiset type. Requires elements to be hashable, and allows duplicates, but has no ordering. Bags are hashable. Do not instantiate directly, instead use the factory functions :py:func:`b` or :py:func:`pbag` to create an instance. Some examples: >>> s = pbag([1, 2, 3, 1]) >>> s2 = s.add(4) >>> s3 = s2.remove(1) >>> s pbag([1, 1, 2, 3]) >>> s2 pbag([1, 1, 2, 3, 4]) >>> s3 pbag([1, 2, 3, 4]) )Ú_countsÚ __weakref__cCs ||_dS)N©r)ÚselfZcountsr r r Ú__init__#sz PBag.__init__cCstt|j|ƒƒS)zà Add an element to the bag. >>> s = pbag([1]) >>> s2 = s.add(1) >>> s3 = s.add(2) >>> s2 pbag([1, 1]) >>> s3 pbag([1, 2]) )rrr©rr r r r Úadd&s zPBag.addcCs|rttt||jƒƒS|S)z‹ Update bag with all elements in iterable. >>> s = pbag([1]) >>> s.update([1, 2]) pbag([1, 1, 2]) )rrrr)rÚiterabler r r Úupdate4sz PBag.updatecCsP||jvrt|ƒ‚n4|j|dkr0|j |¡}n|j ||j|d¡}t|ƒS)zÔ Remove an element from the bag. >>> s = pbag([1, 1, 2]) >>> s2 = s.remove(1) >>> s3 = s.remove(2) >>> s2 pbag([1, 2]) >>> s3 pbag([1, 1]) r)rÚKeyErrorÚremover r)rr Znewcr r r rAs  z PBag.removecCs|j |d¡S)z¦ Return the number of times an element appears. >>> pbag([]).count('non-existent') 0 >>> pbag([1, 1, 2]).count(1) 2 r)rr rr r r ÚcountUs z PBag.countcCst|j ¡ƒS)ze Return the length including duplicates. >>> len(pbag([1, 1, 2])) 3 )ÚsumrZ itervalues©rr r r Ú__len__asz PBag.__len__ccs,|j ¡D]\}}t|ƒD] }|Vqq dS)z® Return an iterator of all elements, including duplicates. >>> list(pbag([1, 1, 2])) [1, 1, 2] >>> list(pbag([1, 2])) [1, 2] N)rÚ iteritemsÚrange)rÚeltrÚir r r Ú__iter__js  z PBag.__iter__cCs ||jvS)z� Check if an element is in the bag. >>> 1 in pbag([1, 1, 2]) True >>> 0 in pbag([1, 2]) False r)rr!r r r Ú __contains__ws zPBag.__contains__cCsd t|ƒ¡S)Nz pbag({0}))ÚformatÚlistrr r r Ú__repr__‚sz PBag.__repr__cCs t|ƒturtdƒ‚|j|jkS)zó Check if two bags are equivalent, honoring the number of duplicates, and ignoring insertion order. >>> pbag([1, 1, 2]) == pbag([1, 2]) False >>> pbag([2, 1, 0]) == pbag([0, 1, 2]) True z Can only compare PBag with PBags)ÚtyperÚ TypeErrorr©rÚotherr r r Ú__eq__…s z PBag.__eq__cCs tdƒ‚dS)NzPBags are not orderable)r)r*r r r Ú__lt__“sz PBag.__lt__cCsJt|tƒstS|j ¡}|j ¡D]\}}| |¡|||<q"t| ¡ƒS)z‚ Combine elements from two PBags. >>> pbag([1, 2, 2]) + pbag([2, 3, 3]) pbag([1, 2, 2, 2, 3, 3]) )Ú isinstancerÚNotImplementedrÚevolverrrÚ persistent)rr+ÚresultÚelemÚ other_countr r r Ú__add__œs   z PBag.__add__cCsjt|tƒstS|j ¡}|j ¡D]:\}}| |¡|}|dkrJ|||<q"||vr"| |¡q"t| ¡ƒS)zœ Remove elements from one PBag that are present in another. >>> pbag([1, 2, 2, 2, 3]) - pbag([2, 3, 3, 4]) pbag([1, 2, 2]) r) r.rr/rr0rrrr1)rr+r2r3r4Únewcountr r r Ú__sub__ªs    z PBag.__sub__cCsTt|tƒstS|j ¡}|j ¡D]$\}}| |¡}t||ƒ}|||<q"t| ¡ƒS)z¢ Union: Keep elements that are present in either of two PBags. >>> pbag([1, 2, 2, 2]) | pbag([2, 3, 3]) pbag([1, 2, 2, 2, 3, 3]) ) r.rr/rr0rrÚmaxr1)rr+r2r3r4rr6r r r Ú__or__¼s     z PBag.__or__cCsXt|tƒstStƒ ¡}|j ¡D](\}}t|| |¡ƒ}|dkr"|||<q"t|  ¡ƒS)z– Intersection: Only keep elements that are present in both PBags. >>> pbag([1, 2, 2, 2]) & pbag([2, 3, 3]) pbag([2]) r) r.rr/rr0rrÚminrr1)rr+r2r3rr6r r r Ú__and__Ìs   z PBag.__and__cCs t|jƒS)zÆ Hash based on value of elements. >>> m = pmap({pbag([1, 2]): "it's here!"}) >>> m[pbag([2, 1])] "it's here!" >>> pbag([1, 1, 2]) in m False )Úhashrrr r r Ú__hash__Üs z PBag.__hash__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú __slots__rrrrrrr#r$r'r,r-Ú__le__Ú__gt__Ú__ge__r5r7r9r;r=r r r r r s*     rcGst|ƒS)zª Construct a persistent bag. Takes an arbitrary number of arguments to insert into the new persistent bag. >>> b(1, 2, 3, 2) pbag([1, 2, 2, 3]) )Úpbag©Úelementsr r r Úbïs rIcCs|stSttt|tƒƒƒS)z• Convert an iterable to a persistent bag. Takes an iterable with elements to insert. >>> pbag([1, 2, 3, 2]) pbag([1, 2, 2, 3]) )Ú _EMPTY_PBAGrrrrrGr r r rFüs rFN)Úcollections.abcrrrrÚ functoolsrZpyrsistent._pmaprrÚobjectrÚregisterrIrFrJr r r r Ús  `