spl_heap.c 35 KB

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