[1201] | 1 | ;;; -*- Mode: Lisp -*-
|
---|
[83] | 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 |
|
---|
[446] | 22 | (defpackage "PRIORITY-QUEUE"
|
---|
[3984] | 23 | (:use :cl :heap)
|
---|
[3985] | 24 | (:export "PRIORITY-QUEUE"
|
---|
[4147] | 25 | "PRIORITY-QUEUE-ELEMENT-TYPE"
|
---|
| 26 | "PRIORITY-QUEUE-TEST"
|
---|
[3993] | 27 | "ENQUEUE"
|
---|
| 28 | "DEQUEUE"
|
---|
| 29 | "QUEUE-EMPTY-P"
|
---|
| 30 | "QUEUE-SIZE"
|
---|
[3991] | 31 | )
|
---|
| 32 | (:documentation "Implements a priority queue."))
|
---|
[83] | 33 |
|
---|
[447] | 34 | (in-package :priority-queue)
|
---|
[50] | 35 |
|
---|
[3989] | 36 | (defclass priority-queue ()
|
---|
[4141] | 37 | ((element-type :initarg :element-type
|
---|
| 38 | :initform 'fixnum
|
---|
[4149] | 39 | :accessor priority-queue-element-type
|
---|
[4141] | 40 | :type symbol
|
---|
| 41 | :documentation "Element type stored in this queue.")
|
---|
| 42 | (test :initarg :test
|
---|
[4149] | 43 | :accessor priority-queue-test
|
---|
[4141] | 44 | :documentation "The partial order used in element comparison.")
|
---|
| 45 | (heap :documentation "A private slot holding the heap."))
|
---|
[4004] | 46 | (:documentation "Representa a priority queue based on heaps.
|
---|
| 47 | Slots HEAP and TEST are read-only after they are initialized."))
|
---|
[50] | 48 |
|
---|
[4149] | 49 |
|
---|
[4150] | 50 | (defmethod initialize-instance :around ((self priority-queue)
|
---|
[4136] | 51 | &key
|
---|
| 52 | (test #'<=)
|
---|
| 53 | (element-key #'identity))
|
---|
[4150] | 54 | "Fill in the HEAP slot with an instance of a heap. NOTE: This has to
|
---|
| 55 | be an :AROUND method, so that the slot ELEMENT-TYPE can be
|
---|
| 56 | initialized in a BEFORE method of a subclass of PRIORITY-QUEUE."
|
---|
[4137] | 57 | (with-slots (heap (test-x test) element-type)
|
---|
[4149] | 58 | (call-next-method)
|
---|
[4150] | 59 | (inspect self)
|
---|
[3989] | 60 | (setf heap (make-heap :element-type element-type)
|
---|
[4150] | 61 | test-x #'(lambda (x y) (funcall test (funcall element-key y) (funcall element-key x))))
|
---|
| 62 | (inspect self))
|
---|
[4149] | 63 | self)
|
---|
[50] | 64 |
|
---|
[3989] | 65 | (defgeneric enqueue (self item)
|
---|
| 66 | (:method ((self priority-queue) (item t))
|
---|
| 67 | (with-slots (heap test)
|
---|
| 68 | self
|
---|
[3999] | 69 | (heap-insert heap item test))
|
---|
| 70 | self))
|
---|
[50] | 71 |
|
---|
[3990] | 72 | (defgeneric dequeue (self)
|
---|
| 73 | (:method ((self priority-queue))
|
---|
| 74 | (with-slots (heap test)
|
---|
| 75 | self
|
---|
| 76 | (heap-remove heap test))))
|
---|
[50] | 77 |
|
---|
[3990] | 78 | (defgeneric queue-empty-p (self)
|
---|
| 79 | (:method ((self priority-queue))
|
---|
| 80 | (with-slots (heap)
|
---|
| 81 | self
|
---|
| 82 | (heap-empty-p heap))))
|
---|
[50] | 83 |
|
---|
[3992] | 84 | (defgeneric queue-size (self)
|
---|
| 85 | (:method ((self priority-queue))
|
---|
| 86 | (with-slots (heap)
|
---|
| 87 | self
|
---|
| 88 | (heap-size heap))))
|
---|