Thread overview
remaping arrays
Aug 02, 2005
BCS
Aug 02, 2005
Regan Heath
Aug 02, 2005
BCS
Aug 03, 2005
Regan Heath
Aug 03, 2005
Derek Parnell
Aug 03, 2005
Regan Heath
August 02, 2005
What is the easiest way to paint something like "real[5][]" over something like
"real[]".
What I’m looking for would result in this:

real[] a;
real [5][] b;

b.length = a.length/b[0].length;
b.ptr = a.ptr;		// or whatever

now:

b[0] == a[0..5];
b[1] == a[5..10];
..
b[last] == a[(b.last-1)*5 .. b.last*5];


this should not requier any copying.


August 02, 2005
On Tue, 2 Aug 2005 20:38:21 +0000 (UTC), BCS <BCS_member@pathlink.com> wrote:
> What is the easiest way to paint something like "real[5][]" over something like
> "real[]".
> What I’m looking for would result in this:
>
> real[] a;
> real [5][] b;
>
> b.length = a.length/b[0].length;
> b.ptr = a.ptr;		// or whatever
>
> now:
>
> b[0] == a[0..5];
> b[1] == a[5..10];
> ..
> b[last] == a[(b.last-1)*5 .. b.last*5];
>
>
> this should not requier any copying.

import std.stdio;

void main()
{
	static int[] a = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
	int[][5] b;
	
	//paint
	foreach(int i, inout int[] row; b)
		row = a[(i*5)..(i*5)+5];
	
	//display
	foreach(int line, int[] row; b)
	{
		writef(line,": ");
		foreach(int i; row)
		{
			writef("%02d ",i);
		}
		writefln("");
	}
	
	writefln("");
	
	//proof
	writefln("No copying, see:");
	writefln("a: %x",a.ptr);
	foreach(int line, int[] row; b)
		writefln("%d: ",line,"%x",row.ptr);
}

Regan
August 02, 2005
In article <opsuwik1iy23k2f5@nrage.netwin.co.nz>, Regan Heath says...

Not quite.

I need a dynamic array of arrays-of-const size
>

>> real [5][] b;

you used an array-of-const size of dynamic arrays

>	int[][5] b;
>

BTW, I havn't run you code yet, but I'll try it later.



August 03, 2005
On Tue, 2 Aug 2005 20:38:21 +0000 (UTC), BCS wrote:

> What is the easiest way to paint something like "real[5][]" over something like
> "real[]".
> What I’m looking for would result in this:
> 
> real[] a;
> real [5][] b;
> 
> b.length = a.length/b[0].length;
> b.ptr = a.ptr;		// or whatever
> 
> now:
> 
> b[0] == a[0..5];
> b[1] == a[5..10];
> ..
> b[last] == a[(b.last-1)*5 .. b.last*5];
> 
> this should not requier any copying.

At first I thought you couldn't do this, but I did some tests and here is my result...

<code>
import std.stdio;

void main()
{
    real[5][] a;
    real[] b;

    // Populate the unmapped array with some test data.
    real k = 6.996; // starting number

    for (int i = 0; i < 7; i++) // create 7 real[5] arrays.
    {
        a.length = a.length + 1;

        for( int j = 0; j < 5; j++, k+=1.569)
            a[$-1][j] = k;
    }

    // Remap the array.
    b = cast(real[])a;
    b.length = a.length * 5;

    // Display the remapped array.
    foreach(int i, real x; b)
        writefln("Elem[%2d] = %s", i, x);
}
</code>
-- 
Derek
Melbourne, Australia
3/08/2005 10:32:50 AM
August 03, 2005
On Tue, 2 Aug 2005 23:51:48 +0000 (UTC), BCS <BCS_member@pathlink.com> wrote:
> In article <opsuwik1iy23k2f5@nrage.netwin.co.nz>, Regan Heath says...
>
> Not quite.
>
> I need a dynamic array of arrays-of-const size
>>
>
>>> real [5][] b;
>
> you used an array-of-const size of dynamic arrays
>
>> 	int[][5] b;
>> 	

I realised this, I thought maybe you had it backwards.

The basic problem is that a const-size array has a fixed memory location, so you cannot make it refer to another existing array, you have to copy into it.

So your options are, fixed-array and copy or dynamic array and no copy. AFAIKS.

> BTW, I havn't run you code yet, but I'll try it later.

NP.

Regan
August 03, 2005
On Wed, 3 Aug 2005 10:34:23 +1000, Derek Parnell <derek@psych.ward> wrote:
> On Tue, 2 Aug 2005 20:38:21 +0000 (UTC), BCS wrote:
>
>> What is the easiest way to paint something like "real[5][]" over something like
>> "real[]".
>> What I’m looking for would result in this:
>>
>> real[] a;
>> real [5][] b;
>>
>> b.length = a.length/b[0].length;
>> b.ptr = a.ptr;		// or whatever
>>
>> now:
>>
>> b[0] == a[0..5];
>> b[1] == a[5..10];
>> ..
>> b[last] == a[(b.last-1)*5 .. b.last*5];
>>
>> this should not requier any copying.
>
> At first I thought you couldn't do this, but I did some tests and here is
> my result...
>
> <code>
> import std.stdio;
>
> void main()
> {
>     real[5][] a;
>     real[] b;
>
>     // Populate the unmapped array with some test data.
>     real k = 6.996; // starting number
>    for (int i = 0; i < 7; i++) // create 7 real[5] arrays.
>     {
>         a.length = a.length + 1;
>
>         for( int j = 0; j < 5; j++, k+=1.569)
>             a[$-1][j] = k;
>     }
>
>     // Remap the array.
>     b = cast(real[])a;
>     b.length = a.length * 5;
>
>     // Display the remapped array.
>     foreach(int i, real x; b)
>         writefln("Elem[%2d] = %s", i, x);
> }
> </code>

You've made a liar out of me. I should learn to read all the replied before posting.

The memory layout of "real[5][]" and the fact that a "real[5]" is not an 'array struct' makes painting as real[] possible, very clever.

Regan