csys/space: loc-iterator for scanning space around a location

This commit is contained in:
Helmut Merz 2026-09-27 15:29:56 +02:00
parent cd85171f22
commit 8249af7e14
2 changed files with 39 additions and 2 deletions

View file

@ -6,7 +6,8 @@
(:local-nicknames (:alx :alexandria) (:local-nicknames (:alx :alexandria)
(:util :scopes/util)) (:util :scopes/util))
(:export #:create #:put #:del #:fetch #:query (:export #:create #:put #:del #:fetch #:query
#:neighbors #:free-loc-at #:distance-sq)) #:neighbors #:free-loc-at #:distance-sq
#:loc-iterator))
(in-package :scopes/csys/space) (in-package :scopes/csys/space)
@ -29,9 +30,37 @@
(defun query (spc pattern) (defun query (spc pattern)
nil) nil)
(defun neighbors (spc loc &key (dist 2))) (defun neighbors (spc loc &key (dist 2))
(let* ((from (floor (sqrt dist)))
(to (- from)))
)
)
(defun free-loc-at (spc loc &key (dist 2))) (defun free-loc-at (spc loc &key (dist 2)))
(defun distance-sq (spc loc1 loc2)) (defun distance-sq (spc loc1 loc2))
(defun do-it (it fn)
(let (v)
(loop
(setf v (funcall it))
(if v
(funcall fn v)
(return)))))
(defun loc-iterator (from to &key (dims 2))
(labels (
(inc (cur idx)
(when (< idx dims)
(let ((new (1+ (aref cur idx))))
(setf (aref cur idx) new)
(when (> new to)
(setf (aref cur idx) from)
(setf cur (inc cur (1+ idx))))
cur))))
(let ((cur (make-array dims :initial-element from)))
(lambda ()
(when cur
(prog1 (make-array dims :initial-contents cur)
(setf cur (inc cur 0))))))))

View file

@ -87,6 +87,14 @@
(== (space:fetch spc #(1 2)) 46) (== (space:fetch spc #(1 2)) 46)
(space:del spc #(1 1)) (space:del spc #(1 1))
(== (space:fetch spc #(1 1)) nil) (== (space:fetch spc #(1 1)) nil)
(let ((it (space:loc-iterator -1 1))
loc all)
(loop
(setf loc (funcall it))
(if loc
(push loc all)
(return)))
(print all))
)) ))
(deftest test-basic-0 () (deftest test-basic-0 ()