Skip to content

Commit 50ac0e1

Browse files
committed
Add CachedIterable (rewindable, allows any key&repeating keys)
1. This eagerly evaluates any iterable, and stores a read only copy of the keys and values. 2. This allows using any value as a key, and allows repeating keys. For example, this can be used to store the exact result of keys and values yielded by a generator. Longer-term, this may be useful in creating internal helper functions/methods such as `someprefix_map(iterable $iterable): CachedIterable`, `someprefix_flip(iterable $iterable): CachedIterable`, ```php function my_generator() { yield '123' => 'numeric string is preserved'; yield '123' => 'allows repeating keys'; yield new stdClass() => 'false'; } $iterable = new CachedIterable(my_generator()); foreach ($iterable as $key1 => $value1) { foreach ($iterable as $key2 => $value2) { ... } } ``` ```php final class CachedIterable implements IteratorAggregate, Countable { public function __construct(iterable $iterator) {} public function getIterator(): InternalIterator {} public function count(): int {} public static function fromPairs(array $pairs): CachedIterable; public function __serialize(): array {} // [$k1, $v1, $k2, $v2,...] public function __unserialize(array $data): void {} // useful for converting iterables back to values public function keys(): array {} public function values(): array {} // useful to efficiently get offsets at the middle/end of a long iterable public function keyAt(int $offset): mixed {} public function valueAt(int $offset): mixed {} // dynamic properties are forbidden } ```
1 parent 320843f commit 50ac0e1

18 files changed

+1275
-4
lines changed

ext/spl/config.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c, no,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
2-
PHP_INSTALL_HEADERS([ext/spl], [php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h])
1+
PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c spl_cachediterable.c, no,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
2+
PHP_INSTALL_HEADERS([ext/spl], [php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h spl_cachediterable.h])
33
PHP_ADD_EXTENSION_DEP(spl, pcre, true)
44
PHP_ADD_EXTENSION_DEP(spl, standard, true)

ext/spl/config.w32

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// vim:ft=javascript
22

3-
EXTENSION("spl", "php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c", false /*never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
3+
EXTENSION("spl", "php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c spl_cachediterable.c", false /*never shared */, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
44
PHP_SPL="yes";
5-
PHP_INSTALL_HEADERS("ext/spl", "php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h");
5+
PHP_INSTALL_HEADERS("ext/spl", "php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h spl_cachediterable.h");

ext/spl/php_spl.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "spl_observer.h"
3434
#include "spl_dllist.h"
3535
#include "spl_fixedarray.h"
36+
#include "spl_cachediterable.h"
3637
#include "spl_heap.h"
3738
#include "zend_exceptions.h"
3839
#include "zend_interfaces.h"
@@ -722,6 +723,7 @@ PHP_MINIT_FUNCTION(spl)
722723
PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
723724
PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
724725
PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
726+
PHP_MINIT(spl_cachediterable)(INIT_FUNC_ARGS_PASSTHRU);
725727
PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
726728

727729
return SUCCESS;

0 commit comments

Comments
 (0)