Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement Map iteration

  • Loading branch information...
commit 4fbd2ca0a45c0dc048f7d97bd401e85480219da8 1 parent 2d5ae49
Etienne Kneuss authored

Showing 2 changed files with 89 additions and 7 deletions. Show diff stats Hide diff stats

  1. +88 7 wr_weakmap.c
  2. +1 0  wr_weakmap.h
95 wr_weakmap.c
@@ -41,22 +41,20 @@ static void wr_weakmap_ref_dtor(void *ref_object, zend_object_handle ref_handle,
41 41
42 42 static void wr_weakmap_object_free_storage(void *object TSRMLS_DC) /* {{{ */
43 43 {
44   - HashPosition map_pos;
45 44 wr_weakmap_object *intern = (wr_weakmap_object *)object;
46 45
47   - zend_hash_internal_pointer_reset_ex(&intern->map, &map_pos);
48   - while(zend_hash_has_more_elements_ex(&intern->map, &map_pos) == SUCCESS) {
  46 + zend_hash_internal_pointer_reset_ex(&intern->map, &intern->pos);
  47 + while(zend_hash_has_more_elements_ex(&intern->map, &intern->pos) == SUCCESS) {
49 48 ulong index;
50   - zend_hash_get_current_key_ex(&intern->map, NULL, NULL, &index, 0, &map_pos);
  49 + zend_hash_get_current_key_ex(&intern->map, NULL, NULL, &index, 0, &intern->pos);
51 50
52 51 wr_store_detach(intern, (zend_object_handle)index TSRMLS_CC);
53 52
54   - zend_hash_move_forward_ex(&intern->map, &map_pos);
  53 + zend_hash_move_forward_ex(&intern->map, &intern->pos);
55 54 }
56 55
57 56 zend_object_std_dtor(&intern->std TSRMLS_CC);
58 57
59   -
60 58 zend_hash_destroy(&intern->map);
61 59 efree(intern);
62 60 }
@@ -427,6 +425,82 @@ PHP_METHOD(WeakMap, offsetUnset)
427 425
428 426 } /* }}} */
429 427
  428 +/* {{{ proto void WeakMap::rewind()
  429 + Rewind to first position */
  430 +PHP_METHOD(WeakMap, rewind)
  431 +{
  432 + wr_weakmap_object *intern = (wr_weakmap_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  433 +
  434 + if (zend_parse_parameters_none() == FAILURE) {
  435 + return;
  436 + }
  437 +
  438 + zend_hash_internal_pointer_reset_ex(&intern->map, &intern->pos);
  439 +} /* }}} */
  440 +
  441 +/* {{{ proto bool WeakMap::valid()
  442 + Returns whether current position is valid */
  443 +PHP_METHOD(WeakMap, valid)
  444 +{
  445 + wr_weakmap_object *intern = (wr_weakmap_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  446 +
  447 + if (zend_parse_parameters_none() == FAILURE) {
  448 + return;
  449 + }
  450 +
  451 + RETURN_BOOL(zend_hash_has_more_elements_ex(&intern->map, &intern->pos) == SUCCESS);
  452 +} /* }}} */
  453 +
  454 +/* {{{ proto mixed WeakMap::key()
  455 + Returns current key */
  456 +PHP_METHOD(WeakMap, key)
  457 +{
  458 + wr_weakmap_object *intern = (wr_weakmap_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  459 + ulong index;
  460 + zval **element;
  461 +
  462 + if (zend_parse_parameters_none() == FAILURE) {
  463 + return;
  464 + }
  465 +
  466 + zend_hash_get_current_key_ex(&intern->map, NULL, NULL, &index, 0, &intern->pos);
  467 +
  468 + if (zend_hash_get_current_data_ex(&intern->map, (void**)&element, &intern->pos) == FAILURE) {
  469 + return;
  470 + }
  471 + RETVAL_ZVAL(*element, 1, 0);
  472 +} /* }}} */
  473 +
  474 +/* {{{ proto mixed WeakMap::current()
  475 + Returns current element */
  476 +PHP_METHOD(WeakMap, current)
  477 +{
  478 + zval **element;
  479 + wr_weakmap_object *intern = (wr_weakmap_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  480 +
  481 + if (zend_parse_parameters_none() == FAILURE) {
  482 + return;
  483 + }
  484 +
  485 + if (zend_hash_get_current_data_ex(&intern->map, (void**)&element, &intern->pos) == FAILURE) {
  486 + return;
  487 + }
  488 + RETVAL_ZVAL(*element, 1, 0);
  489 +} /* }}} */
  490 +
  491 +/* {{{ proto void SplObjectStorage::next()
  492 + Moves position forward */
  493 +PHP_METHOD(WeakMap, next)
  494 +{
  495 + wr_weakmap_object *intern = (wr_weakmap_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  496 +
  497 + if (zend_parse_parameters_none() == FAILURE) {
  498 + return;
  499 + }
  500 +
  501 + zend_hash_move_forward_ex(&intern->map, &intern->pos);
  502 +} /* }}} */
  503 +
430 504 /* Function/Class/Method definitions */
431 505 ZEND_BEGIN_ARG_INFO(arginfo_wr_weakmap_void, 0)
432 506 ZEND_END_ARG_INFO()
@@ -443,10 +517,17 @@ ZEND_END_ARG_INFO()
443 517 static const zend_function_entry wr_funcs_WeakMap[] = {
444 518 PHP_ME(WeakMap, __construct, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
445 519 PHP_ME(WeakMap, count, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
  520 + /* ArrayAccess */
446 521 PHP_ME(WeakMap, offsetExists, arginfo_wr_weakmap_obj, ZEND_ACC_PUBLIC)
447 522 PHP_ME(WeakMap, offsetGet, arginfo_wr_weakmap_obj, ZEND_ACC_PUBLIC)
448 523 PHP_ME(WeakMap, offsetSet, arginfo_wr_weakmap_obj_val, ZEND_ACC_PUBLIC)
449 524 PHP_ME(WeakMap, offsetUnset, arginfo_wr_weakmap_obj, ZEND_ACC_PUBLIC)
  525 + /* Iterator */
  526 + PHP_ME(WeakMap, rewind, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
  527 + PHP_ME(WeakMap, valid, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
  528 + PHP_ME(WeakMap, key, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
  529 + PHP_ME(WeakMap, current, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
  530 + PHP_ME(WeakMap, next, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
450 531 {NULL, NULL, NULL}
451 532 };
452 533 /* }}} */
@@ -471,7 +552,7 @@ PHP_MINIT_FUNCTION(wr_weakmap) /* {{{ */
471 552 wr_handler_WeakMap.has_dimension = wr_weakmap_object_has_dimension;
472 553 wr_handler_WeakMap.count_elements = wr_weakmap_object_count_elements;
473 554
474   - zend_class_implements(wr_ce_WeakMap TSRMLS_CC, 2, spl_ce_Countable, spl_ce_ArrayAccess);
  555 + zend_class_implements(wr_ce_WeakMap TSRMLS_CC, 3, spl_ce_Countable, spl_ce_ArrayAccess, spl_ce_Iterator);
475 556
476 557 return SUCCESS;
477 558 }
1  wr_weakmap.h
@@ -26,6 +26,7 @@
26 26 typedef struct _wr_weakmap_object {
27 27 zend_object std;
28 28 HashTable map;
  29 + HashPosition pos;
29 30 zend_function *fptr_offset_get;
30 31 zend_function *fptr_offset_set;
31 32 zend_function *fptr_offset_has;

0 comments on commit 4fbd2ca

Please sign in to comment.
Something went wrong with that request. Please try again.