| 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 | ;;
|
|---|
| 24 | ;; A conventional implementation of priority queues based on heaps
|
|---|
| 25 | ;;
|
|---|
| 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 27 |
|
|---|
| 28 | (defpackage "HEAP"
|
|---|
| 29 | (:use :cl)
|
|---|
| 30 | (:export "+HEAP-ALLOCATION-SIZE+"
|
|---|
| 31 | "MAKE-HEAP"
|
|---|
| 32 | "HEAP-SIZE"
|
|---|
| 33 | "HEAP-UPHEAP"
|
|---|
| 34 | "HEAP-INSERT"
|
|---|
| 35 | ))
|
|---|
| 36 |
|
|---|
| 37 | (in-package :heap)
|
|---|
| 38 | (defparameter +heap-allocation-size+ 16)
|
|---|
| 39 |
|
|---|
| 40 | (defun make-heap (&key (element-type 'fixnum))
|
|---|
| 41 | (make-array +heap-allocation-size+ :element-type element-type :fill-pointer 1
|
|---|
| 42 | :adjustable t))
|
|---|
| 43 |
|
|---|
| 44 | (defun heap-size (pq)
|
|---|
| 45 | (fill-pointer (priority-queue-heap pq)))
|
|---|
| 46 |
|
|---|
| 47 | (defun heap-upheap (a k
|
|---|
| 48 | &optional
|
|---|
| 49 | (test #'<=)
|
|---|
| 50 | &aux (v (aref a k)))
|
|---|
| 51 | (declare (fixnum k))
|
|---|
| 52 | (assert (< 0 k (fill-pointer a)))
|
|---|
| 53 | (loop
|
|---|
| 54 | (let ((parent (ash k -1)))
|
|---|
| 55 | (when (zerop parent) (return))
|
|---|
| 56 | (unless (funcall test (aref a parent) v)
|
|---|
| 57 | (return))
|
|---|
| 58 | (setf (aref a k) (aref a parent)
|
|---|
| 59 | k parent)))
|
|---|
| 60 | (setf (aref a k) v)
|
|---|
| 61 | a)
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | (defun heap-insert (a item &optional (test #'<=))
|
|---|
| 65 | (vector-push-extend item a)
|
|---|
| 66 | (heap-upheap a (1- (fill-pointer a)) test))
|
|---|
| 67 |
|
|---|
| 68 | (defun heap-downheap (a k
|
|---|
| 69 | &optional
|
|---|
| 70 | (test #'<=)
|
|---|
| 71 | &aux (v (aref a k)) (j 0) (n (fill-pointer a)))
|
|---|
| 72 | (declare (fixnum k n j))
|
|---|
| 73 | (loop
|
|---|
| 74 | (unless (<= k (ash n -1))
|
|---|
| 75 | (return))
|
|---|
| 76 | (setf j (ash k 1))
|
|---|
| 77 | (if (and (< j n) (not (funcall test (aref a (1+ j)) (aref a j))))
|
|---|
| 78 | (incf j))
|
|---|
| 79 | (when (funcall test (aref a j) v)
|
|---|
| 80 | (return))
|
|---|
| 81 | (setf (aref a k) (aref a j)
|
|---|
| 82 | k j))
|
|---|
| 83 | (setf (aref a k) v)
|
|---|
| 84 | a)
|
|---|
| 85 |
|
|---|
| 86 | (defun heap-remove (a &optional (test #'<=) &aux (v (aref a 1)))
|
|---|
| 87 | (when (<= (fill-pointer a) 1) (error "Empty queue."))
|
|---|
| 88 | (setf (aref a 1) (vector-pop a))
|
|---|
| 89 | (priority-queue-downheap a 1 test)
|
|---|
| 90 | (values v a))
|
|---|
| 91 |
|
|---|
| 92 | (defun heap-empty-p (a)
|
|---|
| 93 | (<= (fill-pointer a) 1))
|
|---|