rfc:arrayiterator-improvements

This is an old revision of the document!


ArrayIterator improvements

Proposal

This RFC proposes the addition of a couple of functions that would expose to userland important features of PHP's (linked) hash-tables.

ArrayIterator::seekKey($key):bool

This method would move the Iterator's cursor to the Entry identified by the key $key.

After calling this method and assuming it returned true, ArrayIterator::current() would return the value associated with the specified $key, ArrayIterator::key() would return $key and so on. If the $key doesn't exist the method would instead return false and the ArrayIterator would not change its internal cursor.

The existing ArrayIterator::seek($position) searches for the Entry in a linear manner, and this is not ideal for many uses, as what it does is simply iterating over the array (or part of it) to reach the specified position (or key, assuming the array is a list). Instead, the proposed method ArrayIterator::seekKey($key) would be a O(1) operation as the cursor can immediately jump to the Entry identified by the given $key.

ArrayIterator::prev():void

This method would simply move the Iterator's cursor to the previous Entry in the hash table's linked list. Exactly like ArrayIterator::next(), it would never throw and it would always return null, but if there is no previous Entry, after calling it, an immediate call to the valid() method would return false.

Backward Incompatible Changes

None, for both of the proposed methods.

Proposed PHP Version(s)

PHP 7.2

Proposed Voting Choices

Accept ArrayIterator::seekKey($key):bool for PHP 7.2?

* vote 1 *

Accept ArrayIterator::prev():void for PHP 7.2?

* vote 2 *

Patches and Tests

None yet.

References

rfc/arrayiterator-improvements.1479837684.txt.gz · Last modified: 2017/09/22 13:28 (external edit)