(in-package #:mazes) (defclass distances () ((root :initarg :root :accessor root-cell) (cells :initarg :cells :initform (make-hash-table) :accessor distance-cells)) (:documentation "Keeps track of the distance of each cell from the given root cell.")) (defmethod initialize-instance :after ((d distances) &key) (setf (gethash (root-cell d) (distance-cells d)) 0)) (defgeneric set-distance (distance &key) (:documentation "Set the distance of the given cell from the root cell")) (defmethod set-distance ((d distances) &key cell distance) (let ((cells (distance-cells d))) (setf (gethash cell cells) distance))) (defgeneric distance-all-cells (distances) (:documentation "Return a list of cells with their respective distance from root")) (defmethod distance-all-cells ((d distances)) (get-hash-keys (distance-cells d))) (defun make-distance (root) (make-instance 'distances :root root))