Skip to content

Commit

Permalink
added count function
Browse files Browse the repository at this point in the history
  • Loading branch information
sake committed Feb 18, 2010
1 parent e211450 commit 870bce1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/interface.lisp
Expand Up @@ -31,6 +31,9 @@
"Tree constructor which selects the appropriate tree type depending on the type keyword." "Tree constructor which selects the appropriate tree type depending on the type keyword."
(make-tree-intern test type)) (make-tree-intern test type))


(defgeneric treemap-count (tree)
(:documentation "Return the number of elements in the tree."))

(defgeneric clr-tree (tree) (defgeneric clr-tree (tree)
(:documentation "Remove all elements from a tree.")) (:documentation "Remove all elements from a tree."))


Expand Down
2 changes: 1 addition & 1 deletion src/package.lisp
Expand Up @@ -24,6 +24,6 @@
;; tree classes ;; tree classes
(:export tree-map redblack-tree-map) (:export tree-map redblack-tree-map)
;; basic tree operations ;; basic tree operations
(:export make-tree get-tree-entry del-tree-entry clr-tree) (:export make-tree get-tree-entry del-tree-entry clr-tree treemap-count)
;; split and merge ;; split and merge
(:export split-tree merge-trees)) (:export split-tree merge-trees))
17 changes: 17 additions & 0 deletions src/redblack.lisp
Expand Up @@ -27,6 +27,7 @@
(defclass redblack-tree-map (tree-map) (defclass redblack-tree-map (tree-map)
((data :accessor data :initform nil ((data :accessor data :initform nil
:documentation "A node consists of the values key, data, color, left, right.") :documentation "A node consists of the values key, data, color, left, right.")
(num-elements :accessor num-elements :initform 0)
(testfun :accessor testfun :initarg :testfun :initform (error "No test function specified."))) (testfun :accessor testfun :initarg :testfun :initform (error "No test function specified.")))
(:documentation "Red-Black tree.")) (:documentation "Red-Black tree."))


Expand Down Expand Up @@ -138,6 +139,22 @@
(setf tree (make-instance 'redblack-tree-map :testfun test)))) (setf tree (make-instance 'redblack-tree-map :testfun test))))




(defmethod treemap-count ((tree redblack-tree-map))
"Quick and dirty iterate function. TODO: replace"
(let ((num 0))
(labels ((recurse-node (node)
(if node
(progn
;; recurse to the left
(recurse-node (rb-left node))
;; count this node
(incf num)
;; recurse to the right
(recurse-node (rb-right node))))))
(recurse-node (data tree)))
num))


(defmethod clr-tree ((tree redblack-tree-map)) (defmethod clr-tree ((tree redblack-tree-map))
(setf (data tree) nil) (setf (data tree) nil)
tree) tree)
Expand Down

0 comments on commit 870bce1

Please sign in to comment.