digitalmars.D.learn - remaping arrays
- BCS (13/13) Aug 02 2005 What is the easiest way to paint something like "real[5][]" over somethi...
- Regan Heath (33/47) Aug 02 2005 import std.stdio;
-
BCS
(5/8)
Aug 02 2005
In article
, Regan Heath says... - Regan Heath (10/19) Aug 02 2005 I realised this, I thought maybe you had it backwards.
- Derek Parnell (30/48) Aug 02 2005 At first I thought you couldn't do this, but I did some tests and here i...
- Regan Heath (6/50) Aug 02 2005 You've made a liar out of me. I should learn to read all the replied
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.
Aug 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
Aug 02 2005
In article <opsuwik1iy23k2f5 nrage.netwin.co.nz>, Regan Heath says... Not quite. I need a dynamic array of arrays-of-const sizeyou used an array-of-const size of dynamic arraysreal [5][] b;int[][5] b;BTW, I havn't run you code yet, but I'll try it later.
Aug 02 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 sizeI 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.you used an array-of-const size of dynamic arraysreal [5][] b;int[][5] b;BTW, I havn't run you code yet, but I'll try it later.NP. Regan
Aug 02 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
Aug 02 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: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. ReganWhat 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>
Aug 02 2005