-
Notifications
You must be signed in to change notification settings - Fork 109
/
CFBinaryHeap.c
443 lines (398 loc) · 17.5 KB
/
CFBinaryHeap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* Copyright (c) 2015 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFBinaryHeap.c
Copyright (c) 1998-2014, Apple Inc. All rights reserved.
Responsibility: Christopher Kane
*/
#include <CoreFoundation/CFBinaryHeap.h>
#include <CoreFoundation/CFPriv.h>
#include "CFInternal.h"
const CFBinaryHeapCallBacks kCFStringBinaryHeapCallBacks = {0, __CFTypeCollectionRetain, __CFTypeCollectionRelease, CFCopyDescription, (CFComparisonResult (*)(const void *, const void *, void *))CFStringCompare};
struct __CFBinaryHeapBucket {
void *_item;
};
CF_INLINE CFIndex __CFBinaryHeapRoundUpCapacity(CFIndex capacity) {
if (capacity < 4) return 4;
return (1 << flsl(capacity));
}
CF_INLINE CFIndex __CFBinaryHeapNumBucketsForCapacity(CFIndex capacity) {
return capacity;
}
struct __CFBinaryHeap {
CFRuntimeBase _base;
CFIndex _count; /* number of objects */
CFIndex _capacity; /* maximum number of objects */
CFBinaryHeapCallBacks _callbacks;
CFBinaryHeapCompareContext _context;
struct __CFBinaryHeapBucket *_buckets;
};
CF_INLINE CFIndex __CFBinaryHeapCount(CFBinaryHeapRef heap) {
return heap->_count;
}
CF_INLINE void __CFBinaryHeapSetCount(CFBinaryHeapRef heap, CFIndex v) {
/* for a CFBinaryHeap, _bucketsUsed == _count */
}
CF_INLINE CFIndex __CFBinaryHeapCapacity(CFBinaryHeapRef heap) {
return heap->_capacity;
}
CF_INLINE void __CFBinaryHeapSetCapacity(CFBinaryHeapRef heap, CFIndex v) {
/* for a CFBinaryHeap, _bucketsNum == _capacity */
}
CF_INLINE CFIndex __CFBinaryHeapNumBucketsUsed(CFBinaryHeapRef heap) {
return heap->_count;
}
CF_INLINE void __CFBinaryHeapSetNumBucketsUsed(CFBinaryHeapRef heap, CFIndex v) {
heap->_count = v;
}
CF_INLINE CFIndex __CFBinaryHeapNumBuckets(CFBinaryHeapRef heap) {
return heap->_capacity;
}
CF_INLINE void __CFBinaryHeapSetNumBuckets(CFBinaryHeapRef heap, CFIndex v) {
heap->_capacity = v;
}
enum { /* bits 1-0 */
kCFBinaryHeapMutable = 0x1, /* changeable and variable capacity */
};
/* Bits 4-5 are used by GC */
CF_INLINE bool isStrongMemory_Heap(CFTypeRef collection) {
return __CFBitfieldGetValue(((const CFRuntimeBase *)collection)->_cfinfo[CF_INFO_BITS], 4, 4) == 0;
}
CF_INLINE UInt32 __CFBinaryHeapMutableVariety(const void *cf) {
return __CFBitfieldGetValue(((const CFRuntimeBase *)cf)->_cfinfo[CF_INFO_BITS], 3, 2);
}
CF_INLINE void __CFBinaryHeapSetMutableVariety(void *cf, UInt32 v) {
__CFBitfieldSetValue(((CFRuntimeBase *)cf)->_cfinfo[CF_INFO_BITS], 3, 2, v);
}
CF_INLINE UInt32 __CFBinaryHeapMutableVarietyFromFlags(UInt32 flags) {
return __CFBitfieldGetValue(flags, 1, 0);
}
static Boolean __CFBinaryHeapEqual(CFTypeRef cf1, CFTypeRef cf2) {
CFBinaryHeapRef heap1 = (CFBinaryHeapRef)cf1;
CFBinaryHeapRef heap2 = (CFBinaryHeapRef)cf2;
CFComparisonResult (*compare)(const void *, const void *, void *);
CFIndex idx;
CFIndex cnt;
const void **list1, **list2, *buffer[256];
cnt = __CFBinaryHeapCount(heap1);
if (cnt != __CFBinaryHeapCount(heap2)) return false;
compare = heap1->_callbacks.compare;
if (compare != heap2->_callbacks.compare) return false;
if (0 == cnt) return true; /* after function comparison */
list1 = (cnt <= 128) ? (const void **)buffer : (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault, 2 * cnt * sizeof(void *), 0); // GC OK
if (__CFOASafe && list1 != buffer) __CFSetLastAllocationEventName(list1, "CFBinaryHeap (temp)");
list2 = (cnt <= 128) ? buffer + 128 : list1 + cnt;
CFBinaryHeapGetValues(heap1, list1);
CFBinaryHeapGetValues(heap2, list2);
for (idx = 0; idx < cnt; idx++) {
const void *val1 = list1[idx];
const void *val2 = list2[idx];
// CF: which context info should be passed in? both?
// CF: if the context infos are not equal, should the heaps not be equal?
if (val1 != val2) {
if (NULL == compare) return false;
if (!compare(val1, val2, heap1->_context.info)) return false;
}
}
if (list1 != buffer) CFAllocatorDeallocate(CFGetAllocator(heap1), list1); // GC OK
return true;
}
static CFHashCode __CFBinaryHeapHash(CFTypeRef cf) {
CFBinaryHeapRef heap = (CFBinaryHeapRef)cf;
return __CFBinaryHeapCount(heap);
}
static CFStringRef __CFBinaryHeapCopyDescription(CFTypeRef cf) {
CFBinaryHeapRef heap = (CFBinaryHeapRef)cf;
CFMutableStringRef result;
CFIndex idx;
CFIndex cnt;
const void **list, *buffer[256];
cnt = __CFBinaryHeapCount(heap);
result = CFStringCreateMutable(CFGetAllocator(heap), 0);
CFStringAppendFormat(result, NULL, CFSTR("<CFBinaryHeap %p [%p]>{count = %lu, capacity = %lu, objects = (\n"), cf, CFGetAllocator(heap), (unsigned long)cnt, (unsigned long)__CFBinaryHeapCapacity(heap));
list = (cnt <= 128) ? (const void **)buffer : (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault, cnt * sizeof(void *), 0); // GC OK
if (__CFOASafe && list != buffer) __CFSetLastAllocationEventName(list, "CFBinaryHeap (temp)");
CFBinaryHeapGetValues(heap, list);
for (idx = 0; idx < cnt; idx++) {
CFStringRef desc = NULL;
const void *item = list[idx];
if (NULL != heap->_callbacks.copyDescription) {
desc = heap->_callbacks.copyDescription(item);
}
if (NULL != desc) {
CFStringAppendFormat(result, NULL, CFSTR("\t%lu : %@\n"), (unsigned long)idx, desc);
CFRelease(desc);
} else {
CFStringAppendFormat(result, NULL, CFSTR("\t%lu : <%p>\n"), (unsigned long)idx, item);
}
}
CFStringAppend(result, CFSTR(")}"));
if (list != buffer) CFAllocatorDeallocate(CFGetAllocator(heap), list); // GC OK
return result;
}
static void __CFBinaryHeapDeallocate(CFTypeRef cf) {
CFBinaryHeapRef heap = (CFBinaryHeapRef)cf;
CFAllocatorRef allocator = CFGetAllocator(heap);
if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) {
if (heap->_callbacks.retain == NULL && heap->_callbacks.release == NULL)
return; // GC: keep heap intact during finalization.
}
// CF: should make the heap mutable here first, a la CFArrayDeallocate
CFBinaryHeapRemoveAllValues(heap);
// CF: does not release the context info
if (__CFBinaryHeapMutableVariety(heap) == kCFBinaryHeapMutable) {
_CFAllocatorDeallocateGC(allocator, heap->_buckets);
}
}
static CFTypeID __kCFBinaryHeapTypeID = _kCFRuntimeNotATypeID;
static const CFRuntimeClass __CFBinaryHeapClass = {
_kCFRuntimeScannedObject,
"CFBinaryHeap",
NULL, // init
NULL, // copy
__CFBinaryHeapDeallocate,
__CFBinaryHeapEqual,
__CFBinaryHeapHash,
NULL, //
__CFBinaryHeapCopyDescription
};
CFTypeID CFBinaryHeapGetTypeID(void) {
static dispatch_once_t initOnce;
dispatch_once(&initOnce, ^{ __kCFBinaryHeapTypeID = _CFRuntimeRegisterClass(&__CFBinaryHeapClass); });
return __kCFBinaryHeapTypeID;
}
static CFBinaryHeapRef __CFBinaryHeapInit(CFAllocatorRef allocator, UInt32 flags, CFIndex capacity, const void **values, CFIndex numValues, const CFBinaryHeapCallBacks *callBacks, const CFBinaryHeapCompareContext *compareContext) {
CFBinaryHeapRef memory;
CFIndex idx;
CFIndex size;
CFAssert2(0 <= capacity, __kCFLogAssertion, "%s(): capacity (%d) cannot be less than zero", __PRETTY_FUNCTION__, capacity);
CFAssert2(0 <= numValues, __kCFLogAssertion, "%s(): numValues (%d) cannot be less than zero", __PRETTY_FUNCTION__, numValues);
size = sizeof(struct __CFBinaryHeap) - sizeof(CFRuntimeBase);
if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) {
if (!callBacks || (callBacks->retain == NULL && callBacks->release == NULL)) {
__CFBitfieldSetValue(flags, 4, 4, 1); // setWeak
}
}
memory = (CFBinaryHeapRef)_CFRuntimeCreateInstance(allocator, CFBinaryHeapGetTypeID(), size, NULL);
if (NULL == memory) {
return NULL;
}
__CFBinaryHeapSetCapacity(memory, __CFBinaryHeapRoundUpCapacity(1));
__CFBinaryHeapSetNumBuckets(memory, __CFBinaryHeapNumBucketsForCapacity(__CFBinaryHeapRoundUpCapacity(1)));
void *buckets = _CFAllocatorAllocateGC(allocator, __CFBinaryHeapNumBuckets(memory) * sizeof(struct __CFBinaryHeapBucket), isStrongMemory_Heap(memory) ? __kCFAllocatorGCScannedMemory : 0);
__CFAssignWithWriteBarrier((void **)&memory->_buckets, buckets);
if (__CFOASafe) __CFSetLastAllocationEventName(memory->_buckets, "CFBinaryHeap (store)");
if (NULL == memory->_buckets) {
CFRelease(memory);
return NULL;
}
__CFBinaryHeapSetNumBucketsUsed(memory, 0);
__CFBinaryHeapSetCount(memory, 0);
if (NULL != callBacks) {
memory->_callbacks.retain = callBacks->retain;
memory->_callbacks.release = callBacks->release;
memory->_callbacks.copyDescription = callBacks->copyDescription;
memory->_callbacks.compare = callBacks->compare;
} else {
memory->_callbacks.retain = 0;
memory->_callbacks.release = 0;
memory->_callbacks.copyDescription = 0;
memory->_callbacks.compare = 0;
}
if (compareContext) memcpy(&memory->_context, compareContext, sizeof(CFBinaryHeapCompareContext));
// CF: retain info for proper operation
__CFBinaryHeapSetMutableVariety(memory, kCFBinaryHeapMutable);
for (idx = 0; idx < numValues; idx++) {
CFBinaryHeapAddValue(memory, values[idx]);
}
__CFBinaryHeapSetMutableVariety(memory, __CFBinaryHeapMutableVarietyFromFlags(flags));
return memory;
}
CFBinaryHeapRef CFBinaryHeapCreate(CFAllocatorRef allocator, CFIndex capacity, const CFBinaryHeapCallBacks *callBacks, const CFBinaryHeapCompareContext *compareContext) {
return __CFBinaryHeapInit(allocator, kCFBinaryHeapMutable, capacity, NULL, 0, callBacks, compareContext);
}
CFBinaryHeapRef CFBinaryHeapCreateCopy(CFAllocatorRef allocator, CFIndex capacity, CFBinaryHeapRef heap) {
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
return __CFBinaryHeapInit(allocator, kCFBinaryHeapMutable, capacity, (const void **)heap->_buckets, __CFBinaryHeapCount(heap), &(heap->_callbacks), &(heap->_context));
}
CFIndex CFBinaryHeapGetCount(CFBinaryHeapRef heap) {
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
return __CFBinaryHeapCount(heap);
}
CFIndex CFBinaryHeapGetCountOfValue(CFBinaryHeapRef heap, const void *value) {
CFComparisonResult (*compare)(const void *, const void *, void *);
CFIndex idx;
CFIndex cnt = 0, length;
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
compare = heap->_callbacks.compare;
length = __CFBinaryHeapCount(heap);
for (idx = 0; idx < length; idx++) {
const void *item = heap->_buckets[idx]._item;
if (value == item || (compare && kCFCompareEqualTo == compare(value, item, heap->_context.info))) {
cnt++;
}
}
return cnt;
}
Boolean CFBinaryHeapContainsValue(CFBinaryHeapRef heap, const void *value) {
CFComparisonResult (*compare)(const void *, const void *, void *);
CFIndex idx;
CFIndex length;
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
compare = heap->_callbacks.compare;
length = __CFBinaryHeapCount(heap);
for (idx = 0; idx < length; idx++) {
const void *item = heap->_buckets[idx]._item;
if (value == item || (compare && kCFCompareEqualTo == compare(value, item, heap->_context.info))) {
return true;
}
}
return false;
}
const void *CFBinaryHeapGetMinimum(CFBinaryHeapRef heap) {
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
CFAssert1(0 < __CFBinaryHeapCount(heap), __kCFLogAssertion, "%s(): binary heap is empty", __PRETTY_FUNCTION__);
return (0 < __CFBinaryHeapCount(heap)) ? heap->_buckets[0]._item : NULL;
}
Boolean CFBinaryHeapGetMinimumIfPresent(CFBinaryHeapRef heap, const void **value) {
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
if (0 == __CFBinaryHeapCount(heap)) return false;
if (NULL != value) __CFAssignWithWriteBarrier((void **)value, heap->_buckets[0]._item);
return true;
}
void CFBinaryHeapGetValues(CFBinaryHeapRef heap, const void **values) {
CFBinaryHeapRef heapCopy;
CFIndex idx;
CFIndex cnt;
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
CFAssert1(NULL != values, __kCFLogAssertion, "%s(): pointer to values may not be NULL", __PRETTY_FUNCTION__);
cnt = __CFBinaryHeapCount(heap);
if (0 == cnt) return;
heapCopy = CFBinaryHeapCreateCopy(CFGetAllocator(heap), cnt, heap);
idx = 0;
while (0 < __CFBinaryHeapCount(heapCopy)) {
const void *value = CFBinaryHeapGetMinimum(heapCopy);
CFBinaryHeapRemoveMinimumValue(heapCopy);
values[idx++] = value;
}
CFRelease(heapCopy);
}
void CFBinaryHeapApplyFunction(CFBinaryHeapRef heap, CFBinaryHeapApplierFunction applier, void *context) {
CFBinaryHeapRef heapCopy;
CFIndex cnt;
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
CFAssert1(NULL != applier, __kCFLogAssertion, "%s(): pointer to applier function may not be NULL", __PRETTY_FUNCTION__);
cnt = __CFBinaryHeapCount(heap);
if (0 == cnt) return;
heapCopy = CFBinaryHeapCreateCopy(CFGetAllocator(heap), cnt, heap);
while (0 < __CFBinaryHeapCount(heapCopy)) {
const void *value = CFBinaryHeapGetMinimum(heapCopy);
CFBinaryHeapRemoveMinimumValue(heapCopy);
applier(value, context);
}
CFRelease(heapCopy);
}
static void __CFBinaryHeapGrow(CFBinaryHeapRef heap, CFIndex numNewValues) {
CFIndex oldCount = __CFBinaryHeapCount(heap);
CFIndex capacity = __CFBinaryHeapRoundUpCapacity(oldCount + numNewValues);
CFAllocatorRef allocator = CFGetAllocator(heap);
__CFBinaryHeapSetCapacity(heap, capacity);
__CFBinaryHeapSetNumBuckets(heap, __CFBinaryHeapNumBucketsForCapacity(capacity));
void *buckets = _CFAllocatorReallocateGC(allocator, heap->_buckets, __CFBinaryHeapNumBuckets(heap) * sizeof(struct __CFBinaryHeapBucket), isStrongMemory_Heap(heap) ? __kCFAllocatorGCScannedMemory : 0);
__CFAssignWithWriteBarrier((void **)&heap->_buckets, buckets);
if (__CFOASafe) __CFSetLastAllocationEventName(heap->_buckets, "CFBinaryHeap (store)");
if (NULL == heap->_buckets) HALT;
}
void CFBinaryHeapAddValue(CFBinaryHeapRef heap, const void *value) {
CFIndex idx, pidx;
CFIndex cnt;
CFAllocatorRef allocator = CFGetAllocator(heap);
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
switch (__CFBinaryHeapMutableVariety(heap)) {
case kCFBinaryHeapMutable:
if (__CFBinaryHeapNumBucketsUsed(heap) == __CFBinaryHeapCapacity(heap))
__CFBinaryHeapGrow(heap, 1);
break;
}
cnt = __CFBinaryHeapCount(heap);
idx = cnt;
__CFBinaryHeapSetNumBucketsUsed(heap, cnt + 1);
__CFBinaryHeapSetCount(heap, cnt + 1);
CFComparisonResult (*compare)(const void *, const void *, void *) = heap->_callbacks.compare;
pidx = (idx - 1) >> 1;
while (0 < idx) {
void *item = heap->_buckets[pidx]._item;
if ((!compare && item <= value) || (compare && kCFCompareGreaterThan != compare(item, value, heap->_context.info))) break;
__CFAssignWithWriteBarrier((void **)&heap->_buckets[idx]._item, item);
idx = pidx;
pidx = (idx - 1) >> 1;
}
if (heap->_callbacks.retain) {
__CFAssignWithWriteBarrier((void **)&heap->_buckets[idx]._item, (void *)heap->_callbacks.retain(allocator, (void *)value));
} else {
__CFAssignWithWriteBarrier((void **)&heap->_buckets[idx]._item, (void *)value);
}
}
void CFBinaryHeapRemoveMinimumValue(CFBinaryHeapRef heap) {
void *val;
CFIndex idx, cidx;
CFIndex cnt;
CFAllocatorRef allocator;
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
cnt = __CFBinaryHeapCount(heap);
if (0 == cnt) return;
idx = 0;
__CFBinaryHeapSetNumBucketsUsed(heap, cnt - 1);
__CFBinaryHeapSetCount(heap, cnt - 1);
CFComparisonResult (*compare)(const void *, const void *, void *) = heap->_callbacks.compare;
allocator = CFGetAllocator(heap);
if (heap->_callbacks.release)
heap->_callbacks.release(allocator, heap->_buckets[idx]._item);
val = heap->_buckets[cnt - 1]._item;
cidx = (idx << 1) + 1;
while (cidx < __CFBinaryHeapCount(heap)) {
void *item = heap->_buckets[cidx]._item;
if (cidx + 1 < __CFBinaryHeapCount(heap)) {
void *item2 = heap->_buckets[cidx + 1]._item;
if ((!compare && item > item2) || (compare && kCFCompareGreaterThan == compare(item, item2, heap->_context.info))) {
cidx++;
item = item2;
}
}
if ((!compare && item > val) || (compare && kCFCompareGreaterThan == compare(item, val, heap->_context.info))) break;
__CFAssignWithWriteBarrier((void **)&heap->_buckets[idx]._item, item);
idx = cidx;
cidx = (idx << 1) + 1;
}
__CFAssignWithWriteBarrier((void **)&heap->_buckets[idx]._item, val);
}
void CFBinaryHeapRemoveAllValues(CFBinaryHeapRef heap) {
CFIndex idx;
CFIndex cnt;
__CFGenericValidateType(heap, CFBinaryHeapGetTypeID());
cnt = __CFBinaryHeapCount(heap);
if (heap->_callbacks.release)
for (idx = 0; idx < cnt; idx++)
heap->_callbacks.release(CFGetAllocator(heap), heap->_buckets[idx]._item);
__CFBinaryHeapSetNumBucketsUsed(heap, 0);
__CFBinaryHeapSetCount(heap, 0);
}