[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"
|
---|
[3993] | 25 | "ENQUEUE"
|
---|
| 26 | "DEQUEUE"
|
---|
| 27 | "QUEUE-EMPTY-P"
|
---|
| 28 | "QUEUE-SIZE"
|
---|
[3991] | 29 | )
|
---|
| 30 | (:documentation "Implements a priority queue."))
|
---|
[83] | 31 |
|
---|
[447] | 32 | (in-package :priority-queue)
|
---|
[50] | 33 |
|
---|
[3989] | 34 | (defclass priority-queue ()
|
---|
[4141] | 35 | ((element-type :initarg :element-type
|
---|
| 36 | :initform 'fixnum
|
---|
| 37 | :reader priority-queue-element-type
|
---|
| 38 | :type symbol
|
---|
| 39 | :documentation "Element type stored in this queue.")
|
---|
| 40 | (test :initarg :test
|
---|
| 41 | :reader priority-queue-test
|
---|
| 42 | :documentation "The partial order used in element comparison.")
|
---|
| 43 | (heap :documentation "A private slot holding the heap."))
|
---|
[4004] | 44 | (:documentation "Representa a priority queue based on heaps.
|
---|
| 45 | Slots HEAP and TEST are read-only after they are initialized."))
|
---|
[50] | 46 |
|
---|
[3997] | 47 | (defmethod initialize-instance :after ((self priority-queue)
|
---|
[4136] | 48 | &key
|
---|
| 49 | (test #'<=)
|
---|
| 50 | (element-key #'identity))
|
---|
[4137] | 51 | (with-slots (heap (test-x test) element-type)
|
---|
[3989] | 52 | self
|
---|
| 53 | (setf heap (make-heap :element-type element-type)
|
---|
[3997] | 54 | test-x #'(lambda (x y) (funcall test (funcall element-key y) (funcall element-key x))))))
|
---|
[50] | 55 |
|
---|
[3989] | 56 | (defgeneric enqueue (self item)
|
---|
| 57 | (:method ((self priority-queue) (item t))
|
---|
| 58 | (with-slots (heap test)
|
---|
| 59 | self
|
---|
[3999] | 60 | (heap-insert heap item test))
|
---|
| 61 | self))
|
---|
[50] | 62 |
|
---|
[3990] | 63 | (defgeneric dequeue (self)
|
---|
| 64 | (:method ((self priority-queue))
|
---|
| 65 | (with-slots (heap test)
|
---|
| 66 | self
|
---|
| 67 | (heap-remove heap test))))
|
---|
[50] | 68 |
|
---|
[3990] | 69 | (defgeneric queue-empty-p (self)
|
---|
| 70 | (:method ((self priority-queue))
|
---|
| 71 | (with-slots (heap)
|
---|
| 72 | self
|
---|
| 73 | (heap-empty-p heap))))
|
---|
[50] | 74 |
|
---|
[3992] | 75 | (defgeneric queue-size (self)
|
---|
| 76 | (:method ((self priority-queue))
|
---|
| 77 | (with-slots (heap)
|
---|
| 78 | self
|
---|
| 79 | (heap-size heap))))
|
---|