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

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

* empty log message *

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