Skip to content

Commit e5366ad

Browse files
committed
add index remove and count methods
1 parent e9b1c26 commit e5366ad

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

NumpyDeque/NumpyDeque.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,50 @@ def where(self, value):
561561
"""
562562
return np.where(self.deque == value)[0]
563563

564+
def index(self, value, start=0, stop=None):
565+
"""
566+
Return the deque index of value (at or after index start and before index stop).
567+
Returns the first match
568+
569+
Parameters:
570+
value: The value to search for.
571+
572+
Returns:
573+
int: The the first index that value is found at.
574+
575+
"""
576+
p = self.where(value)
577+
if len(p) > 0:
578+
if stop is None:
579+
stop = self.size
580+
for i in p:
581+
if start <= i < stop:
582+
return i
583+
return None
584+
585+
def remove(self, value):
586+
"""
587+
Remove the first occurrence of value.
588+
589+
Parameters:
590+
value: The value to search for.
591+
"""
592+
i = self.index(value)
593+
if i is not None:
594+
self.drop(i)
595+
596+
def count(self, value):
597+
"""
598+
Count the number of deque elements equal to value.
599+
600+
Parameters:
601+
value: The value that is being counted.
602+
603+
Returns:
604+
int: The count.
605+
"""
606+
return np.count_nonzero(self.deque == value)
607+
564608
def __getitem__(self, index):
565609
return self.queue[index]
566610

0 commit comments

Comments
 (0)