csys/space: loc-iterator for scanning space around a location
This commit is contained in:
parent
cd85171f22
commit
8249af7e14
2 changed files with 39 additions and 2 deletions
|
|
@ -6,7 +6,8 @@
|
|||
(:local-nicknames (:alx :alexandria)
|
||||
(:util :scopes/util))
|
||||
(: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)
|
||||
|
||||
|
|
@ -29,9 +30,37 @@
|
|||
(defun query (spc pattern)
|
||||
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 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))))))))
|
||||
|
||||
|
|
|
|||
|
|
@ -87,6 +87,14 @@
|
|||
(== (space:fetch spc #(1 2)) 46)
|
||||
(space:del spc #(1 1))
|
||||
(== (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 ()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue