]> git.saurik.com Git - apple/cf.git/blob - Collections.subproj/CFBinaryHeap.h
CF-368.28.tar.gz
[apple/cf.git] / Collections.subproj / CFBinaryHeap.h
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* CFBinaryHeap.h
24 Copyright (c) 1998-2005, Apple, Inc. All rights reserved.
25 */
26 /*!
27 @header CFBinaryHeap
28 CFBinaryHeap implements a container which stores values sorted using
29 a binary search algorithm. CFBinaryHeaps can be useful as priority
30 queues.
31 */
32
33 #if !defined(__COREFOUNDATION_CFBINARYHEAP__)
34 #define __COREFOUNDATION_CFBINARYHEAP__ 1
35
36 #include <CoreFoundation/CFBase.h>
37
38 #if defined(__cplusplus)
39 extern "C" {
40 #endif
41
42 typedef struct {
43 CFIndex version;
44 void * info;
45 const void *(*retain)(const void *info);
46 void (*release)(const void *info);
47 CFStringRef (*copyDescription)(const void *info);
48 } CFBinaryHeapCompareContext;
49
50 /*!
51 @typedef CFBinaryHeapCallBacks
52 Structure containing the callbacks for values of a CFBinaryHeap.
53 @field version The version number of the structure type being passed
54 in as a parameter to the CFBinaryHeap creation functions.
55 This structure is version 0.
56 @field retain The callback used to add a retain for the binary heap
57 on values as they are put into the binary heap.
58 This callback returns the value to use as the value in the
59 binary heap, which is usually the value parameter passed to
60 this callback, but may be a different value if a different
61 value should be added to the binary heap. The binary heap's
62 allocator is passed as the first argument.
63 @field release The callback used to remove a retain previously added
64 for the binary heap from values as they are removed from
65 the binary heap. The binary heap's allocator is passed as the
66 first argument.
67 @field copyDescription The callback used to create a descriptive
68 string representation of each value in the binary heap. This
69 is used by the CFCopyDescription() function.
70 @field compare The callback used to compare values in the binary heap for
71 equality in some operations.
72 */
73 typedef struct {
74 CFIndex version;
75 const void *(*retain)(CFAllocatorRef allocator, const void *ptr);
76 void (*release)(CFAllocatorRef allocator, const void *ptr);
77 CFStringRef (*copyDescription)(const void *ptr);
78 CFComparisonResult (*compare)(const void *ptr1, const void *ptr2, void *context);
79 } CFBinaryHeapCallBacks;
80
81 /*!
82 @constant kCFStringBinaryHeapCallBacks
83 Predefined CFBinaryHeapCallBacks structure containing a set
84 of callbacks appropriate for use when the values in a CFBinaryHeap
85 are all CFString types.
86 */
87 CF_EXPORT const CFBinaryHeapCallBacks kCFStringBinaryHeapCallBacks;
88
89 /*!
90 @typedef CFBinaryHeapApplierFunction
91 Type of the callback function used by the apply functions of
92 CFBinaryHeap.
93 @param value The current value from the binary heap.
94 @param context The user-defined context parameter given to the apply
95 function.
96 */
97 typedef void (*CFBinaryHeapApplierFunction)(const void *val, void *context);
98
99 /*!
100 @typedef CFBinaryHeapRef
101 This is the type of a reference to CFBinaryHeaps.
102 */
103 typedef struct __CFBinaryHeap * CFBinaryHeapRef;
104
105 /*!
106 @function CFBinaryHeapGetTypeID
107 Returns the type identifier of all CFBinaryHeap instances.
108 */
109 CF_EXPORT CFTypeID CFBinaryHeapGetTypeID(void);
110
111 /*!
112 @function CFBinaryHeapCreate
113 Creates a new mutable or fixed-mutable binary heap with the given values.
114 @param allocator The CFAllocator which should be used to allocate
115 memory for the binary heap and its storage for values. This
116 parameter may be NULL in which case the current default
117 CFAllocator is used. If this reference is not a valid
118 CFAllocator, the behavior is undefined.
119 @param capacity The maximum number of values that can be contained
120 by the CFBinaryHeap. The binary heap starts empty, and can grow to this
121 number of values (and it can have less). If this parameter
122 is 0, the binary heap's maximum capacity is unlimited (or rather,
123 only limited by address space and available memory
124 constraints). If this parameter is negative, the behavior is
125 undefined.
126 @param callBacks A pointer to a CFBinaryHeapCallBacks structure
127 initialized with the callbacks for the binary heap to use on
128 each value in the binary heap. A copy of the contents of the
129 callbacks structure is made, so that a pointer to a structure
130 on the stack can be passed in, or can be reused for multiple
131 binary heap creations. If the version field of this callbacks
132 structure is not one of the defined ones for CFBinaryHeap, the
133 behavior is undefined. The retain field may be NULL, in which
134 case the CFBinaryHeap will do nothing to add a retain to values
135 as they are put into the binary heap. The release field may be
136 NULL, in which case the CFBinaryHeap will do nothing to remove
137 the binary heap's retain (if any) on the values when the
138 heap is destroyed or a key-value pair is removed. If the
139 copyDescription field is NULL, the binary heap will create a
140 simple description for a value. If the equal field is NULL, the
141 binary heap will use pointer equality to test for equality of
142 values. This callbacks parameter itself may be NULL, which is
143 treated as if a valid structure of version 0 with all fields
144 NULL had been passed in. Otherwise,
145 if any of the fields are not valid pointers to functions
146 of the correct type, or this parameter is not a valid
147 pointer to a CFBinaryHeapCallBacks callbacks structure,
148 the behavior is undefined. If any of the values put into the
149 binary heap is not one understood by one of the callback functions
150 the behavior when that callback function is used is undefined.
151 @param compareContext A pointer to a CFBinaryHeapCompareContext structure.
152 @result A reference to the new CFBinaryHeap.
153 */
154 CF_EXPORT CFBinaryHeapRef CFBinaryHeapCreate(CFAllocatorRef allocator, CFIndex capacity, const CFBinaryHeapCallBacks *callBacks, const CFBinaryHeapCompareContext *compareContext);
155
156 /*!
157 @function CFBinaryHeapCreateCopy
158 Creates a new mutable or fixed-mutable binary heap with the values from the given binary heap.
159 @param allocator The CFAllocator which should be used to allocate
160 memory for the binary heap and its storage for values. This
161 parameter may be NULL in which case the current default
162 CFAllocator is used. If this reference is not a valid
163 CFAllocator, the behavior is undefined.
164 @param capacity The maximum number of values that can be contained
165 by the CFBinaryHeap. The binary heap starts empty, and can grow to this
166 number of values (and it can have less). If this parameter
167 is 0, the binary heap's maximum capacity is unlimited (or rather,
168 only limited by address space and available memory
169 constraints). If this parameter is negative, or less than the number of
170 values in the given binary heap, the behavior is undefined.
171 @param heap The binary heap which is to be copied. The values from the
172 binary heap are copied as pointers into the new binary heap (that is,
173 the values themselves are copied, not that which the values
174 point to, if anything). However, the values are also
175 retained by the new binary heap. The count of the new binary will
176 be the same as the given binary heap. The new binary heap uses the same
177 callbacks as the binary heap to be copied. If this parameter is
178 not a valid CFBinaryHeap, the behavior is undefined.
179 @result A reference to the new mutable or fixed-mutable binary heap.
180 */
181 CF_EXPORT CFBinaryHeapRef CFBinaryHeapCreateCopy(CFAllocatorRef allocator, CFIndex capacity, CFBinaryHeapRef heap);
182
183 /*!
184 @function CFBinaryHeapGetCount
185 Returns the number of values currently in the binary heap.
186 @param heap The binary heap to be queried. If this parameter is not a valid
187 CFBinaryHeap, the behavior is undefined.
188 @result The number of values in the binary heap.
189 */
190 CF_EXPORT CFIndex CFBinaryHeapGetCount(CFBinaryHeapRef heap);
191
192 /*!
193 @function CFBinaryHeapGetCountOfValue
194 Counts the number of times the given value occurs in the binary heap.
195 @param heap The binary heap to be searched. If this parameter is not a
196 valid CFBinaryHeap, the behavior is undefined.
197 @param value The value for which to find matches in the binary heap. The
198 compare() callback provided when the binary heap was created is
199 used to compare. If the compare() callback was NULL, pointer
200 equality (in C, ==) is used. If value, or any of the values
201 in the binary heap, are not understood by the compare() callback,
202 the behavior is undefined.
203 @result The number of times the given value occurs in the binary heap.
204 */
205 CF_EXPORT CFIndex CFBinaryHeapGetCountOfValue(CFBinaryHeapRef heap, const void *value);
206
207 /*!
208 @function CFBinaryHeapContainsValue
209 Reports whether or not the value is in the binary heap.
210 @param heap The binary heap to be searched. If this parameter is not a
211 valid CFBinaryHeap, the behavior is undefined.
212 @param value The value for which to find matches in the binary heap. The
213 compare() callback provided when the binary heap was created is
214 used to compare. If the compare() callback was NULL, pointer
215 equality (in C, ==) is used. If value, or any of the values
216 in the binary heap, are not understood by the compare() callback,
217 the behavior is undefined.
218 @result true, if the value is in the specified binary heap, otherwise false.
219 */
220 CF_EXPORT Boolean CFBinaryHeapContainsValue(CFBinaryHeapRef heap, const void *value);
221
222 /*!
223 @function CFBinaryHeapGetMinimum
224 Returns the minimum value is in the binary heap. If the heap contains several equal
225 minimum values, any one may be returned.
226 @param heap The binary heap to be searched. If this parameter is not a
227 valid CFBinaryHeap, the behavior is undefined.
228 @result A reference to the minimum value in the binary heap, or NULL if the
229 binary heap contains no values.
230 */
231 CF_EXPORT const void * CFBinaryHeapGetMinimum(CFBinaryHeapRef heap);
232
233 /*!
234 @function CFBinaryHeapGetMinimumIfPresent
235 Returns the minimum value is in the binary heap, if present. If the heap contains several equal
236 minimum values, any one may be returned.
237 @param heap The binary heap to be searched. If this parameter is not a
238 valid CFBinaryHeap, the behavior is undefined.
239 @param value A C pointer to pointer-sized storage to be filled with the minimum value in
240 the binary heap. If this value is not a valid C pointer to a pointer-sized block
241 of storage, the result is undefined. If the result of the function is false, the value
242 stored at this address is undefined.
243 @result true, if a minimum value was found in the specified binary heap, otherwise false.
244 */
245 CF_EXPORT Boolean CFBinaryHeapGetMinimumIfPresent(CFBinaryHeapRef heap, const void **value);
246
247 /*!
248 @function CFBinaryHeapGetValues
249 Fills the buffer with values from the binary heap.
250 @param heap The binary heap to be queried. If this parameter is not a
251 valid CFBinaryHeap, the behavior is undefined.
252 @param values A C array of pointer-sized values to be filled with
253 values from the binary heap. The values in the C array are ordered
254 from least to greatest. If this parameter is not a valid pointer to a
255 C array of at least CFBinaryHeapGetCount() pointers, the behavior is undefined.
256 */
257 CF_EXPORT void CFBinaryHeapGetValues(CFBinaryHeapRef heap, const void **values);
258
259 /*!
260 @function CFBinaryHeapApplyFunction
261 Calls a function once for each value in the binary heap.
262 @param heap The binary heap to be operated upon. If this parameter is not a
263 valid CFBinaryHeap, the behavior is undefined.
264 @param applier The callback function to call once for each value in
265 the given binary heap. If this parameter is not a
266 pointer to a function of the correct prototype, the behavior
267 is undefined. If there are values in the binary heap which the
268 applier function does not expect or cannot properly apply
269 to, the behavior is undefined.
270 @param context A pointer-sized user-defined value, which is passed
271 as the second parameter to the applier function, but is
272 otherwise unused by this function. If the context is not
273 what is expected by the applier function, the behavior is
274 undefined.
275 */
276 CF_EXPORT void CFBinaryHeapApplyFunction(CFBinaryHeapRef heap, CFBinaryHeapApplierFunction applier, void *context);
277
278 /*!
279 @function CFBinaryHeapAddValue
280 Adds the value to the binary heap.
281 @param heap The binary heap to which the value is to be added. If this parameter is not a
282 valid mutable CFBinaryHeap, the behavior is undefined.
283 If the binary heap is a fixed-capacity binary heap and it
284 is full before this operation, the behavior is undefined.
285 @param value The value to add to the binary heap. The value is retained by
286 the binary heap using the retain callback provided when the binary heap
287 was created. If the value is not of the sort expected by the
288 retain callback, the behavior is undefined.
289 */
290 CF_EXPORT void CFBinaryHeapAddValue(CFBinaryHeapRef heap, const void *value);
291
292 /*!
293 @function CFBinaryHeapRemoveMinimumValue
294 Removes the minimum value from the binary heap.
295 @param heap The binary heap from which the minimum value is to be removed. If this
296 parameter is not a valid mutable CFBinaryHeap, the behavior is undefined.
297 */
298 CF_EXPORT void CFBinaryHeapRemoveMinimumValue(CFBinaryHeapRef heap);
299
300 /*!
301 @function CFBinaryHeapRemoveAllValues
302 Removes all the values from the binary heap, making it empty.
303 @param heap The binary heap from which all of the values are to be
304 removed. If this parameter is not a valid mutable CFBinaryHeap,
305 the behavior is undefined.
306 */
307 CF_EXPORT void CFBinaryHeapRemoveAllValues(CFBinaryHeapRef heap);
308
309 #if defined(__cplusplus)
310 }
311 #endif
312
313 #endif /* ! __COREFOUNDATION_CFBINARYHEAP__ */
314