[3979] | 1 | ;;; -*- Mode: Lisp -*-
|
---|
| 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 3 | ;;;
|
---|
| 4 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
| 5 | ;;;
|
---|
| 6 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
| 7 | ;;; it under the terms of the GNU General Public License as published by
|
---|
| 8 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | ;;; (at your option) any later version.
|
---|
| 10 | ;;;
|
---|
| 11 | ;;; This program is distributed in the hope that it will be useful,
|
---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | ;;; GNU General Public License for more details.
|
---|
| 15 | ;;;
|
---|
| 16 | ;;; You should have received a copy of the GNU General Public License
|
---|
| 17 | ;;; along with this program; if not, write to the Free Software
|
---|
| 18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
| 19 | ;;;
|
---|
| 20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 21 |
|
---|
| 22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 23 | ;;
|
---|
[3983] | 24 | ;; A conventional implementation of a heap
|
---|
[3979] | 25 | ;;
|
---|
| 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 27 |
|
---|
| 28 | (defpackage "HEAP"
|
---|
| 29 | (:use :cl)
|
---|
| 30 | (:export "+HEAP-ALLOCATION-SIZE+"
|
---|
[3981] | 31 | "MAKE-HEAP"
|
---|
| 32 | "HEAP-SIZE"
|
---|
| 33 | "HEAP-INSERT"
|
---|
[3982] | 34 | "HEAP-REMOVE"
|
---|
| 35 | "HEAP-EMPTY-P"
|
---|
[3979] | 36 | ))
|
---|
| 37 |
|
---|
| 38 | (in-package :heap)
|
---|
[4348] | 39 | (defparameter *heap-allocation-size* 16)
|
---|
[3979] | 40 |
|
---|
[3980] | 41 | (defun make-heap (&key (element-type 'fixnum))
|
---|
[4348] | 42 | (make-array *heap-allocation-size* :element-type element-type :fill-pointer 1
|
---|
[3979] | 43 | :adjustable t))
|
---|
| 44 |
|
---|
[3987] | 45 | (defun heap-size (a)
|
---|
[3988] | 46 | "The number of elements on the heap. Does not count dummy head."
|
---|
[3987] | 47 | (1- (fill-pointer a)))
|
---|
[3979] | 48 |
|
---|
[3980] | 49 | (defun heap-upheap (a k
|
---|
[3979] | 50 | &optional
|
---|
| 51 | (test #'<=)
|
---|
| 52 | &aux (v (aref a k)))
|
---|
| 53 | (declare (fixnum k))
|
---|
| 54 | (assert (< 0 k (fill-pointer a)))
|
---|
| 55 | (loop
|
---|
| 56 | (let ((parent (ash k -1)))
|
---|
| 57 | (when (zerop parent) (return))
|
---|
| 58 | (unless (funcall test (aref a parent) v)
|
---|
| 59 | (return))
|
---|
| 60 | (setf (aref a k) (aref a parent)
|
---|
| 61 | k parent)))
|
---|
| 62 | (setf (aref a k) v)
|
---|
| 63 | a)
|
---|
| 64 |
|
---|
| 65 |
|
---|
[3980] | 66 | (defun heap-insert (a item &optional (test #'<=))
|
---|
[3979] | 67 | (vector-push-extend item a)
|
---|
[3980] | 68 | (heap-upheap a (1- (fill-pointer a)) test))
|
---|
[3979] | 69 |
|
---|
[3980] | 70 | (defun heap-downheap (a k
|
---|
[3979] | 71 | &optional
|
---|
| 72 | (test #'<=)
|
---|
| 73 | &aux (v (aref a k)) (j 0) (n (fill-pointer a)))
|
---|
| 74 | (declare (fixnum k n j))
|
---|
| 75 | (loop
|
---|
| 76 | (unless (<= k (ash n -1))
|
---|
| 77 | (return))
|
---|
| 78 | (setf j (ash k 1))
|
---|
| 79 | (if (and (< j n) (not (funcall test (aref a (1+ j)) (aref a j))))
|
---|
| 80 | (incf j))
|
---|
| 81 | (when (funcall test (aref a j) v)
|
---|
| 82 | (return))
|
---|
| 83 | (setf (aref a k) (aref a j)
|
---|
| 84 | k j))
|
---|
| 85 | (setf (aref a k) v)
|
---|
| 86 | a)
|
---|
| 87 |
|
---|
[3980] | 88 | (defun heap-remove (a &optional (test #'<=) &aux (v (aref a 1)))
|
---|
[3979] | 89 | (when (<= (fill-pointer a) 1) (error "Empty queue."))
|
---|
| 90 | (setf (aref a 1) (vector-pop a))
|
---|
[3988] | 91 | (heap-downheap a 1 test)
|
---|
[3979] | 92 | (values v a))
|
---|
| 93 |
|
---|
[3980] | 94 | (defun heap-empty-p (a)
|
---|
[3979] | 95 | (<= (fill-pointer a) 1))
|
---|