diff --git a/csys/space.lisp b/csys/space.lisp index 809d0aa..a5df309 100644 --- a/csys/space.lisp +++ b/csys/space.lisp @@ -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)))))))) + diff --git a/test/test-csys.lisp b/test/test-csys.lisp index 5814fbe..3aca591 100644 --- a/test/test-csys.lisp +++ b/test/test-csys.lisp @@ -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 ()