[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 |
|
---|
[447] | 22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 23 | ;;
|
---|
| 24 | ;; A conventional implementation of priority queues based on heaps
|
---|
| 25 | ;;
|
---|
| 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 27 |
|
---|
[446] | 28 | (defpackage "PRIORITY-QUEUE"
|
---|
[3984] | 29 | (:use :cl :heap)
|
---|
[3985] | 30 | (:export "PRIORITY-QUEUE"
|
---|
[446] | 31 | "MAKE-PRIORITY-QUEUE"
|
---|
| 32 | "PRIORITY-QUEUE-INSERT"
|
---|
| 33 | "PRIORITY-QUEUE-REMOVE"
|
---|
| 34 | "PRIORITY-QUEUE-EMPTY-P"
|
---|
[448] | 35 | "PRIORITY-QUEUE-SIZE"
|
---|
| 36 | ))
|
---|
[83] | 37 |
|
---|
[447] | 38 | (in-package :priority-queue)
|
---|
[50] | 39 |
|
---|
[3989] | 40 | (defclass priority-queue ()
|
---|
| 41 | ((heap :initarg :heap :accessor priority-queue-heap)
|
---|
| 42 | (test :initarg :ters :accessor priority-queue-test))
|
---|
| 43 | (:documentation "Representa a priority queue."))
|
---|
[50] | 44 |
|
---|
[3989] | 45 | (defmethod initialize-instance ((self priority-queue)
|
---|
| 46 | &key
|
---|
| 47 | (element-type 'fixnum)
|
---|
| 48 | (test #'<=)
|
---|
| 49 | (element-key #'identity))
|
---|
| 50 | (with-slots (heap test)
|
---|
| 51 | self
|
---|
| 52 | (setf heap (make-heap :element-type element-type)
|
---|
| 53 | test #'(lambda (x y) (funcall test (funcall element-key y) (funcall element-key x))))))
|
---|
[50] | 54 |
|
---|
[3989] | 55 | (defgeneric enqueue (self item)
|
---|
| 56 | (:method ((self priority-queue) (item t))
|
---|
| 57 | (with-slots (heap test)
|
---|
| 58 | self
|
---|
| 59 | (heap-insert heap item test))))
|
---|
[50] | 60 |
|
---|
[3990] | 61 | (defgeneric dequeue (self)
|
---|
| 62 | (:method ((self priority-queue))
|
---|
| 63 | (with-slots (heap test)
|
---|
| 64 | self
|
---|
| 65 | (heap-remove heap test))))
|
---|
[50] | 66 |
|
---|
[3990] | 67 | (defgeneric queue-empty-p (self)
|
---|
| 68 | (:method ((self priority-queue))
|
---|
| 69 | (with-slots (heap)
|
---|
| 70 | self
|
---|
| 71 | (heap-empty-p heap))))
|
---|
[50] | 72 |
|
---|
| 73 | (defun priority-queue-size (pq)
|
---|
| 74 | (fill-pointer (priority-queue-heap pq)))
|
---|