Newer
Older
mazes / distances.lisp
;;;; distance.lisp

(in-package :mazes)

(defclass distance ()
  ((root :initarg :root :accessor root)
   (cells :initform (make-hash-table) :accessor cells))
  (:documentation "Keeps track of the distance of each cell from the root"))

(defmethod initialize-instance :after ((d distance) &key)
  (setf (gethash (root d) (cells d)) 0))

(defgeneric distance-to (distance &key)
  (:documentation "return the distance from root to :CELL"))

(defgeneric set-distance (distance &key)
  (:documentation "set the :DISTANCE from root to :CELL"))

(defgeneric cells-map (distance)
  (:documentation "return the hash map cells"))

(defmethod distance-to ((d distance) &key cell)
  (when (gethash cell (cells d))
    (gethash cell (cells d))
    nil))

(defmethod set-distance ((d distance) &key cell distance)
  (setf (gethash cell (cells d)) distance))

(defmethod cells-map ((d distance))
  (cells d))

(defun make-distance (root)
  (make-instance 'distance :root root))