aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThien-Thi Nguyen2007-08-27 02:22:57 +0000
committerThien-Thi Nguyen2007-08-27 02:22:57 +0000
commit5afb301bee960583ddd66044367ac08155ff9be8 (patch)
tree6f7b1cc913d8b406905f13923ba18d5020185fcc
parentdfd4af17e447929c26534bc232333eec7b74a6b4 (diff)
downloademacs-5afb301bee960583ddd66044367ac08155ff9be8.tar.gz
emacs-5afb301bee960583ddd66044367ac08155ff9be8.zip
Do s/elib-avl-/avl-tree-/g. Resulting changed macro and function names:
avl-tree-root, avl-tree-dummyroot, avl-tree-cmpfun, avl-tree-del-balance1, avl-tree-do-del-internal, avl-tree-del-balance2, avl-tree-do-delete, avl-tree-enter-balance1, avl-tree-enter-balance2, avl-tree-do-enter, avl-tree-mapc, avl-tree-do-copy.
-rw-r--r--lisp/emacs-lisp/avl-tree.el96
1 files changed, 46 insertions, 50 deletions
diff --git a/lisp/emacs-lisp/avl-tree.el b/lisp/emacs-lisp/avl-tree.el
index a86fdc60f57..63e88ac21f9 100644
--- a/lisp/emacs-lisp/avl-tree.el
+++ b/lisp/emacs-lisp/avl-tree.el
@@ -112,26 +112,22 @@
112;;; ================================================================ 112;;; ================================================================
113;;; Internal functions for use in the AVL tree package 113;;; Internal functions for use in the AVL tree package
114 114
115;;; 115(defmacro avl-tree-root (tree)
116;;; The functions and macros in this section all start with `elib-avl-'.
117;;;
118
119(defmacro elib-avl-root (tree)
120 ;; Return the root node for an avl-tree. INTERNAL USE ONLY. 116 ;; Return the root node for an avl-tree. INTERNAL USE ONLY.
121 `(elib-node-left (car (cdr ,tree)))) 117 `(elib-node-left (car (cdr ,tree))))
122 118
123(defmacro elib-avl-dummyroot (tree) 119(defmacro avl-tree-dummyroot (tree)
124 ;; Return the dummy node of an avl-tree. INTERNAL USE ONLY. 120 ;; Return the dummy node of an avl-tree. INTERNAL USE ONLY.
125 `(car (cdr ,tree))) 121 `(car (cdr ,tree)))
126 122
127(defmacro elib-avl-cmpfun (tree) 123(defmacro avl-tree-cmpfun (tree)
128 ;; Return the compare function of AVL tree TREE. INTERNAL USE ONLY. 124 ;; Return the compare function of AVL tree TREE. INTERNAL USE ONLY.
129 `(cdr (cdr ,tree))) 125 `(cdr (cdr ,tree)))
130 126
131;; ---------------------------------------------------------------- 127;; ----------------------------------------------------------------
132;; Deleting data 128;; Deleting data
133 129
134(defun elib-avl-del-balance1 (node branch) 130(defun avl-tree-del-balance1 (node branch)
135 ;; Rebalance a tree and return t if the height of the tree has shrunk. 131 ;; Rebalance a tree and return t if the height of the tree has shrunk.
136 (let* ((br (elib-node-branch node branch)) 132 (let* ((br (elib-node-branch node branch))
137 p1 133 p1
@@ -185,7 +181,7 @@
185 (avl-tree-node-set-balance p2 0) 181 (avl-tree-node-set-balance p2 0)
186 t))))) 182 t)))))
187 183
188(defun elib-avl-del-balance2 (node branch) 184(defun avl-tree-del-balance2 (node branch)
189 (let* ((br (elib-node-branch node branch)) 185 (let* ((br (elib-node-branch node branch))
190 p1 186 p1
191 b1 187 b1
@@ -238,18 +234,18 @@
238 (avl-tree-node-set-balance p2 0) 234 (avl-tree-node-set-balance p2 0)
239 t))))) 235 t)))))
240 236
241(defun elib-avl-do-del-internal (node branch q) 237(defun avl-tree-do-del-internal (node branch q)
242 238
243 (let* ((br (elib-node-branch node branch))) 239 (let* ((br (elib-node-branch node branch)))
244 (if (elib-node-right br) 240 (if (elib-node-right br)
245 (if (elib-avl-do-del-internal br +1 q) 241 (if (avl-tree-do-del-internal br +1 q)
246 (elib-avl-del-balance2 node branch)) 242 (avl-tree-del-balance2 node branch))
247 (elib-node-set-data q (elib-node-data br)) 243 (elib-node-set-data q (elib-node-data br))
248 (elib-node-set-branch node branch 244 (elib-node-set-branch node branch
249 (elib-node-left br)) 245 (elib-node-left br))
250 t))) 246 t)))
251 247
252(defun elib-avl-do-delete (cmpfun root branch data) 248(defun avl-tree-do-delete (cmpfun root branch data)
253 ;; Return t if the height of the tree has shrunk. 249 ;; Return t if the height of the tree has shrunk.
254 (let* ((br (elib-node-branch root branch))) 250 (let* ((br (elib-node-branch root branch)))
255 (cond 251 (cond
@@ -257,12 +253,12 @@
257 nil) 253 nil)
258 254
259 ((funcall cmpfun data (elib-node-data br)) 255 ((funcall cmpfun data (elib-node-data br))
260 (if (elib-avl-do-delete cmpfun br 0 data) 256 (if (avl-tree-do-delete cmpfun br 0 data)
261 (elib-avl-del-balance1 root branch))) 257 (avl-tree-del-balance1 root branch)))
262 258
263 ((funcall cmpfun (elib-node-data br) data) 259 ((funcall cmpfun (elib-node-data br) data)
264 (if (elib-avl-do-delete cmpfun br 1 data) 260 (if (avl-tree-do-delete cmpfun br 1 data)
265 (elib-avl-del-balance2 root branch))) 261 (avl-tree-del-balance2 root branch)))
266 262
267 (t 263 (t
268 ;; Found it. Let's delete it. 264 ;; Found it. Let's delete it.
@@ -276,13 +272,13 @@
276 t) 272 t)
277 273
278 (t 274 (t
279 (if (elib-avl-do-del-internal br 0 br) 275 (if (avl-tree-do-del-internal br 0 br)
280 (elib-avl-del-balance1 root branch)))))))) 276 (avl-tree-del-balance1 root branch))))))))
281 277
282;; ---------------------------------------------------------------- 278;; ----------------------------------------------------------------
283;; Entering data 279;; Entering data
284 280
285(defun elib-avl-enter-balance1 (node branch) 281(defun avl-tree-enter-balance1 (node branch)
286 ;; Rebalance a tree and return t if the height of the tree has grown. 282 ;; Rebalance a tree and return t if the height of the tree has grown.
287 (let* ((br (elib-node-branch node branch)) 283 (let* ((br (elib-node-branch node branch))
288 p1 284 p1
@@ -326,7 +322,7 @@
326 (avl-tree-node-set-balance (elib-node-branch node branch) 0) 322 (avl-tree-node-set-balance (elib-node-branch node branch) 0)
327 nil)))) 323 nil))))
328 324
329(defun elib-avl-enter-balance2 (node branch) 325(defun avl-tree-enter-balance2 (node branch)
330 ;; Return t if the tree has grown. 326 ;; Return t if the tree has grown.
331 (let* ((br (elib-node-branch node branch)) 327 (let* ((br (elib-node-branch node branch))
332 p1 328 p1
@@ -369,7 +365,7 @@
369 (avl-tree-node-set-balance (elib-node-branch node branch) 0) 365 (avl-tree-node-set-balance (elib-node-branch node branch) 0)
370 nil)))) 366 nil))))
371 367
372(defun elib-avl-do-enter (cmpfun root branch data) 368(defun avl-tree-do-enter (cmpfun root branch data)
373 ;; Return t if height of tree ROOT has grown. INTERNAL USE ONLY. 369 ;; Return t if height of tree ROOT has grown. INTERNAL USE ONLY.
374 (let ((br (elib-node-branch root branch))) 370 (let ((br (elib-node-branch root branch)))
375 (cond 371 (cond
@@ -380,16 +376,16 @@
380 t) 376 t)
381 377
382 ((funcall cmpfun data (elib-node-data br)) 378 ((funcall cmpfun data (elib-node-data br))
383 (and (elib-avl-do-enter cmpfun 379 (and (avl-tree-do-enter cmpfun
384 br 380 br
385 0 data) 381 0 data)
386 (elib-avl-enter-balance2 root branch))) 382 (avl-tree-enter-balance2 root branch)))
387 383
388 ((funcall cmpfun (elib-node-data br) data) 384 ((funcall cmpfun (elib-node-data br) data)
389 (and (elib-avl-do-enter cmpfun 385 (and (avl-tree-do-enter cmpfun
390 br 386 br
391 1 data) 387 1 data)
392 (elib-avl-enter-balance1 root branch))) 388 (avl-tree-enter-balance1 root branch)))
393 389
394 (t 390 (t
395 (elib-node-set-data br data) 391 (elib-node-set-data br data)
@@ -397,7 +393,7 @@
397 393
398;; ---------------------------------------------------------------- 394;; ----------------------------------------------------------------
399 395
400(defun elib-avl-mapc (map-function root) 396(defun avl-tree-mapc (map-function root)
401 ;; Apply MAP-FUNCTION to all nodes in the tree starting with ROOT. 397 ;; Apply MAP-FUNCTION to all nodes in the tree starting with ROOT.
402 ;; The function is applied in-order. 398 ;; The function is applied in-order.
403 ;; 399 ;;
@@ -423,13 +419,13 @@
423 (setq node (pop stack) 419 (setq node (pop stack)
424 go-left nil)))))) 420 go-left nil))))))
425 421
426(defun elib-avl-do-copy (root) 422(defun avl-tree-do-copy (root)
427 ;; Copy the tree with ROOT as root. 423 ;; Copy the tree with ROOT as root.
428 ;; Highly recursive. INTERNAL USE ONLY. 424 ;; Highly recursive. INTERNAL USE ONLY.
429 (if (null root) 425 (if (null root)
430 nil 426 nil
431 (avl-tree-node-create (elib-avl-do-copy (elib-node-left root)) 427 (avl-tree-node-create (avl-tree-do-copy (elib-node-left root))
432 (elib-avl-do-copy (elib-node-right root)) 428 (avl-tree-do-copy (elib-node-right root))
433 (elib-node-data root) 429 (elib-node-data root)
434 (avl-tree-node-balance root)))) 430 (avl-tree-node-balance root))))
435 431
@@ -451,17 +447,17 @@ and returns non-nil if A is less than B, and nil otherwise."
451 447
452(defun avl-tree-compare-function (tree) 448(defun avl-tree-compare-function (tree)
453 "Return the comparision function for the avl tree TREE." 449 "Return the comparision function for the avl tree TREE."
454 (elib-avl-cmpfun tree)) 450 (avl-tree-cmpfun tree))
455 451
456(defun avl-tree-empty (tree) 452(defun avl-tree-empty (tree)
457 "Return t if TREE is emtpy, otherwise return nil." 453 "Return t if TREE is emtpy, otherwise return nil."
458 (null (elib-avl-root tree))) 454 (null (avl-tree-root tree)))
459 455
460(defun avl-tree-enter (tree data) 456(defun avl-tree-enter (tree data)
461 "In the avl tree TREE insert DATA. 457 "In the avl tree TREE insert DATA.
462Return DATA." 458Return DATA."
463 (elib-avl-do-enter (elib-avl-cmpfun tree) 459 (avl-tree-do-enter (avl-tree-cmpfun tree)
464 (elib-avl-dummyroot tree) 460 (avl-tree-dummyroot tree)
465 0 461 0
466 data) 462 data)
467 data) 463 data)
@@ -469,8 +465,8 @@ Return DATA."
469(defun avl-tree-delete (tree data) 465(defun avl-tree-delete (tree data)
470 "From the avl tree TREE, delete DATA. 466 "From the avl tree TREE, delete DATA.
471Return the element in TREE which matched DATA, nil if no element matched." 467Return the element in TREE which matched DATA, nil if no element matched."
472 (elib-avl-do-delete (elib-avl-cmpfun tree) 468 (avl-tree-do-delete (avl-tree-cmpfun tree)
473 (elib-avl-dummyroot tree) 469 (avl-tree-dummyroot tree)
474 0 470 0
475 data)) 471 data))
476 472
@@ -480,8 +476,8 @@ Matching uses the compare function previously specified in `avl-tree-create'
480when TREE was created. 476when TREE was created.
481 477
482If there is no such element in the tree, the value is nil." 478If there is no such element in the tree, the value is nil."
483 (let ((node (elib-avl-root tree)) 479 (let ((node (avl-tree-root tree))
484 (compare-function (elib-avl-cmpfun tree)) 480 (compare-function (avl-tree-cmpfun tree))
485 found) 481 found)
486 (while (and node 482 (while (and node
487 (not found)) 483 (not found))
@@ -499,16 +495,16 @@ If there is no such element in the tree, the value is nil."
499 495
500(defun avl-tree-map (__map-function__ tree) 496(defun avl-tree-map (__map-function__ tree)
501 "Apply MAP-FUNCTION to all elements in the avl tree TREE." 497 "Apply MAP-FUNCTION to all elements in the avl tree TREE."
502 (elib-avl-mapc 498 (avl-tree-mapc
503 (function (lambda (node) 499 (function (lambda (node)
504 (elib-node-set-data node 500 (elib-node-set-data node
505 (funcall __map-function__ 501 (funcall __map-function__
506 (elib-node-data node))))) 502 (elib-node-data node)))))
507 (elib-avl-root tree))) 503 (avl-tree-root tree)))
508 504
509(defun avl-tree-first (tree) 505(defun avl-tree-first (tree)
510 "Return the first element in TREE, or nil if TREE is empty." 506 "Return the first element in TREE, or nil if TREE is empty."
511 (let ((node (elib-avl-root tree))) 507 (let ((node (avl-tree-root tree)))
512 (if node 508 (if node
513 (progn 509 (progn
514 (while (elib-node-left node) 510 (while (elib-node-left node)
@@ -518,7 +514,7 @@ If there is no such element in the tree, the value is nil."
518 514
519(defun avl-tree-last (tree) 515(defun avl-tree-last (tree)
520 "Return the last element in TREE, or nil if TREE is empty." 516 "Return the last element in TREE, or nil if TREE is empty."
521 (let ((node (elib-avl-root tree))) 517 (let ((node (avl-tree-root tree)))
522 (if node 518 (if node
523 (progn 519 (progn
524 (while (elib-node-right node) 520 (while (elib-node-right node)
@@ -529,33 +525,33 @@ If there is no such element in the tree, the value is nil."
529(defun avl-tree-copy (tree) 525(defun avl-tree-copy (tree)
530 "Return a copy of the avl tree TREE." 526 "Return a copy of the avl tree TREE."
531 (let ((new-tree (avl-tree-create 527 (let ((new-tree (avl-tree-create
532 (elib-avl-cmpfun tree)))) 528 (avl-tree-cmpfun tree))))
533 (elib-node-set-left (elib-avl-dummyroot new-tree) 529 (elib-node-set-left (avl-tree-dummyroot new-tree)
534 (elib-avl-do-copy (elib-avl-root tree))) 530 (avl-tree-do-copy (avl-tree-root tree)))
535 new-tree)) 531 new-tree))
536 532
537(defun avl-tree-flatten (tree) 533(defun avl-tree-flatten (tree)
538 "Return a sorted list containing all elements of TREE." 534 "Return a sorted list containing all elements of TREE."
539 (nreverse 535 (nreverse
540 (let ((treelist nil)) 536 (let ((treelist nil))
541 (elib-avl-mapc (function (lambda (node) 537 (avl-tree-mapc (function (lambda (node)
542 (setq treelist (cons (elib-node-data node) 538 (setq treelist (cons (elib-node-data node)
543 treelist)))) 539 treelist))))
544 (elib-avl-root tree)) 540 (avl-tree-root tree))
545 treelist))) 541 treelist)))
546 542
547(defun avl-tree-size (tree) 543(defun avl-tree-size (tree)
548 "Return the number of elements in TREE." 544 "Return the number of elements in TREE."
549 (let ((treesize 0)) 545 (let ((treesize 0))
550 (elib-avl-mapc (function (lambda (data) 546 (avl-tree-mapc (function (lambda (data)
551 (setq treesize (1+ treesize)) 547 (setq treesize (1+ treesize))
552 data)) 548 data))
553 (elib-avl-root tree)) 549 (avl-tree-root tree))
554 treesize)) 550 treesize))
555 551
556(defun avl-tree-clear (tree) 552(defun avl-tree-clear (tree)
557 "Clear the avl tree TREE." 553 "Clear the avl tree TREE."
558 (elib-node-set-left (elib-avl-dummyroot tree) nil)) 554 (elib-node-set-left (avl-tree-dummyroot tree) nil))
559 555
560(provide 'avl-tree) 556(provide 'avl-tree)
561 557