June 29, 2004
one more thing i would like to add. see my post 'array operation request' to understand why i thought you could do delete array[i]; and have it resize and all. but the thing is i tried it with an int array and a matrix type array and it worked then, but it doesn't work for classes ?

maybe a 'remove' type thing would be a good array thing to have after all? or is it better to leave it to the user.


June 29, 2004
another thing, i hate to flood my own topic, but i have a question about the foreach expression. is it possible to tell what number of the array you're on? like with (for int i = 0; i < blah; i++) the current array item is 'i'. is there a way to get the current item in a foreach statement since there is no 'i'? thanks.


June 29, 2004
On Tue, 29 Jun 2004 04:00:21 +0000 (UTC), clayasaurus <clayasaurus_member@pathlink.com> wrote:

> one more thing i would like to add. see my post 'array operation request' to
> understand why i thought you could do delete array[i]; and have it resize and
> all. but the thing is i tried it with an int array and a matrix type array and
> it worked then, but it doesn't work for classes ?

It doesn't work for me, with an int array...

void main()
{
	int[] a;
	
	a ~= 1;
	a ~= 2;
	a ~= 3;
	a ~= 4;
	a ~= 5;
	
	delete a[2];
	
	foreach(int i; a)
		printf("%x\n",i);
}

prints

1
2
0
4
5

meaning it did not resize, all it did was destruct the int. (setting it to the .init value of 0)

> maybe a 'remove' type thing would be a good array thing to have after all? or is
> it better to leave it to the user.

One of the aims is to have a small fast array type. As we can achieve what you want with a simple template.. ie.

template remove(T) {
	T remove(inout T a, uint i) {
		a = a[0..i] ~ a[i+1..a.length];
		return a;
	}
}

or even..

extern (C) void *memmove( void *dest, void *src, size_t count );

template removeAlt(T) {
	T removeAlt(inout T a, uint i) {
		memmove(&a[i],&a[i+1],typeof(a[0]).sizeof*(a.length-i));
		a.length = a.length-1;
		return a;
	}
}


used like so..

void main()
{
	int[] a;
	
	a ~= 1;
	a ~= 2;
	a ~= 3;
	a ~= 4;
	a ~= 5;
	
	removeAlt!(int[])(a,2);
	//delete a[2];
	
	foreach(int i; a)
		printf("%x\n",i);
}

I don't think a remove property/method is required.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 29, 2004
On Tue, 29 Jun 2004 04:10:19 +0000 (UTC), clayasaurus <clayasaurus_member@pathlink.com> wrote:

> another thing, i hate to flood my own topic, but i have a question about the
> foreach expression. is it possible to tell what number of the array you're on?
> like with (for int i = 0; i < blah; i++) the current array item is 'i'. is there
> a way to get the current item in a foreach statement since there is no 'i'?
> thanks.
>
>

yep. a additional int or uint added to the foreach gives you that.
example:

Foo[] a;

foreach(int i, Foo f; a)
{
}

see:
http://www.digitalmars.com/d/statement.html#foreach

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 29, 2004
#    delete a[n];

This only works for ASSOCIATIVE arrays. For other types of array, it will call the element's destructor.

Actually, this non-standard use of delete is dead confusing. I'd prefer a new keyword (remove). In such a scheme, we could have:

#    // For associative arrays
#    remove a[n];  // remove the key n and its associated value from the array a
#    delete a[n];  // call the destructor of a[n]

and, even better...

#    // For dynamic arrays
#    remove a[n];

could be equivalent to:

#    for (uint i=n+1; i<a.length; ++i)
#    {
#        a[n-1] = a[n];
#    }
#    a[a.length - 1] = null; // help the GC out
#    a.length = a.length - 1;

That would be neat.
Arcane Jill


