rfc:arrayiterator-improvements

This is an old revision of the document!


ArrayIterator improvements for PHP's (linked) hash tables

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, ArrayIterator::current() would return the value associated with the specified $key, ArrayIterator::key() will return $key and so on. If the $key doesn't exist the method would return false and Iterator would keep its current state, otherwise the method would return true.

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 to reach the specified position (or key, assuming the array is a list). Instead, the proposed method ArrayIterator::seekKey($key) is 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? Requires a 50% + 1 majority.

*votes*

Accept ArrayIterator::prev():void? Requires a 50% + 1 majority.

*votes*

Patches and Tests

None yet.

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