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@ 4000

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

* empty log message *

File size: 2.5 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 "ENQUEUE"
26 "DEQUEUE"
27 "QUEUE-EMPTY-P"
28 "QUEUE-SIZE"
29 )
30 (:documentation "Implements a priority queue."))
31
32(in-package :priority-queue)
33
34(defclass priority-queue ()
35 ((heap :initarg :heap :reader priority-queue-heap)
36 (test :initarg :test :reader priority-queue-test))
37 (:documentation "Representa a priority queue."))
38
39(defmethod initialize-instance :after ((self priority-queue)
40 &key
41 (element-type 'fixnum)
42 (test #'<=)
43 (element-key #'identity))
44 (with-accessors ((heap priority-queue-heap)
45 (test-x priority-queue-test))
46 self
47 (setf heap (make-heap :element-type element-type)
48 test-x #'(lambda (x y) (funcall test (funcall element-key y) (funcall element-key x))))))
49
50(defgeneric enqueue (self item)
51 (:method ((self priority-queue) (item t))
52 (with-slots (heap test)
53 self
54 (heap-insert heap item test))
55 self))
56
57
58(defgeneric dequeue (self)
59 (:method ((self priority-queue))
60 (with-slots (heap test)
61 self
62 (heap-remove heap test))))
63
64(defgeneric queue-empty-p (self)
65 (:method ((self priority-queue))
66 (with-slots (heap)
67 self
68 (heap-empty-p heap))))
69
70(defgeneric queue-size (self)
71 (:method ((self priority-queue))
72 (with-slots (heap)
73 self
74 (heap-size heap))))
Note: See TracBrowser for help on using the repository browser.