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 |
|
---|
22 |
|
---|
23 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
24 | ;;
|
---|
25 | ;; A conventional implementation of priority queues based on heaps
|
---|
26 | ;;
|
---|
27 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
28 |
|
---|
29 | (defparameter *priority-queue-allocation-size* 16)
|
---|
30 |
|
---|
31 | (defun priority-queue-make-heap (&key (element-type 'fixnum))
|
---|
32 | (make-array *priority-queue-allocation-size* :element-type element-type :fill-pointer 1
|
---|
33 | :adjustable t))
|
---|
34 |
|
---|
35 | (defstruct (priority-queue (:constructor priority-queue-construct))
|
---|
36 | (heap (priority-queue-make-heap))
|
---|
37 | test)
|
---|
38 |
|
---|
39 | (defun make-priority-queue (&key (element-type 'fixnum)
|
---|
40 | (test #'<=)
|
---|
41 | (element-key #'identity))
|
---|
42 | (priority-queue-construct
|
---|
43 | :heap (priority-queue-make-heap :element-type element-type)
|
---|
44 | :test #'(lambda (x y) (funcall test (funcall element-key y) (funcall element-key x)))))
|
---|
45 |
|
---|
46 | (defun priority-queue-insert (pq item)
|
---|
47 | (priority-queue-heap-insert (priority-queue-heap pq) item (priority-queue-test pq)))
|
---|
48 |
|
---|
49 | (defun priority-queue-remove (pq)
|
---|
50 | (priority-queue-heap-remove (priority-queue-heap pq) (priority-queue-test pq)))
|
---|
51 |
|
---|
52 | (defun priority-queue-empty-p (pq)
|
---|
53 | (priority-queue-heap-empty-p (priority-queue-heap pq)))
|
---|
54 |
|
---|
55 | (defun priority-queue-size (pq)
|
---|
56 | (fill-pointer (priority-queue-heap pq)))
|
---|
57 |
|
---|
58 | (defun priority-queue-upheap (a k
|
---|
59 | &optional
|
---|
60 | (test #'<=)
|
---|
61 | &aux (v (aref a k)))
|
---|
62 | (declare (fixnum k))
|
---|
63 | (assert (< 0 k (fill-pointer a)))
|
---|
64 | (loop
|
---|
65 | (let ((parent (ash k -1)))
|
---|
66 | (when (zerop parent) (return))
|
---|
67 | (unless (funcall test (aref a parent) v)
|
---|
68 | (return))
|
---|
69 | (setf (aref a k) (aref a parent)
|
---|
70 | k parent)))
|
---|
71 | (setf (aref a k) v)
|
---|
72 | a)
|
---|
73 |
|
---|
74 |
|
---|
75 | (defun priority-queue-heap-insert (a item &optional (test #'<=))
|
---|
76 | (vector-push-extend item a)
|
---|
77 | (priority-queue-upheap a (1- (fill-pointer a)) test))
|
---|
78 |
|
---|
79 | (defun priority-queue-downheap (a k
|
---|
80 | &optional
|
---|
81 | (test #'<=)
|
---|
82 | &aux (v (aref a k)) (j 0) (n (fill-pointer a)))
|
---|
83 | (declare (fixnum k n j))
|
---|
84 | (loop
|
---|
85 | (unless (<= k (ash n -1))
|
---|
86 | (return))
|
---|
87 | (setf j (ash k 1))
|
---|
88 | (if (and (< j n) (not (funcall test (aref a (1+ j)) (aref a j))))
|
---|
89 | (incf j))
|
---|
90 | (when (funcall test (aref a j) v)
|
---|
91 | (return))
|
---|
92 | (setf (aref a k) (aref a j)
|
---|
93 | k j))
|
---|
94 | (setf (aref a k) v)
|
---|
95 | a)
|
---|
96 |
|
---|
97 | (defun priority-queue-heap-remove (a &optional (test #'<=) &aux (v (aref a 1)))
|
---|
98 | (when (<= (fill-pointer a) 1) (error "Empty queue."))
|
---|
99 | (setf (aref a 1) (vector-pop a))
|
---|
100 | (priority-queue-downheap a 1 test)
|
---|
101 | (values v a))
|
---|
102 |
|
---|
103 | (defun priority-queue-heap-empty-p (a)
|
---|
104 | (<= (fill-pointer a) 1))
|
---|