I am happy to see some interest in this discussion, I'll try to give more details in the coming days.

To clarify, my first example should be:

$setA = new Set();
$setA->append('a');
$setA->append('a');

$setB = new Set();
$setB->append('a'); // 'a' instead of 'b'

$setA == $setB;

Cheers,
Victor

On 12/18/2012 07:32 PM, Anthony Ferrara wrote:
Guys,

PHP arrays are a great one-size-fits-all data structure. But like all one-size-fits-all <anything>, jack-of-all-trades usually means master of none.

There are definitely benefits to using specific data structures if implemented correctly under the hood.

A good example of this is a Queue. Implemented properly using a Singly-Linked-List or Doubly-Linked-List, inserts are O(1), peeks are O(1) and removals are O(1). Compare that to an array based queue implementation where inserts are O(1) and peeks are O(1) but removals are O(n)... (or inserts and removals are reversed).

For low volume logic, the array can substitute for more custom data structures quite easily. But for needs with more data (or more transactions), there's no replacement for a proper data structure...



To the original question:

I would love to see not just more data structures, but better designed ones in core.

For example, with SPLStack http://php.net/manual/en/class.splstack.php
1. Why the heck does it have unshift() as well as push()? A stack is a One-way-in, one-way-out data structure, why are both exposed? 2. Why the heck does it implement ArrayAccess? Again, completely defeats the point of a stack 3. Why does it *extend* DLL? A Stack can be implemented with a DLL, but it is *not* a DLL. Huge difference...

Now, there was some discussion by some of us about re-doing the entire spl data structures a while ago. This git repo (PHP) is the evolution of that idea. https://github.com/morrisonlevi/Ardent

Now it's not written with the explicit intent of replacing SPL. It's written in an attempt to try to get the data structures designed right. Then, it may be ported to PECL, and then finally may be proposed to core.


As far as MAP, we already have one: http://php.net/manual/en/class.splobjectstorage.php
If you give a closer look to my example, you will notice a difference: $map[$setA] and $map[$setB] point to the same storage which I think is not possible with SPLObjectStorage.

My $0.02 at least,

Anthony


Reply via email to