Merge branch 'master' of github.com:froydnj/trees
[trees.git] / generics.lisp
1 (in-package :trees)
2
3 (defvar *binary-tree-info* nil)
4
5 (defun make-binary-tree (type pred &key key test)
6   "Create a binary tree based on TYPE.  Current acceptable values for TYPE are:
7
8   :NORMAL - a normal binary tree, with no rebalancing
9   :RED-BLACK - a red-black tree
10   :AVL - an AVL tree
11   :AA - an AA tree.
12
13 PRED specifies the ordering relation.  KEY specifies how to access the
14 data for comparison.  TEST is optional and, if given, specifies how to
15 compare two keys for equality."
16   (let* ((pred (coerce pred 'function))
17          (key (coerce key 'function))
18          (test (if test
19                    (coerce test 'function)
20                    (lambda (x y)
21                      (not (or (funcall pred x y)
22                               (funcall pred y x))))))
23          (specifics (assoc type *binary-tree-info*)))
24     (unless specifics
25       (error "Unknown tree kind ~A" type))
26     (apply #'%make-binary-tree pred key test (cdr specifics))))