Permalink
Browse files

added count function

  • Loading branch information...
1 parent e211450 commit 870bce169065f161c2f26ec5222a91bda7abfa81 @sake committed Feb 18, 2010
Showing with 21 additions and 1 deletion.
  1. +3 −0 src/interface.lisp
  2. +1 −1 src/package.lisp
  3. +17 −0 src/redblack.lisp
View
@@ -31,6 +31,9 @@
"Tree constructor which selects the appropriate tree type depending on the type keyword."
(make-tree-intern test type))
+(defgeneric treemap-count (tree)
+ (:documentation "Return the number of elements in the tree."))
+
(defgeneric clr-tree (tree)
(:documentation "Remove all elements from a tree."))
View
@@ -24,6 +24,6 @@
;; tree classes
(:export tree-map redblack-tree-map)
;; 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
(:export split-tree merge-trees))
View
@@ -27,6 +27,7 @@
(defclass redblack-tree-map (tree-map)
((data :accessor data :initform nil
: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.")))
(:documentation "Red-Black tree."))
@@ -138,6 +139,22 @@
(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))
(setf (data tree) nil)
tree)

0 comments on commit 870bce1

Please sign in to comment.