Hello

On 2.1.2010 21:36, Kalle Sommer Nielsen wrote:
How about just array_pos() if any. The remaining functions can easily
be implemented in userland like:

function array_pos_isfirst(Array&$array) {
  return array_pos($array) == 0;
}

function array_pos_islast(Array&$array) {
  return array_pos($array) == (sizeof($array) - 1);
}

I don't think it should be needed to add array_pos_isvalid(), since it
*shouldn't* be possible to point to an invalid position. Rather
"isset($array['key'])" should be used to check if a position is valid.

Well, the internal Zend Hash doesn't know it's position as index, getting to know if the internal pointer is on the last or first entry is much easier as both ends are defined by ht->pListHead and ht->pListTail.

Something like:

int zend_hash_pointer_is_head(HashTable *ht, HashPosition *pos) {
Bucket *p;
p = pos ? (*pos) : ht->pInternalPointer;
if (p == ht->pListHead) return 1;
return 0;
}

You really don't want to return memory pointers to user code so you would need to traverse the array to get some kind of index number.

array_pos_isvalid would be mapped to zend_hash_has_more_elements_ex, that is defined by checking zend_hash_get_current_key_type_ex returning HASH_KEY_NON_EXISTANT. Internally it means really check the pointer for null, so you could call it array_pos_isundefined too.

Generally you can use key($array) === null with the same effect. so this would only be for interface completeness and needs to move less data around then a key() call.

Best regards
Oskar Eisemuth

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to