Repository navigation
bpo-4356: Add key function support to the bisect module #20556
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d1a404
Python version, C version, and tests. Docs still needed
rhettinger 9c9b275
Add documentation
rhettinger 5f2e4e5
Add a blurb
rhettinger 92c40cb
Add spacing around the colon in the in-line code fragments
rhettinger bdfc074
Silence spurious warning from the doc build
rhettinger 5e3e5b9
Fix typo
rhettinger 899767c
Fix typo
rhettinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Python version, C version, and tests. Docs still needed
- Loading branch information
commit 2d1a404b0fdfe4120fef8d8cdf8abc21f28166fb
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,8 @@ module _bisect | |
| _Py_IDENTIFIER(insert); | ||
|
|
||
| static inline Py_ssize_t | ||
| internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi) | ||
| internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi, | ||
| PyObject* key) | ||
|
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. @rhettinger thank you so much for adding |
||
| { | ||
| PyObject *litem; | ||
| Py_ssize_t mid; | ||
|
|
@@ -39,6 +40,14 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t | |
| litem = PySequence_GetItem(list, mid); | ||
| if (litem == NULL) | ||
| return -1; | ||
| if (key != Py_None) { | ||
|
rhettinger marked this conversation as resolved.
|
||
| PyObject *newitem = PyObject_CallOneArg(key, litem); | ||
| if (newitem == NULL) { | ||
| Py_DECREF(litem); | ||
| return -1; | ||
| } | ||
| Py_SETREF(litem, newitem); | ||
| } | ||
| res = PyObject_RichCompareBool(item, litem, Py_LT); | ||
| Py_DECREF(litem); | ||
| if (res < 0) | ||
|
|
@@ -58,6 +67,8 @@ _bisect.bisect_right -> Py_ssize_t | |
| x: object | ||
| lo: Py_ssize_t = 0 | ||
| hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None | ||
| * | ||
| key: object = None | ||
|
|
||
| Return the index where to insert item x in list a, assuming a is sorted. | ||
|
|
||
|
|
@@ -71,10 +82,10 @@ slice of a to be searched. | |
|
|
||
| static Py_ssize_t | ||
| _bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, | ||
| Py_ssize_t lo, Py_ssize_t hi) | ||
| /*[clinic end generated code: output=419e150cf1d2a235 input=e72212b282c83375]*/ | ||
| Py_ssize_t lo, Py_ssize_t hi, PyObject *key) | ||
| /*[clinic end generated code: output=3a4bc09cc7c8a73d input=1313e9ca20c8bc3c]*/ | ||
| { | ||
| return internal_bisect_right(a, x, lo, hi); | ||
| return internal_bisect_right(a, x, lo, hi, key); | ||
| } | ||
|
|
||
| /*[clinic input] | ||
|
|
@@ -84,6 +95,8 @@ _bisect.insort_right | |
| x: object | ||
| lo: Py_ssize_t = 0 | ||
| hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None | ||
| * | ||
| key: object = None | ||
|
|
||
| Insert item x in list a, and keep it sorted assuming a is sorted. | ||
|
|
||
|
|
@@ -95,11 +108,22 @@ slice of a to be searched. | |
|
|
||
| static PyObject * | ||
| _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, | ||
| Py_ssize_t lo, Py_ssize_t hi) | ||
| /*[clinic end generated code: output=c2caa3d4cd02035a input=d1c45bfa68182669]*/ | ||
| Py_ssize_t lo, Py_ssize_t hi, PyObject *key) | ||
| /*[clinic end generated code: output=ac3bf26d07aedda2 input=44e1708e26b7b802]*/ | ||
| { | ||
| PyObject *result; | ||
| Py_ssize_t index = internal_bisect_right(a, x, lo, hi); | ||
| PyObject *result, *key_x; | ||
| Py_ssize_t index; | ||
|
|
||
| if (key == Py_None) { | ||
| index = internal_bisect_right(a, x, lo, hi, key); | ||
| } else { | ||
| key_x = PyObject_CallOneArg(key, x); | ||
| if (x == NULL) { | ||
| return NULL; | ||
| } | ||
| index = internal_bisect_right(a, key_x, lo, hi, key); | ||
| Py_DECREF(key_x); | ||
| } | ||
| if (index < 0) | ||
| return NULL; | ||
| if (PyList_CheckExact(a)) { | ||
|
|
@@ -117,7 +141,8 @@ _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, | |
| } | ||
|
|
||
| static inline Py_ssize_t | ||
| internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi) | ||
| internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi, | ||
| PyObject *key) | ||
| { | ||
| PyObject *litem; | ||
| Py_ssize_t mid; | ||
|
|
@@ -140,6 +165,14 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h | |
| litem = PySequence_GetItem(list, mid); | ||
| if (litem == NULL) | ||
| return -1; | ||
| if (key != Py_None) { | ||
| PyObject *newitem = PyObject_CallOneArg(key, litem); | ||
| if (newitem == NULL) { | ||
| Py_DECREF(litem); | ||
| return -1; | ||
| } | ||
| Py_SETREF(litem, newitem); | ||
| } | ||
| res = PyObject_RichCompareBool(litem, item, Py_LT); | ||
| Py_DECREF(litem); | ||
| if (res < 0) | ||
|
|
@@ -160,6 +193,8 @@ _bisect.bisect_left -> Py_ssize_t | |
| x: object | ||
| lo: Py_ssize_t = 0 | ||
| hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None | ||
| * | ||
| key: object = None | ||
|
|
||
| Return the index where to insert item x in list a, assuming a is sorted. | ||
|
|
||
|
|
@@ -173,10 +208,10 @@ slice of a to be searched. | |
|
|
||
| static Py_ssize_t | ||
| _bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, | ||
| Py_ssize_t lo, Py_ssize_t hi) | ||
| /*[clinic end generated code: output=af82168bc2856f24 input=2bd90f34afe5609f]*/ | ||
| Py_ssize_t lo, Py_ssize_t hi, PyObject *key) | ||
| /*[clinic end generated code: output=70749d6e5cae9284 input=3cbeec690f2f6c6e]*/ | ||
| { | ||
| return internal_bisect_left(a, x, lo, hi); | ||
| return internal_bisect_left(a, x, lo, hi, key); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -187,6 +222,8 @@ _bisect.insort_left | |
| x: object | ||
| lo: Py_ssize_t = 0 | ||
| hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None | ||
| * | ||
| key: object = None | ||
|
|
||
| Insert item x in list a, and keep it sorted assuming a is sorted. | ||
|
|
||
|
|
@@ -198,11 +235,22 @@ slice of a to be searched. | |
|
|
||
| static PyObject * | ||
| _bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x, | ||
| Py_ssize_t lo, Py_ssize_t hi) | ||
| /*[clinic end generated code: output=9e8356c0844a182b input=bc4583308bce00cc]*/ | ||
| Py_ssize_t lo, Py_ssize_t hi, PyObject *key) | ||
| /*[clinic end generated code: output=b1d33e5e7ffff11e input=3ab65d8784f585b1]*/ | ||
| { | ||
| PyObject *result; | ||
| Py_ssize_t index = internal_bisect_left(a, x, lo, hi); | ||
| PyObject *result, *key_x; | ||
| Py_ssize_t index; | ||
|
|
||
| if (key == Py_None) { | ||
| index = internal_bisect_left(a, x, lo, hi, key); | ||
| } else { | ||
| key_x = PyObject_CallOneArg(key, x); | ||
| if (x == NULL) { | ||
| return NULL; | ||
| } | ||
| index = internal_bisect_left(a, key_x, lo, hi, key); | ||
| Py_DECREF(key_x); | ||
| } | ||
| if (index < 0) | ||
| return NULL; | ||
| if (PyList_CheckExact(a)) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find suspicious that
key(x)is called here and that it's not handled bybisect_right(), e.g. I would have expected this to work:Is this on purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recall the discussion in the other PR whether the "searched for item" should be an argument to
keyor a value produced bykey.It's a bit clearer in the case of
insort_*functions because array member is needed for insertion, thus, key will be applied to this value, thus the latter above.Perhaps it's easier to disambiguate in the docs by mentioning how the
keywill be used, and/or talk about type or shape of key and array members?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the API is correct in that it supports typical usage patterns. insort() will insert entire records and bisect() will scan the key field for the first match in a range at or above the key value.
In SQL, the pattern would look like this:
In Python, the example would look like this:
If bisect() required an entire record as an input, it would preclude the ability to do searches like like this.
Here's another example that we would want to support:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll echo what Raymond explained. It would be more useful to a library like sortedcontainers if the argument to bisect was the field value rather than the record. After all, the key function can be applied to the record to get the field value if need be. And there are many cases where constructing the record may be expensive or impossible just to determine rank within a list. The field value API is compatible with having the record and key function but not the other way around.
The SortedKeyList data type provides both
bisect_leftandbisect_key_leftvariants. Thebisect_leftvariant simply applies the key function and callsbisect_key_leftso I think the API specifying the field value is more generically useful.