June 29, 2004
In article <cbr6l0$rj5$1@digitaldaemon.com>, Arcane Jill says...
>
>
>#    delete a[n];
>
>This only works for ASSOCIATIVE arrays. For other types of array, it will call the element's destructor.
>
>Actually, this non-standard use of delete is dead confusing. I'd prefer a new keyword (remove). In such a scheme, we could have:
>
>#    // For associative arrays
>#    remove a[n];  // remove the key n and its associated value from the array a
>#    delete a[n];  // call the destructor of a[n]
>
>and, even better...
>
>#    // For dynamic arrays
>#    remove a[n];
>
>could be equivalent to:
>
>#    for (uint i=n+1; i<a.length; ++i)
>#    {
>#        a[n-1] = a[n];
>#    }
>#    a[a.length - 1] = null; // help the GC out
>#    a.length = a.length - 1;
>
>That would be neat.
>Arcane Jill

While you're at it, wouldn't the following...

#    remove a[m..n];

..work well for removing a slice from an array?

I've yearned for this kind of flexibility from arrays in a language for some
time now.  I'd even settle for some standard array methods like '.remove(x)'
'.clear()' or '.isEmpty()'.

- Pragma


June 29, 2004
> While you're at it, wouldn't the following...
>
> #    remove a[m..n];
>
> ..work well for removing a slice from an array?

That would be cool.

> I've yearned for this kind of flexibility from arrays in a language for
some
> time now.  I'd even settle for some standard array methods like
'.remove(x)'
> '.clear()' or '.isEmpty()'.

I don't think they need to be pre-defined.


June 29, 2004
In article <cbrrtf$1otv$1@digitaldaemon.com>, Bent Rasmussen says...
>
>> While you're at it, wouldn't the following...
>>
>> #    remove a[m..n];
>>
>> ..work well for removing a slice from an array?
>
>That would be cool.
>
>> I've yearned for this kind of flexibility from arrays in a language for
>some
>> time now.  I'd even settle for some standard array methods like
>'.remove(x)'
>> '.clear()' or '.isEmpty()'.
>
>I don't think they need to be pre-defined.

I'm genuinely curious about your take on this.  What do you have in mind instead of this?  Should there should be an Array class with static array mutators, or a free-function library to supplement D arrays? Something else? :)

(I'm just thinking aloud here, but maybe what we're missing here is something that can be added to Demios to extend arrays beyond their current use and design... provided its not already there yet.)

- Pragma


June 29, 2004
> I'm genuinely curious about your take on this.  What do you have in mind
instead
> of this?  Should there should be an Array class with static array
mutators, or a
> free-function library to supplement D arrays? Something else? :)

A free-function module should do it. Of course there'll probably be classes for sophisticated structures in DTL. I don't at all like the Java way of using classes with static methods for array and collection operations. For collections they ought to be part of the datatype they operate upon. For arrays its probably an artifact of the language: look at Math. Now it seems Java will get static import so you can import Math functions in the Tiger release.

http://www.jcp.org/aboutJava/communityprocess/jsr/tiger/static-import.html

Ahh, the benefits of D...

I haven't looked into slicing in D yet, but I suspect sorting with them will be a joy.


June 30, 2004
On Tue, 29 Jun 2004 07:44:32 +0000 (UTC), Arcane Jill <Arcane_member@pathlink.com> wrote:
> #    delete a[n];
>
> This only works for ASSOCIATIVE arrays. For other types of array, it will call
> the element's destructor.
>
> Actually, this non-standard use of delete is dead confusing. I'd prefer a new
> keyword (remove). In such a scheme, we could have:
>
> #    // For associative arrays
> #    remove a[n];  // remove the key n and its associated value from the array a
> #    delete a[n];  // call the destructor of a[n]
>
> and, even better...
>
> #    // For dynamic arrays
> #    remove a[n];
>
> could be equivalent to:
>
> #    for (uint i=n+1; i<a.length; ++i)
> #    {
> #        a[n-1] = a[n];
> #    }
> #    a[a.length - 1] = null; // help the GC out
> #    a.length = a.length - 1;

Yep.

extern (C) void *memmove( void *dest, void *src, size_t count );

template remove(T) {
	T remove(inout T a, uint i) {
		memmove(&a[i],&a[i+1],typeof(a[0]).sizeof*(a.length-i));
		a.length = a.length-1;
		return a;
	}
}

Regan.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/