Skip to content

Commit 1b6f141

Browse files
Reduce overhead. (#338)
* Reduce overhead. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Reduce overhead. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 04dcbc3 commit 1b6f141

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

rtree/index.py

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -331,42 +331,38 @@ def flush(self) -> None:
331331
def get_coordinate_pointers(
332332
self, coordinates: Sequence[float]
333333
) -> tuple[float, float]:
334-
try:
335-
iter(coordinates)
336-
except TypeError:
337-
raise TypeError("Bounds must be a sequence")
338334
dimension = self.properties.dimension
335+
coordinates = list(coordinates)
339336

340-
mins = ctypes.c_double * dimension
341-
maxs = ctypes.c_double * dimension
342-
343-
if not self.interleaved:
344-
coordinates = Index.interleave(coordinates)
337+
arr = ctypes.c_double * dimension
338+
mins = arr()
345339

346-
# it's a point make it into a bbox. [x, y] => [x, y, x, y]
340+
# Point
347341
if len(coordinates) == dimension:
348-
coordinates = *coordinates, *coordinates
342+
mins[:] = coordinates
343+
maxs = mins
344+
# Bounding box
345+
else:
346+
maxs = arr()
349347

350-
if len(coordinates) != dimension * 2:
351-
raise RTreeError(
352-
"Coordinates must be in the form "
353-
"(minx, miny, maxx, maxy) or (x, y) for 2D indexes"
354-
)
348+
# Interleaved box
349+
if self.interleaved:
350+
p = coordinates[:dimension]
351+
q = coordinates[dimension:]
352+
# Non-interleaved box
353+
else:
354+
p = coordinates[::2]
355+
q = coordinates[1::2]
355356

356-
# so here all coords are in the form:
357-
# [xmin, ymin, zmin, xmax, ymax, zmax]
358-
for i in range(dimension):
359-
if not coordinates[i] <= coordinates[i + dimension]:
357+
mins[:] = p
358+
maxs[:] = q
359+
360+
if not p <= q:
360361
raise RTreeError(
361362
"Coordinates must not have minimums more than maximums"
362363
)
363364

364-
p_mins = mins(*[ctypes.c_double(coordinates[i]) for i in range(dimension)])
365-
p_maxs = maxs(
366-
*[ctypes.c_double(coordinates[i + dimension]) for i in range(dimension)]
367-
)
368-
369-
return (p_mins, p_maxs)
365+
return mins, maxs
370366

371367
@staticmethod
372368
def _get_time_doubles(times):
@@ -1231,16 +1227,14 @@ def py_next_item(p_id, p_mins, p_maxs, p_dimension, p_data, p_length):
12311227
return -1
12321228

12331229
if self.interleaved:
1234-
coordinates = Index.deinterleave(coordinates)
1235-
1236-
# this code assumes the coords are not interleaved.
1237-
# xmin, xmax, ymin, ymax, zmin, zmax
1238-
for i in range(dimension):
1239-
mins[i] = coordinates[i * 2]
1240-
maxs[i] = coordinates[(i * 2) + 1]
1230+
mins[:] = coordinates[:dimension]
1231+
maxs[:] = coordinates[dimension:]
1232+
else:
1233+
mins[:] = coordinates[::2]
1234+
maxs[:] = coordinates[1::2]
12411235

1242-
p_mins[0] = ctypes.cast(mins, ctypes.POINTER(ctypes.c_double))
1243-
p_maxs[0] = ctypes.cast(maxs, ctypes.POINTER(ctypes.c_double))
1236+
p_mins[0] = mins
1237+
p_maxs[0] = maxs
12441238

12451239
# set the dimension
12461240
p_dimension[0] = dimension
@@ -1510,9 +1504,15 @@ def __str__(self) -> str:
15101504
return pprint.pformat(self.as_dict())
15111505

15121506
def get_index_type(self) -> int:
1513-
return core.rt.IndexProperty_GetIndexType(self.handle)
1507+
try:
1508+
return self._type
1509+
except AttributeError:
1510+
type = core.rt.IndexProperty_GetIndexType(self.handle)
1511+
self._type: int = type
1512+
return type
15141513

15151514
def set_index_type(self, value: int) -> None:
1515+
self._type = value
15161516
return core.rt.IndexProperty_SetIndexType(self.handle, value)
15171517

15181518
type = property(get_index_type, set_index_type)
@@ -1531,11 +1531,17 @@ def set_variant(self, value: int) -> None:
15311531
:data:`RT_Linear`, :data:`RT_Quadratic`, and :data:`RT_Star`"""
15321532

15331533
def get_dimension(self) -> int:
1534-
return core.rt.IndexProperty_GetDimension(self.handle)
1534+
try:
1535+
return self._dimension
1536+
except AttributeError:
1537+
dim = core.rt.IndexProperty_GetDimension(self.handle)
1538+
self._dimension: int = dim
1539+
return dim
15351540

15361541
def set_dimension(self, value: int) -> None:
15371542
if value <= 0:
15381543
raise RTreeError("Negative or 0 dimensional indexes are not allowed")
1544+
self._dimension = value
15391545
return core.rt.IndexProperty_SetDimension(self.handle, value)
15401546

15411547
dimension = property(get_dimension, set_dimension)

0 commit comments

Comments
 (0)