[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/heap e2c16be 06/31: Version 0.10.3 of the predictive co
From: |
Stefan Monnier |
Subject: |
[elpa] externals/heap e2c16be 06/31: Version 0.10.3 of the predictive completion package. |
Date: |
Mon, 14 Dec 2020 12:13:33 -0500 (EST) |
branch: externals/heap
commit e2c16bec4b231e5caa990449381e4d5d63f90c5a
Author: Toby Cubitt <toby-predictive@dr-qubit.org>
Commit: tsc25 <tsc25@cantab.net>
Version 0.10.3 of the predictive completion package.
(Versions 0.10.1-2 were very minor, and were skipped.)
---
heap.el | 93 ++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 54 insertions(+), 39 deletions(-)
diff --git a/heap.el b/heap.el
index bbc048a..17e5893 100644
--- a/heap.el
+++ b/heap.el
@@ -2,15 +2,15 @@
;;; heap.el --- heap (a.k.a. priority queue) data structure package
-;; Copyright (C) 2004 2005 Toby Cubitt
+;; Copyright (C) 2004-2006 Toby Cubitt
;; Author: Toby Cubitt <toby-predictive@dr-qubit.org>
-;; Version: 0.1.2
+;; Version: 0.1.4
;; Keywords: heap, priority queue
;; URL: http://www.dr-qubit.org/emacs.php
-;; This file is part of the Emacs Predictive Completion package.
+;; This file is NOT part of Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
@@ -30,13 +30,22 @@
;;; Commentary:
;;
-;; A heap consists of two cons cells, the first one holding the tag 'HEAP in
-;; the car cell and the second one having the heap in the car and the compare
-;; function in the cdr cell. The compare function must take two arguments of
-;; the type which is to be stored in the heap and must return non-nil or
-;; nil. To implement a max-heap, it should return non-nil if the first
-;; argument is "greater" than the second. To implement a min-heap, it should
-;; return non-nil if the first argument is "less than" the second.
+;; A heap is a form of efficient self-sorting tree (sometimes called a
+;; priority queue). In particular, the root node is guaranteed to be
+;; the highest-ranked entry in the tree. (The comparison function used
+;; for ranking the data can, of course, be freely defined). Therefore
+;; repeatedly removing the root node will return the data in order of
+;; increasing rank. They are often used as priority queues, for
+;; scheduling tasks in order of importance.
+;;
+;; A heap consists of two cons cells, the first one holding the tag
+;; 'HEAP in the car cell and the second one having the heap in the car
+;; and the compare function in the cdr cell. The compare function must
+;; take two arguments of the type which is to be stored in the heap
+;; and must return non-nil or nil. To implement a max-heap, it should
+;; return non-nil if the first argument is "greater" than the
+;; second. To implement a min-heap, it should return non-nil if the
+;; first argument is "less than" the second.
;;
;; Note that this package implements a ternary heap, since ternary
;; heaps are about 12% more efficient than binary heaps for heaps
@@ -46,6 +55,12 @@
;;; Change log:
;;
+;; Version 0.1.4
+;; * fixed internal function and macro names
+;;
+;; Version 0.1.3
+;; * added more commentary
+;;
;; Version 0.1.2
;; * moved defmacros before their first use so byte-compilation works
;;
@@ -72,33 +87,33 @@
;;; Internal functions for use in the heap package
-(defmacro hp-vect (heap)
+(defmacro heap--vect (heap)
;; Return the heap vector. ;; INTERNAL USE ONLY
`(car (cdr ,heap))
)
-(defmacro hp-cmpfun (heap)
+(defmacro heap--cmpfun (heap)
;; Return the comparison function of a heap. ;; INTERNAL USE ONLY
`(cdr (cdr ,heap))
)
-(defmacro hp-set-vect (heap vect)
+(defmacro heap--set-vect (heap vect)
;; Set the vector containing the heap itself to VECT.
`(setcar (cdr ,heap) ,vect)
)
-(defmacro hp-child (heap i)
+(defmacro heap--child (heap i)
;; Compare the 3 children of element I, and return element reference of the
;; smallest/largest (depending on whethen it's a min- or max-heap).
;; INTERNAL USE ONLY
- `(let* ((vect (hp-vect ,heap))
- (cmpfun (hp-cmpfun ,heap))
+ `(let* ((vect (heap--vect ,heap))
+ (cmpfun (heap--cmpfun ,heap))
(len (length vect)) (j nil) (k (* 3 ,i)))
;; Lots of if's in case I has less than three children.
(if (>= (1+ k) len) nil
@@ -121,14 +136,14 @@
-(defun hp-sift-up (heap n)
+(defun heap--sift-up (heap n)
;; Sift-up starting from element N of the heap vector belonging to
;; heap HEAP. ;; INTERNAL USE ONLY
- (let* ((i n) (j nil) (vect (hp-vect heap)) (v (aref vect n)))
+ (let* ((i n) (j nil) (vect (heap--vect heap)) (v (aref vect n)))
;; Keep moving element up until it reaches top or is smaller/bigger
;; than its parent.
(while (and (> i 0)
- (funcall (hp-cmpfun heap) v
+ (funcall (heap--cmpfun heap) v
(aref vect (setq j (/ (1- i) 3)))))
(vswap vect i j)
(setq i j)))
@@ -136,19 +151,19 @@
-(defun hp-sift-down (heap n)
+(defun heap--sift-down (heap n)
;; Sift-down from element N of the heap vector belonging to
;; heap HEAP. ;; INTERNAL USE ONLY
- (let* ((vect (hp-vect heap))
- (cmpfun (hp-cmpfun heap))
- (i n) (j (hp-child heap i)) (len (length vect)) (v (aref vect n)))
+ (let* ((vect (heap--vect heap))
+ (cmpfun (heap--cmpfun heap))
+ (i n) (j (heap--child heap i)) (len (length vect)) (v (aref vect n)))
;; Keep moving the element down until it reaches the bottom of the tree or
;; reaches a position where it is bigger/smaller than all its children.
(while (and j (funcall cmpfun (aref vect j) v))
(vswap vect i j)
(setq i j)
- (setq j (hp-child heap i)))
+ (setq j (heap--child heap i)))
)
)
@@ -171,8 +186,8 @@ B. To implemenet a min-heap, it should return non-nil if A
is less than B."
(defun heap-copy (heap)
"Return a copy of heap HEAP."
- (let ((newheap (heap-create (hp-cmpfun heap))))
- (hp-set-vect newheap (hp-vect heap))
+ (let ((newheap (heap-create (heap--cmpfun heap))))
+ (heap--set-vect newheap (heap--vect heap))
newheap)
)
@@ -186,21 +201,21 @@ B. To implemenet a min-heap, it should return non-nil if
A is less than B."
(defun heap-empty (heap)
"Return t if the heap is empty, nil otherwise."
- (= 0 (length (hp-vect heap)))
+ (= 0 (length (heap--vect heap)))
)
(defun heap-size (heap)
"Return the number of entries in the heap."
- (length (hp-vect heap))
+ (length (heap--vect heap))
)
(defun heap-compare-function (heap)
"Return the comparison function for the heap HEAP."
- (hp-cmpfun heap)
+ (heap--cmpfun heap)
)
@@ -208,26 +223,26 @@ B. To implemenet a min-heap, it should return non-nil if
A is less than B."
(defun heap-add (heap data)
"Add DATA to the heap."
;; Add data to bottom of heap and sift-up from bottom.
- (hp-set-vect heap (vconcat (hp-vect heap) (vector data)))
- (hp-sift-up heap (1- (length (hp-vect heap))))
+ (heap--set-vect heap (vconcat (heap--vect heap) (vector data)))
+ (heap--sift-up heap (1- (length (heap--vect heap))))
)
(defun heap-delete-root (heap)
"Return the root of the heap and delete it from the heap."
- (let ((vect (hp-vect heap))
+ (let ((vect (heap--vect heap))
(root nil) (len nil))
;; Deal with special cases of empty heap and heap with just one element.
(if (heap-empty heap) nil
(setq len (length vect))
(setq root (aref vect 0))
- (if (= 1 len) (hp-set-vect heap [])
+ (if (= 1 len) (heap--set-vect heap [])
;; Delete root, swap last element to top, and sift-down from top.
- (hp-set-vect heap (vconcat (vector (aref vect (1- len)))
+ (heap--set-vect heap (vconcat (vector (aref vect (1- len)))
(subseq vect 1 (1- len))))
- (hp-sift-down heap 0))
+ (heap--sift-down heap 0))
root)
)
)
@@ -243,7 +258,7 @@ heap, and return non-nil if it should be modified, nil
otherwise.
Note that only the match highest up the heap is modified."
- (let ((vect (hp-vect heap)) (i 0))
+ (let ((vect (heap--vect heap)) (i 0))
;; search vector for the first match
(while (and (< i (length vect))
(not (funcall match-function (aref vect i))))
@@ -254,9 +269,9 @@ Note that only the match highest up the heap is modified."
(aset vect i data)
;; if the new data is greater than old data, sift-up, otherwise
;; sift-down
- (if (funcall (hp-cmpfun heap) data olddata)
- (hp-sift-up heap i)
- (hp-sift-down heap i))
+ (if (funcall (heap--cmpfun heap) data olddata)
+ (heap--sift-up heap i)
+ (heap--sift-down heap i))
t ; return t if the match was successfully modified
)
nil ; return nil if no match was found
- [elpa] branch externals/heap created (now 32e75bb), Stefan Monnier, 2020/12/14
- [elpa] externals/heap 37bc0e9 05/31: Version 0.10 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 03a876d 02/31: Version 0.2 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/heap ceb5dd1 21/31: Trivial whitespace tidying., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 17429ee 07/31: Version 0.12 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 55d47fd 11/31: Fixed ancient but overlooked bug in heap resizing in heap-add., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 9bcd8d3 16/31: Added heap-build function for efficiently building a heap out of a vector., Stefan Monnier, 2020/12/14
- [elpa] externals/heap ed90e4d 09/31: Adding missing 'buffer setting to customization menu for predictive-auto-add-to-dict., Stefan Monnier, 2020/12/14
- [elpa] externals/heap f74c766 15/31: Converted heap data structures to defstructs., Stefan Monnier, 2020/12/14
- [elpa] externals/heap e354b4f 19/31: Revert default heap size to 10., Stefan Monnier, 2020/12/14
- [elpa] externals/heap e2c16be 06/31: Version 0.10.3 of the predictive completion package.,
Stefan Monnier <=
- [elpa] externals/heap 75b42f4 04/31: Version 0.9.1 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 2d51c84 01/31: Version 0.1 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 227de61 03/31: Version 0.7 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/heap da9637e 25/31: Added heap-clear function., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 4407826 22/31: More minor whitespace and commentary changes., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 7f5ab59 10/31: Add heap-root function., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 304c604 13/31: Fixed bug in heap-copy., Stefan Monnier, 2020/12/14
- [elpa] externals/heap fcf3edd 18/31: Added autoload cookies., Stefan Monnier, 2020/12/14
- [elpa] externals/heap a3ddd78 23/31: Remove ChangeLogs from library headers., Stefan Monnier, 2020/12/14
- [elpa] externals/heap 5ad96c3 14/31: Updated Package-Version, Package-Requires, and Keywords package headers., Stefan Monnier, 2020/12/14