View mode: basic / threaded / horizontal-split · Log in · Help
March 16, 2005
Re: Accessing Dynamic Arrays - best approach?
Derek Parnell says...

>   d ~= 3;
>   d ~= 8;
>   d ~= ... etc ...

>You would probably select the method that made more sense for the
>application. Presetting the length is fine if you know how many 
>items you are adding, but the concatenation method may be more 
>useful when you don't know how many items there will be.

Thanx for the ~= example. I have read about it but was not aware that 
would work when adding array elements.

Just continued reading the documentation, and found an "optimized" 
example:

   int[] array;
   array.length = 100;         // guess
   for (i = 0; 1; i++)
   {   
       c = getinput();
       if (!c)
           break;
       if (i == array.length)
           array.length = array.length * 2;
       array[i] = c;
   }
   array.length = i;           // Resize to actual array size! 

And indeed the manual frowns on, doing "inefficient" array.length++; 
calls.


Regan,

>>> int[] original = "12345"; //original.length is 5
>>> ...
>Doh! sorry, the code above will never compile.

Glad, I am not totally wacking out :)

>Let me try again:

>    char[] original = "12345";
>    char[] a;

>    a = original.dup;
>    a = original[0..3];

I have read a scary section in the manual under Arrays, Setting 
Dynamic Array Length... where it seems to turn out that slicing can 
lead to code that will not be fully predicatble. In the above example, 
if you where to change elements in a

   a = original[0..3];
   a[2] = 'd';

then original[2] could be 'd' or it could still be '3'. Because when 
slicing it seems to be not clear if a is actually a seperate copy of 
original or just a "reference". To ensure that you are manipulating a 
copy, the manual suggests one  use .dup:

   a = original.dup;
   a[2] = 'd';
-> original[2] uneffected.

This is only how I understand things from first reading, I may have 
missunderstood something.


I had the feeling that the example in Arrays, Setting Dynamic Array 
Length, had a mistake, or at least I did not understand what was 
meant:

   char[] a = new char[20];
   char[] b = a[0..10];
   char[] c = a[10..20];

   b.length = 15;  // always resized in place because it is sliced
                   // from a[] which has enough memory for 15 chars
   b[11] = 'x';    // a[15] and c[5] are also affected
   ...

Should that not read?:

   b[11] = 'x';    // a[11] and c[1] are also affected

Visualization of the memory:

   var  element number
                  1111111111 
   a    01234567890123456789
   b    0123456789 
   c              0123456789

With this behavior I would assume that slices are probably wonderful 
as convenient "aliases" to (in this example) substrings, but should 
not be used to manipulate the original arrays? 


anders,
>>     d.length ++;
>>     d[1] = 8;     // 2nd number

>As a side note, "d.length++;" is not allowed
>(since you *might* use it to hurt yourself...)

>One must do "d.length = d.length + 1;" instead.
>Or just use "d ~= 8;", which is a lot easier ?

Aha... good to know!

AEon
March 16, 2005
Re: Accessing Dynamic Arrays - best approach?
> I have read a scary section in the manual under Arrays, Setting 
> Dynamic Array Length... where it seems to turn out that slicing can 
> lead to code that will not be fully predicatble. In the above example, 
> if you where to change elements in a
> 
>     a = original[0..3];
>     a[2] = 'd';
> 
> then original[2] could be 'd' or it could still be '3'. Because when 
> slicing it seems to be not clear if a is actually a seperate copy of 
> original or just a "reference". To ensure that you are manipulating a 
> copy, the manual suggests one  use .dup:

I didn't see anything "scary" or "unclear".  If you use a slice, you get 
the memory, not a copy of it.  If you use .dup, you get a copy. 
Sometimes you don't want a copy - imagine:

if (str[0 .. 7] == "http://")
   ...

Do you want to actually allocate more memory for that, or are you happy 
with the comparison as is?

> With this behavior I would assume that slices are probably wonderful 
> as convenient "aliases" to (in this example) substrings, but should 
> not be used to manipulate the original arrays? 

Why not?  What if you did this:

char[] str = "test<script>";
int i = find(str, "<script>");
str[i .. i + 8] = ' ';

Doesn't it make sense, there, to modify in place?  Faster than making a 
copy, modifying it, and then replacing the old, no?

-[Unknown]
March 16, 2005
Re: Accessing Dynamic Arrays - best approach?
On Wed, 16 Mar 2005 10:18:26 +0000 (UTC), AEon <AEon_member@pathlink.com>  
wrote:
>>>> int[] original = "12345"; //original.length is 5
>>>> ...
>> Doh! sorry, the code above will never compile.
>
> Glad, I am not totally wacking out :)

No, that was me :)

>> Let me try again:
>
>>    char[] original = "12345";
>>    char[] a;
>
>>    a = original.dup;
>>    a = original[0..3];
>
> I have read a scary section in the manual under Arrays, Setting
> Dynamic Array Length... where it seems to turn out that slicing can
> lead to code that will not be fully predicatble. In the above example,
> if you where to change elements in a
>
>     a = original[0..3];
>     a[2] = 'd';
>
> then original[2] could be 'd' or it could still be '3'.

original[2] will be 'd'. This is guaranteed IIRC. When you type "a = " you  
always make a reference to the original data.

However, if you said:

char[] original = "12345";
char[] a;

a.length = 3;
a[0..3] = original[0..3];

it would copy the values from 'original' to 'a'.

> Because when
> slicing it seems to be not clear if a is actually a seperate copy of
> original or just a "reference".

