| 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 "PRIORITY-QUEUE"
|
---|
| 29 | (:use :cl)
|
---|
| 30 | (:export "*PRIORITY-QUEUE-ALLOCATION-SIZE*"
|
---|
| 31 | "PRIORITY-QUEUE"
|
---|
| 32 | "PRIORITY-QUEUE-HEAP"
|
---|
| 33 | "PRIORITY-QUEUE-TEST"
|
---|
| 34 | "MAKE-PRIORITY-QUEUE"
|
---|
| 35 | "PRIORITY-QUEUE-INSERT"
|
---|
| 36 | "PRIORITY-QUEUE-REMOVE"
|
---|
| 37 | "PRIORITY-QUEUE-EMPTY-P"
|
---|
| 38 | "PRIORITY-QUEUE-SIZE"
|
---|
| 39 | ))
|
---|
| 40 |
|
---|
| 41 | (in-package :priority-queue)
|
---|
| 42 | (defparameter *priority-queue-allocation-size* 16)
|
---|
| 43 |
|
---|
| 44 | (defun priority-queue-make-heap (&key (element-type 'fixnum))
|
---|
| 45 | (make-array *priority-queue-allocation-size* :element-type element-type :fill-pointer 1
|
---|
| 46 | :adjustable t))
|
---|
| 47 |
|
---|
| 48 | (defstruct (priority-queue (:constructor priority-queue-construct))
|
---|
| 49 | (heap (priority-queue-make-heap))
|
---|
| 50 | test)
|
---|
| 51 |
|
---|
| 52 | (defun make-priority-queue (&key (element-type 'fixnum)
|
---|
| 53 | (test #'<=)
|
---|
| 54 | (element-key #'identity))
|
---|
| 55 | (priority-queue-construct
|
---|
| 56 | :heap (priority-queue-make-heap :element-type element-type)
|
---|
| 57 | :test #'(lambda (x y) (funcall test (funcall element-key y) (funcall element-key x)))))
|
---|
| 58 |
|
---|
| 59 | (defun priority-queue-insert (pq item)
|
---|
| 60 | (priority-queue-heap-insert (priority-queue-heap pq) item (priority-queue-test pq)))
|
---|
| 61 |
|
---|
| 62 | (defun priority-queue-remove (pq)
|
---|
| 63 | (priority-queue-heap-remove (priority-queue-heap pq) (priority-queue-test pq)))
|
---|
| 64 |
|
---|
| 65 | (defun priority-queue-empty-p (pq)
|
---|
| 66 | (priority-queue-heap-empty-p (priority-queue-heap pq)))
|
---|
| 67 |
|
---|
| 68 | (defun priority-queue-size (pq)
|
---|
| 69 | (fill-pointer (priority-queue-heap pq)))
|
---|
| 70 |
|
---|
| 71 | (defun priority-queue-upheap (a k
|
---|
| 72 | &optional
|
---|
| 73 | (test #'<=)
|
---|
| 74 | &aux (v (aref a k)))
|
---|
| 75 | (declare (fixnum k))
|
---|
| 76 | (assert (< 0 k (fill-pointer a)))
|
---|
| 77 | (loop
|
---|
| 78 | (let ((parent (ash k -1)))
|
---|
| 79 | (when (zerop parent) (return))
|
---|
| 80 | (unless (funcall test (aref a parent) v)
|
---|
| 81 | (return))
|
---|
| 82 | (setf (aref a k) (aref a parent)
|
---|
| 83 | k parent)))
|
---|
| 84 | (setf (aref a k) v)
|
---|
| 85 | a)
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | (defun priority-queue-heap-insert (a item &optional (test #'<=))
|
---|
| 89 | (vector-push-extend item a)
|
---|
| 90 | (priority-queue-upheap a (1- (fill-pointer a)) test))
|
---|
| 91 |
|
---|
| 92 | (defun priority-queue-downheap (a k
|
---|
| 93 | &optional
|
---|
| 94 | (test #'<=)
|
---|
| 95 | &aux (v (aref a k)) (j 0) (n (fill-pointer a)))
|
---|
| 96 | (declare (fixnum k n j))
|
---|
| 97 | (loop
|
---|
| 98 | (unless (<= k (ash n -1))
|
---|
| 99 | (return))
|
---|
| 100 | (setf j (ash k 1))
|
---|
| 101 | (if (and (< j n) (not (funcall test (aref a (1+ j)) (aref a j))))
|
---|
| 102 | (incf j))
|
---|
| 103 | (when (funcall test (aref a j) v)
|
---|
| 104 | (return))
|
---|
| 105 | (setf (aref a k) (aref a j)
|
---|
| 106 | k j))
|
---|
| 107 | (setf (aref a k) v)
|
---|
| 108 | a)
|
---|
| 109 |
|
---|
| 110 | (defun priority-queue-heap-remove (a &optional (test #'<=) &aux (v (aref a 1)))
|
---|
| 111 | (when (<= (fill-pointer a) 1) (error "Empty queue."))
|
---|
| 112 | (setf (aref a 1) (vector-pop a))
|
---|
| 113 | (priority-queue-downheap a 1 test)
|
---|
| 114 | (values v a))
|
---|
| 115 |
|
---|
| 116 | (defun priority-queue-heap-empty-p (a)
|
---|
| 117 | (<= (fill-pointer a) 1))
|
---|