diff options
Diffstat (limited to 'union-find/quick-find.lisp')
| -rw-r--r-- | union-find/quick-find.lisp | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/union-find/quick-find.lisp b/union-find/quick-find.lisp index 936c647..dd2c54b 100644 --- a/union-find/quick-find.lisp +++ b/union-find/quick-find.lisp @@ -5,34 +5,35 @@ (in-package :com.lisp-algo.union-find) -;;; Base functions - -(defun qf-create-network (n) - "Build a quick-find network using a dynamic vector" - (let ((nodes (make-array n :fill-pointer 0))) - (dotimes (id n) +(defclass quick-find () + ((nw-size + :initarg :network-size + :initform 10 + :accessor network-size) + (nw + :initform nil + :accessor network))) + +(defmethod initialize-instance :after ((algo quick-find) &key) + ;; Initialize network using dynamic vector + (let* ((nw-size (slot-value algo 'nw-size)) + (nodes (make-array nw-size :fill-pointer 0))) + (dotimes (id nw-size) (vector-push id nodes)) - nodes)) + (setf (slot-value algo 'nw) nodes))) -;; Link two nodes in the network -(defun qf-union (network n1 n2) - "Link two nodes in the quick-find network. union_ represent the union operation of the Quick Find Algorithm" - (let ((v-n1 (elt network n1)) - (v-n2 (elt network n2)) - (new-network (copy-seq network))) +(defmethod union ((algo-instance quick-find) n1 n2) + (with-slots ((nw nw)) algo-instance + (let ((v-n1 (elt nw n1)) + (v-n2 (elt nw n2)) + (new-network (copy-seq nw))) (dotimes (n (length new-network)) (if (= (elt new-network n) v-n2) (setf (elt new-network n) v-n1))) - new-network)) - -;;; Macro definitions + (setf nw new-network)))) -(defmacro qf-connected (network n1 n2) +(defmethod connected ((algo-instance quick-find) n1 n2) " Return t if there is a path between n1 and n2, nil otherwise. connected represent the find operation of the Quick Find Algorithm" - `(= (elt ,network ,n1) (elt ,network ,n2))) - -(defmacro qf-nunion (network n1 n2) - "A destructive version of union_" - `(setq ,network (union ,network ,n1 ,n2))) - + (with-slots ((nw nw)) algo-instance + (= (elt nw n1) (elt nw n2)))) |
