CFBinaryHeap.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. { CFBinaryHeap.h
  2. Copyright (c) 1998-2005, Apple, Inc. All rights reserved.
  3. }
  4. { Pascal Translation Updated: Peter N Lewis, <[email protected]>, September 2005 }
  5. {
  6. Modified for use with Free Pascal
  7. Version 200
  8. Please report any bugs to <[email protected]>
  9. }
  10. {$mode macpas}
  11. {$packenum 1}
  12. {$macro on}
  13. {$inline on}
  14. {$CALLING MWPASCAL}
  15. unit CFBinaryHeap;
  16. interface
  17. {$setc UNIVERSAL_INTERFACES_VERSION := $0342}
  18. {$setc GAP_INTERFACES_VERSION := $0200}
  19. {$ifc not defined USE_CFSTR_CONSTANT_MACROS}
  20. {$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
  21. {$endc}
  22. {$ifc defined CPUPOWERPC and defined CPUI386}
  23. {$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
  24. {$endc}
  25. {$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
  26. {$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
  27. {$endc}
  28. {$ifc not defined __ppc__ and defined CPUPOWERPC}
  29. {$setc __ppc__ := 1}
  30. {$elsec}
  31. {$setc __ppc__ := 0}
  32. {$endc}
  33. {$ifc not defined __i386__ and defined CPUI386}
  34. {$setc __i386__ := 1}
  35. {$elsec}
  36. {$setc __i386__ := 0}
  37. {$endc}
  38. {$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
  39. {$error Conflicting definitions for __ppc__ and __i386__}
  40. {$endc}
  41. {$ifc defined __ppc__ and __ppc__}
  42. {$setc TARGET_CPU_PPC := TRUE}
  43. {$setc TARGET_CPU_X86 := FALSE}
  44. {$elifc defined __i386__ and __i386__}
  45. {$setc TARGET_CPU_PPC := FALSE}
  46. {$setc TARGET_CPU_X86 := TRUE}
  47. {$elsec}
  48. {$error Neither __ppc__ nor __i386__ is defined.}
  49. {$endc}
  50. {$setc TARGET_CPU_PPC_64 := FALSE}
  51. {$ifc defined FPC_BIG_ENDIAN}
  52. {$setc TARGET_RT_BIG_ENDIAN := TRUE}
  53. {$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
  54. {$elifc defined FPC_LITTLE_ENDIAN}
  55. {$setc TARGET_RT_BIG_ENDIAN := FALSE}
  56. {$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
  57. {$elsec}
  58. {$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
  59. {$endc}
  60. {$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
  61. {$setc CALL_NOT_IN_CARBON := FALSE}
  62. {$setc OLDROUTINENAMES := FALSE}
  63. {$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
  64. {$setc OPAQUE_UPP_TYPES := TRUE}
  65. {$setc OTCARBONAPPLICATION := TRUE}
  66. {$setc OTKERNEL := FALSE}
  67. {$setc PM_USE_SESSION_APIS := TRUE}
  68. {$setc TARGET_API_MAC_CARBON := TRUE}
  69. {$setc TARGET_API_MAC_OS8 := FALSE}
  70. {$setc TARGET_API_MAC_OSX := TRUE}
  71. {$setc TARGET_CARBON := TRUE}
  72. {$setc TARGET_CPU_68K := FALSE}
  73. {$setc TARGET_CPU_MIPS := FALSE}
  74. {$setc TARGET_CPU_SPARC := FALSE}
  75. {$setc TARGET_OS_MAC := TRUE}
  76. {$setc TARGET_OS_UNIX := FALSE}
  77. {$setc TARGET_OS_WIN32 := FALSE}
  78. {$setc TARGET_RT_MAC_68881 := FALSE}
  79. {$setc TARGET_RT_MAC_CFM := FALSE}
  80. {$setc TARGET_RT_MAC_MACHO := TRUE}
  81. {$setc TYPED_FUNCTION_POINTERS := TRUE}
  82. {$setc TYPE_BOOL := FALSE}
  83. {$setc TYPE_EXTENDED := FALSE}
  84. {$setc TYPE_LONGLONG := TRUE}
  85. uses MacTypes,CFBase;
  86. {$ALIGN POWER}
  87. {!
  88. @header CFBinaryHeap
  89. CFBinaryHeap implements a container which stores values sorted using
  90. a binary search algorithm. CFBinaryHeaps can be useful as priority
  91. queues.
  92. }
  93. type
  94. CFBinaryHeapCompareContext = record
  95. version: CFIndex;
  96. info: UnivPtr;
  97. retain: function( info: {const} UnivPtr ): UnivPtr;
  98. release: procedure( info: {const} UnivPtr );
  99. copyDescription: function( info: {const} UnivPtr ): CFStringRef;
  100. end;
  101. CFBinaryHeapCompareContextPtr = ^CFBinaryHeapCompareContext;
  102. {!
  103. @typedef CFBinaryHeapCallBacks
  104. Structure containing the callbacks for values of a CFBinaryHeap.
  105. @field version The version number of the structure type being passed
  106. in as a parameter to the CFBinaryHeap creation functions.
  107. This structure is version 0.
  108. @field retain The callback used to add a retain for the binary heap
  109. on values as they are put into the binary heap.
  110. This callback returns the value to use as the value in the
  111. binary heap, which is usually the value parameter passed to
  112. this callback, but may be a different value if a different
  113. value should be added to the binary heap. The binary heap's
  114. allocator is passed as the first argument.
  115. @field release The callback used to remove a retain previously added
  116. for the binary heap from values as they are removed from
  117. the binary heap. The binary heap's allocator is passed as the
  118. first argument.
  119. @field copyDescription The callback used to create a descriptive
  120. string representation of each value in the binary heap. This
  121. is used by the CFCopyDescription() function.
  122. @field compare The callback used to compare values in the binary heap for
  123. equality in some operations.
  124. }
  125. type
  126. CFBinaryHeapCallBacks = record
  127. version: CFIndex;
  128. retain: function( allocator: CFAllocatorRef; info: {const} UnivPtr ): UnivPtr;
  129. release: procedure( allocator: CFAllocatorRef; info: {const} UnivPtr );
  130. copyDescription: function( info: {const} UnivPtr ): CFStringRef;
  131. compare: function( info1, info2: {const} UnivPtr; context: UnivPtr ): CFComparisonResult;
  132. end;
  133. CFBinaryHeapCallBacksPtr = ^CFBinaryHeapCallBacks;
  134. {!
  135. @constant kCFStringBinaryHeapCallBacks
  136. Predefined CFBinaryHeapCallBacks structure containing a set
  137. of callbacks appropriate for use when the values in a CFBinaryHeap
  138. are all CFString types.
  139. }
  140. var kCFStringBinaryHeapCallBacks: CFBinaryHeapCallBacks; external name '_kCFStringBinaryHeapCallBacks'; (* attribute const *)
  141. {!
  142. @typedef CFBinaryHeapApplierFunction
  143. Type of the callback function used by the apply functions of
  144. CFBinaryHeap.
  145. @param value The current value from the binary heap.
  146. @param context The user-defined context parameter given to the apply
  147. function.
  148. }
  149. type
  150. CFBinaryHeapApplierFunction = procedure( val: {const} UnivPtr; context: UnivPtr );
  151. {!
  152. @typedef CFBinaryHeapRef
  153. This is the type of a reference to CFBinaryHeaps.
  154. }
  155. type
  156. CFBinaryHeapRef = ^SInt32; { an opaque 32-bit type }
  157. {!
  158. @function CFBinaryHeapGetTypeID
  159. Returns the type identifier of all CFBinaryHeap instances.
  160. }
  161. function CFBinaryHeapGetTypeID: CFTypeID; external name '_CFBinaryHeapGetTypeID';
  162. {!
  163. @function CFBinaryHeapCreate
  164. Creates a new mutable or fixed-mutable binary heap with the given values.
  165. @param allocator The CFAllocator which should be used to allocate
  166. memory for the binary heap and its storage for values. This
  167. parameter may be NULL in which case the current default
  168. CFAllocator is used. If this reference is not a valid
  169. CFAllocator, the behavior is undefined.
  170. @param capacity The maximum number of values that can be contained
  171. by the CFBinaryHeap. The binary heap starts empty, and can grow to this
  172. number of values (and it can have less). If this parameter
  173. is 0, the binary heap's maximum capacity is unlimited (or rather,
  174. only limited by address space and available memory
  175. constraints). If this parameter is negative, the behavior is
  176. undefined.
  177. @param callBacks A pointer to a CFBinaryHeapCallBacks structure
  178. initialized with the callbacks for the binary heap to use on
  179. each value in the binary heap. A copy of the contents of the
  180. callbacks structure is made, so that a pointer to a structure
  181. on the stack can be passed in, or can be reused for multiple
  182. binary heap creations. If the version field of this callbacks
  183. structure is not one of the defined ones for CFBinaryHeap, the
  184. behavior is undefined. The retain field may be NULL, in which
  185. case the CFBinaryHeap will do nothing to add a retain to values
  186. as they are put into the binary heap. The release field may be
  187. NULL, in which case the CFBinaryHeap will do nothing to remove
  188. the binary heap's retain (if any) on the values when the
  189. heap is destroyed or a key-value pair is removed. If the
  190. copyDescription field is NULL, the binary heap will create a
  191. simple description for a value. If the equal field is NULL, the
  192. binary heap will use pointer equality to test for equality of
  193. values. This callbacks parameter itself may be NULL, which is
  194. treated as if a valid structure of version 0 with all fields
  195. NULL had been passed in. Otherwise,
  196. if any of the fields are not valid pointers to functions
  197. of the correct type, or this parameter is not a valid
  198. pointer to a CFBinaryHeapCallBacks callbacks structure,
  199. the behavior is undefined. If any of the values put into the
  200. binary heap is not one understood by one of the callback functions
  201. the behavior when that callback function is used is undefined.
  202. @param compareContext A pointer to a CFBinaryHeapCompareContext structure.
  203. @result A reference to the new CFBinaryHeap.
  204. }
  205. function CFBinaryHeapCreate( allocator: CFAllocatorRef; capacity: CFIndex; callBacks: CFBinaryHeapCallBacksPtr; const (*var*) compareContext: CFBinaryHeapCompareContext ): CFBinaryHeapRef; external name '_CFBinaryHeapCreate';
  206. {!
  207. @function CFBinaryHeapCreateCopy
  208. Creates a new mutable or fixed-mutable binary heap with the values from the given binary heap.
  209. @param allocator The CFAllocator which should be used to allocate
  210. memory for the binary heap and its storage for values. This
  211. parameter may be NULL in which case the current default
  212. CFAllocator is used. If this reference is not a valid
  213. CFAllocator, the behavior is undefined.
  214. @param capacity The maximum number of values that can be contained
  215. by the CFBinaryHeap. The binary heap starts empty, and can grow to this
  216. number of values (and it can have less). If this parameter
  217. is 0, the binary heap's maximum capacity is unlimited (or rather,
  218. only limited by address space and available memory
  219. constraints). If this parameter is negative, or less than the number of
  220. values in the given binary heap, the behavior is undefined.
  221. @param heap The binary heap which is to be copied. The values from the
  222. binary heap are copied as pointers into the new binary heap (that is,
  223. the values themselves are copied, not that which the values
  224. point to, if anything). However, the values are also
  225. retained by the new binary heap. The count of the new binary will
  226. be the same as the given binary heap. The new binary heap uses the same
  227. callbacks as the binary heap to be copied. If this parameter is
  228. not a valid CFBinaryHeap, the behavior is undefined.
  229. @result A reference to the new mutable or fixed-mutable binary heap.
  230. }
  231. function CFBinaryHeapCreateCopy( allocator: CFAllocatorRef; capacity: CFIndex; heap: CFBinaryHeapRef ): CFBinaryHeapRef; external name '_CFBinaryHeapCreateCopy';
  232. {!
  233. @function CFBinaryHeapGetCount
  234. Returns the number of values currently in the binary heap.
  235. @param heap The binary heap to be queried. If this parameter is not a valid
  236. CFBinaryHeap, the behavior is undefined.
  237. @result The number of values in the binary heap.
  238. }
  239. function CFBinaryHeapGetCount( heap: CFBinaryHeapRef ): CFIndex; external name '_CFBinaryHeapGetCount';
  240. {!
  241. @function CFBinaryHeapGetCountOfValue
  242. Counts the number of times the given value occurs in the binary heap.
  243. @param heap The binary heap to be searched. If this parameter is not a
  244. valid CFBinaryHeap, the behavior is undefined.
  245. @param value The value for which to find matches in the binary heap. The
  246. compare() callback provided when the binary heap was created is
  247. used to compare. If the compare() callback was NULL, pointer
  248. equality (in C, ==) is used. If value, or any of the values
  249. in the binary heap, are not understood by the compare() callback,
  250. the behavior is undefined.
  251. @result The number of times the given value occurs in the binary heap.
  252. }
  253. function CFBinaryHeapGetCountOfValue( heap: CFBinaryHeapRef; value: {const} UnivPtr ): CFIndex; external name '_CFBinaryHeapGetCountOfValue';
  254. {!
  255. @function CFBinaryHeapContainsValue
  256. Reports whether or not the value is in the binary heap.
  257. @param heap The binary heap to be searched. If this parameter is not a
  258. valid CFBinaryHeap, the behavior is undefined.
  259. @param value The value for which to find matches in the binary heap. The
  260. compare() callback provided when the binary heap was created is
  261. used to compare. If the compare() callback was NULL, pointer
  262. equality (in C, ==) is used. If value, or any of the values
  263. in the binary heap, are not understood by the compare() callback,
  264. the behavior is undefined.
  265. @result true, if the value is in the specified binary heap, otherwise false.
  266. }
  267. function CFBinaryHeapContainsValue( heap: CFBinaryHeapRef; value: {const} UnivPtr ): Boolean; external name '_CFBinaryHeapContainsValue';
  268. {!
  269. @function CFBinaryHeapGetMinimum
  270. Returns the minimum value is in the binary heap. If the heap contains several equal
  271. minimum values, any one may be returned.
  272. @param heap The binary heap to be searched. If this parameter is not a
  273. valid CFBinaryHeap, the behavior is undefined.
  274. @result A reference to the minimum value in the binary heap, or NULL if the
  275. binary heap contains no values.
  276. }
  277. function CFBinaryHeapGetMinimum( heap: CFBinaryHeapRef ): UnivPtr; external name '_CFBinaryHeapGetMinimum';
  278. {!
  279. @function CFBinaryHeapGetMinimumIfPresent
  280. Returns the minimum value is in the binary heap, if present. If the heap contains several equal
  281. minimum values, any one may be returned.
  282. @param heap The binary heap to be searched. If this parameter is not a
  283. valid CFBinaryHeap, the behavior is undefined.
  284. @param value A C pointer to pointer-sized storage to be filled with the minimum value in
  285. the binary heap. If this value is not a valid C pointer to a pointer-sized block
  286. of storage, the result is undefined. If the result of the function is false, the value
  287. stored at this address is undefined.
  288. @result true, if a minimum value was found in the specified binary heap, otherwise false.
  289. }
  290. function CFBinaryHeapGetMinimumIfPresent( heap: CFBinaryHeapRef; {const} value: {variable-size-array} UnivPtrPtr ): Boolean; external name '_CFBinaryHeapGetMinimumIfPresent';
  291. {!
  292. @function CFBinaryHeapGetValues
  293. Fills the buffer with values from the binary heap.
  294. @param heap The binary heap to be queried. If this parameter is not a
  295. valid CFBinaryHeap, the behavior is undefined.
  296. @param values A C array of pointer-sized values to be filled with
  297. values from the binary heap. The values in the C array are ordered
  298. from least to greatest. If this parameter is not a valid pointer to a
  299. C array of at least CFBinaryHeapGetCount() pointers, the behavior is undefined.
  300. }
  301. type
  302. CFBinaryHeapValues = array[0..($7F000000 div SizeOf(Ptr))] of Ptr;
  303. CFBinaryHeapValuesPtr = ^CFBinaryHeapValues;
  304. procedure CFBinaryHeapGetValues( heap: CFBinaryHeapRef; {const} values: {variable-size-array} CFBinaryHeapValuesPtr ); external name '_CFBinaryHeapGetValues';
  305. {!
  306. @function CFBinaryHeapApplyFunction
  307. Calls a function once for each value in the binary heap.
  308. @param heap The binary heap to be operated upon. If this parameter is not a
  309. valid CFBinaryHeap, the behavior is undefined.
  310. @param applier The callback function to call once for each value in
  311. the given binary heap. If this parameter is not a
  312. pointer to a function of the correct prototype, the behavior
  313. is undefined. If there are values in the binary heap which the
  314. applier function does not expect or cannot properly apply
  315. to, the behavior is undefined.
  316. @param context A pointer-sized user-defined value, which is passed
  317. as the second parameter to the applier function, but is
  318. otherwise unused by this function. If the context is not
  319. what is expected by the applier function, the behavior is
  320. undefined.
  321. }
  322. procedure CFBinaryHeapApplyFunction( heap: CFBinaryHeapRef; applier: CFBinaryHeapApplierFunction; context: UnivPtr ); external name '_CFBinaryHeapApplyFunction';
  323. {!
  324. @function CFBinaryHeapAddValue
  325. Adds the value to the binary heap.
  326. @param heap The binary heap to which the value is to be added. If this parameter is not a
  327. valid mutable CFBinaryHeap, the behavior is undefined.
  328. If the binary heap is a fixed-capacity binary heap and it
  329. is full before this operation, the behavior is undefined.
  330. @param value The value to add to the binary heap. The value is retained by
  331. the binary heap using the retain callback provided when the binary heap
  332. was created. If the value is not of the sort expected by the
  333. retain callback, the behavior is undefined.
  334. }
  335. procedure CFBinaryHeapAddValue( heap: CFBinaryHeapRef; value: {const} UnivPtr ); external name '_CFBinaryHeapAddValue';
  336. {!
  337. @function CFBinaryHeapRemoveMinimumValue
  338. Removes the minimum value from the binary heap.
  339. @param heap The binary heap from which the minimum value is to be removed. If this
  340. parameter is not a valid mutable CFBinaryHeap, the behavior is undefined.
  341. }
  342. procedure CFBinaryHeapRemoveMinimumValue( heap: CFBinaryHeapRef ); external name '_CFBinaryHeapRemoveMinimumValue';
  343. {!
  344. @function CFBinaryHeapRemoveAllValues
  345. Removes all the values from the binary heap, making it empty.
  346. @param heap The binary heap from which all of the values are to be
  347. removed. If this parameter is not a valid mutable CFBinaryHeap,
  348. the behavior is undefined.
  349. }
  350. procedure CFBinaryHeapRemoveAllValues( heap: CFBinaryHeapRef ); external name '_CFBinaryHeapRemoveAllValues';
  351. end.