summaryrefslogtreecommitdiff
path: root/robots/algorithms.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'robots/algorithms.pyx')
-rw-r--r--robots/algorithms.pyx162
1 files changed, 0 insertions, 162 deletions
diff --git a/robots/algorithms.pyx b/robots/algorithms.pyx
deleted file mode 100644
index 3bf6916..0000000
--- a/robots/algorithms.pyx
+++ /dev/null
@@ -1,162 +0,0 @@
-# cython: language_level=3
-
-cimport cython
-
-import numpy as np
-cimport numpy as np
-
-ITYPE = np.int32
-ctypedef np.int32_t ITYPE_t
-
-ctypedef cython.bint bool
-
-INFINITY = float('inf')
-FAKE_INFINITY = 1<<31 - 1
-
-PREDECESSORS = {}
-for i in range(16):
- p = []
- if i & 1:
- p.append('U')
- if i & 2:
- p.append('D')
- if i & 4:
- p.append('L')
- if i & 8:
- p.append('R')
- PREDECESSORS[i] = p
-
-def distance_relaxer(distances, iterations=None):
- width = len(distances[0])
- height = len(distances)
-
- predecessors = [
- [
- []
- for _ in range(width)
- ]
- for _ in range(height)
- ]
-
- convert = {
- INFINITY: FAKE_INFINITY,
- None: -1,
- }
- unconvert = {
- FAKE_INFINITY: INFINITY,
- -1: None,
- }
- _distances = np.array(
- [
- [
- convert.get(v, v)
- for v in row
- ]
- for row in distances
- ],
- dtype=ITYPE,
- order='c',
- )
-
- if iterations is None:
- iterations = -1
-
- _distance_relaxer(
- _distances,
- predecessors,
- iterations,
- width, height,
- )
-
- for y in range(height):
- for x in range(width):
- v = _distances[y, x]
- distances[y][x] = unconvert.get(v, v)
-
- return predecessors
-
-@cython.boundscheck(False)
-cdef void _distance_relaxer(
- np.ndarray[ITYPE_t, ndim=2, mode='c'] distances,
- predecessors,
- int iterations,
- Py_ssize_t width,
- Py_ssize_t height,
-):
- cdef Py_ssize_t x, y
- cdef Py_ssize_t nx, ny
- cdef ITYPE_t distance, this_distance
-
- cdef int i = 0
- cdef bool updated
- cdef int p
-
- while i != iterations:
- updated = False
-
- for y from 0 <= y < height:
- for x from 0 <= x < width:
- distance = distances[y, x]
- if distance == -1:
- continue
-
- p = 0
-
- nx = x
- ny = (y - 1) % height
- this_distance = distances[ny, nx]
- if this_distance != -1:
- if this_distance != FAKE_INFINITY:
- this_distance += 1
- if this_distance < distances[y, x]:
- p = 0
- distances[y, x] = this_distance
- updated = True
- if this_distance == distances[y, x]:
- p |= 1
-
- nx = x
- ny = (y + 1) % height
- this_distance = distances[ny, nx]
- if this_distance != -1:
- if this_distance != FAKE_INFINITY:
- this_distance += 1
- if this_distance < distances[y, x]:
- p = 0
- distances[y, x] = this_distance
- updated = True
- if this_distance == distances[y, x]:
- p |= 2
-
- nx = (x - 1) % width
- ny = y
- this_distance = distances[ny, nx]
- if this_distance != -1:
- if this_distance != FAKE_INFINITY:
- this_distance += 1
- if this_distance < distances[y, x]:
- p = 0
- distances[y, x] = this_distance
- updated = True
- if this_distance == distances[y, x]:
- p |= 4
-
- nx = (x + 1) % width
- ny = y
- this_distance = distances[ny, nx]
- if this_distance != -1:
- if this_distance != FAKE_INFINITY:
- this_distance += 1
- if this_distance < distances[y, x]:
- p = 0
- distances[y, x] = this_distance
- updated = True
- if this_distance == distances[y, x]:
- p |= 8
-
- if p:
- predecessors[y][x] = PREDECESSORS[p]
-
- if not updated:
- break
- i += 1