View mode: basic / threaded / horizontal-split · Log in · Help
April 06, 2005
array.sort/array.delete/array.key/array.value
This post is one issue and few suggestions about the arrays:

the issues is with array.sort - how does this thing works? I made a class and
overloaded opCmp() it was something like that:

class foo
{
int opCmp(foo a){return cmp(name,a.name);}
char[] name;
}
foo[] foo2;
foo2.sort;

and it was OK but when I changed cmp to icmp it was still working like cmp, so
obviously I do something wrong or I just do not know how it works...

And about the suggestions:

I think array.delete(index) will make sense. I mean it has .sort and on other
things like that. Also to get keys/values of associative arrays by index will be
usefull in many cases instead of getting new array of keys/values. We\ll have to
write less code in some cases where we don't need new array... I mean something
like that array.key(5) //key of the 6th element of the assotiative array...
April 06, 2005
Re: array.sort/array.delete/array.key/array.value
> int opCmp(foo a){return cmp(name,a.name);}

it should be int opCmp(Object a)...
April 06, 2005
Re: array.sort/array.delete/array.key/array.value
bobef wrote:
> This post is one issue and few suggestions about the arrays:
> 
> the issues is with array.sort - how does this thing works? I made a class and
> overloaded opCmp() it was something like that:
> 
> class foo
> {
> int opCmp(foo a){return cmp(name,a.name);}
> char[] name;
> }
> foo[] foo2;
> foo2.sort;

http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/74

> and it was OK but when I changed cmp to icmp it was still working like cmp, so
> obviously I do something wrong or I just do not know how it works...
> 
> And about the suggestions:
> 
> I think array.delete(index) will make sense. I mean it has .sort and on other
> things like that. Also to get keys/values of associative arrays by index will be
> usefull in many cases instead of getting new array of keys/values. We\ll have to
> write less code in some cases where we don't need new array... I mean something
> like that array.key(5) //key of the 6th element of the assotiative array...

AAs aren't ordered as such.  Though you can do

    array.keys[5]

It's probably true that there's some degree of internal ordering, which 
is reflected in both the foreach order and the keys and values arrays. 
And I'm guessing (this might be implementation dependent) that:

(a) foreach, array.keys and array.values will produce the same order at 
any time during which the AA is not changed in any way (either 
implicitly or explicitly)

(b) repeated uses of foreach, .keys and .values on the same AA will give 
the same order if the AA hasn't changed in the meantime

(c) when stuff is added to or removed from the AA (including accessing a 
non-existent element) or the AA is rehashed, the order can change 
arbitrarily

(d) changing the value associated with an existing key is done in place, 
and so will not change the order

The guaranteedness or not of these statements probably ought to be 
written into the spec.

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.
Top | Discussion index | About this forum | D home