Created
August 1, 2013 16:58
-
-
Save PuercoPop/6133213 to your computer and use it in GitHub Desktop.
What is the best approach for setting flag in an method? I could set a global an book keep but then there can only be one insertion method at the time. I could also pass it as an extra parameter in the method call but that would pollute the signature. There is probably a better way to do this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-condition element-already-present (error) | |
((text :initarg :text :reader text))) | |
(defmethod insert :around (element (node node)) | |
"Wrap the function call around a catch statement so to prevent unnecesary | |
copying when element already belongs in the tree. When run for the first time, | |
setup handler case of the element already present." | |
(cond ((first-call | |
(handler-case | |
(progn | |
(let ((result (call-next-method))) | |
result)) | |
(element-already-present () node))) | |
(t (let ((result (call-next-method))) | |
result))))) | |
(defmethod insert (element (node node)) | |
(cond | |
((ord-lt element (element node)) | |
(make-instance 'node | |
:left (insert element (left-branch node)) | |
:element (element node) | |
:right (right-branch node))) | |
((ord-gt element (element node)) | |
(make-instance 'node | |
:left (left-branch node) | |
:element (element node) | |
:right (insert element (right-branch node)))) | |
((ord-eql element (element node)) | |
(error 'element-already-present :text "Don't copy in vain.")) | |
(t node))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment