spl_heap.c 31 KB

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