spl_dllist.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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 "zend_hash.h"
  24. #include "php_spl.h"
  25. #include "ext/standard/info.h"
  26. #include "ext/standard/php_var.h"
  27. #include "zend_smart_str.h"
  28. #include "spl_functions.h"
  29. #include "spl_engine.h"
  30. #include "spl_iterators.h"
  31. #include "spl_dllist.h"
  32. #include "spl_exceptions.h"
  33. zend_object_handlers spl_handler_SplDoublyLinkedList;
  34. PHPAPI zend_class_entry *spl_ce_SplDoublyLinkedList;
  35. PHPAPI zend_class_entry *spl_ce_SplQueue;
  36. PHPAPI zend_class_entry *spl_ce_SplStack;
  37. #define SPL_LLIST_DELREF(elem) if(!--(elem)->rc) { \
  38. efree(elem); \
  39. }
  40. #define SPL_LLIST_CHECK_DELREF(elem) if((elem) && !--(elem)->rc) { \
  41. efree(elem); \
  42. }
  43. #define SPL_LLIST_ADDREF(elem) (elem)->rc++
  44. #define SPL_LLIST_CHECK_ADDREF(elem) if(elem) (elem)->rc++
  45. #define SPL_DLLIST_IT_DELETE 0x00000001 /* Delete flag makes the iterator delete the current element on next */
  46. #define SPL_DLLIST_IT_LIFO 0x00000002 /* LIFO flag makes the iterator traverse the structure as a LastInFirstOut */
  47. #define SPL_DLLIST_IT_MASK 0x00000003 /* Mask to isolate flags related to iterators */
  48. #define SPL_DLLIST_IT_FIX 0x00000004 /* Backward/Forward bit is fixed */
  49. #ifdef accept
  50. #undef accept
  51. #endif
  52. typedef struct _spl_ptr_llist_element {
  53. struct _spl_ptr_llist_element *prev;
  54. struct _spl_ptr_llist_element *next;
  55. int rc;
  56. zval data;
  57. } spl_ptr_llist_element;
  58. typedef void (*spl_ptr_llist_dtor_func)(spl_ptr_llist_element *);
  59. typedef void (*spl_ptr_llist_ctor_func)(spl_ptr_llist_element *);
  60. typedef struct _spl_ptr_llist {
  61. spl_ptr_llist_element *head;
  62. spl_ptr_llist_element *tail;
  63. spl_ptr_llist_dtor_func dtor;
  64. spl_ptr_llist_ctor_func ctor;
  65. int count;
  66. } spl_ptr_llist;
  67. typedef struct _spl_dllist_object spl_dllist_object;
  68. typedef struct _spl_dllist_it spl_dllist_it;
  69. struct _spl_dllist_object {
  70. spl_ptr_llist *llist;
  71. int traverse_position;
  72. spl_ptr_llist_element *traverse_pointer;
  73. int flags;
  74. zend_function *fptr_offset_get;
  75. zend_function *fptr_offset_set;
  76. zend_function *fptr_offset_has;
  77. zend_function *fptr_offset_del;
  78. zend_function *fptr_count;
  79. zend_class_entry *ce_get_iterator;
  80. zval *gc_data;
  81. int gc_data_count;
  82. zend_object std;
  83. };
  84. /* define an overloaded iterator structure */
  85. struct _spl_dllist_it {
  86. zend_user_iterator intern;
  87. spl_ptr_llist_element *traverse_pointer;
  88. int traverse_position;
  89. int flags;
  90. };
  91. static inline spl_dllist_object *spl_dllist_from_obj(zend_object *obj) /* {{{ */ {
  92. return (spl_dllist_object*)((char*)(obj) - XtOffsetOf(spl_dllist_object, std));
  93. }
  94. /* }}} */
  95. #define Z_SPLDLLIST_P(zv) spl_dllist_from_obj(Z_OBJ_P((zv)))
  96. /* {{{ spl_ptr_llist */
  97. static void spl_ptr_llist_zval_dtor(spl_ptr_llist_element *elem) { /* {{{ */
  98. if (!Z_ISUNDEF(elem->data)) {
  99. zval_ptr_dtor(&elem->data);
  100. ZVAL_UNDEF(&elem->data);
  101. }
  102. }
  103. /* }}} */
  104. static void spl_ptr_llist_zval_ctor(spl_ptr_llist_element *elem) { /* {{{ */
  105. if (Z_REFCOUNTED(elem->data)) {
  106. Z_ADDREF(elem->data);
  107. }
  108. }
  109. /* }}} */
  110. static spl_ptr_llist *spl_ptr_llist_init(spl_ptr_llist_ctor_func ctor, spl_ptr_llist_dtor_func dtor) /* {{{ */
  111. {
  112. spl_ptr_llist *llist = emalloc(sizeof(spl_ptr_llist));
  113. llist->head = NULL;
  114. llist->tail = NULL;
  115. llist->count = 0;
  116. llist->dtor = dtor;
  117. llist->ctor = ctor;
  118. return llist;
  119. }
  120. /* }}} */
  121. static zend_long spl_ptr_llist_count(spl_ptr_llist *llist) /* {{{ */
  122. {
  123. return (zend_long)llist->count;
  124. }
  125. /* }}} */
  126. static void spl_ptr_llist_destroy(spl_ptr_llist *llist) /* {{{ */
  127. {
  128. spl_ptr_llist_element *current = llist->head, *next;
  129. spl_ptr_llist_dtor_func dtor = llist->dtor;
  130. while (current) {
  131. next = current->next;
  132. if (dtor) {
  133. dtor(current);
  134. }
  135. SPL_LLIST_DELREF(current);
  136. current = next;
  137. }
  138. efree(llist);
  139. }
  140. /* }}} */
  141. static spl_ptr_llist_element *spl_ptr_llist_offset(spl_ptr_llist *llist, zend_long offset, int backward) /* {{{ */
  142. {
  143. spl_ptr_llist_element *current;
  144. int pos = 0;
  145. if (backward) {
  146. current = llist->tail;
  147. } else {
  148. current = llist->head;
  149. }
  150. while (current && pos < offset) {
  151. pos++;
  152. if (backward) {
  153. current = current->prev;
  154. } else {
  155. current = current->next;
  156. }
  157. }
  158. return current;
  159. }
  160. /* }}} */
  161. static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */
  162. {
  163. spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
  164. elem->rc = 1;
  165. elem->prev = NULL;
  166. elem->next = llist->head;
  167. ZVAL_COPY_VALUE(&elem->data, data);
  168. if (llist->head) {
  169. llist->head->prev = elem;
  170. } else {
  171. llist->tail = elem;
  172. }
  173. llist->head = elem;
  174. llist->count++;
  175. if (llist->ctor) {
  176. llist->ctor(elem);
  177. }
  178. }
  179. /* }}} */
  180. static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */
  181. {
  182. spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
  183. elem->rc = 1;
  184. elem->prev = llist->tail;
  185. elem->next = NULL;
  186. ZVAL_COPY_VALUE(&elem->data, data);
  187. if (llist->tail) {
  188. llist->tail->next = elem;
  189. } else {
  190. llist->head = elem;
  191. }
  192. llist->tail = elem;
  193. llist->count++;
  194. if (llist->ctor) {
  195. llist->ctor(elem);
  196. }
  197. }
  198. /* }}} */
  199. static void spl_ptr_llist_pop(spl_ptr_llist *llist, zval *ret) /* {{{ */
  200. {
  201. spl_ptr_llist_element *tail = llist->tail;
  202. if (tail == NULL) {
  203. ZVAL_UNDEF(ret);
  204. return;
  205. }
  206. if (tail->prev) {
  207. tail->prev->next = NULL;
  208. } else {
  209. llist->head = NULL;
  210. }
  211. llist->tail = tail->prev;
  212. llist->count--;
  213. ZVAL_COPY(ret, &tail->data);
  214. tail->prev = NULL;
  215. if (llist->dtor) {
  216. llist->dtor(tail);
  217. }
  218. ZVAL_UNDEF(&tail->data);
  219. SPL_LLIST_DELREF(tail);
  220. }
  221. /* }}} */
  222. static zval *spl_ptr_llist_last(spl_ptr_llist *llist) /* {{{ */
  223. {
  224. spl_ptr_llist_element *tail = llist->tail;
  225. if (tail == NULL) {
  226. return NULL;
  227. } else {
  228. return &tail->data;
  229. }
  230. }
  231. /* }}} */
  232. static zval *spl_ptr_llist_first(spl_ptr_llist *llist) /* {{{ */
  233. {
  234. spl_ptr_llist_element *head = llist->head;
  235. if (head == NULL) {
  236. return NULL;
  237. } else {
  238. return &head->data;
  239. }
  240. }
  241. /* }}} */
  242. static void spl_ptr_llist_shift(spl_ptr_llist *llist, zval *ret) /* {{{ */
  243. {
  244. spl_ptr_llist_element *head = llist->head;
  245. if (head == NULL) {
  246. ZVAL_UNDEF(ret);
  247. return;
  248. }
  249. if (head->next) {
  250. head->next->prev = NULL;
  251. } else {
  252. llist->tail = NULL;
  253. }
  254. llist->head = head->next;
  255. llist->count--;
  256. ZVAL_COPY(ret, &head->data);
  257. head->next = NULL;
  258. if (llist->dtor) {
  259. llist->dtor(head);
  260. }
  261. ZVAL_UNDEF(&head->data);
  262. SPL_LLIST_DELREF(head);
  263. }
  264. /* }}} */
  265. static void spl_ptr_llist_copy(spl_ptr_llist *from, spl_ptr_llist *to) /* {{{ */
  266. {
  267. spl_ptr_llist_element *current = from->head, *next;
  268. //??? spl_ptr_llist_ctor_func ctor = from->ctor;
  269. while (current) {
  270. next = current->next;
  271. /*??? FIXME
  272. if (ctor) {
  273. ctor(current);
  274. }
  275. */
  276. spl_ptr_llist_push(to, &current->data);
  277. current = next;
  278. }
  279. }
  280. /* }}} */
  281. /* }}} */
  282. static void spl_dllist_object_free_storage(zend_object *object) /* {{{ */
  283. {
  284. spl_dllist_object *intern = spl_dllist_from_obj(object);
  285. zval tmp;
  286. zend_object_std_dtor(&intern->std);
  287. while (intern->llist->count > 0) {
  288. spl_ptr_llist_pop(intern->llist, &tmp);
  289. zval_ptr_dtor(&tmp);
  290. }
  291. if (intern->gc_data != NULL) {
  292. efree(intern->gc_data);
  293. };
  294. spl_ptr_llist_destroy(intern->llist);
  295. SPL_LLIST_CHECK_DELREF(intern->traverse_pointer);
  296. }
  297. /* }}} */
  298. zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
  299. static zend_object *spl_dllist_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig) /* {{{ */
  300. {
  301. spl_dllist_object *intern;
  302. zend_class_entry *parent = class_type;
  303. int inherited = 0;
  304. intern = zend_object_alloc(sizeof(spl_dllist_object), parent);
  305. zend_object_std_init(&intern->std, class_type);
  306. object_properties_init(&intern->std, class_type);
  307. intern->flags = 0;
  308. intern->traverse_position = 0;
  309. if (orig) {
  310. spl_dllist_object *other = Z_SPLDLLIST_P(orig);
  311. intern->ce_get_iterator = other->ce_get_iterator;
  312. if (clone_orig) {
  313. intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(other->llist->ctor, other->llist->dtor);
  314. spl_ptr_llist_copy(other->llist, intern->llist);
  315. intern->traverse_pointer = intern->llist->head;
  316. SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
  317. } else {
  318. intern->llist = other->llist;
  319. intern->traverse_pointer = intern->llist->head;
  320. SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
  321. }
  322. intern->flags = other->flags;
  323. } else {
  324. intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(spl_ptr_llist_zval_ctor, spl_ptr_llist_zval_dtor);
  325. intern->traverse_pointer = intern->llist->head;
  326. SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
  327. }
  328. while (parent) {
  329. if (parent == spl_ce_SplStack) {
  330. intern->flags |= (SPL_DLLIST_IT_FIX | SPL_DLLIST_IT_LIFO);
  331. intern->std.handlers = &spl_handler_SplDoublyLinkedList;
  332. } else if (parent == spl_ce_SplQueue) {
  333. intern->flags |= SPL_DLLIST_IT_FIX;
  334. intern->std.handlers = &spl_handler_SplDoublyLinkedList;
  335. }
  336. if (parent == spl_ce_SplDoublyLinkedList) {
  337. intern->std.handlers = &spl_handler_SplDoublyLinkedList;
  338. break;
  339. }
  340. parent = parent->parent;
  341. inherited = 1;
  342. }
  343. if (!parent) { /* this must never happen */
  344. php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplDoublyLinkedList");
  345. }
  346. if (inherited) {
  347. intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
  348. if (intern->fptr_offset_get->common.scope == parent) {
  349. intern->fptr_offset_get = NULL;
  350. }
  351. intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
  352. if (intern->fptr_offset_set->common.scope == parent) {
  353. intern->fptr_offset_set = NULL;
  354. }
  355. intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
  356. if (intern->fptr_offset_has->common.scope == parent) {
  357. intern->fptr_offset_has = NULL;
  358. }
  359. intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
  360. if (intern->fptr_offset_del->common.scope == parent) {
  361. intern->fptr_offset_del = NULL;
  362. }
  363. intern->fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
  364. if (intern->fptr_count->common.scope == parent) {
  365. intern->fptr_count = NULL;
  366. }
  367. }
  368. return &intern->std;
  369. }
  370. /* }}} */
  371. static zend_object *spl_dllist_object_new(zend_class_entry *class_type) /* {{{ */
  372. {
  373. return spl_dllist_object_new_ex(class_type, NULL, 0);
  374. }
  375. /* }}} */
  376. static zend_object *spl_dllist_object_clone(zval *zobject) /* {{{ */
  377. {
  378. zend_object *old_object;
  379. zend_object *new_object;
  380. old_object = Z_OBJ_P(zobject);
  381. new_object = spl_dllist_object_new_ex(old_object->ce, zobject, 1);
  382. zend_objects_clone_members(new_object, old_object);
  383. return new_object;
  384. }
  385. /* }}} */
  386. static int spl_dllist_object_count_elements(zval *object, zend_long *count) /* {{{ */
  387. {
  388. spl_dllist_object *intern = Z_SPLDLLIST_P(object);
  389. if (intern->fptr_count) {
  390. zval rv;
  391. zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
  392. if (!Z_ISUNDEF(rv)) {
  393. *count = zval_get_long(&rv);
  394. zval_ptr_dtor(&rv);
  395. return SUCCESS;
  396. }
  397. *count = 0;
  398. return FAILURE;
  399. }
  400. *count = spl_ptr_llist_count(intern->llist);
  401. return SUCCESS;
  402. }
  403. /* }}} */
  404. static HashTable* spl_dllist_object_get_debug_info(zval *obj, int *is_temp) /* {{{{ */
  405. {
  406. spl_dllist_object *intern = Z_SPLDLLIST_P(obj);
  407. spl_ptr_llist_element *current = intern->llist->head, *next;
  408. zval tmp, dllist_array;
  409. zend_string *pnstr;
  410. int i = 0;
  411. HashTable *debug_info;
  412. *is_temp = 1;
  413. if (!intern->std.properties) {
  414. rebuild_object_properties(&intern->std);
  415. }
  416. debug_info = zend_new_array(1);
  417. zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
  418. pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "flags", sizeof("flags")-1);
  419. ZVAL_LONG(&tmp, intern->flags);
  420. zend_hash_add(debug_info, pnstr, &tmp);
  421. zend_string_release_ex(pnstr, 0);
  422. array_init(&dllist_array);
  423. while (current) {
  424. next = current->next;
  425. add_index_zval(&dllist_array, i, &current->data);
  426. if (Z_REFCOUNTED(current->data)) {
  427. Z_ADDREF(current->data);
  428. }
  429. i++;
  430. current = next;
  431. }
  432. pnstr = spl_gen_private_prop_name(spl_ce_SplDoublyLinkedList, "dllist", sizeof("dllist")-1);
  433. zend_hash_add(debug_info, pnstr, &dllist_array);
  434. zend_string_release_ex(pnstr, 0);
  435. return debug_info;
  436. }
  437. /* }}}} */
  438. static HashTable *spl_dllist_object_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
  439. {
  440. spl_dllist_object *intern = Z_SPLDLLIST_P(obj);
  441. spl_ptr_llist_element *current = intern->llist->head;
  442. int i = 0;
  443. if (intern->gc_data_count < intern->llist->count) {
  444. intern->gc_data_count = intern->llist->count;
  445. intern->gc_data = safe_erealloc(intern->gc_data, intern->gc_data_count, sizeof(zval), 0);
  446. }
  447. while (current) {
  448. ZVAL_COPY_VALUE(&intern->gc_data[i++], &current->data);
  449. current = current->next;
  450. }
  451. *gc_data = intern->gc_data;
  452. *gc_data_count = i;
  453. return zend_std_get_properties(obj);
  454. }
  455. /* }}} */
  456. /* {{{ proto bool SplDoublyLinkedList::push(mixed value)
  457. Push $value on the SplDoublyLinkedList */
  458. SPL_METHOD(SplDoublyLinkedList, push)
  459. {
  460. zval *value;
  461. spl_dllist_object *intern;
  462. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
  463. return;
  464. }
  465. intern = Z_SPLDLLIST_P(getThis());
  466. spl_ptr_llist_push(intern->llist, value);
  467. RETURN_TRUE;
  468. }
  469. /* }}} */
  470. /* {{{ proto bool SplDoublyLinkedList::unshift(mixed value)
  471. Unshift $value on the SplDoublyLinkedList */
  472. SPL_METHOD(SplDoublyLinkedList, unshift)
  473. {
  474. zval *value;
  475. spl_dllist_object *intern;
  476. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
  477. return;
  478. }
  479. intern = Z_SPLDLLIST_P(getThis());
  480. spl_ptr_llist_unshift(intern->llist, value);
  481. RETURN_TRUE;
  482. }
  483. /* }}} */
  484. /* {{{ proto mixed SplDoublyLinkedList::pop()
  485. Pop an element out of the SplDoublyLinkedList */
  486. SPL_METHOD(SplDoublyLinkedList, pop)
  487. {
  488. spl_dllist_object *intern;
  489. if (zend_parse_parameters_none() == FAILURE) {
  490. return;
  491. }
  492. intern = Z_SPLDLLIST_P(getThis());
  493. spl_ptr_llist_pop(intern->llist, return_value);
  494. if (Z_ISUNDEF_P(return_value)) {
  495. zend_throw_exception(spl_ce_RuntimeException, "Can't pop from an empty datastructure", 0);
  496. RETURN_NULL();
  497. }
  498. }
  499. /* }}} */
  500. /* {{{ proto mixed SplDoublyLinkedList::shift()
  501. Shift an element out of the SplDoublyLinkedList */
  502. SPL_METHOD(SplDoublyLinkedList, shift)
  503. {
  504. spl_dllist_object *intern;
  505. if (zend_parse_parameters_none() == FAILURE) {
  506. return;
  507. }
  508. intern = Z_SPLDLLIST_P(getThis());
  509. spl_ptr_llist_shift(intern->llist, return_value);
  510. if (Z_ISUNDEF_P(return_value)) {
  511. zend_throw_exception(spl_ce_RuntimeException, "Can't shift from an empty datastructure", 0);
  512. RETURN_NULL();
  513. }
  514. }
  515. /* }}} */
  516. /* {{{ proto mixed SplDoublyLinkedList::top()
  517. Peek at the top element of the SplDoublyLinkedList */
  518. SPL_METHOD(SplDoublyLinkedList, top)
  519. {
  520. zval *value;
  521. spl_dllist_object *intern;
  522. if (zend_parse_parameters_none() == FAILURE) {
  523. return;
  524. }
  525. intern = Z_SPLDLLIST_P(getThis());
  526. value = spl_ptr_llist_last(intern->llist);
  527. if (value == NULL || Z_ISUNDEF_P(value)) {
  528. zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
  529. return;
  530. }
  531. ZVAL_COPY_DEREF(return_value, value);
  532. }
  533. /* }}} */
  534. /* {{{ proto mixed SplDoublyLinkedList::bottom()
  535. Peek at the bottom element of the SplDoublyLinkedList */
  536. SPL_METHOD(SplDoublyLinkedList, bottom)
  537. {
  538. zval *value;
  539. spl_dllist_object *intern;
  540. if (zend_parse_parameters_none() == FAILURE) {
  541. return;
  542. }
  543. intern = Z_SPLDLLIST_P(getThis());
  544. value = spl_ptr_llist_first(intern->llist);
  545. if (value == NULL || Z_ISUNDEF_P(value)) {
  546. zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0);
  547. return;
  548. }
  549. ZVAL_COPY_DEREF(return_value, value);
  550. }
  551. /* }}} */
  552. /* {{{ proto int SplDoublyLinkedList::count()
  553. Return the number of elements in the datastructure. */
  554. SPL_METHOD(SplDoublyLinkedList, count)
  555. {
  556. zend_long count;
  557. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  558. if (zend_parse_parameters_none() == FAILURE) {
  559. return;
  560. }
  561. count = spl_ptr_llist_count(intern->llist);
  562. RETURN_LONG(count);
  563. }
  564. /* }}} */
  565. /* {{{ proto int SplDoublyLinkedList::isEmpty()
  566. Return true if the SplDoublyLinkedList is empty. */
  567. SPL_METHOD(SplDoublyLinkedList, isEmpty)
  568. {
  569. zend_long count;
  570. if (zend_parse_parameters_none() == FAILURE) {
  571. return;
  572. }
  573. spl_dllist_object_count_elements(getThis(), &count);
  574. RETURN_BOOL(count == 0);
  575. }
  576. /* }}} */
  577. /* {{{ proto int SplDoublyLinkedList::setIteratorMode(int flags)
  578. Set the mode of iteration */
  579. SPL_METHOD(SplDoublyLinkedList, setIteratorMode)
  580. {
  581. zend_long value;
  582. spl_dllist_object *intern;
  583. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &value) == FAILURE) {
  584. return;
  585. }
  586. intern = Z_SPLDLLIST_P(getThis());
  587. if (intern->flags & SPL_DLLIST_IT_FIX
  588. && (intern->flags & SPL_DLLIST_IT_LIFO) != (value & SPL_DLLIST_IT_LIFO)) {
  589. zend_throw_exception(spl_ce_RuntimeException, "Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen", 0);
  590. return;
  591. }
  592. intern->flags = (value & SPL_DLLIST_IT_MASK) | (intern->flags & SPL_DLLIST_IT_FIX);
  593. RETURN_LONG(intern->flags);
  594. }
  595. /* }}} */
  596. /* {{{ proto int SplDoublyLinkedList::getIteratorMode()
  597. Return the mode of iteration */
  598. SPL_METHOD(SplDoublyLinkedList, getIteratorMode)
  599. {
  600. spl_dllist_object *intern;
  601. if (zend_parse_parameters_none() == FAILURE) {
  602. return;
  603. }
  604. intern = Z_SPLDLLIST_P(getThis());
  605. RETURN_LONG(intern->flags);
  606. }
  607. /* }}} */
  608. /* {{{ proto bool SplDoublyLinkedList::offsetExists(mixed index)
  609. Returns whether the requested $index exists. */
  610. SPL_METHOD(SplDoublyLinkedList, offsetExists)
  611. {
  612. zval *zindex;
  613. spl_dllist_object *intern;
  614. zend_long index;
  615. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
  616. return;
  617. }
  618. intern = Z_SPLDLLIST_P(getThis());
  619. index = spl_offset_convert_to_long(zindex);
  620. RETURN_BOOL(index >= 0 && index < intern->llist->count);
  621. } /* }}} */
  622. /* {{{ proto mixed SplDoublyLinkedList::offsetGet(mixed index)
  623. Returns the value at the specified $index. */
  624. SPL_METHOD(SplDoublyLinkedList, offsetGet)
  625. {
  626. zval *zindex;
  627. zend_long index;
  628. spl_dllist_object *intern;
  629. spl_ptr_llist_element *element;
  630. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
  631. return;
  632. }
  633. intern = Z_SPLDLLIST_P(getThis());
  634. index = spl_offset_convert_to_long(zindex);
  635. if (index < 0 || index >= intern->llist->count) {
  636. zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
  637. return;
  638. }
  639. element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
  640. if (element != NULL) {
  641. zval *value = &element->data;
  642. ZVAL_COPY_DEREF(return_value, value);
  643. } else {
  644. zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
  645. }
  646. } /* }}} */
  647. /* {{{ proto void SplDoublyLinkedList::offsetSet(mixed index, mixed newval)
  648. Sets the value at the specified $index to $newval. */
  649. SPL_METHOD(SplDoublyLinkedList, offsetSet)
  650. {
  651. zval *zindex, *value;
  652. spl_dllist_object *intern;
  653. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
  654. return;
  655. }
  656. intern = Z_SPLDLLIST_P(getThis());
  657. if (Z_TYPE_P(zindex) == IS_NULL) {
  658. /* $obj[] = ... */
  659. spl_ptr_llist_push(intern->llist, value);
  660. } else {
  661. /* $obj[$foo] = ... */
  662. zend_long index;
  663. spl_ptr_llist_element *element;
  664. index = spl_offset_convert_to_long(zindex);
  665. if (index < 0 || index >= intern->llist->count) {
  666. zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
  667. return;
  668. }
  669. element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
  670. if (element != NULL) {
  671. /* call dtor on the old element as in spl_ptr_llist_pop */
  672. if (intern->llist->dtor) {
  673. intern->llist->dtor(element);
  674. }
  675. /* the element is replaced, delref the old one as in
  676. * SplDoublyLinkedList::pop() */
  677. zval_ptr_dtor(&element->data);
  678. ZVAL_COPY_VALUE(&element->data, value);
  679. /* new element, call ctor as in spl_ptr_llist_push */
  680. if (intern->llist->ctor) {
  681. intern->llist->ctor(element);
  682. }
  683. } else {
  684. zval_ptr_dtor(value);
  685. zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
  686. return;
  687. }
  688. }
  689. } /* }}} */
  690. /* {{{ proto void SplDoublyLinkedList::offsetUnset(mixed index)
  691. Unsets the value at the specified $index. */
  692. SPL_METHOD(SplDoublyLinkedList, offsetUnset)
  693. {
  694. zval *zindex;
  695. zend_long index;
  696. spl_dllist_object *intern;
  697. spl_ptr_llist_element *element;
  698. spl_ptr_llist *llist;
  699. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zindex) == FAILURE) {
  700. return;
  701. }
  702. intern = Z_SPLDLLIST_P(getThis());
  703. index = spl_offset_convert_to_long(zindex);
  704. llist = intern->llist;
  705. if (index < 0 || index >= intern->llist->count) {
  706. zend_throw_exception(spl_ce_OutOfRangeException, "Offset out of range", 0);
  707. return;
  708. }
  709. element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
  710. if (element != NULL) {
  711. /* connect the neightbors */
  712. if (element->prev) {
  713. element->prev->next = element->next;
  714. }
  715. if (element->next) {
  716. element->next->prev = element->prev;
  717. }
  718. /* take care of head/tail */
  719. if (element == llist->head) {
  720. llist->head = element->next;
  721. }
  722. if (element == llist->tail) {
  723. llist->tail = element->prev;
  724. }
  725. /* finally, delete the element */
  726. llist->count--;
  727. if(llist->dtor) {
  728. llist->dtor(element);
  729. }
  730. if (intern->traverse_pointer == element) {
  731. SPL_LLIST_DELREF(element);
  732. intern->traverse_pointer = NULL;
  733. }
  734. zval_ptr_dtor(&element->data);
  735. ZVAL_UNDEF(&element->data);
  736. SPL_LLIST_DELREF(element);
  737. } else {
  738. zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0);
  739. return;
  740. }
  741. } /* }}} */
  742. static void spl_dllist_it_dtor(zend_object_iterator *iter) /* {{{ */
  743. {
  744. spl_dllist_it *iterator = (spl_dllist_it *)iter;
  745. SPL_LLIST_CHECK_DELREF(iterator->traverse_pointer);
  746. zend_user_it_invalidate_current(iter);
  747. zval_ptr_dtor(&iterator->intern.it.data);
  748. }
  749. /* }}} */
  750. static void spl_dllist_it_helper_rewind(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */
  751. {
  752. SPL_LLIST_CHECK_DELREF(*traverse_pointer_ptr);
  753. if (flags & SPL_DLLIST_IT_LIFO) {
  754. *traverse_position_ptr = llist->count-1;
  755. *traverse_pointer_ptr = llist->tail;
  756. } else {
  757. *traverse_position_ptr = 0;
  758. *traverse_pointer_ptr = llist->head;
  759. }
  760. SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
  761. }
  762. /* }}} */
  763. static void spl_dllist_it_helper_move_forward(spl_ptr_llist_element **traverse_pointer_ptr, int *traverse_position_ptr, spl_ptr_llist *llist, int flags) /* {{{ */
  764. {
  765. if (*traverse_pointer_ptr) {
  766. spl_ptr_llist_element *old = *traverse_pointer_ptr;
  767. if (flags & SPL_DLLIST_IT_LIFO) {
  768. *traverse_pointer_ptr = old->prev;
  769. (*traverse_position_ptr)--;
  770. if (flags & SPL_DLLIST_IT_DELETE) {
  771. zval prev;
  772. spl_ptr_llist_pop(llist, &prev);
  773. zval_ptr_dtor(&prev);
  774. }
  775. } else {
  776. *traverse_pointer_ptr = old->next;
  777. if (flags & SPL_DLLIST_IT_DELETE) {
  778. zval prev;
  779. spl_ptr_llist_shift(llist, &prev);
  780. zval_ptr_dtor(&prev);
  781. } else {
  782. (*traverse_position_ptr)++;
  783. }
  784. }
  785. SPL_LLIST_DELREF(old);
  786. SPL_LLIST_CHECK_ADDREF(*traverse_pointer_ptr);
  787. }
  788. }
  789. /* }}} */
  790. static void spl_dllist_it_rewind(zend_object_iterator *iter) /* {{{ */
  791. {
  792. spl_dllist_it *iterator = (spl_dllist_it *)iter;
  793. spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
  794. spl_ptr_llist *llist = object->llist;
  795. spl_dllist_it_helper_rewind(&iterator->traverse_pointer, &iterator->traverse_position, llist, object->flags);
  796. }
  797. /* }}} */
  798. static int spl_dllist_it_valid(zend_object_iterator *iter) /* {{{ */
  799. {
  800. spl_dllist_it *iterator = (spl_dllist_it *)iter;
  801. spl_ptr_llist_element *element = iterator->traverse_pointer;
  802. return (element != NULL ? SUCCESS : FAILURE);
  803. }
  804. /* }}} */
  805. static zval *spl_dllist_it_get_current_data(zend_object_iterator *iter) /* {{{ */
  806. {
  807. spl_dllist_it *iterator = (spl_dllist_it *)iter;
  808. spl_ptr_llist_element *element = iterator->traverse_pointer;
  809. if (element == NULL || Z_ISUNDEF(element->data)) {
  810. return NULL;
  811. }
  812. return &element->data;
  813. }
  814. /* }}} */
  815. static void spl_dllist_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
  816. {
  817. spl_dllist_it *iterator = (spl_dllist_it *)iter;
  818. ZVAL_LONG(key, iterator->traverse_position);
  819. }
  820. /* }}} */
  821. static void spl_dllist_it_move_forward(zend_object_iterator *iter) /* {{{ */
  822. {
  823. spl_dllist_it *iterator = (spl_dllist_it *)iter;
  824. spl_dllist_object *object = Z_SPLDLLIST_P(&iter->data);
  825. zend_user_it_invalidate_current(iter);
  826. spl_dllist_it_helper_move_forward(&iterator->traverse_pointer, &iterator->traverse_position, object->llist, object->flags);
  827. }
  828. /* }}} */
  829. /* {{{ proto int SplDoublyLinkedList::key()
  830. Return current array key */
  831. SPL_METHOD(SplDoublyLinkedList, key)
  832. {
  833. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  834. if (zend_parse_parameters_none() == FAILURE) {
  835. return;
  836. }
  837. RETURN_LONG(intern->traverse_position);
  838. }
  839. /* }}} */
  840. /* {{{ proto void SplDoublyLinkedList::prev()
  841. Move to next entry */
  842. SPL_METHOD(SplDoublyLinkedList, prev)
  843. {
  844. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  845. if (zend_parse_parameters_none() == FAILURE) {
  846. return;
  847. }
  848. spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags ^ SPL_DLLIST_IT_LIFO);
  849. }
  850. /* }}} */
  851. /* {{{ proto void SplDoublyLinkedList::next()
  852. Move to next entry */
  853. SPL_METHOD(SplDoublyLinkedList, next)
  854. {
  855. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  856. if (zend_parse_parameters_none() == FAILURE) {
  857. return;
  858. }
  859. spl_dllist_it_helper_move_forward(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
  860. }
  861. /* }}} */
  862. /* {{{ proto bool SplDoublyLinkedList::valid()
  863. Check whether the datastructure contains more entries */
  864. SPL_METHOD(SplDoublyLinkedList, valid)
  865. {
  866. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  867. if (zend_parse_parameters_none() == FAILURE) {
  868. return;
  869. }
  870. RETURN_BOOL(intern->traverse_pointer != NULL);
  871. }
  872. /* }}} */
  873. /* {{{ proto void SplDoublyLinkedList::rewind()
  874. Rewind the datastructure back to the start */
  875. SPL_METHOD(SplDoublyLinkedList, rewind)
  876. {
  877. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  878. if (zend_parse_parameters_none() == FAILURE) {
  879. return;
  880. }
  881. spl_dllist_it_helper_rewind(&intern->traverse_pointer, &intern->traverse_position, intern->llist, intern->flags);
  882. }
  883. /* }}} */
  884. /* {{{ proto mixed|NULL SplDoublyLinkedList::current()
  885. Return current datastructure entry */
  886. SPL_METHOD(SplDoublyLinkedList, current)
  887. {
  888. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  889. spl_ptr_llist_element *element = intern->traverse_pointer;
  890. if (zend_parse_parameters_none() == FAILURE) {
  891. return;
  892. }
  893. if (element == NULL || Z_ISUNDEF(element->data)) {
  894. RETURN_NULL();
  895. } else {
  896. zval *value = &element->data;
  897. ZVAL_COPY_DEREF(return_value, value);
  898. }
  899. }
  900. /* }}} */
  901. /* {{{ proto string SplDoublyLinkedList::serialize()
  902. Serializes storage */
  903. SPL_METHOD(SplDoublyLinkedList, serialize)
  904. {
  905. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  906. smart_str buf = {0};
  907. spl_ptr_llist_element *current = intern->llist->head, *next;
  908. zval flags;
  909. php_serialize_data_t var_hash;
  910. if (zend_parse_parameters_none() == FAILURE) {
  911. return;
  912. }
  913. PHP_VAR_SERIALIZE_INIT(var_hash);
  914. /* flags */
  915. ZVAL_LONG(&flags, intern->flags);
  916. php_var_serialize(&buf, &flags, &var_hash);
  917. /* elements */
  918. while (current) {
  919. smart_str_appendc(&buf, ':');
  920. next = current->next;
  921. php_var_serialize(&buf, &current->data, &var_hash);
  922. current = next;
  923. }
  924. smart_str_0(&buf);
  925. /* done */
  926. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  927. if (buf.s) {
  928. RETURN_NEW_STR(buf.s);
  929. } else {
  930. RETURN_NULL();
  931. }
  932. } /* }}} */
  933. /* {{{ proto void SplDoublyLinkedList::unserialize(string serialized)
  934. Unserializes storage */
  935. SPL_METHOD(SplDoublyLinkedList, unserialize)
  936. {
  937. spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
  938. zval *flags, *elem;
  939. char *buf;
  940. size_t buf_len;
  941. const unsigned char *p, *s;
  942. php_unserialize_data_t var_hash;
  943. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
  944. return;
  945. }
  946. if (buf_len == 0) {
  947. return;
  948. }
  949. while (intern->llist->count > 0) {
  950. zval tmp;
  951. spl_ptr_llist_pop(intern->llist, &tmp);
  952. zval_ptr_dtor(&tmp);
  953. }
  954. s = p = (const unsigned char*)buf;
  955. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  956. /* flags */
  957. flags = var_tmp_var(&var_hash);
  958. if (!php_var_unserialize(flags, &p, s + buf_len, &var_hash) || Z_TYPE_P(flags) != IS_LONG) {
  959. goto error;
  960. }
  961. intern->flags = (int)Z_LVAL_P(flags);
  962. /* elements */
  963. while(*p == ':') {
  964. ++p;
  965. elem = var_tmp_var(&var_hash);
  966. if (!php_var_unserialize(elem, &p, s + buf_len, &var_hash)) {
  967. goto error;
  968. }
  969. var_push_dtor(&var_hash, elem);
  970. spl_ptr_llist_push(intern->llist, elem);
  971. }
  972. if (*p != '\0') {
  973. goto error;
  974. }
  975. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  976. return;
  977. error:
  978. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  979. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
  980. return;
  981. } /* }}} */
  982. /* {{{ proto void SplDoublyLinkedList::add(mixed index, mixed newval)
  983. Inserts a new entry before the specified $index consisting of $newval. */
  984. SPL_METHOD(SplDoublyLinkedList, add)
  985. {
  986. zval *zindex, *value;
  987. spl_dllist_object *intern;
  988. spl_ptr_llist_element *element;
  989. zend_long index;
  990. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &zindex, &value) == FAILURE) {
  991. return;
  992. }
  993. intern = Z_SPLDLLIST_P(getThis());
  994. index = spl_offset_convert_to_long(zindex);
  995. if (index < 0 || index > intern->llist->count) {
  996. zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid or out of range", 0);
  997. return;
  998. }
  999. Z_TRY_ADDREF_P(value);
  1000. if (index == intern->llist->count) {
  1001. /* If index is the last entry+1 then we do a push because we're not inserting before any entry */
  1002. spl_ptr_llist_push(intern->llist, value);
  1003. } else {
  1004. /* Create the new element we want to insert */
  1005. spl_ptr_llist_element *elem = emalloc(sizeof(spl_ptr_llist_element));
  1006. /* Get the element we want to insert before */
  1007. element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
  1008. ZVAL_COPY_VALUE(&elem->data, value);
  1009. elem->rc = 1;
  1010. /* connect to the neighbours */
  1011. elem->next = element;
  1012. elem->prev = element->prev;
  1013. /* connect the neighbours to this new element */
  1014. if (elem->prev == NULL) {
  1015. intern->llist->head = elem;
  1016. } else {
  1017. element->prev->next = elem;
  1018. }
  1019. element->prev = elem;
  1020. intern->llist->count++;
  1021. if (intern->llist->ctor) {
  1022. intern->llist->ctor(elem);
  1023. }
  1024. }
  1025. } /* }}} */
  1026. /* {{{ iterator handler table */
  1027. static const zend_object_iterator_funcs spl_dllist_it_funcs = {
  1028. spl_dllist_it_dtor,
  1029. spl_dllist_it_valid,
  1030. spl_dllist_it_get_current_data,
  1031. spl_dllist_it_get_current_key,
  1032. spl_dllist_it_move_forward,
  1033. spl_dllist_it_rewind,
  1034. NULL
  1035. }; /* }}} */
  1036. zend_object_iterator *spl_dllist_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  1037. {
  1038. spl_dllist_it *iterator;
  1039. spl_dllist_object *dllist_object = Z_SPLDLLIST_P(object);
  1040. if (by_ref) {
  1041. zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
  1042. return NULL;
  1043. }
  1044. iterator = emalloc(sizeof(spl_dllist_it));
  1045. zend_iterator_init((zend_object_iterator*)iterator);
  1046. ZVAL_COPY(&iterator->intern.it.data, object);
  1047. iterator->intern.it.funcs = &spl_dllist_it_funcs;
  1048. iterator->intern.ce = ce;
  1049. iterator->traverse_position = dllist_object->traverse_position;
  1050. iterator->traverse_pointer = dllist_object->traverse_pointer;
  1051. iterator->flags = dllist_object->flags & SPL_DLLIST_IT_MASK;
  1052. ZVAL_UNDEF(&iterator->intern.value);
  1053. SPL_LLIST_CHECK_ADDREF(iterator->traverse_pointer);
  1054. return &iterator->intern.it;
  1055. }
  1056. /* }}} */
  1057. /* Function/Class/Method definitions */
  1058. ZEND_BEGIN_ARG_INFO(arginfo_dllist_setiteratormode, 0)
  1059. ZEND_ARG_INFO(0, flags)
  1060. ZEND_END_ARG_INFO()
  1061. ZEND_BEGIN_ARG_INFO(arginfo_dllist_push, 0)
  1062. ZEND_ARG_INFO(0, value)
  1063. ZEND_END_ARG_INFO()
  1064. ZEND_BEGIN_ARG_INFO_EX(arginfo_dllist_offsetGet, 0, 0, 1)
  1065. ZEND_ARG_INFO(0, index)
  1066. ZEND_END_ARG_INFO()
  1067. ZEND_BEGIN_ARG_INFO_EX(arginfo_dllist_offsetSet, 0, 0, 2)
  1068. ZEND_ARG_INFO(0, index)
  1069. ZEND_ARG_INFO(0, newval)
  1070. ZEND_END_ARG_INFO()
  1071. ZEND_BEGIN_ARG_INFO(arginfo_dllist_void, 0)
  1072. ZEND_END_ARG_INFO()
  1073. ZEND_BEGIN_ARG_INFO(arginfo_dllist_serialized, 0)
  1074. ZEND_ARG_INFO(0, serialized)
  1075. ZEND_END_ARG_INFO();
  1076. static const zend_function_entry spl_funcs_SplQueue[] = {
  1077. SPL_MA(SplQueue, enqueue, SplDoublyLinkedList, push, arginfo_dllist_push, ZEND_ACC_PUBLIC)
  1078. SPL_MA(SplQueue, dequeue, SplDoublyLinkedList, shift, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1079. PHP_FE_END
  1080. };
  1081. static const zend_function_entry spl_funcs_SplDoublyLinkedList[] = {
  1082. SPL_ME(SplDoublyLinkedList, pop, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1083. SPL_ME(SplDoublyLinkedList, shift, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1084. SPL_ME(SplDoublyLinkedList, push, arginfo_dllist_push, ZEND_ACC_PUBLIC)
  1085. SPL_ME(SplDoublyLinkedList, unshift, arginfo_dllist_push, ZEND_ACC_PUBLIC)
  1086. SPL_ME(SplDoublyLinkedList, top, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1087. SPL_ME(SplDoublyLinkedList, bottom, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1088. SPL_ME(SplDoublyLinkedList, isEmpty, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1089. SPL_ME(SplDoublyLinkedList, setIteratorMode, arginfo_dllist_setiteratormode, ZEND_ACC_PUBLIC)
  1090. SPL_ME(SplDoublyLinkedList, getIteratorMode, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1091. /* Countable */
  1092. SPL_ME(SplDoublyLinkedList, count, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1093. /* ArrayAccess */
  1094. SPL_ME(SplDoublyLinkedList, offsetExists, arginfo_dllist_offsetGet, ZEND_ACC_PUBLIC)
  1095. SPL_ME(SplDoublyLinkedList, offsetGet, arginfo_dllist_offsetGet, ZEND_ACC_PUBLIC)
  1096. SPL_ME(SplDoublyLinkedList, offsetSet, arginfo_dllist_offsetSet, ZEND_ACC_PUBLIC)
  1097. SPL_ME(SplDoublyLinkedList, offsetUnset, arginfo_dllist_offsetGet, ZEND_ACC_PUBLIC)
  1098. SPL_ME(SplDoublyLinkedList, add, arginfo_dllist_offsetSet, ZEND_ACC_PUBLIC)
  1099. /* Iterator */
  1100. SPL_ME(SplDoublyLinkedList, rewind, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1101. SPL_ME(SplDoublyLinkedList, current, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1102. SPL_ME(SplDoublyLinkedList, key, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1103. SPL_ME(SplDoublyLinkedList, next, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1104. SPL_ME(SplDoublyLinkedList, prev, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1105. SPL_ME(SplDoublyLinkedList, valid, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1106. /* Serializable */
  1107. SPL_ME(SplDoublyLinkedList, unserialize, arginfo_dllist_serialized, ZEND_ACC_PUBLIC)
  1108. SPL_ME(SplDoublyLinkedList, serialize, arginfo_dllist_void, ZEND_ACC_PUBLIC)
  1109. PHP_FE_END
  1110. };
  1111. /* }}} */
  1112. PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */
  1113. {
  1114. REGISTER_SPL_STD_CLASS_EX(SplDoublyLinkedList, spl_dllist_object_new, spl_funcs_SplDoublyLinkedList);
  1115. memcpy(&spl_handler_SplDoublyLinkedList, &std_object_handlers, sizeof(zend_object_handlers));
  1116. spl_handler_SplDoublyLinkedList.offset = XtOffsetOf(spl_dllist_object, std);
  1117. spl_handler_SplDoublyLinkedList.clone_obj = spl_dllist_object_clone;
  1118. spl_handler_SplDoublyLinkedList.count_elements = spl_dllist_object_count_elements;
  1119. spl_handler_SplDoublyLinkedList.get_debug_info = spl_dllist_object_get_debug_info;
  1120. spl_handler_SplDoublyLinkedList.get_gc = spl_dllist_object_get_gc;
  1121. spl_handler_SplDoublyLinkedList.dtor_obj = zend_objects_destroy_object;
  1122. spl_handler_SplDoublyLinkedList.free_obj = spl_dllist_object_free_storage;
  1123. REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_LIFO", SPL_DLLIST_IT_LIFO);
  1124. REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_FIFO", 0);
  1125. REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_DELETE",SPL_DLLIST_IT_DELETE);
  1126. REGISTER_SPL_CLASS_CONST_LONG(SplDoublyLinkedList, "IT_MODE_KEEP", 0);
  1127. REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Iterator);
  1128. REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Countable);
  1129. REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, ArrayAccess);
  1130. REGISTER_SPL_IMPLEMENTS(SplDoublyLinkedList, Serializable);
  1131. spl_ce_SplDoublyLinkedList->get_iterator = spl_dllist_get_iterator;
  1132. REGISTER_SPL_SUB_CLASS_EX(SplQueue, SplDoublyLinkedList, spl_dllist_object_new, spl_funcs_SplQueue);
  1133. REGISTER_SPL_SUB_CLASS_EX(SplStack, SplDoublyLinkedList, spl_dllist_object_new, NULL);
  1134. spl_ce_SplQueue->get_iterator = spl_dllist_get_iterator;
  1135. spl_ce_SplStack->get_iterator = spl_dllist_get_iterator;
  1136. return SUCCESS;
  1137. }
  1138. /* }}} */
  1139. /*
  1140. * Local variables:
  1141. * tab-width: 4
  1142. * c-basic-offset: 4
  1143. * End:
  1144. * vim600: fdm=marker
  1145. * vim: noet sw=4 ts=4
  1146. */