-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-125420: implement Sequence.count
API on memoryview
objects
#125443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
12a429e
8234c5f
a253c9b
35c3c0a
be66e19
697b10c
2e3d8a7
d311794
41d977a
0c8d8ec
2a387d5
83dd0ea
d35d124
47c162c
f68900b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add :meth:`memoryview.count` to :class:`memoryview` objects. Patch by | ||
Bénédikt Tran. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2748,6 +2748,55 @@ static PySequenceMethods memory_as_sequence = { | |
}; | ||
|
||
|
||
/****************************************************************************/ | ||
/* Counting */ | ||
/****************************************************************************/ | ||
|
||
/*[clinic input] | ||
memoryview.count | ||
corona10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
value: object | ||
/ | ||
|
||
Count the number of occurrences of a value. | ||
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
memoryview_count(PyMemoryViewObject *self, PyObject *value) | ||
/*[clinic end generated code: output=e2c255a8d54eaa12 input=e3036ce1ed7d1823]*/ | ||
{ | ||
PyObject *iter = PyObject_GetIter(_PyObject_CAST(self)); | ||
if (iter == NULL) { | ||
return NULL; | ||
} | ||
|
||
Py_ssize_t count = 0; | ||
PyObject *item = NULL; | ||
while (PyIter_NextItem(iter, &item)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could probably make this significantly more efficient by looking at the memory directly without materializing an iterator and all the elements. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's what I suspected so I went for the simplest approach (at least to have the code around). |
||
if (item == NULL) { | ||
Py_DECREF(iter); | ||
return NULL; | ||
} | ||
if (item == value) { | ||
Py_DECREF(item); | ||
count++; // no overflow since count <= len(mv) <= PY_SSIZE_T_MAX | ||
continue; | ||
} | ||
int contained = PyObject_RichCompareBool(item, value, Py_EQ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, |
||
Py_DECREF(item); | ||
if (contained > 0) { // more likely than 'contained < 0' | ||
count++; // no overflow since count <= len(mv) <= PY_SSIZE_T_MAX | ||
} | ||
else if (contained < 0) { | ||
Py_DECREF(iter); | ||
return NULL; | ||
} | ||
} | ||
Py_DECREF(iter); | ||
return PyLong_FromSsize_t(count); | ||
} | ||
|
||
|
||
/**************************************************************************/ | ||
/* Lookup */ | ||
/**************************************************************************/ | ||
|
@@ -3370,6 +3419,7 @@ static PyMethodDef memory_methods[] = { | |
MEMORYVIEW_CAST_METHODDEF | ||
MEMORYVIEW_TOREADONLY_METHODDEF | ||
MEMORYVIEW__FROM_FLAGS_METHODDEF | ||
MEMORYVIEW_COUNT_METHODDEF | ||
MEMORYVIEW_INDEX_METHODDEF | ||
{"__enter__", memory_enter, METH_NOARGS, NULL}, | ||
{"__exit__", memory_exit, METH_VARARGS, memory_exit_doc}, | ||
|
Uh oh!
There was an error while loading. Please reload this page.