Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 06, 2014 Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
What's the syntax for a new empty dynamic array or associative array? Every time I want to set a AA, I have to say: (supposing I already have some variable int[int] aa which points to the wrong one) int[int] throwaway; aa = throwaway; Is there a way to say something like: aa = new int[int] ? like I would with a class. What about for a normal dynamic array? |
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to dnspies | You can just set it to null. Then, next time you add anything to it, a new one will be automatically created. |
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 6 April 2014 at 03:23:17 UTC, Adam D. Ruppe wrote:
> You can just set it to null. Then, next time you add anything to it, a new one will be automatically created.
What about if I have a 2D array (ie int[][]) and I want to append an empty int[] on the end? Will this work?
int[][] nested;
nested ~= null;
~ is overloaded, so how does it know what type I intend null to be? How can I specify it?
|
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to dnspies | On Sunday, 6 April 2014 at 03:28:50 UTC, dnspies wrote:
> On Sunday, 6 April 2014 at 03:23:17 UTC, Adam D. Ruppe wrote:
>> You can just set it to null. Then, next time you add anything to it, a new one will be automatically created.
>
> What about if I have a 2D array (ie int[][]) and I want to append an empty int[] on the end? Will this work?
>
> int[][] nested;
>
> nested ~= null;
>
> ~ is overloaded, so how does it know what type I intend null to be? How can I specify it?
For dynamic arrays you can use the new operator.
nested ~= new int[];
Or in this case, you could just increment the length.
++nested.length;
|
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to dnspies | dnspies:
> What about if I have a 2D array (ie int[][]) and I want to append an empty int[] on the end? Will this work?
>
> int[][] nested;
>
> nested ~= null;
>
> ~ is overloaded, so how does it know what type I intend null to be? How can I specify it?
You can increase by one of the length of the outer array, or you can append an empty one:
nested.length++;
or:
nested ~= [];
I don't remember if you can also append a null. Appending a null, if it works, could be more efficient. Take a look at the ASM.
Bye,
bearophile
|
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to dnspies | On Sunday, 6 April 2014 at 03:17:25 UTC, dnspies wrote: > What's the syntax for a new empty dynamic array or associative array? > > Every time I want to set a AA, I have to say: > (supposing I already have some variable int[int] aa which points to the wrong one) > > int[int] throwaway; > aa = throwaway; > > What about for a normal dynamic array? AA's and Dynamic Arrays are different beasts. An Dynamic Array is merelly a "fat pointer" that holds both pointer and length. There is no need to create or new a Dynamic Array. An AA, on the other hand, is completely different. It's a pointer to an implementation, and the implementation does the actual work. The AA lazily initializes on the first operations. However, until initialized, an AA is pretty much just a null pointer. This can lead to interesting scenarios such as: //---- int[int] a; //null int[int] b = a; //both null assert(a is b); //They are both null a[1] = 1; //a gets initialized assert(a !is b); //but b remains null. int[int] c = a; //c points to the same implementation a[2] = 2; //a's implementation is changed assert(a is c); //and c "sees" it. //---- > Is there a way to say something like: > > aa = new int[int] ? > > like I would with a class. Currently, no. However, you can force initialization with a dummy insertion: int[int] b = [1:1]; b.remove(1); //b is now an empty, but initialized, AA It's a bit dirty, but that's how it is. |
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sunday, 6 April 2014 at 09:41:10 UTC, bearophile wrote: > dnspies: > >> What about if I have a 2D array (ie int[][]) and I want to append an empty int[] on the end? Will this work? >> >> int[][] nested; [...] > > You can increase by one of the length of the outer array, or you can append an empty one: [...] > nested ~= []; That doesn't add an element. [] is interpreted to be an empty int[][]. You need to write [[]] which is an int[][] holding one empty int[]. |
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | anonymous:
>> You can increase by one of the length of the outer array, or you can append an empty one:
> [...]
>> nested ~= [];
>
> That doesn't add an element. [] is interpreted to be an empty
> int[][]. You need to write [[]] which is an int[][] holding one
> empty int[].
Thank you catching my mistake.
But adding a null increases the length by 1:
a ~= null;
This difference is another reason for me to desire the removal of "null" as array literal.
Bye,
bearophile
|
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to dnspies | On 04/06/2014 05:28 AM, dnspies wrote:
>
> int[][] nested;
>
> nested ~= null;
>
> ~ is overloaded, so how does it know what type I intend null to be? How
> can I specify it?
(int[]).init
|
April 06, 2014 Re: Empty array and AA literals | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Sunday, 6 April 2014 at 09:52:04 UTC, monarch_dodra wrote:
> An Dynamic Array is merelly a "fat pointer" that holds both pointer and length. There is no need to create or new a Dynamic Array.
new allows for setting the length immediately, though.
auto arr = new int[](99);
// arr.length = 99; // avoided this
Does doing it in two steps allocate twice?
|
Copyright © 1999-2021 by the D Language Foundation