Re: Should be easy

2009-06-16 Thread Saaa
template BaseType(T: T[]) { alias BaseType!(T) BaseType; }
template BaseType(T) { alias T BaseType; }

 Otherwise, you need indexAssign:

 void indexAssign(TArray : TArray[])(TArray array, BaseType!(TArray) value, 
 int[] indices...)
 {
 static if (is (typeof (array[0]) == typeof(value))
 {
 array[indices[0]] = value;
 }
 else
 {
 indexAssign(array[indices[0]], value, indices[1..$]);
 }
 }

int[][][] a;
a.length = 3;
a[1].length = 3;
a[1][2].length = 3;

int[3] index;
index[0]=1;
index[1]=2;
index[2]=3;

int value = 10;

writefln(BaseType!(value).stringof);
// template instance BaseType!(value) does not match any template 
declaration

indexAssign(a,value,index);
// template ddata.main.indexAssign(T : T[]) does not match any template 
declaration
// template ddata.main.indexAssign(T : T[]) cannot deduce template function 
from argument types !()(int[][][],int,int[3u])


What am I doing wrong this time..?
just when I though I understood the code a bit :) 




Re: Should be easy

2009-06-15 Thread Saaa
 Ever heard of recursion?

 Why don't you simply handle all types recursively?

 I'm still very interested in what exactly that means. Could you maybe
 give a small example?


 int Index(T)(T arr, int[] ind)
 {
  static if(is(T B == B[][])) // if array of array
  return Index!(B[])(arr[ind[0]], ind[1..$]);
  else
  {
  static assert(is(T B == B[])); // had better be an array;
  return arr[ind[0]];
  }
 }

 void main()
 {
int[][][] d;
d.length = 3;
d[1].length = 3;
d[1][2].length = 3;
d[1][2] = [0,1,1];

assert(0==Index!(int[][][])(d,[1,2,0]));
 }

Thanks,
but how does this differ from what Christopher Wright suggested? 




Re: Should be easy

2009-06-15 Thread BCS

Hello Saaa,


Ever heard of recursion?

Why don't you simply handle all types recursively?


I'm still very interested in what exactly that means. Could you
maybe give a small example?


int Index(T)(T arr, int[] ind)
{
static if(is(T B == B[][])) // if array of array
return Index!(B[])(arr[ind[0]], ind[1..$]);
else
{
static assert(is(T B == B[])); // had better be an array;
return arr[ind[0]];
}
}
void main()
{
int[][][] d;
d.length = 3;
d[1].length = 3;
d[1][2].length = 3;
d[1][2] = [0,1,1];
assert(0==Index!(int[][][])(d,[1,2,0]));
}

Thanks,
but how does this differ from what Christopher Wright suggested?


I don't know if it does, it is a direct answer to your question: Could you 
maybe give a small example?


This code is an example of what handling all (array) types recursively means.




Re: Should be easy

2009-06-13 Thread Christopher Wright

Saaa wrote:

I did get it to compile at one time, but didn't know how to use it,
like your code..
index( array, index2);  //compiles and all, but how do I set the value?
index( array, index2) = -1; // doesn't work


If you're using d2, add 'ref' to the return type.

Otherwise, you need indexAssign:

void indexAssign(TArray : TArray[])(TArray array, BaseType!(TArray) 
value, int[] indices...)

