Since I was the one who started this discussion, I'd like to reply to
some of these points.

First off, let me say - as you pointed out, when the values are
unique, they are best represented
as keys... however, this of course applies only to value-types, which
isn't the problem, and not
why I started this conversation in the first place. Objects don't work
as keys. And objects do
not always have a value that you can use as keys.

I believe I understand the nature of arrays quite well - the
situations you covered in this e-mail
simply do not address the issues I was trying to tackle. I too have
used the key/value duplication
strategy you mentioned:

array("val1" => "val1", "val2" => "val2");

I stopped doing that many years ago - what's the benefit of storing
everything twice? At some
point I started doing this instead:

array("val1" => true, "val2" => true);

You're storing the same amount of information, but now there's no
confusion as far as which is
the key and which is the value - and for longer keys, you don't need
twice the memory. And when
modifying the array, you don't have to maintain both the key and the value.

Effectively this *is* a set, and as such works fine - but works only
for scalar values, not for objects.

> function create_set($arr) {
>   return array_combine($arr, $arr);
> }
> $set = create_set(array("val1","val2"));

This seems very misleading to me, as what comes out of create_set() is
not a set, but
just another array - which happens to contain a set. I get what you're
trying to do here,
but... it just doesn't work.

I don't think there's any "work-around" for not having sets, and I'm
not sure if that's what
you're suggesting either, but - there's no way you can educate anyone
out of that issue
if or when someone needs a set of objects that (per definition) do not
have unique keys.

You can implement a set of course, as a class - using array_search()
internally in the
class, you can check if a given value (or object) is present in the
set. I've done this on
one occasion, and it actually performs acceptably for a small (~1000)
set of objects, so
it's not that this problem represents a roadblock by any means. But it
is a solution with
obvious performance deficiencies and trade-offs...


> From: Adam Jon Richardson <adamj...@gmail.com>
> To: internals@lists.php.net
> Cc:
> Date: Thu, 4 Oct 2012 12:48:53 -0400
> Subject: Arrays which have properties of sets
> A while back, there was a thread discussing adding a specific function
> for removing elements from an array by value. Rasmus L. noted that
> when the values are unique, they would be better represented as keys:
> > The problem is that it is a rather unusual thing to do. I don't mean
> > removing an element, that is very common, but you are implying that you
> > know you don't have duplicates and you want a fast way to remove an
> > element by value in that case. To me that means you built your array
> > badly. If the values are unique then they should be the keys, or at
> > least a representation of the value should be the key such that you
> > don't need to scan the entire hash for the element when you need to
> > access it. And removal is just like any other access.
> When I come across situations where an array's properties match those
> of a set, I create array "sets" by duplicating keys and values:
> array("val1" => "val1", "val2" => "val2");
> This approach has several benefits:
> - Form the union of values using the "+" operator (which forms unions
> through use of keys)
> - Fast, easy removal of values through unset (e.g., unset($elements["val1"]))
> - Standard handling of values in for loops:
> for ($elements as $element) {
>   echo $element;
> }
> Writing a userland function to create array sets from
> numerically-indexed arrays (avoiding the duplication of typing the key
> and value) is indeed trivial:
> function create_set($arr) {
>   return array_combine($arr, $arr);
> }
> $set = create_set(array("val1","val2"));
> However, I find that the use of array sets is very common and helpful
> to me, and I see many situations where a developer would have been
> better served by leveraging the key to store the value. I suspect many
> PHP users don't understand the nature of PHP arrays. I'm wondering if
> adding a function (or language construct) for creating array sets
> would prove helpful (just because of the vast amount of code which
> could make use of it) and facilitate the education of developers so
> they might better leverage the benefits of the underlying hash map
> structure when their array is essentially a set and they wish to
> perform operations such as removing an item by value.
> Ada

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

Reply via email to