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/heap.lisp@ 3980

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

* empty log message *

File size: 2.9 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23;;
24;; A conventional implementation of priority queues based on heaps
25;;
26;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27
28(defpackage "HEAP"
29 (:use :cl)
30 (:export "+HEAP-ALLOCATION-SIZE+"
31 ))
32
33(in-package :heap)
34(defparameter +heap-allocation-size+ 16)
35
36(defun make-heap (&key (element-type 'fixnum))
37 (make-array *priority-queue-allocation-size* :element-type element-type :fill-pointer 1
38 :adjustable t))
39
40(defun heap-size (pq)
41 (fill-pointer (priority-queue-heap pq)))
42
43(defun heap-upheap (a k
44 &optional
45 (test #'<=)
46 &aux (v (aref a k)))
47 (declare (fixnum k))
48 (assert (< 0 k (fill-pointer a)))
49 (loop
50 (let ((parent (ash k -1)))
51 (when (zerop parent) (return))
52 (unless (funcall test (aref a parent) v)
53 (return))
54 (setf (aref a k) (aref a parent)
55 k parent)))
56 (setf (aref a k) v)
57 a)
58
59
60(defun heap-insert (a item &optional (test #'<=))
61 (vector-push-extend item a)
62 (heap-upheap a (1- (fill-pointer a)) test))
63
64(defun heap-downheap (a k
65 &optional
66 (test #'<=)
67 &aux (v (aref a k)) (j 0) (n (fill-pointer a)))
68 (declare (fixnum k n j))
69 (loop
70 (unless (<= k (ash n -1))
71 (return))
72 (setf j (ash k 1))
73 (if (and (< j n) (not (funcall test (aref a (1+ j)) (aref a j))))
74 (incf j))
75 (when (funcall test (aref a j) v)
76 (return))
77 (setf (aref a k) (aref a j)
78 k j))
79 (setf (aref a k) v)
80 a)
81
82(defun heap-remove (a &optional (test #'<=) &aux (v (aref a 1)))
83 (when (<= (fill-pointer a) 1) (error "Empty queue."))
84 (setf (aref a 1) (vector-pop a))
85 (priority-queue-downheap a 1 test)
86 (values v a))
87
88(defun heap-empty-p (a)
89 (<= (fill-pointer a) 1))
Note: See TracBrowser for help on using the repository browser.