2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 Copyright (c) 1998-2009, Apple Inc. All rights reserved.
28 CFBinaryHeap implements a container which stores values sorted using
29 a binary search algorithm. CFBinaryHeaps can be useful as priority
33 #if !defined(__COREFOUNDATION_CFBINARYHEAP__)
34 #define __COREFOUNDATION_CFBINARYHEAP__ 1
36 #include <CoreFoundation/CFBase.h>
43 const void *(*retain
)(const void *info
);
44 void (*release
)(const void *info
);
45 CFStringRef (*copyDescription
)(const void *info
);
46 } CFBinaryHeapCompareContext
;
49 @typedef CFBinaryHeapCallBacks
50 Structure containing the callbacks for values of a CFBinaryHeap.
51 @field version The version number of the structure type being passed
52 in as a parameter to the CFBinaryHeap creation functions.
53 This structure is version 0.
54 @field retain The callback used to add a retain for the binary heap
55 on values as they are put into the binary heap.
56 This callback returns the value to use as the value in the
57 binary heap, which is usually the value parameter passed to
58 this callback, but may be a different value if a different
59 value should be added to the binary heap. The binary heap's
60 allocator is passed as the first argument.
61 @field release The callback used to remove a retain previously added
62 for the binary heap from values as they are removed from
63 the binary heap. The binary heap's allocator is passed as the
65 @field copyDescription The callback used to create a descriptive
66 string representation of each value in the binary heap. This
67 is used by the CFCopyDescription() function.
68 @field compare The callback used to compare values in the binary heap for
69 equality in some operations.
73 const void *(*retain
)(CFAllocatorRef allocator
, const void *ptr
);
74 void (*release
)(CFAllocatorRef allocator
, const void *ptr
);
75 CFStringRef (*copyDescription
)(const void *ptr
);
76 CFComparisonResult (*compare
)(const void *ptr1
, const void *ptr2
, void *context
);
77 } CFBinaryHeapCallBacks
;
80 @constant kCFStringBinaryHeapCallBacks
81 Predefined CFBinaryHeapCallBacks structure containing a set
82 of callbacks appropriate for use when the values in a CFBinaryHeap
83 are all CFString types.
85 CF_EXPORT
const CFBinaryHeapCallBacks kCFStringBinaryHeapCallBacks
;
88 @typedef CFBinaryHeapApplierFunction
89 Type of the callback function used by the apply functions of
91 @param value The current value from the binary heap.
92 @param context The user-defined context parameter given to the apply
95 typedef void (*CFBinaryHeapApplierFunction
)(const void *val
, void *context
);
98 @typedef CFBinaryHeapRef
99 This is the type of a reference to CFBinaryHeaps.
101 typedef struct __CFBinaryHeap
* CFBinaryHeapRef
;
104 @function CFBinaryHeapGetTypeID
105 Returns the type identifier of all CFBinaryHeap instances.
107 CF_EXPORT CFTypeID
CFBinaryHeapGetTypeID(void);
110 @function CFBinaryHeapCreate
111 Creates a new mutable binary heap with the given values.
112 @param allocator The CFAllocator which should be used to allocate
113 memory for the binary heap and its storage for values. This
114 parameter may be NULL in which case the current default
115 CFAllocator is used. If this reference is not a valid
116 CFAllocator, the behavior is undefined.
117 @param capacity A hint about the number of values that will be held
118 by the CFBinaryHeap. Pass 0 for no hint. The implementation may
119 ignore this hint, or may use it to optimize various
120 operations. A heap's actual capacity is only limited by
121 address space and available memory constraints). If this
122 parameter is negative, the behavior is undefined.
123 @param callBacks A pointer to a CFBinaryHeapCallBacks structure
124 initialized with the callbacks for the binary heap to use on
125 each value in the binary heap. A copy of the contents of the
126 callbacks structure is made, so that a pointer to a structure
127 on the stack can be passed in, or can be reused for multiple
128 binary heap creations. If the version field of this callbacks
129 structure is not one of the defined ones for CFBinaryHeap, the
130 behavior is undefined. The retain field may be NULL, in which
131 case the CFBinaryHeap will do nothing to add a retain to values
132 as they are put into the binary heap. The release field may be
133 NULL, in which case the CFBinaryHeap will do nothing to remove
134 the binary heap's retain (if any) on the values when the
135 heap is destroyed or a key-value pair is removed. If the
136 copyDescription field is NULL, the binary heap will create a
137 simple description for a value. If the equal field is NULL, the
138 binary heap will use pointer equality to test for equality of
139 values. This callbacks parameter itself may be NULL, which is
140 treated as if a valid structure of version 0 with all fields
141 NULL had been passed in. Otherwise,
142 if any of the fields are not valid pointers to functions
143 of the correct type, or this parameter is not a valid
144 pointer to a CFBinaryHeapCallBacks callbacks structure,
145 the behavior is undefined. If any of the values put into the
146 binary heap is not one understood by one of the callback functions
147 the behavior when that callback function is used is undefined.
148 @param compareContext A pointer to a CFBinaryHeapCompareContext structure.
149 @result A reference to the new CFBinaryHeap.
151 CF_EXPORT CFBinaryHeapRef
CFBinaryHeapCreate(CFAllocatorRef allocator
, CFIndex capacity
, const CFBinaryHeapCallBacks
*callBacks
, const CFBinaryHeapCompareContext
*compareContext
);
154 @function CFBinaryHeapCreateCopy
155 Creates a new mutable binary heap with the values from the given binary heap.
156 @param allocator The CFAllocator which should be used to allocate
157 memory for the binary heap and its storage for values. This
158 parameter may be NULL in which case the current default
159 CFAllocator is used. If this reference is not a valid
160 CFAllocator, the behavior is undefined.
161 @param capacity A hint about the number of values that will be held
162 by the CFBinaryHeap. Pass 0 for no hint. The implementation may
163 ignore this hint, or may use it to optimize various
164 operations. A heap's actual capacity is only limited by
165 address space and available memory constraints).
166 This parameter must be greater than or equal
167 to the count of the heap which is to be copied, or the
168 behavior is undefined. If this parameter is negative, the
169 behavior is undefined.
170 @param heap The binary heap which is to be copied. The values from the
171 binary heap are copied as pointers into the new binary heap (that is,
172 the values themselves are copied, not that which the values
173 point to, if anything). However, the values are also
174 retained by the new binary heap. The count of the new binary will
175 be the same as the given binary heap. The new binary heap uses the same
176 callbacks as the binary heap to be copied. If this parameter is
177 not a valid CFBinaryHeap, the behavior is undefined.
178 @result A reference to the new mutable binary heap.
180 CF_EXPORT CFBinaryHeapRef
CFBinaryHeapCreateCopy(CFAllocatorRef allocator
, CFIndex capacity
, CFBinaryHeapRef heap
);
183 @function CFBinaryHeapGetCount
184 Returns the number of values currently in the binary heap.
185 @param heap The binary heap to be queried. If this parameter is not a valid
186 CFBinaryHeap, the behavior is undefined.
187 @result The number of values in the binary heap.
189 CF_EXPORT CFIndex
CFBinaryHeapGetCount(CFBinaryHeapRef heap
);
192 @function CFBinaryHeapGetCountOfValue
193 Counts the number of times the given value occurs in the binary heap.
194 @param heap The binary heap to be searched. If this parameter is not a
195 valid CFBinaryHeap, the behavior is undefined.
196 @param value The value for which to find matches in the binary heap. The
197 compare() callback provided when the binary heap was created is
198 used to compare. If the compare() callback was NULL, pointer
199 equality (in C, ==) is used. If value, or any of the values
200 in the binary heap, are not understood by the compare() callback,
201 the behavior is undefined.
202 @result The number of times the given value occurs in the binary heap.
204 CF_EXPORT CFIndex
CFBinaryHeapGetCountOfValue(CFBinaryHeapRef heap
, const void *value
);
207 @function CFBinaryHeapContainsValue
208 Reports whether or not the value is in the binary heap.
209 @param heap The binary heap to be searched. If this parameter is not a
210 valid CFBinaryHeap, the behavior is undefined.
211 @param value The value for which to find matches in the binary heap. The
212 compare() callback provided when the binary heap was created is
213 used to compare. If the compare() callback was NULL, pointer
214 equality (in C, ==) is used. If value, or any of the values
215 in the binary heap, are not understood by the compare() callback,
216 the behavior is undefined.
217 @result true, if the value is in the specified binary heap, otherwise false.
219 CF_EXPORT Boolean
CFBinaryHeapContainsValue(CFBinaryHeapRef heap
, const void *value
);
222 @function CFBinaryHeapGetMinimum
223 Returns the minimum value is in the binary heap. If the heap contains several equal
224 minimum values, any one may be returned.
225 @param heap The binary heap to be searched. If this parameter is not a
226 valid CFBinaryHeap, the behavior is undefined.
227 @result A reference to the minimum value in the binary heap, or NULL if the
228 binary heap contains no values.
230 CF_EXPORT
const void * CFBinaryHeapGetMinimum(CFBinaryHeapRef heap
);
233 @function CFBinaryHeapGetMinimumIfPresent
234 Returns the minimum value is in the binary heap, if present. If the heap contains several equal
235 minimum values, any one may be returned.
236 @param heap The binary heap to be searched. If this parameter is not a
237 valid CFBinaryHeap, the behavior is undefined.
238 @param value A C pointer to pointer-sized storage to be filled with the minimum value in
239 the binary heap. If this value is not a valid C pointer to a pointer-sized block
240 of storage, the result is undefined. If the result of the function is false, the value
241 stored at this address is undefined.
242 @result true, if a minimum value was found in the specified binary heap, otherwise false.
244 CF_EXPORT Boolean
CFBinaryHeapGetMinimumIfPresent(CFBinaryHeapRef heap
, const void **value
);
247 @function CFBinaryHeapGetValues
248 Fills the buffer with values from the binary heap.
249 @param heap The binary heap to be queried. If this parameter is not a
250 valid CFBinaryHeap, the behavior is undefined.
251 @param values A C array of pointer-sized values to be filled with
252 values from the binary heap. The values in the C array are ordered
253 from least to greatest. If this parameter is not a valid pointer to a
254 C array of at least CFBinaryHeapGetCount() pointers, the behavior is undefined.
256 CF_EXPORT
void CFBinaryHeapGetValues(CFBinaryHeapRef heap
, const void **values
);
259 @function CFBinaryHeapApplyFunction
260 Calls a function once for each value in the binary heap.
261 @param heap The binary heap to be operated upon. If this parameter is not a
262 valid CFBinaryHeap, the behavior is undefined.
263 @param applier The callback function to call once for each value in
264 the given binary heap. If this parameter is not a
265 pointer to a function of the correct prototype, the behavior
266 is undefined. If there are values in the binary heap which the
267 applier function does not expect or cannot properly apply
268 to, the behavior is undefined.
269 @param context A pointer-sized user-defined value, which is passed
270 as the second parameter to the applier function, but is
271 otherwise unused by this function. If the context is not
272 what is expected by the applier function, the behavior is
275 CF_EXPORT
void CFBinaryHeapApplyFunction(CFBinaryHeapRef heap
, CFBinaryHeapApplierFunction applier
, void *context
);
278 @function CFBinaryHeapAddValue
279 Adds the value to the binary heap.
280 @param heap The binary heap to which the value is to be added. If this parameter is not a
281 valid mutable CFBinaryHeap, the behavior is undefined.
282 @param value The value to add to the binary heap. The value is retained by
283 the binary heap using the retain callback provided when the binary heap
284 was created. If the value is not of the sort expected by the
285 retain callback, the behavior is undefined.
287 CF_EXPORT
void CFBinaryHeapAddValue(CFBinaryHeapRef heap
, const void *value
);
290 @function CFBinaryHeapRemoveMinimumValue
291 Removes the minimum value from the binary heap.
292 @param heap The binary heap from which the minimum value is to be removed. If this
293 parameter is not a valid mutable CFBinaryHeap, the behavior is undefined.
295 CF_EXPORT
void CFBinaryHeapRemoveMinimumValue(CFBinaryHeapRef heap
);
298 @function CFBinaryHeapRemoveAllValues
299 Removes all the values from the binary heap, making it empty.
300 @param heap The binary heap from which all of the values are to be
301 removed. If this parameter is not a valid mutable CFBinaryHeap,
302 the behavior is undefined.
304 CF_EXPORT
void CFBinaryHeapRemoveAllValues(CFBinaryHeapRef heap
);
308 #endif /* ! __COREFOUNDATION_CFBINARYHEAP__ */