{
static if (is (typeof (array[0]) == typeof(value))
{
array[indices[0]] = value;
}
else
{
indexAssign(array[indices[0]], value, indices[1..$]);
}
}


Also, why the ... ?


In case you know the number of indices ahead of time. It costs nothing 
and lets you use a more natural syntax some of the time.


Should be easy

2009-06-12 Thread Saaa
I can't figure out how to create the IndexArray function,
it should work on arrays of any depth

int[][][] array; //any depth
array.length=10;
array[1].length=3;
array[1][2].length=4;
array[1][2][1]=99;

writefln(array);
//[[],[[],[],[0,99,0,0]],[],[],[],[],[],[],[],[]]

int[3] index; //same length as depth
index[0]=1;
index[1]=2;
index[2]=3;

IndexArray( array, index) = -1;
//equal to :
//array[ index[0] ][ index[1] ][ index[2] ] = -1;

writefln(array);
//[[],[[],[],[0,99,0,-1]],[],[],[],[],[],[],[],[]]

All suggestions are greatly appreciated !





Re: Should be easy

2009-06-12 Thread Joel Christensen
For 2 dim array I like auto a=new char[][](40,25); so that 
a[39][24]='B'; 'B' is at the bottom right of the 2D array.


Re: Should be easy

2009-06-12 Thread Saaa
Thanks.
But my problem lays in supporting arrays of arbitrary depth.

 For 2 dim array I like auto a=new char[][](40,25); so that 
 a[39][24]='B'; 'B' is at the bottom right of the 2D array. 




Re: Should be easy

2009-06-12 Thread downs
Saaa wrote:
 I can't figure out how to create the IndexArray function,
 it should work on arrays of any depth
 
 int[][][] array; //any depth
 array.length=10;
 array[1].length=3;
 array[1][2].length=4;
 array[1][2][1]=99;
 
 writefln(array);
 //[[],[[],[],[0,99,0,0]],[],[],[],[],[],[],[],[]]
 
 int[3] index; //same length as depth
 index[0]=1;
 index[1]=2;
 index[2]=3;
 
 IndexArray( array, index) = -1;
 //equal to :
 //array[ index[0] ][ index[1] ][ index[2] ] = -1;
 
 writefln(array);
 //[[],[[],[],[0,99,0,-1]],[],[],[],[],[],[],[],[]]
 
 All suggestions are greatly appreciated !
 
 
 

Here's one.

module test144;

import std.stdio, std.metastrings;

struct PointerAssign(T) {
  T* ptr;
  T opAssign(T t) { *ptr = t; return t; }
  static PointerAssign opCall(T* p) { PointerAssign res; res.ptr = p; return 
res; }
}

template isArray(T: T[]) { const bool isArray = true; }
template isArray(T) { const bool isArray = false; }

template Init(T) { const T Init; }
template ElemType(T) { alias typeof(Init!(T)[0]) ElemType; }

template BaseType(T) {
  static if (isArray!(T)) alias BaseType!(ElemType!(T)) BaseType;
  else alias T BaseType;
}

template Depth(T) {
  static if (isArray!(T)) const int Depth = 1 + Depth!(ElemType!(T));
  else const int Depth = 0;
}

string ctToString(int i) {
  if (!i) return 0;
  string res;
  while (i) {
res = 0123456789[i%10] ~ res;
i /= 10;
  }
  return res;
}

string index(int len) {
  string res;
  for (int i = 0; i  len; ++i)
res ~= [indices[~ctToString(i)~]] ;
  return res;
}

PointerAssign!(BaseType!(T)) IndexArray(T)(T array, int[] indices...) {
  return PointerAssign!(BaseType!(T)) (mixin(array~index(Depth!(T;
}

void main() {
  int[][][] array;
  array.length = 10;
  array[1].length = 3;
  array[1][2].length = 4;
  array[1][2][1] = 99;
  writefln(array);
  IndexArray(array, 1, 2, 3) = -1;
  writefln(array);
}


Re: Should be easy

2009-06-12 Thread downs
Saaa wrote:
 Thanks!
 I thought about the idea of just creating the code and mix it in, but now I 
 can see why I failed at that: Your code is kind of read-only to me, for now, 
 because I need to change it a bit to accept an array instead of seperat 
 indices.
 
 Wouldn't it be nice if it worked like this:
 int[] index = (1,2,3);
 array(index) = -1;
 
 Here's one.

 module test144;

 import std.stdio, std.metastrings;

 struct PointerAssign(T) {
  T* ptr;
  T opAssign(T t) { *ptr = t; return t; }
  static PointerAssign opCall(T* p) { PointerAssign res; res.ptr = p; 
 return res; }
 }

 template isArray(T: T[]) { const bool isArray = true; }
 template isArray(T) { const bool isArray = false; }

 template Init(T) { const T Init; }
 template ElemType(T) { alias typeof(Init!(T)[0]) ElemType; }

 template BaseType(T) {
  static if (isArray!(T)) alias BaseType!(ElemType!(T)) BaseType;
  else alias T BaseType;
 }

 template Depth(T) {
  static if (isArray!(T)) const int Depth = 1 + Depth!(ElemType!(T));
  else const int Depth = 0;
 }

 string ctToString(int i) {
  if (!i) return 0;
  string res;
  while (i) {
res = 0123456789[i%10] ~ res;
i /= 10;
  }
  return res;
 }

 string index(int len) {
  string res;
  for (int i = 0; i  len; ++i)
res ~= [indices[~ctToString(i)~]] ;
  return res;
 }

 PointerAssign!(BaseType!(T)) IndexArray(T)(T array, int[] indices...) {
  return PointerAssign!(BaseType!(T)) (mixin(array~index(Depth!(T;
 }

 void main() {
  int[][][] array;
  array.length = 10;
  array[1].length = 3;
  array[1][2].length = 4;
  array[1][2][1] = 99;
  writefln(array);
  IndexArray(array, 1, 2, 3) = -1;
  writefln(array);
 } 
 
 

IndexArray should take an array of integers as well. The int[] foo... syntax is 
implicit convert to array anyway.

Also, please bottom-post. It's the convention.

:)


Re: Should be easy

2009-06-12 Thread Saaa

 IndexArray should take an array of integers as well. The int[] foo... 
 syntax is implicit convert to array anyway.

I think I'll go with Christophers code, hope you don't mind :)


 Also, please bottom-post. It's the convention.

 :)

Why is that?
scroll
scroll
I probably use a retarded newsreader :D 




Re: Should be easy

2009-06-12 Thread Denis Koroskin

On Sat, 13 Jun 2009 03:37:59 +0400, Saaa em...@needmail.com wrote:



IndexArray should take an array of integers as well. The int[] foo...
syntax is implicit convert to array anyway.


I think I'll go with Christophers code, hope you don't mind :)



Also, please bottom-post. It's the convention.

:)


Why is that?
scroll
scroll
I probably use a retarded newsreader :D




Just don't quote. Why include quote, if there is nothing under it?


Re: Should be easy

2009-06-12 Thread Saaa

 Saaa wrote:
 I can't figure out how to create the IndexArray function,
 it should work on arrays of any depth

 BaseType!(TArray) index(TArray : TArray[])(TArray array, int[] indices...)
 {
 return index(array[indices[0]], indices[1..$]);
 }

 TElement index(TElement)(TElement element, int[] ignored...)
 {
 return element;
 }

 This uses template specialization to handle arrays differently than 
 non-arrays (that's what the TArray : TArray[] business means). It uses 
 the BaseType template worked out in one of your previous threads. It's 
 close enough to being the simplest thing that could work that I wouldn't 
 bother looking for anything simpler.

I really like templates :D
I actually tried something like this, but I couldn't get it to compile.
My try misses quit a few things I see

void IndexArray(T : T[], U)(T array, U index)
{
  IndexArray( array[index[0]][], index[1..$]);
}

void IndexArray(T : int, U)(T array, U index)
{
  array[index[0]] = -7;//test
}

I did get it to compile at one time, but didn't know how to use it,
like your code..
index( array, index2);  //compiles and all, but how do I set the value?
index( array, index2) = -1; // doesn't work
Also, why the ... ?