From e5366ad6bf2a28db556a0589fa0dc896f6447324 Mon Sep 17 00:00:00 2001 From: Scott Boyce Date: Sun, 13 Oct 2024 19:09:19 -0700 Subject: [PATCH] add index remove and count methods --- NumpyDeque/NumpyDeque.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/NumpyDeque/NumpyDeque.py b/NumpyDeque/NumpyDeque.py index 55d1ddb..48b6ba8 100644 --- a/NumpyDeque/NumpyDeque.py +++ b/NumpyDeque/NumpyDeque.py @@ -561,6 +561,50 @@ def where(self, value): """ return np.where(self.deque == value)[0] + def index(self, value, start=0, stop=None): + """ + Return the deque index of value (at or after index start and before index stop). + Returns the first match + + Parameters: + value: The value to search for. + + Returns: + int: The the first index that value is found at. + + """ + p = self.where(value) + if len(p) > 0: + if stop is None: + stop = self.size + for i in p: + if start <= i < stop: + return i + return None + + def remove(self, value): + """ + Remove the first occurrence of value. + + Parameters: + value: The value to search for. + """ + i = self.index(value) + if i is not None: + self.drop(i) + + def count(self, value): + """ + Count the number of deque elements equal to value. + + Parameters: + value: The value that is being counted. + + Returns: + int: The count. + """ + return np.count_nonzero(self.deque == value) + def __getitem__(self, index): return self.queue[index]