Skip to content

Commit d004460

Browse files
Add WBTREE-CORRELATE and WBTREE-TEST
1 parent b4b74de commit d004460

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ a specialized comparison predicate for the actual key type.
109109
- **Function** `wbtreep object` => `boolean`
110110

111111
Answers true, if `object` is a `wbtree` instance, and false otherwise
112+
113+
- **Function** `wbtree-test` => `function`
114+
115+
Answers the binary predicate function that controls the tree structure. The result
116+
is a function value and matches whatever was specified as the tree type's `:test`
117+
function when it was defined
112118

113119
- **Function** `wbtree-empty-p tree` => `boolean`
114120

@@ -274,6 +280,8 @@ a specialized comparison predicate for the actual key type.
274280
or less than value `start`, and will stop before reaching any node,
275281
whose key is equal to or less than the given `end`. If no `end` is
276282
supplied, the traversal stops after all nodes have been visited.
283+
284+
- **Function** `wbtree-correlate function tree1 tree2 &key test direction` => unspecific
277285

278286
- **Function** `wbtree-difference tree1 tree2` => `new-tree`
279287
- **Function** `wbtree-union tree1 tree2 &key combiner` => `new-tree`

src/wbtree/implementation.lisp

+45
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
"Answers the number of valid key/value pairs contained in `tree'"
8585
(node-count node))
8686

87+
(defgeneric wbtree-test (object))
88+
8789
(defun wbtree-node-value (node)
8890
"Obtains the value associated with `node'. If `node' is the empty
8991
tree, raises a condition of type `simple-error'."
@@ -1011,6 +1013,9 @@
10111013
(defmethod wbtree-rebalance ((,tree-var ,name))
10121014
(wbtree-rebalance-1 ,tree-var #',node-constructor ,empty-node))
10131015

1016+
(defmethod wbtree-test ((,tree-var ,name))
1017+
(declare (ignore ,tree-var)) #',lessp-function)
1018+
10141019
(defmethod make-load-form ((,tree-var ,name) &optional environment)
10151020
(declare (ignore environment))
10161021
(wbtree-load-form ,tree-var ',node-constructor ',empty-node))
@@ -1054,3 +1059,43 @@
10541059
,setter
10551060
,value-temp)
10561061
`(wbtree-find ,key-temp ,getter ,@(when have-default (list default-temp))))))))
1062+
1063+
1064+
(defun wbtree-correlate (function tree1 tree2
1065+
&key test (direction :forward))
1066+
(let* ((predicate (or test (wbtree-test tree1)))
1067+
(test (if (eq direction :forward) predicate (lambda (o1 o2) (funcall predicate o2 o1))))
1068+
(iter1 (wbtree-iterator tree1 :direction direction))
1069+
(iter2 (wbtree-iterator tree2 :direction direction))
1070+
(node1 (funcall iter1))
1071+
(node2 (funcall iter2)))
1072+
(loop
1073+
while (and node1 node2)
1074+
do (let ((key1 (wbtree-node-key node1))
1075+
(key2 (wbtree-node-key node2)))
1076+
(cond
1077+
((funcall test key1 key2)
1078+
(funcall function node1 nil)
1079+
(setf node1 (funcall iter1)))
1080+
((funcall test key2 key1)
1081+
(funcall function nil node2)
1082+
(setf node2 (funcall iter2)))
1083+
(t
1084+
(funcall function node1 node2)
1085+
(setf node1 (funcall iter1))
1086+
(setf node2 (funcall iter2))))))
1087+
(loop
1088+
while node1
1089+
do (funcall function node1 nil)
1090+
(setf node1 (funcall iter1)))
1091+
(loop
1092+
while node2
1093+
do (funcall function nil node2)
1094+
(setf node2 (funcall iter2)))))
1095+
1096+
(defmacro do-correlated-wbtree-nodes (((bind1 tree1) (bind2 tree2) &rest options) &body body)
1097+
`(block nil
1098+
(wbtree-correlate (lambda (,bind1 ,bind2) ,@body)
1099+
,tree1 ,tree2
1100+
,@options)))
1101+

src/wbtree/package.lisp

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
#:wbtree-iterator #:wbtree-equal #:define-wbtree #:wbtree-lower-boundary-node
3131
#:wbtree-upper-boundary-node #:wbtree-check-invariants #:wbtree-rebalance
3232
#:wbtree-fold #:wbtree-minimum-node #:wbtree-maximum-node #:wbtree-ceiling-node
33-
#:wbtree-floor-node #:do-wbtree)
33+
#:wbtree-floor-node #:do-wbtree #:wbtree-test #:wbtree-correlate
34+
#:do-correlated-wbtree-nodes)
3435
(:documentation "Generalized weight-balanced binary search trees. This
3536
package provides a variant of the weight-balanced binary trees implemented
3637
in package DARTS.LIB.PTREE. The variant exposed here can be used with arbitrary

0 commit comments

Comments
 (0)