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

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

* empty log message *

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