July 04, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Im sorry, but I still get an ArrayBoundsError upon doing:
layer_map[layer] ~= new_element;
Same goes for:
auto single_layer = &layer_map[layer];
*single_layer ~= new_element;
And whats the point of doing:
ScreenElement[][] layer_map = new ScreenElement[][5];
Do I HAVE to initialise a number of empty arrays before concatenating instances of classes into it or not?
I dont see the point in giving some lenght in an dynamic array, for this would make it static, or am I wrong?
My whole add_element() function looks like this, just to give you all the info:
int add_element(ScreenElement new_element, uint layer)
{
writefln("layer: %d", layer);
writefln("layer_map.length: %d", layer_map.length);
if(layer <= layer_map.length)
{
auto single_layer = &layer_map[layer];
*single_layer ~= new_element;
//comented out, enalbed this instead of the upper two lines as an alternative
//layer_map[layer] ~= new_element;
}
else
{
writefln("cant insert ScreenElement into non existing layer");
}
return 0;
}
> No, it's because you're using a temp variable when you shouldn't be.
>
> ScreenElement[] single_layer = layer_map[layer];
> single_layer ~= new_element;
>
> That will get a reference the layer map at index `layer`, but when you concatenate, only the local single_layer will refer to the newer, longer array. Just do it in place:
>
> layer_map[layer] ~= new_element;
>
> (Also if you don't feel like typing all those long types for variables, it's completely unnecessary.
>
> auto single_layer = layer_map[layer];
>
> Dah.)
|
July 04, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | Moritz wrote: > And whats the point of doing: > ScreenElement[][] layer_map = new ScreenElement[][5]; > > Do I HAVE to initialise a number of empty arrays before concatenating instances of classes into it or not? > I dont see the point in giving some lenght in an dynamic array, for this would make it static, or am I wrong? You need to add the layers before you can add ScreenElements to them. Do you have a fixed number of layers? Then what you suggested will work: ScreenElement[][] layer_map = new ScreenElement[][5]; If you are used to working with pointers, dynamic arrays are just that, except that they store the length of what the pointer points to too. It's only that some operations on them actually allocate memory. "~", "~=", and explicitly setting .length allocates. Indexing does not allocate. > > My whole add_element() function looks like this, just to give you all the info: > > int add_element(ScreenElement new_element, uint layer) > { > writefln("layer: %d", layer); > writefln("layer_map.length: %d", layer_map.length); > > if(layer <= layer_map.length) > { Replace "<=" with "<", since layer_map.length always is one past the end of the array. |
July 04, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to torhu | Thank you torhu, I finally managed to get something into this array! :-)
But I still dont understand if and how I can add a new ScreenElement array to the layer map *without* setting its size in advance(because I want to remain as generic as possible).
My original idea was to have a create_layer() function, which creates a new layer by adding an empty array to the layer map, so I can keep creating as many layers as I want to.
And another question: Can I modify the size of an array after I set it manually?
> You need to add the layers before you can add ScreenElements to them. Do you have a fixed number of layers? Then what you suggested will work:
>
> ScreenElement[][] layer_map = new ScreenElement[][5];
>
> If you are used to working with pointers, dynamic arrays are just that, except that they store the length of what the pointer points to too. It's only that some operations on them actually allocate memory. "~", "~=", and explicitly setting .length allocates. Indexing does not allocate.
|
July 05, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | Moritz wrote: > Thank you torhu, I finally managed to get something into this array! :-) > > But I still dont understand if and how I can add a new ScreenElement array to the layer map *without* setting its size in advance(because I want to remain as generic as possible). I'm wondering if you're aware what a two-dimensional array really is. When you do this: ScreenElement[][] layer_map = new ScreenElement[][5]; layer_map.length will be 5. But the lengths of the arrays in the second dimension will all be zero. For example, layer_map[0].length and layer_map[3].length will be zero. So you have allocated space for 5 arrays of ScreenElements, but no space for the ScreenElements themselves. Just 5 array references that don't have any space to refer to yet. I hope this makes sense to you. > > And another question: Can I modify the size of an array after I set it manually? Sure, these two will work anytime: array.length = whatever; // set length to whatever array ~= stuff; // increase length by 1, add 'stuff' at end |
July 05, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to torhu | Thanks again, torhu. I understand multidimensional arrays (at least I think so :-) ), and that these arrays contain pointers to other arrays.
I thought the .length() operation would make the array static in its size, thats why I didnt try this earlier.
But after reading what you wrote, I still dont see why I cant go without the length(), by just using ~=, like:
ScreenElement[][] layer_map;
int add_element(ScreenElement new_element, uint layer)
{
layer_map[layer] ~= new_element;
}
You wrote setting the length and using ~= both increase the size of the array, but I get an ArrayBoundsError when using the ~=.
And thank you all for your help so far!!!!
torhu schrieb:
> Moritz wrote:
>> Thank you torhu, I finally managed to get something into this array! :-)
>>
>> But I still dont understand if and how I can add a new ScreenElement array to the layer map *without* setting its size in advance(because I want to remain as generic as possible).
>
> I'm wondering if you're aware what a two-dimensional array really is.
>
> When you do this:
>
> ScreenElement[][] layer_map = new ScreenElement[][5];
>
> layer_map.length will be 5. But the lengths of the arrays in the second dimension will all be zero. For example, layer_map[0].length and layer_map[3].length will be zero. So you have allocated space for 5 arrays of ScreenElements, but no space for the ScreenElements themselves. Just 5 array references that don't have any space to refer to yet.
>
> I hope this makes sense to you.
>
>>
>> And another question: Can I modify the size of an array after I set it manually?
>
> Sure, these two will work anytime:
>
> array.length = whatever; // set length to whatever
>
> array ~= stuff; // increase length by 1, add 'stuff' at end
>
|
July 05, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | Moritz wrote: > Thanks again, torhu. I understand multidimensional arrays (at least I think so :-) ), and that these arrays contain pointers to other arrays. > > I thought the .length() operation would make the array static in its size, thats why I didnt try this earlier. > > But after reading what you wrote, I still dont see why I cant go without the length(), by just using ~=, like: > > ScreenElement[][] layer_map; > > int add_element(ScreenElement new_element, uint layer) > { > layer_map[layer] ~= new_element; > } > > You wrote setting the length and using ~= both increase the size of the array, but I get an ArrayBoundsError when using the ~=. That probably means you're not doing quite what you think you're doing. Try adding an assert and see if it gets triggered: int add_element(ScreenElement new_element, uint layer) { assert(layer < layer_map.length, "there's no layer with that number"); layer_map[layer] ~= new_element; } > > And thank you all for your help so far!!!! > My pleasure. |
July 05, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | If you want more instant help, you could use IRC, it's #D on freenode. Web-based IRC here: http://embed.mibbit.com/?server=irc.freenode.org&channel=%23D |
Copyright © 1999-2021 by the D Language Foundation