It depends on what you say, "a = " always creates a reference, but  
"a[x..y] = " always copies the data.

> To ensure that you are manipulating a
> copy, the manual suggests one  use .dup:
>
>     a = original.dup;
>     a[2] = 'd';
> -> original[2] uneffected.

Correct. dup creates a copy of the array or slice. eg. (note .dup on a  
slice)

a = original[0..3].dup;
a[2] = 'd'
//original[2] unaffected

> This is only how I understand things from first reading, I may have
> missunderstood something.

There may be a few subtle things left to learn. I've tried to show them  
above.

> I had the feeling that the example in Arrays, Setting Dynamic Array
> Length, had a mistake, or at least I did not understand what was
> meant:
>
>     char[] a = new char[20];
>     char[] b = a[0..10];
>     char[] c = a[10..20];
>
>     b.length = 15;  // always resized in place because it is sliced
>                     // from a[] which has enough memory for 15 chars
>     b[11] = 'x';    // a[15] and c[5] are also affected
>     ...
>
> Should that not read?:
>
>     b[11] = 'x';    // a[11] and c[1] are also affected

I agree. I think it's a typo/mistake.

> Visualization of the memory:
>
>     var  element number
>                    1111111111
>     a    01234567890123456789
>     b    0123456789
>     c              0123456789
>
> With this behavior I would assume that slices are probably wonderful
> as convenient "aliases" to (in this example) substrings, but should
> not be used to manipulate the original arrays?

You can use them to manipulate the originals, it can be quite handy.

The rest of the time, make sure you use COW (Copy On Write), meaning every  
time you write to an array (which you're not intentionally using to  
manipulate the original) call "dup" first, then write to it. eg.

char[] original = "12345";
char[] a;

a = original;

..lots of code..

a = a.dup;
a[2] = 'd';

Regan
March 16, 2005
Re: Accessing Dynamic Arrays - best approach?
On Thu, 17 Mar 2005 00:20:44 +1300, Regan Heath <regan@netwin.co.nz> wrote:
>>     char[] a = new char[20];
>>     char[] b = a[0..10];
>>     char[] c = a[10..20];
>>
>>     b.length = 15;  // always resized in place because it is sliced
>>                     // from a[] which has enough memory for 15 chars
>>     b[11] = 'x';    // a[15] and c[5] are also affected
>>     ...
>>
>> Should that not read?:
>>
>>     b[11] = 'x';    // a[11] and c[1] are also affected
>
> I agree. I think it's a typo/mistake.

It appears someone else agreed also:
http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Arrays

If you find any other possible typos you can amend them here:
http://www.prowiki.org/wiki4d/wiki.cgi?DocComments

I believe Walter will eventually get round to making all these changes to  
the real docs?

Regan
March 16, 2005
Re: Accessing Dynamic Arrays - best approach?
Unknown W. Brackets says...

>I didn't see anything "scary" or "unclear".  If you use a slice, you get 
>the memory, not a copy of it.  If you use .dup, you get a copy. 

Well it had surprised me, would have been a better way of putting it.


>Sometimes you don't want a copy - imagine:
>
>if (str[0 .. 7] == "http://")
>    ...
>
>Do you want to actually allocate more memory for that, or are you happy 
>with the comparison as is?

Ah... indeed.


>> With this behavior I would assume that slices are probably wonderful 
>> as convenient "aliases" to (in this example) substrings, but should 
>> not be used to manipulate the original arrays? 
>
>Why not?  What if you did this:
>
>char[] str = "test<script>";
>int i = find(str, "<script>");
>str[i .. i + 8] = ' ';
>
>Doesn't it make sense, there, to modify in place?  Faster than making a 
>copy, modifying it, and then replacing the old, no?

Ahem :)... yes it does make sense. But I had only been thinking of working with
"copies" and manipulating those, my mistake. When directly manipulating the
"original" strings via slices, that would indeed be predicatable in outcome and
useful.

Thanx for the real world examples.



Regan,

>It appears someone else agreed also:
>http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Arrays

Indeed. Thanx for the link.

>If you find any other possible typos you can amend them here:
>http://www.prowiki.org/wiki4d/wiki.cgi?DocComments

Hmmm... I have trouble finding some link/button to actually submit something
new?! I use the Firefox browser, could that be the problem?

AEon
March 16, 2005
Re: Accessing Dynamic Arrays - best approach?
On Wed, 16 Mar 2005 12:20:24 +0000 (UTC), AEon <AEon_member@pathlink.com>  
wrote:
>> It appears someone else agreed also:
>> http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Arrays
>
> Indeed. Thanx for the link.
>
>> If you find any other possible typos you can amend them here:
>> http://www.prowiki.org/wiki4d/wiki.cgi?DocComments
>
> Hmmm... I have trouble finding some link/button to actually submit  
> something
> new?! I use the Firefox browser, could that be the problem?

At the bottom of each page, i.e.
http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Arrays

there is and 'Edit' link and als 'Edit text of this page'.
before you can do that you need to go to preferences and enter your name,  
so it can track who changed what.

Regan
March 16, 2005
Re: Accessing Dynamic Arrays - best approach?
Regan Heath says...

>At the bottom of each page, i.e.
>http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Arrays
>
>there is and 'Edit' link and als 'Edit text of this page'.
>before you can do that you need to go to preferences and enter your name,  
>so it can track who changed what.

Thanx... sigh, need to check my eyes ;)

AEon
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home