Skip to content

Commit 44eee4a

Browse files
committed
deque: fix __contains__ and index implementation
1 parent 5eb9416 commit 44eee4a

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

graalpython/lib-graalpython/_collections.py

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,73 @@ def __compare__(self, other, op):
389389
return x1 >= x2
390390
assert False, "bad value for op"
391391

392-
def __contains__(self, item):
393-
for itm in self:
394-
if itm == item:
392+
def __contains__(self, v):
393+
lock = self._getlock()
394+
n = self.len
395+
index = self.leftindex
396+
b = self.leftblock
397+
398+
while n >= 0:
399+
n -= 1
400+
assert b is not None
401+
item = b.data[index]
402+
if item == v:
395403
return True
404+
405+
self._checklock(lock)
406+
407+
index += 1
408+
if index == BLOCKLEN:
409+
b = b.rightlink
410+
index = 0
411+
396412
return False
397413

414+
def index(self, v, start=0, stop=-1):
415+
lock = self._getlock()
416+
if start < 0:
417+
start += self.len
418+
if start < 0:
419+
start = 0
420+
421+
if stop < 0:
422+
stop += self.len
423+
if stop < 0:
424+
stop = 0
425+
426+
if stop > self.len:
427+
stop = self.len
428+
429+
if start > stop:
430+
start = stop
431+
432+
index = self.leftindex
433+
b = self.leftblock
434+
435+
i = 0
436+
for i in range(start):
437+
index += 1
438+
if index == BLOCKLEN:
439+
b = b.rightlink
440+
index = 0
441+
442+
n = stop - i
443+
while n >= 0:
444+
n -= 1
445+
assert b is not None
446+
item = b.data[index]
447+
if item == v:
448+
return stop - n - 1
449+
450+
self._checklock(lock)
451+
452+
index += 1
453+
if index == BLOCKLEN:
454+
b = b.rightlink
455+
index = 0
456+
457+
raise ValueError("%s is not in deque" % v)
458+
398459
def __lt__(self, other):
399460
return self.__compare__(other, 'lt')
400461

@@ -413,7 +474,7 @@ def __gt__(self, other):
413474
def __ge__(self, other):
414475
return self.__compare__(other, 'ge')
415476

416-
def index(self, i):
477+
def _locate(self, i):
417478
if i < (self.len >> 1):
418479
i += self.leftindex
419480
b = self.leftblock
@@ -438,11 +499,11 @@ def _delitem(self, i):
438499
self.rotate(i)
439500

440501
def __getitem__(self, idx):
441-
b, i = self.index(idx)
502+
b, i = self._locate(idx)
442503
return b.data[i]
443504

444505
def __setitem__(self, idx, value):
445-
b, i = self.index(idx)
506+
b, i = self._locate(idx)
446507
b.data[i] = value
447508

448509
def __delitem__(self, idx):

0 commit comments

Comments
 (0)