1 .\" $Id: heap.mdoc,v 1.3 2004/03/09 06:30:08 marka Exp $
3 .\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4 .\" Copyright (c) 1997,1999 by Internet Software Consortium.
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 .\"Os OPERATING_SYSTEM [version/release]
21 .Dt HEAP @SYSCALL_EXT@
31 .Nd heap implementation of priority queues
33 .Fd #include \&"heap.h\&"
35 .Fn heap_new "heap_higher_priority_func higher_priority" \
36 "heap_index_func index" "int array_size_increment"
38 .Fn heap_free "heap_context ctx"
40 .Fn heap_insert "heap_context ctx" "void *elt"
42 .Fn heap_delete "heap_context ctx" "int i"
44 .Fn heap_increased "heap_context ctx" "int i"
46 .Fn heap_decreased "heap_context ctx" "int i"
48 .Fn heap_element "heap_context ctx" "int i"
50 .Fn heap_for_each "heap_context ctx" "heap_for_each_func action" "void *uap"
52 These functions implement heap\-based priority queues. The user defines a
53 priority scheme, and provides a function for comparison of the priority
55 (see the description of the
56 .Ft heap_higher_priority_func
57 function pointer, below).
59 Each of the functions depends upon the
61 type, which is a pointer to a
62 .Ft struct heap_context
63 .Pq see Pa heap.h No for more information .
67 header file also defines the following set of function
69 .Bd -literal -offset indent
70 typedef int (*heap_higher_priority_func)(void *, void *);
71 typedef void (*heap_index_func)(void *, int);
72 typedef void (*heap_for_each_func)(void *, void *);
75 These are pointers to user-defined functions.
77 .Ft heap_higher_priority_func
78 type is a pointer to a function which compares two
79 different heap (queue) elements and returns an
81 which answers the question, "Does the first queue element
82 have a higher priority than the second?" In other words,
83 a function pointer of this type
85 return a number greater than zero
86 if the element indicated by the first argument is of a higher priority than
87 that indicated by the second element, and zero otherwise.
89 The other two function pointers are documented in the descriptions
92 .Pq Va heap_index_func
95 .Pq Va heap_for_each_func ,
101 .Ft struct heap_context
102 and returns a pointer to it. The
107 .No non\- Ns Dv NULL .
108 As explained above, this refers to a
109 function supplied by the user which compares the priority of two different
110 queue or heap elements; see above for more information.
113 is a pointer to a user-defined function whose arguments are
114 a heap element and its index in the heap.
116 is intended to provide the user a means of knowing the internal index
117 of an element in the heap while maintaining the opacity of the implementation;
118 since the user has to know the actual indexes of heap elements in order to use,
125 function could store the index in the heap element, itself. If
128 .No non\- Ns Dv NULL ,
131 the index of an element changes, allowing the user to stay up\-to\-date
134 .Fa array_size_increment
135 will be used, as its name suggests, by
139 to increment the array which implements the heap; if zero, a default value
144 function frees the given
148 which also frees the entire
151 .No non\- Ns Dv NULL .
155 .No non\- Ns Dv NULL .
159 function is used to insert the new heap element
161 into the appropriate place (priority\-wise) in the
168 .No non\- Ns Dv NULL ,
171 function pointer associated with the indicated
173 is used to determine that
174 .Dq appropriate place ;
175 the highest\-priority elements are at the front of the queue (top of
177 (See the description of
179 above, for more information.)
183 is used to delete the
185 element of the queue (heap), and fixing up the queue (heap) from that
186 element onward via the priority as determined by the user function
202 element of the queue/heap indicated by
208 function provides a mechanism for the user to increment through the entire
209 queue (heap) and perform some
211 upon each of the queue elements. This
213 is pointer to a user\-defined function with two arguments, the first of
214 which should be interpreted by the user's function as a heap element. The
215 second value passed to the user function is just the
219 this allows the user to specify additional arguments, if necessary, to
220 the function pointed to by
222 .\" The following requests should be uncommented and
223 .\" used where appropriate. This next request is
224 .\" for sections 2 and 3 function return values only.
226 .Bl -tag -width "heap_decreased()"
232 .Ft struct heap_context
262 the heap array fails (with
277 is out\-of\-range (with
282 .It Fn heap_increased
285 .It Fn heap_decreased
299 otherwise, a pointer to the
315 .\" This next request is for sections 1, 6, 7 & 8 only
318 .Bl -tag -width "heap.h000"
320 heap library header file
323 .\" This next request is for sections 1, 6, 7 & 8 only
324 .\" (command return values (to shell) and
325 .\" fprintf/stderr type diagnostics)
329 .\" The next request is for sections 2 and 3 error
330 .\" and signal handling only.
341 under the conditions of invalid input
354 .%B Introduction to Algorithms
355 .%Q "MIT Press / McGraw Hill"
357 .%O ISBN 0\-262\-03141\-8
362 .%B Algorithms, 2nd ed'n
365 .%O ISBN 0\-201\-06673\-4
373 library was implemented by Bob Halley (halley@vix.com) of Vixie Enterprises,
374 Inc., for the Internet Software consortium, and was adapted from
375 the two books listed in the