January 17, 2010
Michal Minich wrote:
> On Sun, 17 Jan 2010 11:28:49 -0500, bearophile wrote:
>
>> In D the built in AAs require an equality test and/or cmp, plus opHash.

AAs require all three for classes: opEquals, opCmp, and toHash. Further, opEquals and opCmp must be consistent: when opEquals returns true, opCmp must return 0, and when opCmp returns non-zero, opEquals must return false.

toHash is optional for structs.

> Why should AAs require opCmp. What if object is not logically sortable,
> (like Guid, DbConnection, ...). Equality & Hash should be enough...
> Is the opCmp here to optimize lookup? Because opCmp may be quite slow,
> which may affect appending performance...

I don't know the answer, but the spec says so at

  http://digitalmars.com/d/2.0/arrays.html

<quote>
The implementation may use either opEquals or opCmp or both. Care should be taken so that the results of opEquals and opCmp are consistent with each other when the class objects are the same or not.
</quote>

Ali
January 17, 2010
Hello Michal,

> if one has double indexed aa, how to find that it contains value under
> keys 'a' and 123
> 
> float[int][char] aa;
> 
> aa['a'][123] = 4.56;
> 
> I had to make following helper function. Is there more elegant way /
> built in into D?
> 
> float* isIn = doubleIn(123, 'a');
> 
> float* doubleIn (int i, char ch) {
> 
> float[int]* first = ch in aa;
> 
> if (first is null)
> return null;
> else
> return i in *first;
> }

That's how I'd do it, more or less. However I'd skip the function (or go with the more general case as shown by Simen).

float* isIn = null;
if(auto a = 'a' in aa) b = 123 in *a;


January 18, 2010
Ali Çehreli:
> I don't know the answer, but the spec says so at

I have already given the answer, the hash collisions are resolved with an ordered search tree.

Bye,
bearophile
January 19, 2010
Michal Minich wrote:

> if one has double indexed aa, how to find that it contains value under keys 'a' and 123
>
> float[int][char] aa;
>
> aa['a'][123] = 4.56;
>
> I had to make following helper function. Is there more elegant way / built in into D?
>
> float* isIn = doubleIn(123, 'a');
>
> float* doubleIn (int i, char ch) {
>
>     float[int]* first = ch in aa;
>
>     if (first is null)
>         return null;
>     else
>         return i in *first;
> }

Don't know if this would have any affect on what you want, but there is this bugzilla: "Associative array of associative arrays gets confused"

http://d.puremagic.com/issues/show_bug.cgi?id=3709
January 19, 2010
Hello Jesse,

> Don't know if this would have any affect on what you want, but there
> is this bugzilla: "Associative array of associative arrays gets
> confused"
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=3709


Thank you, but it is some other issue. I posted enhacement request already http://d.puremagic.com/issues/show_bug.cgi?id=3718


1 2
Next ›   Last »