From bb9920d25024258c22f2a006f4ee572f61e45ef6 Mon Sep 17 00:00:00 2001 From: Grant Jenks Date: Mon, 10 Oct 2022 23:17:11 -0700 Subject: [PATCH 1/2] Experiment with Cython optimizations --- src/sortedcontainers/sortedlist.pxd | 18 ++++ src/sortedcontainers/sortedlist.py | 41 ++------ tests/results_cython_sortedlist.txt | 144 ++++++++++++++-------------- 3 files changed, 98 insertions(+), 105 deletions(-) create mode 100644 src/sortedcontainers/sortedlist.pxd diff --git a/src/sortedcontainers/sortedlist.pxd b/src/sortedcontainers/sortedlist.pxd new file mode 100644 index 00000000..428aaf78 --- /dev/null +++ b/src/sortedcontainers/sortedlist.pxd @@ -0,0 +1,18 @@ +import cython + +cdef class SortedList: + cdef int _len + cdef int _load + cdef list _lists + cdef list _maxes + cdef list _index + cdef int _offset + + @cython.locals(pos=int, idx=int) + cpdef bisect_right(self, value) + + @cython.locals(pos=int, idx=int, total=int) + cdef _loc(self, pos, idx) + + @cython.locals(row0=list, head=int, tail=int, row1=list, size=int, tree=list, row=list) + cdef _build_index(self) diff --git a/src/sortedcontainers/sortedlist.py b/src/sortedcontainers/sortedlist.py index e3b58eb9..bae04b33 100644 --- a/src/sortedcontainers/sortedlist.py +++ b/src/sortedcontainers/sortedlist.py @@ -82,7 +82,7 @@ def wrapper(self): ############################################################################### -class SortedList(MutableSequence): +class SortedList: """Sorted list is a sorted mutable sequence. Sorted list values are maintained in sorted order. @@ -171,36 +171,6 @@ def __init__(self, iterable=None, key=None): self._update(iterable) - def __new__(cls, iterable=None, key=None): - """Create new sorted list or sorted-key list instance. - - Optional `key`-function argument will return an instance of subtype - :class:`SortedKeyList`. - - >>> sl = SortedList() - >>> isinstance(sl, SortedList) - True - >>> sl = SortedList(key=lambda x: -x) - >>> isinstance(sl, SortedList) - True - >>> isinstance(sl, SortedKeyList) - True - - :param iterable: initial values (optional) - :param key: function used to extract comparison key (optional) - :return: sorted list or sorted-key list instance - - """ - # pylint: disable=unused-argument - if key is None: - return object.__new__(cls) - else: - if cls is SortedList: - return object.__new__(SortedKeyList) - else: - raise TypeError('inherit SortedKeyList for key argument') - - @property def key(self): # pylint: disable=useless-return """Function used to extract comparison key from values. @@ -787,13 +757,15 @@ def __delitem__(self, index): if step == 1 and start < stop: if start == 0 and stop == self._len: - return self._clear() + self._clear() + return elif self._len <= 8 * (stop - start): values = self._getitem(slice(None, start)) if stop < self._len: values += self._getitem(slice(stop, None)) self._clear() - return self._update(values) + self._update(values) + return indices = range(start, stop, step) @@ -1683,6 +1655,9 @@ def _check(self): raise +MutableSequence.register(SortedList) + + def identity(value): "Identity function." return value diff --git a/tests/results_cython_sortedlist.txt b/tests/results_cython_sortedlist.txt index c27cc2aa..956fd078 100644 --- a/tests/results_cython_sortedlist.txt +++ b/tests/results_cython_sortedlist.txt @@ -1,72 +1,72 @@ -add SortedList_Cython 1000 1.0282965376973152e-05 1.1350028216838837e-05 1.0449904948472977e-05 1.0585598647594453e-05 -add SortedList_Cython 100000 0.001511867856606841 0.002125415951013565 0.0016422830522060394 0.0017123510129749774 -update_small SortedList_Cython 1000 0.00010433699935674667 0.0001062639057636261 0.00010458612814545631 0.00010487861000001431 -update_small SortedList_Cython 100000 0.014421820873394608 0.021340234903618693 0.01548386411741376 0.01696408954448998 -update_large SortedList_Cython 1000 0.00013786018826067448 0.00014123786240816116 0.00013873795978724957 0.00013909214176237582 -update_large SortedList_Cython 100000 0.028363503981381655 0.03286164510063827 0.03163244412280619 0.030968934018164872 -contains SortedList_Cython 1000 6.218906491994858e-06 6.959075108170509e-06 6.5159983932971954e-06 6.505381315946579e-06 -contains SortedList_Cython 100000 0.000971547793596983 0.0022372750099748373 0.0011588260531425476 0.0013576059602200985 -remove SortedList_Cython 1000 1.2140022590756416e-05 2.046814188361168e-05 1.331116072833538e-05 1.478227786719799e-05 -remove SortedList_Cython 100000 0.0013791928067803383 0.0026536339428275824 0.0014450699090957642 0.0016914472915232181 -delitem SortedList_Cython 1000 1.579313538968563e-05 3.283005207777023e-05 2.7597183361649513e-05 2.504806034266949e-05 -delitem SortedList_Cython 100000 0.002808838849887252 0.004047841997817159 0.003155739977955818 0.003327037952840328 -bisect SortedList_Cython 1000 7.104827091097832e-06 8.124858140945435e-06 7.310183718800545e-06 7.440382614731789e-06 -bisect SortedList_Cython 100000 0.0015003678854554892 0.001714952988550067 0.001644493080675602 0.001632282231003046 -getitem SortedList_Cython 1000 2.6798807084560394e-06 4.8461370170116425e-06 3.034016117453575e-06 3.356859087944031e-06 -getitem SortedList_Cython 100000 0.0011920849792659283 0.0014498550444841385 0.0013695149682462215 0.0013256249949336052 -pop SortedList_Cython 1000 7.1229878813028336e-06 7.953960448503494e-06 7.333001121878624e-06 7.417425513267517e-06 -pop SortedList_Cython 100000 0.0005630019586533308 0.0007037250325083733 0.0006666791159659624 0.0006358282174915075 -index SortedList_Cython 1000 9.892042726278305e-06 1.123105175793171e-05 1.023104414343834e-05 1.03700440376997e-05 -index SortedList_Cython 100000 0.0015908738132566214 0.0020019318908452988 0.001859043026342988 0.0018106119241565466 -iter SortedList_Cython 1000 9.056087583303452e-05 9.32798720896244e-05 9.30149108171463e-05 9.244997054338455e-05 -iter SortedList_Cython 100000 0.007698029046878219 0.009451698046177626 0.007783093024045229 0.00820884802378714 -count SortedList_Cython 1000 1.0218936949968338e-05 1.0987045243382454e-05 1.0464806109666824e-05 1.0484550148248673e-05 -count SortedList_Cython 100000 0.0014831149019300938 0.0017475818749517202 0.0016376259736716747 0.001607820764183998 -priorityqueue SortedList_Cython 1000 0.0001656238455325365 0.00018162583000957966 0.00017157196998596191 0.00017258529551327227 -priorityqueue SortedList_Cython 100000 0.01876824419014156 0.019855290884152055 0.0195867489092052 0.019461944606155156 -multiset SortedList_Cython 1000 0.00013532978482544422 0.00016640801914036274 0.00014195009134709835 0.00014775176532566547 -multiset SortedList_Cython 100000 0.019741414114832878 0.022189694922417402 0.020186125999316573 0.0206146914511919 -ranking SortedList_Cython 1000 0.00016269506886601448 0.00018059485591948032 0.00017019198276102543 0.00017122295685112476 -ranking SortedList_Cython 100000 0.023597739869728684 0.0264685379806906 0.024085784098133445 0.024582854146137832 -neighbor SortedList_Cython 1000 0.0001812279224395752 0.0002431059256196022 0.00018845312297344208 0.00019861962646245958 -neighbor SortedList_Cython 100000 0.02456414606422186 0.02625997317954898 0.025390984024852514 0.0254774184897542 -intervals SortedList_Cython 1000 0.00018244609236717224 0.00019659101963043213 0.00019196094945073128 0.00018957918509840966 -intervals SortedList_Cython 100000 0.02684336807578802 0.02973539289087057 0.028015755116939545 0.02828471721149981 -init SortedList_Cython 1000 9.97059978544712e-05 0.00010627787560224533 0.00010091206058859825 0.0001018451526761055 -init SortedList_Cython 100000 0.020764207001775503 0.02263542916625738 0.021233607083559036 0.02148824227042496 -add SortedList_NoCython 1000 1.2703938409686089e-05 1.4470890164375305e-05 1.302594318985939e-05 1.3323361054062844e-05 -add SortedList_NoCython 100000 0.0014942979905754328 0.0016952070873230696 0.0015394019428640604 0.00155768315307796 -update_small SortedList_NoCython 1000 0.00010994588956236839 0.00011379504576325417 0.0001102411188185215 0.00011094240471720695 -update_small SortedList_NoCython 100000 0.013594563119113445 0.0145641069393605 0.013915546936914325 0.014017350832000374 -update_large SortedList_NoCython 1000 0.00013976986519992352 0.0001497508492320776 0.00014039408415555954 0.00014227917417883874 -update_large SortedList_NoCython 100000 0.025172156048938632 0.026811009040102363 0.025622522924095392 0.02585788955911994 -contains SortedList_NoCython 1000 6.802845746278763e-06 7.51204788684845e-06 7.001915946602821e-06 7.1123242378234865e-06 -contains SortedList_NoCython 100000 0.0009690520819276571 0.0011068019084632397 0.0009809799958020449 0.0010102054104208947 -remove SortedList_NoCython 1000 1.267390325665474e-05 1.3980083167552948e-05 1.285085454583168e-05 1.3072555884718894e-05 -remove SortedList_NoCython 100000 0.0015128080267459154 0.0017439799848943949 0.0015516069252043962 0.0015814946498721838 -delitem SortedList_NoCython 1000 1.8308870494365692e-05 2.1714018657803535e-05 1.915404573082924e-05 1.9578402861952783e-05 -delitem SortedList_NoCython 100000 0.004452356835827231 0.004808577010408044 0.004590703174471855 0.004626936605200171 -bisect SortedList_NoCython 1000 7.850117981433868e-06 8.660135790705681e-06 8.03195871412754e-06 8.115265518426896e-06 -bisect SortedList_NoCython 100000 0.0022534357849508524 0.002627043053507805 0.00239735608920455 0.0024164220318198203 -getitem SortedList_NoCython 1000 6.71902671456337e-06 7.2659458965063095e-06 6.923917680978775e-06 6.9714151322841644e-06 -getitem SortedList_NoCython 100000 0.002208190970122814 0.0026764790527522564 0.0024654101580381393 0.0024734144564718006 -pop SortedList_NoCython 1000 9.774928912520409e-06 1.1201947927474976e-05 1.0117189958691597e-05 1.027560792863369e-05 -pop SortedList_NoCython 100000 0.0008459780365228653 0.0010066349059343338 0.0008917748928070068 0.0009119533933699131 -index SortedList_NoCython 1000 1.0761898010969162e-05 1.2122094631195068e-05 1.0942108929157257e-05 1.1233240365982056e-05 -index SortedList_NoCython 100000 0.002584321890026331 0.0031314590014517307 0.002662468934431672 0.0028127513360232115 -iter SortedList_NoCython 1000 7.981713861227036e-05 8.026300929486752e-05 8.013704791665077e-05 8.006184361875057e-05 -iter SortedList_NoCython 100000 0.007859930861741304 0.009185057133436203 0.008989966008812189 0.008723416598513722 -count SortedList_NoCython 1000 1.2832926586270332e-05 1.3699056580662727e-05 1.2859003618359566e-05 1.303376629948616e-05 -count SortedList_NoCython 100000 0.0015466799959540367 0.0016764409374445677 0.0016161459498107433 0.0016128033865243196 -priorityqueue SortedList_NoCython 1000 0.0001654301304370165 0.0001830151304602623 0.00016956799663603306 0.00017216005362570287 -priorityqueue SortedList_NoCython 100000 0.020327133825048804 0.022182474145665765 0.021629646187648177 0.02131588160991669 -multiset SortedList_NoCython 1000 0.00014235894195735455 0.00018803193233907223 0.000151381129398942 0.00015688962303102018 -multiset SortedList_NoCython 100000 0.02211070992052555 0.024902089964598417 0.022644519107416272 0.023191482201218604 -ranking SortedList_NoCython 1000 0.00015719211660325527 0.00019993307068943977 0.00016106199473142624 0.00016936538740992547 -ranking SortedList_NoCython 100000 0.035726954927667975 0.040121186058968306 0.03779471595771611 0.03802287857979536 -neighbor SortedList_NoCython 1000 0.00016713794320821762 0.00018147798255085945 0.00017752591520547867 0.00017519714310765266 -neighbor SortedList_NoCython 100000 0.033184452913701534 0.037659441120922565 0.03486145194619894 0.035245278803631666 -intervals SortedList_NoCython 1000 0.00019860407337546349 0.00021687382832169533 0.00020599900744855404 0.00020737797021865844 -intervals SortedList_NoCython 100000 0.04160309792496264 0.04442327911965549 0.04396203299984336 0.043163195811212064 -init SortedList_NoCython 1000 0.0001137938816100359 0.0001225180458277464 0.00011622300371527672 0.00011720918118953704 -init SortedList_NoCython 100000 0.021217013942077756 0.02263573999516666 0.022167363204061985 0.021971521573141216 +add SortedList_Cython 1000 1.3520009815692902e-05 1.544598489999771e-05 1.4172866940498352e-05 1.4288816601037979e-05 +add SortedList_Cython 100000 0.0016620811074972153 0.0020236289128661156 0.0019171591848134995 0.0018806386739015578 +update_small SortedList_Cython 1000 0.00013065501116216183 0.00013645505532622337 0.0001316559500992298 0.00013238596729934216 +update_small SortedList_Cython 100000 0.01446120603941381 0.015117344912141562 0.01474749413318932 0.014743279432877898 +update_large SortedList_Cython 1000 0.00012681400403380394 0.00013010087423026562 0.00012781587429344654 0.00012793997302651405 +update_large SortedList_Cython 100000 0.02856804709881544 0.031640419038012624 0.030459654983133078 0.03014136659912765 +contains SortedList_Cython 1000 8.234987035393715e-06 9.472016245126724e-06 8.736969903111458e-06 8.791033178567886e-06 +contains SortedList_Cython 100000 0.0010650181211531162 0.0011860330123454332 0.0011396321933716536 0.001126285083591938 +remove SortedList_Cython 1000 1.3664131984114647e-05 1.4912104234099388e-05 1.406506635248661e-05 1.4138827100396157e-05 +remove SortedList_Cython 100000 0.0018336849752813578 0.002332191914319992 0.00192033383063972 0.0019978697411715983 +delitem SortedList_Cython 1000 1.940503716468811e-05 2.3169908672571182e-05 2.0434148609638214e-05 2.1081604063510895e-05 +delitem SortedList_Cython 100000 0.004717967938631773 0.0050023498479276896 0.004853629972785711 0.00484746708534658 +bisect SortedList_Cython 1000 7.456867024302483e-06 8.876202628016472e-06 7.831957191228867e-06 7.922621443867684e-06 +bisect SortedList_Cython 100000 0.002507169032469392 0.00263350922614336 0.0025408489163964987 0.00256128222681582 +getitem SortedList_Cython 1000 5.566049367189407e-06 5.983049049973488e-06 5.715060979127884e-06 5.761254578828812e-06 +getitem SortedList_Cython 100000 0.0023161659482866526 0.002533762948587537 0.0024023018777370453 0.0024064425379037857 +pop SortedList_Cython 1000 1.0665971785783768e-05 1.5319092199206352e-05 1.0768184438347816e-05 1.1782441288232803e-05 +pop SortedList_Cython 100000 0.0009043749887496233 0.0010169469751417637 0.0009520470630377531 0.0009584980085492134 +index SortedList_Cython 1000 1.2220814824104309e-05 1.302408054471016e-05 1.234002411365509e-05 1.245434395968914e-05 +index SortedList_Cython 100000 0.002730316948145628 0.003037016838788986 0.002770820865407586 0.002816773485392332 +iter SortedList_Cython 1000 7.99668487161398e-05 8.260505273938179e-05 8.16520769149065e-05 8.154241368174553e-05 +iter SortedList_Cython 100000 0.00802747206762433 0.008339553838595748 0.008088847156614065 0.008138177217915654 +count SortedList_Cython 1000 1.1154916137456894e-05 1.1800089851021767e-05 1.1219177395105362e-05 1.1355383321642876e-05 +count SortedList_Cython 100000 0.0016540950164198875 0.001853482099249959 0.0017235041595995426 0.0017455142457038163 +priorityqueue SortedList_Cython 1000 0.00017695198766887188 0.00019033486023545265 0.0001854151487350464 0.00018412480130791664 +priorityqueue SortedList_Cython 100000 0.020374572835862637 0.023506593890488148 0.021794720087200403 0.021832241164520382 +multiset SortedList_Cython 1000 0.00012691994197666645 0.00015235808677971363 0.0001346999779343605 0.00013773376122117043 +multiset SortedList_Cython 100000 0.02195705007761717 0.024066413985565305 0.02214368199929595 0.02254639924503863 +ranking SortedList_Cython 1000 0.00015772716142237186 0.0001670289784669876 0.00016251602210104465 0.00016275225207209587 +ranking SortedList_Cython 100000 0.03520679590292275 0.038326303008943796 0.035943676019087434 0.0364161959849298 +neighbor SortedList_Cython 1000 0.00014732102863490582 0.00015922589227557182 0.00015113106928765774 0.00015250416472554207 +neighbor SortedList_Cython 100000 0.03307073190808296 0.035357526037842035 0.03396537993103266 0.03413629122078419 +intervals SortedList_Cython 1000 0.00018159998580813408 0.00021166307851672173 0.00018713902682065964 0.00019162986427545547 +intervals SortedList_Cython 100000 0.040242854971438646 0.043967243982478976 0.04097003978677094 0.04144595973193645 +init SortedList_Cython 1000 9.499397128820419e-05 0.00010108202695846558 9.706895798444748e-05 9.736092761158943e-05 +init SortedList_Cython 100000 0.02288922807201743 0.023580998880788684 0.023207637947052717 0.023218103172257542 +add SortedList_NoCython 1000 1.402711495757103e-05 1.540803350508213e-05 1.412699930369854e-05 1.4426419511437416e-05 +add SortedList_NoCython 100000 0.001654328079894185 0.0021721399389207363 0.0017959640827029943 0.001860568020492792 +update_small SortedList_NoCython 1000 0.00012081093154847622 0.00012335390783846378 0.0001223569270223379 0.00012231394648551941 +update_small SortedList_NoCython 100000 0.014225948136299849 0.015335384057834744 0.014547240920364857 0.014675149274989963 +update_large SortedList_NoCython 1000 0.0001392969861626625 0.00014903303235769272 0.0001452208962291479 0.0001437768340110779 +update_large SortedList_NoCython 100000 0.026894232956692576 0.0300550339743495 0.027340935077518225 0.027879917807877064 +contains SortedList_NoCython 1000 7.351860404014587e-06 8.224975317716599e-06 7.500872015953064e-06 7.674191147089005e-06 +contains SortedList_NoCython 100000 0.0010091110598295927 0.001224234001711011 0.0010918742045760155 0.001119283866137266 +remove SortedList_NoCython 1000 1.4070887118577957e-05 1.548090949654579e-05 1.4456920325756073e-05 1.4547724276781082e-05 +remove SortedList_NoCython 100000 0.001592904096469283 0.001725250855088234 0.0016593141481280327 0.0016654114238917827 +delitem SortedList_NoCython 1000 1.7049023881554604e-05 1.99850182980299e-05 1.8167076632380486e-05 1.820344477891922e-05 +delitem SortedList_NoCython 100000 0.004554056096822023 0.004876077175140381 0.0047672458458691835 0.004712387640029192 +bisect SortedList_NoCython 1000 7.5660645961761475e-06 8.10110941529274e-06 7.71298073232174e-06 7.777055725455283e-06 +bisect SortedList_NoCython 100000 0.002358820056542754 0.00239745294675231 0.0023766839876770973 0.002379934350028634 +getitem SortedList_NoCython 1000 5.356036126613617e-06 6.0889869928359985e-06 5.38211315870285e-06 5.566840991377831e-06 +getitem SortedList_NoCython 100000 0.002274903003126383 0.00266569503583014 0.0024277649354189634 0.002432610187679529 +pop SortedList_NoCython 1000 1.0394956916570663e-05 1.2140022590756416e-05 1.0516960173845291e-05 1.0824762284755707e-05 +pop SortedList_NoCython 100000 0.0009169438853859901 0.0010673790238797665 0.0009386080782860518 0.0009568922221660614 +index SortedList_NoCython 1000 1.1418946087360382e-05 1.4331890270113945e-05 1.270999200642109e-05 1.257890835404396e-05 +index SortedList_NoCython 100000 0.0027110851369798183 0.0029504899866878986 0.0027761131059378386 0.0027978264726698397 +iter SortedList_NoCython 1000 7.708091288805008e-05 7.951701991260052e-05 7.760617882013321e-05 7.79174268245697e-05 +iter SortedList_NoCython 100000 0.007727029966190457 0.008566227043047547 0.008086557034403086 0.00814053718931973 +count SortedList_NoCython 1000 1.2520933523774147e-05 1.3238983228802681e-05 1.2831063941121101e-05 1.2897001579403877e-05 +count SortedList_NoCython 100000 0.0015995500143617392 0.0016703670844435692 0.0016180290840566158 0.001626088609918952 +priorityqueue SortedList_NoCython 1000 0.0001685810275375843 0.00018175900913774967 0.0001747331116348505 0.00017452966421842574 +priorityqueue SortedList_NoCython 100000 0.020543314050883055 0.023762802127748728 0.0219338140450418 0.021897096280008556 +multiset SortedList_NoCython 1000 0.00015673693269491196 0.00019525783136487007 0.0001675160601735115 0.00017097434028983115 +multiset SortedList_NoCython 100000 0.021965665044263005 0.02402835199609399 0.022606218932196498 0.022794065391644837 +ranking SortedList_NoCython 1000 0.00015661492943763733 0.00017442903481423855 0.00016027595847845078 0.00016299216076731682 +ranking SortedList_NoCython 100000 0.035245703998953104 0.03782511409372091 0.03558035800233483 0.036278882436454295 +neighbor SortedList_NoCython 1000 0.00016591791063547134 0.00018138298764824867 0.0001724350731819868 0.0001736905425786972 +neighbor SortedList_NoCython 100000 0.03403324307873845 0.0389704080298543 0.037652424070984125 0.03704938907176256 +intervals SortedList_NoCython 1000 0.00018548802472651005 0.00020548910833895206 0.00019328715279698372 0.0001954326406121254 +intervals SortedList_NoCython 100000 0.04170571290887892 0.04449074296280742 0.04377819621004164 0.04335606563836336 +init SortedList_NoCython 1000 0.0001146811991930008 0.00012445892207324505 0.00011671707034111023 0.0001179664395749569 +init SortedList_NoCython 100000 0.021722911158576608 0.0231752370018512 0.0229025820735842 0.022618040814995764 From b23c661b2f8e813dbbfa548472e917eb43b40a24 Mon Sep 17 00:00:00 2001 From: Grant Jenks Date: Sat, 15 Oct 2022 15:55:24 -0700 Subject: [PATCH 2/2] Try adding static types using pxd --- src/sortedcontainers/sortedlist.pxd | 51 ++++++++++++++++++++++++++--- src/sortedcontainers/sortedlist.py | 10 +++--- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/sortedcontainers/sortedlist.pxd b/src/sortedcontainers/sortedlist.pxd index 428aaf78..36124abb 100644 --- a/src/sortedcontainers/sortedlist.pxd +++ b/src/sortedcontainers/sortedlist.pxd @@ -1,5 +1,6 @@ import cython + cdef class SortedList: cdef int _len cdef int _load @@ -8,11 +9,53 @@ cdef class SortedList: cdef list _index cdef int _offset + @cython.locals(pos=int) + cpdef add(self, value) + + @cython.locals(pos=int, _load=int, _lists_pos=list, child=int) + cpdef _expand(self, pos) + + @cython.locals(pos=int, idx=int) + cpdef discard(self, value) + + @cython.locals(pos=int, idx=int) + cpdef remove(self, value) + + @cython.locals(pos=int, idx=int, _lists_pos=list, child=int) + cpdef _delete(self, pos, idx) + + @cython.locals(pos=int, idx=int, total=int, index_pos_1=int) + cpdef _loc(self, pos, idx) + + @cython.locals( + idx=int, + _lists_1=list, + last_len=int, + pos=int, + child=int, + index_child=int, + ) + cpdef _pos(self, idx) + + @cython.locals( + row0=list, + head=int, + tail=int, + row1=list, + size=int, + tree=list, + row=list, + ) + cpdef _build_index(self) + + @cython.locals(pos=int, idx=int) + cpdef bisect_left(self, value) + @cython.locals(pos=int, idx=int) cpdef bisect_right(self, value) - @cython.locals(pos=int, idx=int, total=int) - cdef _loc(self, pos, idx) + cpdef _islice(self, min_pos, min_idx, max_pos, max_idx, reverse) + + cpdef count(self, value) - @cython.locals(row0=list, head=int, tail=int, row1=list, size=int, tree=list, row=list) - cdef _build_index(self) + cpdef insert(self, index, value) diff --git a/src/sortedcontainers/sortedlist.py b/src/sortedcontainers/sortedlist.py index bae04b33..1f525e5f 100644 --- a/src/sortedcontainers/sortedlist.py +++ b/src/sortedcontainers/sortedlist.py @@ -268,11 +268,11 @@ def _expand(self, pos): _load = self._load _lists = self._lists _index = self._index + _lists_pos = _lists[pos] - if len(_lists[pos]) > (_load << 1): + if len(_lists_pos) > (_load << 1): _maxes = self._maxes - _lists_pos = _lists[pos] half = _lists_pos[_load:] del _lists_pos[_load:] _maxes[pos] = _lists_pos[-1] @@ -559,7 +559,8 @@ def _loc(self, pos, idx): # account the total below the left child node. if not pos & 1: - total += _index[pos - 1] + index_pos_1 = _index[pos - 1] + total += index_pos_1 # Advance pos to the parent node. @@ -624,7 +625,8 @@ def _pos(self, idx): """ if idx < 0: - last_len = len(self._lists[-1]) + _lists_1 = self._lists[-1] + last_len = len(_lists_1) if (-idx) <= last_len: return len(self._lists) - 1, last_len + idx