close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/priority-queue.lisp@ 4156

Last change on this file since 4156 was 4156, checked in by Marek Rychlik, 8 years ago

* empty log message *

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