A python package for binary search
Project description
jbisect
Reusable implementation of the bisect / binary search algorithm.
This (obviously) competes with the standard library
bisect
package. Whereas bisect
only searches lists this package supports searching on a function, and supports both integer and
floating-point indices.
Install with:
pip install jbisect
Basic searching
jbisect
provides the function bisect_seq
for searching sequences:
from jbisect import bisect_seq
print(bisect_seq("011222355", "2"))
By default the entire sequence is searched, but you can use the parameters low
and high
to limit
the search range:
print(bisect_seq("011222355", "2", low=1, high=5))
You can use the side
parameters to configure whether to return the first match, or just past the
last match:
print(bisect_seq("011222355", "2", side="right"))
If you have a sequence that is descending, instead of ascending, you need to set the ordering
parameter:
print(bisect_seq("553222110", "2", ordering="descending"))
Searching functions:
The functions bisect_int_fn
and bisect_float_fn
can be used to search a function instead of a
sequence. These functions take the same low
, high
, side
and ordering
arguments as
bisect_seq
.
from jbisect import bisect_int_fn, bisect_float_fn
print(bisect_int_fn(lambda i: i * i, 16))
print(bisect_float_fn(lambda i: i * i, 2.0))
Searching predicates:
Finally the functions bisect_int_pred
and bisect_float_pred
can be used to find the first value
accepted by a predicate. pred
must be a function that returns a bool
, and for which there exists
some x
so that for all y<x
pred(y)
is False
; and for all y>=x
pred(y)
is
True
. bisect_*_pred
will then find x
.
from jbisect import bisect_int_pred, bisect_float_pred
print(bisect_int_pred(lambda i: i * i >= 16))
print(bisect_float_pred(lambda i: i * i >= 2.0))
NumPy support:
The package jbisect.numpy
adds functionality for searching NumPy arrays and functions.
Again, this obviously competes with the NumPy function
searchsorted
.
However, searchsorted
is limited in that it only searches existing arrays, and not functions.
jbisect.numpy
provides three functions bisect_numpy_array
, bisect_numpy_fn
and
bisect_numpy_pred
, mirroring the pure-python functions above. In the case of NumPy, we do not need
to distinguish between int
and float
up-front, as this is determined by the dtype
.
bisect_numpy_array
takes the new argument axis
that determines which axis of the input array to
search.
bisect_numpy_fn
and bisect_numpy_pred
takes the new arguments dtype
and shape
which
determines the dtype and shape of the input to the function/predicate, and the return type of the
function.
import numpy as np
from jbisect.numpy import bisect_numpy_array, bisect_numpy_fn, bisect_numpy_pred
print(bisect_numpy_array([0, 1, 1, 2, 2, 2, 3, 5, 5], 2))
print(
bisect_numpy_array(
[
[
[111, 112, 113, 114],
[121, 122, 123, 124],
[131, 132, 133, 134],
],
[
[211, 212, 213, 214],
[221, 222, 223, 224],
[231, 232, 233, 234],
],
],
[[122], [223]],
axis=1,
)
)
print(bisect_numpy_fn(lambda i: i * i, 16, low=0, high=1000, shape=(), dtype=np.int64))
print(bisect_numpy_pred(lambda i: i >= 4, shape=[], dtype=np.int64))
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jbisect-0.3.0.tar.gz
.
File metadata
- Download URL: jbisect-0.3.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 124fa3a58dd21122803c51223e4d85055e2beeee0480b119751dd56d6e163073 |
|
MD5 | 1a9695a75842f327e1a523a93ca80298 |
|
BLAKE2b-256 | c8e0f84ccf6d312546a6fd38b3312898abaabfa668d9f7d7bf0219e19d3873da |
File details
Details for the file jbisect-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: jbisect-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e34bbf9b9b5222d0f4cdc89b0dadf99270ace23037776a72c5e74392aa3d6d59 |
|
MD5 | ed2dce1f6adf3129534e62d0fac57040 |
|
BLAKE2b-256 | 1f8226f68c5c17182e5b9d72da06d68c7126a141e0d474129d537af252b89238 |