Re: hash table guidance?

From: Date: Wed, 02 Nov 2011 19:12:05 +0000
Subject: Re: hash table guidance?
References: 1 2  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-8795@lists.php.net to get a copy of this message
Thanks, Gustavo. I've purchased a copy of Sara's book but it will take awhile to get here. I appreciate your input about "1" versus the integer 1 as an array key. Given that I will be serializing data from userspace, I expect the conversions will have already been applied. For unserialization of incoming data (most likely from Adobe Flash Player), I will likely preserve precisely what has been serialized -- at least initially. I therefore don't expect in the short term to do any type conversions at all for array or object keys. Please let me know if you think this is a bad idea. As for the detection of previously encountered objects/arrays, I am specifically talking about identity here rather than equality. As I understand it, the AMF3 checks for identity for two reasons: 1) To handle recursive data structures properly -- something the current version of AMFEXT does not do. 2) To reduce the size of serialized data. The way that AMF3 is supposed to work is that the first time an array/object is encountered during serialization, the array's contents are serialized at length and a reference to the array/object is stored in an "object table." If the same array or object is encountered again during the serialization of single data structure -- e.g., through recursion or as a distinct element in an array, etc. -- then rather than serializing the object's contents over again (possibly resulting in infinite recursion) the object is serialized as a reference. I.e., the serialized data is just a short marker to say "this is a reference" and then a number to describe which previously serialized object we are referring to. Based on this behavior, it is my belief that I only need to check for identity and not equality. I therefore hope that a simple pointer check will suffice when preparing to serialize an array or object. I noticed when writing my serializer function in PHP (see original message) that the strict equality operator (===) returns true for two arrays if they have the same keys and the same values: if ($arr1 === $arr2) // returns TRUE if two distinct arrays have the same keys and the same values I therefore chose to fully serialize all arrays encountered rather than storing the first instance in an object table and storing a reference for all subsequent instances of the array. Although this is not the prescribed behavior of AMF3, it results in tolerable behavior in practice. Still struggling a bit with zend_hash. I am by no means a master of C/C++. At the moment, I'm using the json source as my guide and puzzling over why the json_encode function, php_json_encode, receives parameter *val and then the address of this parameter is passed to json_encode_array which has parameter **val. http://lxr.php.net/xref/PHP_5_3/ext/json/json.c#432 http://lxr.php.net/xref/PHP_5_3/ext/json/json.c#json_encode_array I'm really rusty on the user of pointers and references, but suspect that we send the address of the value to be serialized to json_encode_array so that we can treat it as a complex data type and then declare it with a double-dereference to satisfy compiler constraints. What I'm wondering is how to check for identity in my own amf_encode_array function which mirrors json_encode_array. I'm thinking that the parameter "val" inside that function is the address of an array and that I would simply need to check if val had been encountered before rather than &val or *val. On 11/2/2011 10:41 AM, Gustavo Lopes wrote:
Em Wed, 02 Nov 2011 17:31:28 -0000, J. Adams <zardozrocks@gmail.com> escreveu:
I'm in the process of updating the AMFEXT extension. Specifically, I'm trying to serialize objects and arrays. I'm using the JSON extension as my guide and I'm a bit confused about how to handle PHP's hash table object. Can anyone direct me to a primer or introduction that explains how to deal with arrays/objects/hashtables?
You can view the API in zend_hash.h: http://lxr.php.net/opengrok/xref/PHP_TRUNK/Zend/zend_hash.h If you want a high-level review and details of the most important functions (without having to go through the code), you can read Sara's book: http://www.amazon.com/Extending-Embedding-PHP-Sara-Golemon/dp/067232704X/ref=sr_1_1?s=books&ie=UTF8&qid=1320255383&sr=1-1
I would also appreciate recommendations or advice on how to accomplish the following: * determine if array keys are all numeric and contiguous or, if the array/object has associative keys or non-contiguous, detect that condition.
No easy way, you have to go through the all the elements in the hash table in order to determine that. Also note that while for a hash table the keys 1 and "1" (string) are different things, userspace arrays are manipulated to interfaces that convert numeric strings to numbers when possible.
* detect if the current value to be serialized has been encountered before during the serialization of a particular object -- e.g., if a single array array or object appears more than once in a particular object via recursion or by referenced. I suspect this requires me to check the pointer fed to my function, but am unsure whether to dereference the pointer.
For arrays there's no easy way; there's no concept of "same array". They can be or not be equal, but the identify the pointer comparison would give you would not be reliable since arrays, like other zvals, can be separated at any time. For objects, you can compare the ids (if you're interesting in identity, not equality).
[...]


« previous php.pecl.dev (#8795) next »