php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #14918 Need function to explicitly set a hash's internal pointer to a certain key pos
Submitted: 2002-01-07 16:32 UTC Modified: 2015-02-17 09:31 UTC
Votes:4
Avg. Score:4.5 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: paule at cs dot tamu dot edu Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: paule at cs dot tamu dot edu
New email:
PHP Version: OS:

 

 [2002-01-07 16:32 UTC] paule at cs dot tamu dot edu
I have found it useful from time to time to have a function that will set the internal pointer of an array to a particular key position. I therefore propose the addition of the following function:

mixed setpos(array array, mixed keyval)

The function merely sets the internal pointer of the array to the element referenced by keyval, and returns the value of the element at that position.

I have included what I believe is source code that should add the functionality, though it is untested. I have included only the actual C code, but I think the header file lines to be added are pretty obvious. 

_______________________________________

ADDITIONS TO /EXT/STANDARD/ARRAY.C:
-----------------------------------

/* {{{ proto mixed setpos(array array_arg, mixed keyval)
   Set array argument's internal pointer to the element indexed by keyval and return it */	
PHP_FUNCTION(setpos)
{
	pval **array, **entry;
	HashTable *target_hash;

	if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &array) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	target_hash = HASH_OF(*array);
	if (!target_hash) {
		php_error(E_WARNING, "Variable passed to setpos() is not an array or object");
		RETURN_FALSE;
	}
	if(Z_TYPE_PP(keyval) == IS_LONG)
		zend_hash_internal_pointer_index_set(target_hash, keyval);
	else if(Z_TYPE_PP(keyval) == IS_STRING)
		zend_hash_internal_pointer_set(target_hash, keyval, Z_STRLEN_PP(keyval)+1);
	else {
		php_error(E_WARNING, "Can only have STRING and INTEGER keys!");
	}

	if (return_value_used) {	
		if (zend_hash_get_current_data(target_hash, (void **) &entry) == FAILURE) {
			RETURN_FALSE;
		}

		*return_value = **entry;
		zval_copy_ctor(return_value);
	}
}
/* }}} */

________________________________________

ADDITIONS TO /ZEND/ZEND_HASH.C:
-------------------------------


/* Returns SUCCESS if found and FAILURE if not. If the key is found, the internal pointer is set to point at that element. */
ZEND_API int zend_internal_pointer_set(HashTable *ht, char *arKey, uint nKeyLength)
{
	ulong h;
	uint nIndex;
	Bucket *p;

	IS_CONSISTENT(ht);

	HANDLE_NUMERIC(arKey, nKeyLength, zend_internal_pointer_index_set(ht, idx));

	h = ht->pHashFunction(arKey, nKeyLength);
	nIndex = h % ht->nTableSize;

	p = ht->arBuckets[nIndex];
	while (p != NULL) {
		if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
			if (!memcmp(p->arKey, arKey, nKeyLength)) {
				ht->pInternalPointer = p;
				return SUCCESS;
			}
		}
		p = p->pNext;
	}
	return FAILURE;
}


ZEND_API int zend_internal_pointer_index_set(HashTable *ht, ulong h)
{
	uint nIndex;
	Bucket *p;

	IS_CONSISTENT(ht);

	nIndex = h % ht->nTableSize;

	p = ht->arBuckets[nIndex];
	while (p != NULL) {
		if ((p->h == h) && (p->nKeyLength == 0)) {
			ht->pInternalPointer = p;
			return SUCCESS;
		}
		p = p->pNext;
	}
	return FAILURE;
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-29 12:05 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: All +Operating System: * -PHP Version: 4.1.1 +PHP Version: *
 [2015-02-17 09:31 UTC] krakjoe@php.net
-Status: Open +Status: Wont fix
 [2015-02-17 09:31 UTC] krakjoe@php.net
I'm not sure if this is a good idea or not.

This kind of feature requires an RFC today, I'm therefore going to mark the bug as won't fix.

This report can still be found and could be referenced by any future RFC.

Sorry about the wait.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC