K i'>dZddlZdgZGddZGddZy)/Priority queue class with updatable priorities.N MappedQueuecHeZdZdZgdZdZdZdZdZdZ dZ d Z d Z y ) _HeapElementaThis proxy class separates the heap element from its priority. The idea is that using a 2-tuple (priority, element) works for sorting, but not for dict lookup because priorities are often floating point values so round-off can mess up equality. So, we need inequalities to look at the priority (for sorting) and equality (and hash) to look at the element to enable updates to the priority. Unfortunately, this class can be tricky to work with if you forget that `__lt__` compares the priority while `__eq__` compares the element. In `greedy_modularity_communities()` the following code is used to check that two _HeapElements differ in either element or priority: if d_oldmax != row_max or d_oldmax.priority != row_max.priority: If the priorities are the same, this implementation uses the element as a tiebreaker. This provides compatibility with older systems that use tuples to combine priority and elements. )priorityelement_hashc@||_||_t||_yN)rrhashr )selfrrs a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/utils/mapped_queue.py__init__z_HeapElement.__init__!s   '] c |j}|j|k(r |j|jkS|j|kS#t$r|j|kcYSwxYw#t$r}tdd}~wwxYwNzCConsider using a tuple, with a priority value that can be compared.rAttributeErrorr TypeErrorr otherother_priorityerrs r__lt__z_HeapElement.__lt__& )"^^N ==N * ||emm33 }}~-- )==5( ( )  Y ( AA$A! A!$ A=- A88A=c |j}|j|k(r |j|jkDS|j|kDS#t$r|j|kDcYSwxYw#t$r}tdd}~wwxYwrrrs r__gt__z_HeapElement.__gt__5rrcr |j|jk(S#t$r|j|k(cYSwxYwr )rr)r rs r__eq__z_HeapElement.__eq__Ds8 )<<5==0 0 )<<5( ( )s 66c|jSr )r r s r__hash__z_HeapElement.__hash__Js zzrcH|dk(r |jS|j|dz S)Nrrr)r indxs r __getitem__z_HeapElement.__getitem__Ms$ $ t}}Et||D1H/EErc#K|j |jEd{y7#t$r|jYywxYwwr )rrrr"s r__iter__z_HeapElement.__iter__Ps:mm || # # ,,  s1A)')A)AAAAc<d|jd|jdS)Nz _HeapElement(z, )r&r"s r__repr__z_HeapElement.__repr__Wst}}oR ~Q??rN) __name__ __module__ __qualname____doc__ __slots__rrrr r#r(r*r-rrrrs8,1I# . .) F@rrcLeZdZdZd dZdZdZd dZdZd dZ d Z d Z d Z y) raThe MappedQueue class implements a min-heap with removal and update-priority. The min heap uses heapq as well as custom written _siftup and _siftdown methods to allow the heap positions to be tracked by an additional dict keyed by element to position. The smallest element can be popped in O(1) time, new elements can be pushed in O(log n) time, and any element can be removed or updated in O(log n) time. The queue cannot contain duplicate elements and an attempt to push an element already in the queue will have no effect. MappedQueue complements the heapq package from the python standard library. While MappedQueue is designed for maximum compatibility with heapq, it adds element removal, lookup, and priority update. Parameters ---------- data : dict or iterable Examples -------- A `MappedQueue` can be created empty, or optionally, given a dictionary of initial elements and priorities. The methods `push`, `pop`, `remove`, and `update` operate on the queue. >>> colors_nm = {"red": 665, "blue": 470, "green": 550} >>> q = MappedQueue(colors_nm) >>> q.remove("red") >>> q.update("green", "violet", 400) >>> q.push("indigo", 425) True >>> [q.pop().element for i in range(len(q.heap))] ['violet', 'indigo', 'blue'] A `MappedQueue` can also be initialized with a list or other iterable. The priority is assumed to be the sort order of the items in the list. >>> q = MappedQueue([916, 50, 4609, 493, 237]) >>> q.remove(493) >>> q.update(237, 1117) >>> [q.pop() for i in range(len(q.heap))] [50, 916, 1117, 4609] An exception is raised if the elements are not comparable. >>> q = MappedQueue([100, "a"]) Traceback (most recent call last): ... TypeError: '<' not supported between instances of 'int' and 'str' To avoid the exception, use a dictionary to assign priorities to the elements. >>> q = MappedQueue({100: 0, "a": 1}) References ---------- .. [1] Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2001). Introduction to algorithms second edition. .. [2] Knuth, D. E. (1997). The art of computer programming (Vol. 3). Pearson Education. Nc|g|_nSt|tr3|jDcgc]\}}t ||c}}|_nt ||_i|_|jycc}}w)rN)heap isinstancedictitemsrlistposition_heapify)r datakvs rrzMappedQueue.__init__s\ <DI d #8< E1a+EDIT DI   FsA6ctj|jt|jDcic]\}}|| c}}|_t |jt |jk7r t dycc}}w)z+Restore heap invariant and recalculate map.z Heap contains duplicate elementsN)heapqheapifyr6 enumerater;lenAssertionError)r poselts rr<zMappedQueue._heapifyse dii 2;DII2FGhc3cG tyy>S/ / !CD D 0Hs Bc,t|jSr )rDr6r"s r__len__zMappedQueue.__len__s499~rc| t||}||jvryt|j}|jj |||j|<|j d|y)zAdd an element to the queue.FrT)rr;rDr6append _siftdown)r rGrrFs rpushzMappedQueue.pushsa  x-C $-- $))n   c q#rc8|jd}|j|=t|jdk(r|jj|S|jj}||jd<d|j|<|j d|S)z4Remove and return the smallest element in the queue.rr%)r6r;rDpop_siftup)r rGlasts rrOzMappedQueue.popsyiil MM#  tyy>Q  IIMMOJyy}} !  d Q rc| t||}|j|}||j|<|j|=||j|<|j|y)z/Replace an element in the queue with a new one.N)rr;r6rP)r rGnewrrFs rupdatezMappedQueue.updatesU  x-CmmC  # MM#   c SrcX |j|}|j|=|t|jdz k(r|jj y|jj }||j|<||j|<|j |y#t$rwxYw)z!Remove an element from the queue.r%N)r;KeyErrorrDr6rOrP)r rGrFrQs rremovezMappedQueue.removes --$C c" #dii.1$ $ IIMMO yy}} #! d S   s B B)cH|j|j}}t|}|}||}|dzdz}||kr7||}|dz} | |kr|| } || ks| }| }|||<|||<|}|dzdz}||kr7|dkDr%|dz dz } || } || ksn| ||<||| <| }|dkDr%|||<|||<y)zMove smaller child up until hitting a leaf. Built to mimic code for heapq._siftup only updating position dict too. r%rN)r6r;rD) r rFr6r;end_posstartposnewitem child_poschild right_posright parent_posparents rrPzMappedQueue._siftups DMMhd)s)AXN '!OE!A I7"Yu}!E )IDI!HUOCQI'! Ag'aJ*%FV#DI"HV CAgS rc|j|j}}||}||kDr%|dz dz }||}||ksn|||<|||<|}||kDr%|||<|||<y)zRestore invariant. keep swapping with parent until smaller. Built to mimic code for heapq._siftdown only updating position dict too. r%N)r6r;)r start_posrFr6r;r[r`ras rrLzMappedQueue._siftdowns DMMhs)Io'aJ*%FV#DI"HV CIoS rr ) r.r/r0r1rr<rIrMrOrTrWrPrLr3rrrr[s7;z E $ (% N r)r1rA__all__rrr3rrres-5 /P@P@fN N r