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