spl_heap.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Etienne Kneuss <colder@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "zend_exceptions.h"
  23. #include "php_spl.h"
  24. #include "spl_functions.h"
  25. #include "spl_engine.h"
  26. #include "spl_iterators.h"
  27. #include "spl_heap.h"
  28. #include "spl_exceptions.h"
  29. #define PTR_HEAP_BLOCK_SIZE 64
  30. #define SPL_HEAP_CORRUPTED 0x00000001
  31. #define SPL_PQUEUE_EXTR_MASK 0x00000003
  32. #define SPL_PQUEUE_EXTR_BOTH 0x00000003
  33. #define SPL_PQUEUE_EXTR_DATA 0x00000001
  34. #define SPL_PQUEUE_EXTR_PRIORITY 0x00000002
  35. zend_object_handlers spl_handler_SplHeap;
  36. zend_object_handlers spl_handler_SplPriorityQueue;
  37. PHPAPI zend_class_entry *spl_ce_SplHeap;
  38. PHPAPI zend_class_entry *spl_ce_SplMaxHeap;
  39. PHPAPI zend_class_entry *spl_ce_SplMinHeap;
  40. PHPAPI zend_class_entry *spl_ce_SplPriorityQueue;
  41. typedef void (*spl_ptr_heap_dtor_func)(zval *);
  42. typedef void (*spl_ptr_heap_ctor_func)(zval *);
  43. typedef int (*spl_ptr_heap_cmp_func)(zval *, zval *, zval *);
  44. typedef struct _spl_ptr_heap {
  45. zval *elements;
  46. spl_ptr_heap_ctor_func ctor;
  47. spl_ptr_heap_dtor_func dtor;
  48. spl_ptr_heap_cmp_func cmp;
  49. int count;
  50. int max_size;
  51. int flags;
  52. } spl_ptr_heap;
  53. typedef struct _spl_heap_object spl_heap_object;
  54. typedef struct _spl_heap_it spl_heap_it;
  55. struct _spl_heap_object {
  56. spl_ptr_heap *heap;
  57. int flags;
  58. zend_class_entry *ce_get_iterator;
  59. zend_function *fptr_cmp;
  60. zend_function *fptr_count;
  61. zend_object std;
  62. };
  63. /* define an overloaded iterator structure */
  64. struct _spl_heap_it {
  65. zend_user_iterator intern;
  66. int flags;
  67. };
  68. static inline spl_heap_object *spl_heap_from_obj(zend_object *obj) /* {{{ */ {
  69. return (spl_heap_object*)((char*)(obj) - XtOffsetOf(spl_heap_object, std));
  70. }
  71. /* }}} */
  72. #define Z_SPLHEAP_P(zv) spl_heap_from_obj(Z_OBJ_P((zv)))
  73. static void spl_ptr_heap_zval_dtor(zval *elem) { /* {{{ */
  74. if (!Z_ISUNDEF_P(elem)) {
  75. zval_ptr_dtor(elem);
  76. }
  77. }
  78. /* }}} */
  79. static void spl_ptr_heap_zval_ctor(zval *elem) { /* {{{ */
  80. Z_TRY_ADDREF_P(elem);
  81. }
  82. /* }}} */
  83. static int spl_ptr_heap_cmp_cb_helper(zval *object, spl_heap_object *heap_object, zval *a, zval *b, zend_long *result) { /* {{{ */
  84. zval zresult;
  85. zend_call_method_with_2_params(object, heap_object->std.ce, &heap_object->fptr_cmp, "compare", &zresult, a, b);
  86. if (EG(exception)) {
  87. return FAILURE;
  88. }
  89. *result = zval_get_long(&zresult);
  90. zval_ptr_dtor(&zresult);
  91. return SUCCESS;
  92. }
  93. /* }}} */
  94. static zval *spl_pqueue_extract_helper(zval *value, int flags) /* {{{ */
  95. {
  96. if ((flags & SPL_PQUEUE_EXTR_BOTH) == SPL_PQUEUE_EXTR_BOTH) {
  97. return value;
  98. } else if ((flags & SPL_PQUEUE_EXTR_BOTH) > 0) {
  99. if ((flags & SPL_PQUEUE_EXTR_DATA) == SPL_PQUEUE_EXTR_DATA) {
  100. zval *data;
  101. if ((data = zend_hash_str_find(Z_ARRVAL_P(value), "data", sizeof("data") - 1)) != NULL) {
  102. return data;
  103. }
  104. } else {
  105. zval *priority;
  106. if ((priority = zend_hash_str_find(Z_ARRVAL_P(value), "priority", sizeof("priority") - 1)) != NULL) {
  107. return priority;
  108. }
  109. }
  110. }
  111. return NULL;
  112. }
  113. /* }}} */
  114. static int spl_ptr_heap_zval_max_cmp(zval *a, zval *b, zval *object) { /* {{{ */
  115. zval result;
  116. if (EG(exception)) {
  117. return 0;
  118. }
  119. if (object) {
  120. spl_heap_object *heap_object = Z_SPLHEAP_P(object);
  121. if (heap_object->fptr_cmp) {
  122. zend_long lval = 0;
  123. if (spl_ptr_heap_cmp_cb_helper(object, heap_object, a, b, &lval) == FAILURE) {
  124. /* exception or call failure */
  125. return 0;
  126. }
  127. return lval > 0 ? 1 : (lval < 0 ? -1 : 0);
  128. }
  129. }
  130. compare_function(&result, a, b);
  131. return (int)Z_LVAL(result);
  132. }
  133. /* }}} */
  134. static int spl_ptr_heap_zval_min_cmp(zval *a, zval *b, zval *object) { /* {{{ */
  135. zval result;
  136. if (EG(exception)) {
  137. return 0;
  138. }
  139. if (object) {
  140. spl_heap_object *heap_object = Z_SPLHEAP_P(object);
  141. if (heap_object->fptr_cmp) {
  142. zend_long lval = 0;
  143. if (spl_ptr_heap_cmp_cb_helper(object, heap_object, a, b, &lval) == FAILURE) {
  144. /* exception or call failure */
  145. return 0;
  146. }
  147. return lval > 0 ? 1 : (lval < 0 ? -1 : 0);
  148. }
  149. }
  150. compare_function(&result, b, a);
  151. return (int)Z_LVAL(result);
  152. }
  153. /* }}} */
  154. static int spl_ptr_pqueue_zval_cmp(zval *a, zval *b, zval *object) { /* {{{ */
  155. zval result;
  156. zval *a_priority_p = spl_pqueue_extract_helper(a, SPL_PQUEUE_EXTR_PRIORITY);
  157. zval *b_priority_p = spl_pqueue_extract_helper(b, SPL_PQUEUE_EXTR_PRIORITY);
  158. if ((!a_priority_p) || (!b_priority_p)) {
  159. zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node");
  160. return 0;
  161. }
  162. if (EG(exception)) {
  163. return 0;
  164. }
  165. if (object) {
  166. spl_heap_object *heap_object = Z_SPLHEAP_P(object);
  167. if (heap_object->fptr_cmp) {
  168. zend_long lval = 0;
  169. if (spl_ptr_heap_cmp_cb_helper((zval *)object, heap_object, a_priority_p, b_priority_p, &lval) == FAILURE) {
  170. /* exception or call failure */
  171. return 0;
  172. }
  173. return lval > 0 ? 1 : (lval < 0 ? -1 : 0);
  174. }
  175. }
  176. compare_function(&result, a_priority_p, b_priority_p);
  177. return (int)Z_LVAL(result);
  178. }
  179. /* }}} */
  180. static spl_ptr_heap *spl_ptr_heap_init(spl_ptr_heap_cmp_func cmp, spl_ptr_heap_ctor_func ctor, spl_ptr_heap_dtor_func dtor) /* {{{ */
  181. {
  182. spl_ptr_heap *heap = emalloc(sizeof(spl_ptr_heap));
  183. heap->dtor = dtor;
  184. heap->ctor = ctor;
  185. heap->cmp = cmp;
  186. heap->elements = ecalloc(PTR_HEAP_BLOCK_SIZE, sizeof(zval));
  187. heap->max_size = PTR_HEAP_BLOCK_SIZE;
  188. heap->count = 0;
  189. heap->flags = 0;
  190. return heap;
  191. }
  192. /* }}} */
  193. static void spl_ptr_heap_insert(spl_ptr_heap *heap, zval *elem, void *cmp_userdata) { /* {{{ */
  194. int i;
  195. if (heap->count+1 > heap->max_size) {
  196. /* we need to allocate more memory */
  197. heap->elements = erealloc(heap->elements, heap->max_size * 2 * sizeof(zval));
  198. memset(heap->elements + heap->max_size, 0, heap->max_size * sizeof(zval));
  199. heap->max_size *= 2;
  200. }
  201. /* sifting up */
  202. for (i = heap->count; i > 0 && heap->cmp(&heap->elements[(i-1)/2], elem, cmp_userdata) < 0; i = (i-1)/2) {
  203. heap->elements[i] = heap->elements[(i-1)/2];
  204. }
  205. heap->count++;
  206. if (EG(exception)) {
  207. /* exception thrown during comparison */
  208. heap->flags |= SPL_HEAP_CORRUPTED;
  209. }
  210. ZVAL_COPY_VALUE(&heap->elements[i], elem);
  211. }
  212. /* }}} */
  213. static zval *spl_ptr_heap_top(spl_ptr_heap *heap) { /* {{{ */
  214. if (heap->count == 0) {
  215. return NULL;
  216. }
  217. return Z_ISUNDEF(heap->elements[0])? NULL : &heap->elements[0];
  218. }
  219. /* }}} */
  220. static void spl_ptr_heap_delete_top(spl_ptr_heap *heap, zval *elem, void *cmp_userdata) { /* {{{ */
  221. int i, j;
  222. const int limit = (heap->count-1)/2;
  223. zval *bottom;
  224. if (heap->count == 0) {
  225. ZVAL_UNDEF(elem);
  226. return;
  227. }
  228. ZVAL_COPY_VALUE(elem, &heap->elements[0]);
  229. bottom = &heap->elements[--heap->count];
  230. for (i = 0; i < limit; i = j) {
  231. /* Find smaller child */
  232. j = i * 2 + 1;
  233. if(j != heap->count && heap->cmp(&heap->elements[j+1], &heap->elements[j], cmp_userdata) > 0) {
  234. j++; /* next child is bigger */
  235. }
  236. /* swap elements between two levels */
  237. if(heap->cmp(bottom, &heap->elements[j], cmp_userdata) < 0) {
  238. heap->elements[i] = heap->elements[j];
  239. } else {
  240. break;
  241. }
  242. }
  243. if (EG(exception)) {
  244. /* exception thrown during comparison */
  245. heap->flags |= SPL_HEAP_CORRUPTED;
  246. }
  247. ZVAL_COPY_VALUE(&heap->elements[i], bottom);
  248. }
  249. /* }}} */
  250. static spl_ptr_heap *spl_ptr_heap_clone(spl_ptr_heap *from) { /* {{{ */
  251. int i;
  252. spl_ptr_heap *heap = emalloc(sizeof(spl_ptr_heap));
  253. heap->dtor = from->dtor;
  254. heap->ctor = from->ctor;
  255. heap->cmp = from->cmp;
  256. heap->max_size = from->max_size;
  257. heap->count = from->count;
  258. heap->flags = from->flags;
  259. heap->elements = safe_emalloc(sizeof(zval), from->max_size, 0);
  260. memcpy(heap->elements, from->elements, sizeof(zval)*from->max_size);
  261. for (i=0; i < heap->count; ++i) {
  262. heap->ctor(&heap->elements[i]);
  263. }
  264. return heap;
  265. }
  266. /* }}} */
  267. static void spl_ptr_heap_destroy(spl_ptr_heap *heap) { /* {{{ */
  268. int i;
  269. for (i=0; i < heap->count; ++i) {
  270. heap->dtor(&heap->elements[i]);
  271. }
  272. efree(heap->elements);
  273. efree(heap);
  274. }
  275. /* }}} */
  276. static int spl_ptr_heap_count(spl_ptr_heap *heap) { /* {{{ */
  277. return heap->count;
  278. }
  279. /* }}} */
  280. zend_object_iterator *spl_heap_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
  281. static void spl_heap_object_free_storage(zend_object *object) /* {{{ */
  282. {
  283. spl_heap_object *intern = spl_heap_from_obj(object);
  284. zend_object_std_dtor(&intern->std);
  285. spl_ptr_heap_destroy(intern->heap);
  286. }
  287. /* }}} */
  288. static zend_object *spl_heap_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig) /* {{{ */
  289. {
  290. spl_heap_object *intern;
  291. zend_class_entry *parent = class_type;
  292. int inherited = 0;
  293. intern = zend_object_alloc(sizeof(spl_heap_object), parent);
  294. zend_object_std_init(&intern->std, class_type);
  295. object_properties_init(&intern->std, class_type);
  296. intern->flags = 0;
  297. intern->fptr_cmp = NULL;
  298. if (orig) {
  299. spl_heap_object *other = Z_SPLHEAP_P(orig);
  300. intern->ce_get_iterator = other->ce_get_iterator;
  301. if (clone_orig) {
  302. intern->heap = spl_ptr_heap_clone(other->heap);
  303. } else {
  304. intern->heap = other->heap;
  305. }
  306. intern->flags = other->flags;
  307. } else {
  308. intern->heap = spl_ptr_heap_init(spl_ptr_heap_zval_max_cmp, spl_ptr_heap_zval_ctor, spl_ptr_heap_zval_dtor);
  309. }
  310. intern->std.handlers = &spl_handler_SplHeap;
  311. while (parent) {
  312. if (parent == spl_ce_SplPriorityQueue) {
  313. intern->heap->cmp = spl_ptr_pqueue_zval_cmp;
  314. intern->flags = SPL_PQUEUE_EXTR_DATA;
  315. intern->std.handlers = &spl_handler_SplPriorityQueue;
  316. break;
  317. }
  318. if (parent == spl_ce_SplMinHeap) {
  319. intern->heap->cmp = spl_ptr_heap_zval_min_cmp;
  320. break;
  321. }
  322. if (parent == spl_ce_SplMaxHeap) {
  323. intern->heap->cmp = spl_ptr_heap_zval_max_cmp;
  324. break;
  325. }
  326. if (parent == spl_ce_SplHeap) {
  327. break;
  328. }
  329. parent = parent->parent;
  330. inherited = 1;
  331. }
  332. if (!parent) { /* this must never happen */
  333. php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplHeap");
  334. }
  335. if (inherited) {
  336. intern->fptr_cmp = zend_hash_str_find_ptr(&class_type->function_table, "compare", sizeof("compare") - 1);
  337. if (intern->fptr_cmp->common.scope == parent) {
  338. intern->fptr_cmp = NULL;
  339. }
  340. intern->fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
  341. if (intern->fptr_count->common.scope == parent) {
  342. intern->fptr_count = NULL;
  343. }
  344. }
  345. return &intern->std;
  346. }
  347. /* }}} */
  348. static zend_object *spl_heap_object_new(zend_class_entry *class_type) /* {{{ */
  349. {
  350. return spl_heap_object_new_ex(class_type, NULL, 0);
  351. }
  352. /* }}} */
  353. static zend_object *spl_heap_object_clone(zval *zobject) /* {{{ */
  354. {
  355. zend_object *old_object;
  356. zend_object *new_object;
  357. old_object = Z_OBJ_P(zobject);
  358. new_object = spl_heap_object_new_ex(old_object->ce, zobject, 1);
  359. zend_objects_clone_members(new_object, old_object);
  360. return new_object;
  361. }
  362. /* }}} */
  363. static int spl_heap_object_count_elements(zval *object, zend_long *count) /* {{{ */
  364. {
  365. spl_heap_object *intern = Z_SPLHEAP_P(object);
  366. if (intern->fptr_count) {
  367. zval rv;
  368. zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
  369. if (!Z_ISUNDEF(rv)) {
  370. *count = zval_get_long(&rv);
  371. zval_ptr_dtor(&rv);
  372. return SUCCESS;
  373. }
  374. *count = 0;
  375. return FAILURE;
  376. }
  377. *count = spl_ptr_heap_count(intern->heap);
  378. return SUCCESS;
  379. }
  380. /* }}} */
  381. static HashTable* spl_heap_object_get_debug_info_helper(zend_class_entry *ce, zval *obj, int *is_temp) { /* {{{ */
  382. spl_heap_object *intern = Z_SPLHEAP_P(obj);
  383. zval tmp, heap_array;
  384. zend_string *pnstr;
  385. HashTable *debug_info;
  386. int i;
  387. *is_temp = 1;
  388. if (!intern->std.properties) {
  389. rebuild_object_properties(&intern->std);
  390. }
  391. debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 1);
  392. zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
  393. pnstr = spl_gen_private_prop_name(ce, "flags", sizeof("flags")-1);
  394. ZVAL_LONG(&tmp, intern->flags);
  395. zend_hash_update(debug_info, pnstr, &tmp);
  396. zend_string_release_ex(pnstr, 0);
  397. pnstr = spl_gen_private_prop_name(ce, "isCorrupted", sizeof("isCorrupted")-1);
  398. ZVAL_BOOL(&tmp, intern->heap->flags&SPL_HEAP_CORRUPTED);
  399. zend_hash_update(debug_info, pnstr, &tmp);
  400. zend_string_release_ex(pnstr, 0);
  401. array_init(&heap_array);
  402. for (i = 0; i < intern->heap->count; ++i) {
  403. add_index_zval(&heap_array, i, &intern->heap->elements[i]);
  404. if (Z_REFCOUNTED(intern->heap->elements[i])) {
  405. Z_ADDREF(intern->heap->elements[i]);
  406. }
  407. }
  408. pnstr = spl_gen_private_prop_name(ce, "heap", sizeof("heap")-1);
  409. zend_hash_update(debug_info, pnstr, &heap_array);
  410. zend_string_release_ex(pnstr, 0);
  411. return debug_info;
  412. }
  413. /* }}} */
  414. static HashTable *spl_heap_object_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
  415. {
  416. spl_heap_object *intern = Z_SPLHEAP_P(obj);
  417. *gc_data = intern->heap->elements;
  418. *gc_data_count = intern->heap->count;
  419. return zend_std_get_properties(obj);
  420. }
  421. /* }}} */
  422. static HashTable* spl_heap_object_get_debug_info(zval *obj, int *is_temp) /* {{{ */
  423. {
  424. return spl_heap_object_get_debug_info_helper(spl_ce_SplHeap, obj, is_temp);
  425. }
  426. /* }}} */
  427. static HashTable* spl_pqueue_object_get_debug_info(zval *obj, int *is_temp) /* {{{ */
  428. {
  429. return spl_heap_object_get_debug_info_helper(spl_ce_SplPriorityQueue, obj, is_temp);
  430. }
  431. /* }}} */
  432. /* {{{ proto int SplHeap::count()
  433. Return the number of elements in the heap. */
  434. SPL_METHOD(SplHeap, count)
  435. {
  436. zend_long count;
  437. spl_heap_object *intern = Z_SPLHEAP_P(getThis());
  438. if (zend_parse_parameters_none() == FAILURE) {
  439. return;
  440. }
  441. count = spl_ptr_heap_count(intern->heap);
  442. RETURN_LONG(count);
  443. }
  444. /* }}} */
  445. /* {{{ proto int SplHeap::isEmpty()
  446. Return true if the heap is empty. */
  447. SPL_METHOD(SplHeap, isEmpty)
  448. {
  449. spl_heap_object *intern = Z_SPLHEAP_P(getThis());
  450. if (zend_parse_parameters_none() == FAILURE) {
  451. return;
  452. }
  453. RETURN_BOOL(spl_ptr_heap_count(intern->heap) == 0);
  454. }
  455. /* }}} */
  456. /* {{{ proto bool SplHeap::insert(mixed value)
  457. Push $value on the heap */
  458. SPL_METHOD(SplHeap, insert)
  459. {
  460. zval *value;
  461. spl_heap_object *intern;
  462. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
  463. return;
  464. }
  465. intern = Z_SPLHEAP_P(getThis());
  466. if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
  467. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  468. return;
  469. }
  470. Z_TRY_ADDREF_P(value);
  471. spl_ptr_heap_insert(intern->heap, value, getThis());
  472. RETURN_TRUE;
  473. }
  474. /* }}} */
  475. /* {{{ proto mixed SplHeap::extract()
  476. extract the element out of the top of the heap */
  477. SPL_METHOD(SplHeap, extract)
  478. {
  479. spl_heap_object *intern;
  480. if (zend_parse_parameters_none() == FAILURE) {
  481. return;
  482. }
  483. intern = Z_SPLHEAP_P(getThis());
  484. if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
  485. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  486. return;
  487. }
  488. spl_ptr_heap_delete_top(intern->heap, return_value, getThis());
  489. if (Z_ISUNDEF_P(return_value)) {
  490. zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0);
  491. return;
  492. }
  493. }
  494. /* }}} */
  495. /* {{{ proto bool SplPriorityQueue::insert(mixed value, mixed priority)
  496. Push $value with the priority $priodiry on the priorityqueue */
  497. SPL_METHOD(SplPriorityQueue, insert)
  498. {
  499. zval *data, *priority, elem;
  500. spl_heap_object *intern;
  501. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &data, &priority) == FAILURE) {
  502. return;
  503. }
  504. intern = Z_SPLHEAP_P(getThis());
  505. if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
  506. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  507. return;
  508. }
  509. Z_TRY_ADDREF_P(data);
  510. Z_TRY_ADDREF_P(priority);
  511. array_init(&elem);
  512. add_assoc_zval_ex(&elem, "data", sizeof("data") - 1, data);
  513. add_assoc_zval_ex(&elem, "priority", sizeof("priority") - 1, priority);
  514. spl_ptr_heap_insert(intern->heap, &elem, getThis());
  515. RETURN_TRUE;
  516. }
  517. /* }}} */
  518. /* {{{ proto mixed SplPriorityQueue::extract()
  519. extract the element out of the top of the priority queue */
  520. SPL_METHOD(SplPriorityQueue, extract)
  521. {
  522. zval value, *value_out;
  523. spl_heap_object *intern;
  524. if (zend_parse_parameters_none() == FAILURE) {
  525. return;
  526. }
  527. intern = Z_SPLHEAP_P(getThis());
  528. if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
  529. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  530. return;
  531. }
  532. spl_ptr_heap_delete_top(intern->heap, &value, getThis());
  533. if (Z_ISUNDEF(value)) {
  534. zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0);
  535. return;
  536. }
  537. value_out = spl_pqueue_extract_helper(&value, intern->flags);
  538. if (!value_out) {
  539. zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node");
  540. zval_ptr_dtor(&value);
  541. return;
  542. }
  543. ZVAL_COPY_DEREF(return_value, value_out);
  544. zval_ptr_dtor(&value);
  545. }
  546. /* }}} */
  547. /* {{{ proto mixed SplPriorityQueue::top()
  548. Peek at the top element of the priority queue */
  549. SPL_METHOD(SplPriorityQueue, top)
  550. {
  551. zval *value, *value_out;
  552. spl_heap_object *intern;
  553. if (zend_parse_parameters_none() == FAILURE) {
  554. return;
  555. }
  556. intern = Z_SPLHEAP_P(getThis());
  557. if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
  558. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  559. return;
  560. }
  561. value = spl_ptr_heap_top(intern->heap);
  562. if (!value) {
  563. zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty heap", 0);
  564. return;
  565. }
  566. value_out = spl_pqueue_extract_helper(value, intern->flags);
  567. if (!value_out) {
  568. zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node");
  569. return;
  570. }
  571. ZVAL_COPY_DEREF(return_value, value_out);
  572. }
  573. /* }}} */
  574. /* {{{ proto int SplPriorityQueue::setExtractFlags(int flags)
  575. Set the flags of extraction*/
  576. SPL_METHOD(SplPriorityQueue, setExtractFlags)
  577. {
  578. zend_long value;
  579. spl_heap_object *intern;
  580. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) {
  581. return;
  582. }
  583. intern = Z_SPLHEAP_P(getThis());
  584. intern->flags = value & SPL_PQUEUE_EXTR_MASK;
  585. RETURN_LONG(intern->flags);
  586. }
  587. /* }}} */
  588. /* {{{ proto int SplPriorityQueue::getExtractFlags()
  589. Get the flags of extraction*/
  590. SPL_METHOD(SplPriorityQueue, getExtractFlags)
  591. {
  592. spl_heap_object *intern;
  593. if (zend_parse_parameters_none() == FAILURE) {
  594. return;
  595. }
  596. intern = Z_SPLHEAP_P(getThis());
  597. RETURN_LONG(intern->flags);
  598. }
  599. /* }}} */
  600. /* {{{ proto int SplHeap::recoverFromCorruption()
  601. Recover from a corrupted state*/
  602. SPL_METHOD(SplHeap, recoverFromCorruption)
  603. {
  604. spl_heap_object *intern;
  605. if (zend_parse_parameters_none() == FAILURE) {
  606. return;
  607. }
  608. intern = Z_SPLHEAP_P(getThis());
  609. intern->heap->flags = intern->heap->flags & ~SPL_HEAP_CORRUPTED;
  610. RETURN_TRUE;
  611. }
  612. /* }}} */
  613. /* {{{ proto int SplHeap::isCorrupted()
  614. Tells if the heap is in a corrupted state*/
  615. SPL_METHOD(SplHeap, isCorrupted)
  616. {
  617. spl_heap_object *intern;
  618. if (zend_parse_parameters_none() == FAILURE) {
  619. return;
  620. }
  621. intern = Z_SPLHEAP_P(getThis());
  622. RETURN_BOOL(intern->heap->flags & SPL_HEAP_CORRUPTED);
  623. }
  624. /* }}} */
  625. /* {{{ proto bool SplPriorityQueue::compare(mixed $a, mixed $b)
  626. compare the priorities */
  627. SPL_METHOD(SplPriorityQueue, compare)
  628. {
  629. zval *a, *b;
  630. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) {
  631. return;
  632. }
  633. RETURN_LONG(spl_ptr_heap_zval_max_cmp(a, b, NULL));
  634. }
  635. /* }}} */
  636. /* {{{ proto mixed SplHeap::top()
  637. Peek at the top element of the heap */
  638. SPL_METHOD(SplHeap, top)
  639. {
  640. zval *value;
  641. spl_heap_object *intern;
  642. if (zend_parse_parameters_none() == FAILURE) {
  643. return;
  644. }
  645. intern = Z_SPLHEAP_P(getThis());
  646. if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
  647. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  648. return;
  649. }
  650. value = spl_ptr_heap_top(intern->heap);
  651. if (!value) {
  652. zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty heap", 0);
  653. return;
  654. }
  655. ZVAL_COPY_DEREF(return_value, value);
  656. }
  657. /* }}} */
  658. /* {{{ proto bool SplMinHeap::compare(mixed $a, mixed $b)
  659. compare the values */
  660. SPL_METHOD(SplMinHeap, compare)
  661. {
  662. zval *a, *b;
  663. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) {
  664. return;
  665. }
  666. RETURN_LONG(spl_ptr_heap_zval_min_cmp(a, b, NULL));
  667. }
  668. /* }}} */
  669. /* {{{ proto bool SplMaxHeap::compare(mixed $a, mixed $b)
  670. compare the values */
  671. SPL_METHOD(SplMaxHeap, compare)
  672. {
  673. zval *a, *b;
  674. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a, &b) == FAILURE) {
  675. return;
  676. }
  677. RETURN_LONG(spl_ptr_heap_zval_max_cmp(a, b, NULL));
  678. }
  679. /* }}} */
  680. static void spl_heap_it_dtor(zend_object_iterator *iter) /* {{{ */
  681. {
  682. spl_heap_it *iterator = (spl_heap_it *)iter;
  683. zend_user_it_invalidate_current(iter);
  684. zval_ptr_dtor(&iterator->intern.it.data);
  685. }
  686. /* }}} */
  687. static void spl_heap_it_rewind(zend_object_iterator *iter) /* {{{ */
  688. {
  689. /* do nothing, the iterator always points to the top element */
  690. }
  691. /* }}} */
  692. static int spl_heap_it_valid(zend_object_iterator *iter) /* {{{ */
  693. {
  694. return ((Z_SPLHEAP_P(&iter->data))->heap->count != 0 ? SUCCESS : FAILURE);
  695. }
  696. /* }}} */
  697. static zval *spl_heap_it_get_current_data(zend_object_iterator *iter) /* {{{ */
  698. {
  699. spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
  700. zval *element = &object->heap->elements[0];
  701. if (object->heap->flags & SPL_HEAP_CORRUPTED) {
  702. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  703. return NULL;
  704. }
  705. if (object->heap->count == 0 || Z_ISUNDEF_P(element)) {
  706. return NULL;
  707. } else {
  708. return element;
  709. }
  710. }
  711. /* }}} */
  712. static zval *spl_pqueue_it_get_current_data(zend_object_iterator *iter) /* {{{ */
  713. {
  714. spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
  715. zval *element = &object->heap->elements[0];
  716. if (object->heap->flags & SPL_HEAP_CORRUPTED) {
  717. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  718. return NULL;
  719. }
  720. if (object->heap->count == 0 || Z_ISUNDEF_P(element)) {
  721. return NULL;
  722. } else {
  723. zval *data = spl_pqueue_extract_helper(element, object->flags);
  724. if (!data) {
  725. zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node");
  726. }
  727. return data;
  728. }
  729. }
  730. /* }}} */
  731. static void spl_heap_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
  732. {
  733. spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
  734. ZVAL_LONG(key, object->heap->count - 1);
  735. }
  736. /* }}} */
  737. static void spl_heap_it_move_forward(zend_object_iterator *iter) /* {{{ */
  738. {
  739. spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
  740. zval elem;
  741. if (object->heap->flags & SPL_HEAP_CORRUPTED) {
  742. zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
  743. return;
  744. }
  745. spl_ptr_heap_delete_top(object->heap, &elem, &iter->data);
  746. zval_ptr_dtor(&elem);
  747. zend_user_it_invalidate_current(iter);
  748. }
  749. /* }}} */
  750. /* {{{ proto int SplHeap::key()
  751. Return current array key */
  752. SPL_METHOD(SplHeap, key)
  753. {
  754. spl_heap_object *intern = Z_SPLHEAP_P(getThis());
  755. if (zend_parse_parameters_none() == FAILURE) {
  756. return;
  757. }
  758. RETURN_LONG(intern->heap->count - 1);
  759. }
  760. /* }}} */
  761. /* {{{ proto void SplHeap::next()
  762. Move to next entry */
  763. SPL_METHOD(SplHeap, next)
  764. {
  765. spl_heap_object *intern = Z_SPLHEAP_P(getThis());
  766. zval elem;
  767. spl_ptr_heap_delete_top(intern->heap, &elem, getThis());
  768. if (zend_parse_parameters_none() == FAILURE) {
  769. return;
  770. }
  771. zval_ptr_dtor(&elem);
  772. }
  773. /* }}} */
  774. /* {{{ proto bool SplHeap::valid()
  775. Check whether the datastructure contains more entries */
  776. SPL_METHOD(SplHeap, valid)
  777. {
  778. spl_heap_object *intern = Z_SPLHEAP_P(getThis());
  779. if (zend_parse_parameters_none() == FAILURE) {
  780. return;
  781. }
  782. RETURN_BOOL(intern->heap->count != 0);
  783. }
  784. /* }}} */
  785. /* {{{ proto void SplHeap::rewind()
  786. Rewind the datastructure back to the start */
  787. SPL_METHOD(SplHeap, rewind)
  788. {
  789. if (zend_parse_parameters_none() == FAILURE) {
  790. return;
  791. }
  792. /* do nothing, the iterator always points to the top element */
  793. }
  794. /* }}} */
  795. /* {{{ proto mixed|NULL SplHeap::current()
  796. Return current datastructure entry */
  797. SPL_METHOD(SplHeap, current)
  798. {
  799. spl_heap_object *intern = Z_SPLHEAP_P(getThis());
  800. zval *element = &intern->heap->elements[0];
  801. if (zend_parse_parameters_none() == FAILURE) {
  802. return;
  803. }
  804. if (!intern->heap->count || Z_ISUNDEF_P(element)) {
  805. RETURN_NULL();
  806. } else {
  807. ZVAL_COPY_DEREF(return_value, element);
  808. }
  809. }
  810. /* }}} */
  811. /* {{{ proto mixed|NULL SplPriorityQueue::current()
  812. Return current datastructure entry */
  813. SPL_METHOD(SplPriorityQueue, current)
  814. {
  815. spl_heap_object *intern = Z_SPLHEAP_P(getThis());
  816. zval *element = &intern->heap->elements[0];
  817. if (zend_parse_parameters_none() == FAILURE) {
  818. return;
  819. }
  820. if (!intern->heap->count || Z_ISUNDEF_P(element)) {
  821. RETURN_NULL();
  822. } else {
  823. zval *data = spl_pqueue_extract_helper(element, intern->flags);
  824. if (!data) {
  825. zend_error(E_RECOVERABLE_ERROR, "Unable to extract from the PriorityQueue node");
  826. RETURN_NULL();
  827. }
  828. ZVAL_COPY_DEREF(return_value, data);
  829. }
  830. }
  831. /* }}} */
  832. /* iterator handler table */
  833. static const zend_object_iterator_funcs spl_heap_it_funcs = {
  834. spl_heap_it_dtor,
  835. spl_heap_it_valid,
  836. spl_heap_it_get_current_data,
  837. spl_heap_it_get_current_key,
  838. spl_heap_it_move_forward,
  839. spl_heap_it_rewind,
  840. NULL
  841. };
  842. static const zend_object_iterator_funcs spl_pqueue_it_funcs = {
  843. spl_heap_it_dtor,
  844. spl_heap_it_valid,
  845. spl_pqueue_it_get_current_data,
  846. spl_heap_it_get_current_key,
  847. spl_heap_it_move_forward,
  848. spl_heap_it_rewind,
  849. NULL
  850. };
  851. zend_object_iterator *spl_heap_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  852. {
  853. spl_heap_it *iterator;
  854. spl_heap_object *heap_object = Z_SPLHEAP_P(object);
  855. if (by_ref) {
  856. zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
  857. return NULL;
  858. }
  859. iterator = emalloc(sizeof(spl_heap_it));
  860. zend_iterator_init(&iterator->intern.it);
  861. ZVAL_COPY(&iterator->intern.it.data, object);
  862. iterator->intern.it.funcs = &spl_heap_it_funcs;
  863. iterator->intern.ce = ce;
  864. iterator->flags = heap_object->flags;
  865. ZVAL_UNDEF(&iterator->intern.value);
  866. return &iterator->intern.it;
  867. }
  868. /* }}} */
  869. zend_object_iterator *spl_pqueue_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  870. {
  871. spl_heap_it *iterator;
  872. spl_heap_object *heap_object = Z_SPLHEAP_P(object);
  873. if (by_ref) {
  874. zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
  875. return NULL;
  876. }
  877. iterator = emalloc(sizeof(spl_heap_it));
  878. zend_iterator_init((zend_object_iterator*)iterator);
  879. ZVAL_COPY(&iterator->intern.it.data, object);
  880. iterator->intern.it.funcs = &spl_pqueue_it_funcs;
  881. iterator->intern.ce = ce;
  882. iterator->flags = heap_object->flags;
  883. ZVAL_UNDEF(&iterator->intern.value);
  884. return &iterator->intern.it;
  885. }
  886. /* }}} */
  887. ZEND_BEGIN_ARG_INFO(arginfo_heap_insert, 0)
  888. ZEND_ARG_INFO(0, value)
  889. ZEND_END_ARG_INFO()
  890. ZEND_BEGIN_ARG_INFO(arginfo_heap_compare, 0)
  891. ZEND_ARG_INFO(0, a)
  892. ZEND_ARG_INFO(0, b)
  893. ZEND_END_ARG_INFO()
  894. ZEND_BEGIN_ARG_INFO(arginfo_pqueue_insert, 0)
  895. ZEND_ARG_INFO(0, value)
  896. ZEND_ARG_INFO(0, priority)
  897. ZEND_END_ARG_INFO()
  898. ZEND_BEGIN_ARG_INFO(arginfo_pqueue_setflags, 0)
  899. ZEND_ARG_INFO(0, flags)
  900. ZEND_END_ARG_INFO()
  901. ZEND_BEGIN_ARG_INFO(arginfo_splheap_void, 0)
  902. ZEND_END_ARG_INFO()
  903. static const zend_function_entry spl_funcs_SplMinHeap[] = {
  904. SPL_ME(SplMinHeap, compare, arginfo_heap_compare, ZEND_ACC_PROTECTED)
  905. PHP_FE_END
  906. };
  907. static const zend_function_entry spl_funcs_SplMaxHeap[] = {
  908. SPL_ME(SplMaxHeap, compare, arginfo_heap_compare, ZEND_ACC_PROTECTED)
  909. PHP_FE_END
  910. };
  911. static const zend_function_entry spl_funcs_SplPriorityQueue[] = {
  912. SPL_ME(SplPriorityQueue, compare, arginfo_heap_compare, ZEND_ACC_PUBLIC)
  913. SPL_ME(SplPriorityQueue, insert, arginfo_pqueue_insert, ZEND_ACC_PUBLIC)
  914. SPL_ME(SplPriorityQueue, setExtractFlags, arginfo_pqueue_setflags, ZEND_ACC_PUBLIC)
  915. SPL_ME(SplPriorityQueue, getExtractFlags, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  916. SPL_ME(SplPriorityQueue, top, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  917. SPL_ME(SplPriorityQueue, extract, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  918. SPL_ME(SplHeap, count, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  919. SPL_ME(SplHeap, isEmpty, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  920. SPL_ME(SplHeap, rewind, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  921. SPL_ME(SplPriorityQueue, current, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  922. SPL_ME(SplHeap, key, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  923. SPL_ME(SplHeap, next, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  924. SPL_ME(SplHeap, valid, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  925. SPL_ME(SplHeap, recoverFromCorruption, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  926. SPL_ME(SplHeap, isCorrupted, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  927. PHP_FE_END
  928. };
  929. static const zend_function_entry spl_funcs_SplHeap[] = {
  930. SPL_ME(SplHeap, extract, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  931. SPL_ME(SplHeap, insert, arginfo_heap_insert, ZEND_ACC_PUBLIC)
  932. SPL_ME(SplHeap, top, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  933. SPL_ME(SplHeap, count, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  934. SPL_ME(SplHeap, isEmpty, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  935. SPL_ME(SplHeap, rewind, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  936. SPL_ME(SplHeap, current, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  937. SPL_ME(SplHeap, key, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  938. SPL_ME(SplHeap, next, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  939. SPL_ME(SplHeap, valid, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  940. SPL_ME(SplHeap, recoverFromCorruption, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  941. SPL_ME(SplHeap, isCorrupted, arginfo_splheap_void, ZEND_ACC_PUBLIC)
  942. ZEND_FENTRY(compare, NULL, NULL, ZEND_ACC_PROTECTED|ZEND_ACC_ABSTRACT)
  943. PHP_FE_END
  944. };
  945. /* }}} */
  946. PHP_MINIT_FUNCTION(spl_heap) /* {{{ */
  947. {
  948. REGISTER_SPL_STD_CLASS_EX(SplHeap, spl_heap_object_new, spl_funcs_SplHeap);
  949. memcpy(&spl_handler_SplHeap, &std_object_handlers, sizeof(zend_object_handlers));
  950. spl_handler_SplHeap.offset = XtOffsetOf(spl_heap_object, std);
  951. spl_handler_SplHeap.clone_obj = spl_heap_object_clone;
  952. spl_handler_SplHeap.count_elements = spl_heap_object_count_elements;
  953. spl_handler_SplHeap.get_debug_info = spl_heap_object_get_debug_info;
  954. spl_handler_SplHeap.get_gc = spl_heap_object_get_gc;
  955. spl_handler_SplHeap.dtor_obj = zend_objects_destroy_object;
  956. spl_handler_SplHeap.free_obj = spl_heap_object_free_storage;
  957. REGISTER_SPL_IMPLEMENTS(SplHeap, Iterator);
  958. REGISTER_SPL_IMPLEMENTS(SplHeap, Countable);
  959. spl_ce_SplHeap->get_iterator = spl_heap_get_iterator;
  960. REGISTER_SPL_SUB_CLASS_EX(SplMinHeap, SplHeap, spl_heap_object_new, spl_funcs_SplMinHeap);
  961. REGISTER_SPL_SUB_CLASS_EX(SplMaxHeap, SplHeap, spl_heap_object_new, spl_funcs_SplMaxHeap);
  962. spl_ce_SplMaxHeap->get_iterator = spl_heap_get_iterator;
  963. spl_ce_SplMinHeap->get_iterator = spl_heap_get_iterator;
  964. REGISTER_SPL_STD_CLASS_EX(SplPriorityQueue, spl_heap_object_new, spl_funcs_SplPriorityQueue);
  965. memcpy(&spl_handler_SplPriorityQueue, &std_object_handlers, sizeof(zend_object_handlers));
  966. spl_handler_SplPriorityQueue.offset = XtOffsetOf(spl_heap_object, std);
  967. spl_handler_SplPriorityQueue.clone_obj = spl_heap_object_clone;
  968. spl_handler_SplPriorityQueue.count_elements = spl_heap_object_count_elements;
  969. spl_handler_SplPriorityQueue.get_debug_info = spl_pqueue_object_get_debug_info;
  970. spl_handler_SplPriorityQueue.get_gc = spl_heap_object_get_gc;
  971. spl_handler_SplPriorityQueue.dtor_obj = zend_objects_destroy_object;
  972. spl_handler_SplPriorityQueue.free_obj = spl_heap_object_free_storage;
  973. REGISTER_SPL_IMPLEMENTS(SplPriorityQueue, Iterator);
  974. REGISTER_SPL_IMPLEMENTS(SplPriorityQueue, Countable);
  975. spl_ce_SplPriorityQueue->get_iterator = spl_pqueue_get_iterator;
  976. REGISTER_SPL_CLASS_CONST_LONG(SplPriorityQueue, "EXTR_BOTH", SPL_PQUEUE_EXTR_BOTH);
  977. REGISTER_SPL_CLASS_CONST_LONG(SplPriorityQueue, "EXTR_PRIORITY", SPL_PQUEUE_EXTR_PRIORITY);
  978. REGISTER_SPL_CLASS_CONST_LONG(SplPriorityQueue, "EXTR_DATA", SPL_PQUEUE_EXTR_DATA);
  979. return SUCCESS;
  980. }
  981. /* }}} */
  982. /*
  983. * Local variables:
  984. * tab-width: 4
  985. * c-basic-offset: 4
  986. * End:
  987. * vim600: fdm=marker
  988. * vim: noet sw=4 ts=4
  989. */