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

Last change on this file since 459 was 456, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 3.9 KB
RevLine 
[83]1;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
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"
29 (:use :cl)
30 (:export "*PRIORITY-QUEUE-ALLOCATION-SIZE*"
[456]31 "PRIORITY-QUEUE"
[446]32 "MAKE-PRIORITY-QUEUE"
33 "PRIORITY-QUEUE-INSERT"
34 "PRIORITY-QUEUE-REMOVE"
35 "PRIORITY-QUEUE-EMPTY-P"
[448]36 "PRIORITY-QUEUE-SIZE"
37 ))
[83]38
[447]39(in-package :priority-queue)
[50]40(defparameter *priority-queue-allocation-size* 16)
41
42(defun priority-queue-make-heap (&key (element-type 'fixnum))
43 (make-array *priority-queue-allocation-size* :element-type element-type :fill-pointer 1
44 :adjustable t))
45
46(defstruct (priority-queue (:constructor priority-queue-construct))
47 (heap (priority-queue-make-heap))
48 test)
49
50(defun make-priority-queue (&key (element-type 'fixnum)
51 (test #'<=)
52 (element-key #'identity))
53 (priority-queue-construct
54 :heap (priority-queue-make-heap :element-type element-type)
55 :test #'(lambda (x y) (funcall test (funcall element-key y) (funcall element-key x)))))
56
57(defun priority-queue-insert (pq item)
58 (priority-queue-heap-insert (priority-queue-heap pq) item (priority-queue-test pq)))
59
60(defun priority-queue-remove (pq)
61 (priority-queue-heap-remove (priority-queue-heap pq) (priority-queue-test pq)))
62
63(defun priority-queue-empty-p (pq)
64 (priority-queue-heap-empty-p (priority-queue-heap pq)))
65
66(defun priority-queue-size (pq)
67 (fill-pointer (priority-queue-heap pq)))
68
69(defun priority-queue-upheap (a k
70 &optional
71 (test #'<=)
72 &aux (v (aref a k)))
73 (declare (fixnum k))
74 (assert (< 0 k (fill-pointer a)))
75 (loop
76 (let ((parent (ash k -1)))
77 (when (zerop parent) (return))
78 (unless (funcall test (aref a parent) v)
79 (return))
80 (setf (aref a k) (aref a parent)
81 k parent)))
82 (setf (aref a k) v)
83 a)
84
85
86(defun priority-queue-heap-insert (a item &optional (test #'<=))
87 (vector-push-extend item a)
88 (priority-queue-upheap a (1- (fill-pointer a)) test))
89
90(defun priority-queue-downheap (a k
91 &optional
92 (test #'<=)
93 &aux (v (aref a k)) (j 0) (n (fill-pointer a)))
94 (declare (fixnum k n j))
95 (loop
96 (unless (<= k (ash n -1))
97 (return))
98 (setf j (ash k 1))
99 (if (and (< j n) (not (funcall test (aref a (1+ j)) (aref a j))))
100 (incf j))
101 (when (funcall test (aref a j) v)
102 (return))
103 (setf (aref a k) (aref a j)
104 k j))
105 (setf (aref a k) v)
106 a)
107
108(defun priority-queue-heap-remove (a &optional (test #'<=) &aux (v (aref a 1)))
109 (when (<= (fill-pointer a) 1) (error "Empty queue."))
110 (setf (aref a 1) (vector-pop a))
111 (priority-queue-downheap a 1 test)
112 (values v a))
113
114(defun priority-queue-heap-empty-p (a)
115 (<= (fill-pointer a) 1))
Note: See TracBrowser for help on using the repository browser.