Re: [D1,static array] fill static multidimensional array.

2010-10-22 Thread spir
On Fri, 22 Oct 2010 01:50:56 + (UTC)
%u  wrote:

> What is the fastest way to fill a static multidimensional array?
> 
> Looping over all dimension's elements sounds inefficient, especially as a
> static array is essentially continuous memory.
> What is the best practice?
> 
> int[2][2][2] arr = 0;
> arr[] = 3; //Error: cannot implicitly convert expression (3) of type int to
> int[2u][2u][]
> arr[][][] = 3; // this would be fine too :)
> 
> int[2][2][2] arr2 = [[[1,2],[3,4]],[[5,6],[7,8]]];
> 
> Somehow I find it surprising that this copies the whole array.
> arr[] = arr2;

I would use the following feature (from ref):

Array Setting
If a slice operator appears as the lvalue of an assignment expression, and the 
type of the rvalue is the same as the element type of the lvalue, then the 
lvalue's array contents are set to the rvalue.
int[3] s;
int* p;
s[] = 3;// same as s[0] = 3, s[1] = 3, s[2] = 3
p[0..2] = 3;// same as p[0] = 3, p[1] = 3

Using this, one could fill a multidimensional array by setting, so to say, from 
inside to outside. This is actually a variant of "(cast(T[n1*n2*n3])arr)[] = 
e;" that bearophile proposed, but it does not need any low-level hack:

void main () {
int[2][2][2] arr3;
int[2][2] arr2;
int[2] arr1;
arr1[] = 1;
arr2[] = arr1;
arr3[] = arr2;
assert(arr3 == [[[1, 1], [1, 1]], [[1, 1], [1, 1]]]);
}

I find the syntax somewhat wrong, would prefere it explicit like:
arr.fill(element);
or
arrayFill(arr, element);
How does this method perform? would you be kind enough to include it in your 
tests, %u? Even if it's slightly slower than "(cast(int[n1*n2*n3])arr)[] = e;", 
I would use it because I find it cleaner.

Is D reflexive enough to know at runtime the number of dimensions of an array 
and their sizes, and write a generic
arrayDeepFill(arr, element);
where element is the "terminal" value? Or maybe
arrayDeepFill(arr, element, [sizes]);


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



custom exception type

2010-10-22 Thread spir
Hello,

Where can one find descriptions of Throwable, Error, & Exception? (I mean, how 
do you even know they exist?) I could finally guess the constructor must have a 
string parameter used for error output.

Also, is it possible to implicitely reuse the superclass's constructor? I had 
to write:

class E : Exception {
this (string msg) {
super(msg) ;
}
}

E.this performs nothing new. But without it, I get a compiler error:
trial.d(7): Error: constructor trial.E.this no match for implicit super() 
call in constructor
Isn't the constructor inherited like other attributes?

Finally, is it possible to customize the error message construction, using eg 
tostring? A big issue is that, currently, an exception's message is computed at 
construction time, even if the exception will never be thrown, or more 
ccommonly never be output -- because it is caught by a 'catch' clause. In some 
cases, constructing the message can be costly; some programming schemes may 
throw huge numbers of exceptions, all caught (or nearly all).
Example: in a parsing library, pattern match methods throw an instance of 
MatchFailure when matching fails. When there is a pattern choice, there may be 
numerous failures for each success. MatchFailure is just a clean way of 
signaling this fact (*): each failure exception is caught at a higher level to 
allow trying the alternative patterns. Since error messages can be rather 
complicated, constructing them uselessly would multiply parsing time by a 
rather big factor (in my case, ~ X 30!).
I guess tostring is the right feature for this: it would return the exception's 
textual form, ie the message. (For information, this is how Python works.) I 
tried to use it, but it seems to be simply ignored. What is the func/method 
that constructs the text of an exception, eg what is implicitely called by 
"writeln(e);"?


Denis

(*) This is exactly the programming pattern somewhere explained in D docs: 
throw an exception instead of returning a fake value used as failure flag.
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



Re: custom exception type

2010-10-22 Thread Trass3r

E.this performs nothing new. But without it, I get a compiler error:
trial.d(7): Error: constructor trial.E.this no match for implicit  
super() call in constructor

Isn't the constructor inherited like other attributes?


I think if you don't provide a constructor a default one is created:  
this(){}

Then an implicit call to super() is inserted cause there's no explicit one.
But there is no this() without parameters in the Exception class if I'm  
not mistaken. You can look it up in _object.d in the druntime source.


Re: custom exception type

2010-10-22 Thread Lars T. Kyllingstad
On Fri, 22 Oct 2010 13:00:43 +0200, spir wrote:

> Hello,
> 
> Where can one find descriptions of Throwable, Error, & Exception? (I
> mean, how do you even know they exist?) I could finally guess the
> constructor must have a string parameter used for error output.

Well, they should be in the documentation for the 'object' module, but I 
see that they aren't.  Until that is fixed, you can check out the 
source.  Throwable starts at line 1210 here:

  http://www.dsource.org/projects/druntime/browser/trunk/src/object_.d

Exception and Error immediately follow it, but they don't really add 
anything to Throwable.


> Also, is it possible to implicitely reuse the superclass's constructor?
> I had to write:
> 
> class E : Exception {
> this (string msg) {
> super(msg) ;
> }
> }
> 
> E.this performs nothing new. But without it, I get a compiler error:
> trial.d(7): Error: constructor trial.E.this no match for implicit
> super() call in constructor
> Isn't the constructor inherited like other attributes?

No.  With the exception of a no-argument constructor, which Throwable 
doesn't have, you have to call it explicitly with super(...).


> Finally, is it possible to customize the error message construction,
> using eg tostring? A big issue is that, currently, an exception's
> message is computed at construction time, even if the exception will
> never be thrown, or more ccommonly never be output -- because it is
> caught by a 'catch' clause. In some cases, constructing the message can
> be costly; some programming schemes may throw huge numbers of
> exceptions, all caught (or nearly all). Example: in a parsing library,
> pattern match methods throw an instance of MatchFailure when matching
> fails. When there is a pattern choice, there may be numerous failures
> for each success. MatchFailure is just a clean way of signaling this
> fact (*): each failure exception is caught at a higher level to allow
> trying the alternative patterns. Since error messages can be rather
> complicated, constructing them uselessly would multiply parsing time by
> a rather big factor (in my case, ~ X 30!). I guess tostring is the right
> feature for this: it would return the exception's textual form, ie the
> message. (For information, this is how Python works.) I tried to use it,
> but it seems to be simply ignored. What is the func/method that
> constructs the text of an exception, eg what is implicitely called by
> "writeln(e);"?

That would be toString(), not tostring().

-Lars


Re: [D1,static array] fill static multidimensional array.

2010-10-22 Thread Trass3r

Somehow I find it surprising that this copies the whole array.
arr[] = arr2;


Static arrays are value types.


Re: custom exception type

2010-10-22 Thread vano
Although it is somewhat annoying that there is no default value for the 
msg parameter in the first constructor, it is pretty easy to use the 
mixin templates to overcome the issue:



public mixin template ExceptionCtorMixin() {
this(string msg = null, Throwable next = null) { super(msg, next); }
this(string msg, string file, size_t line, Throwable next = null) {
super(msg, file, line, next);
}
}

class MyException : Exception { mixin ExceptionCtorMixin; }



On 22.10.2010 13:00, spir wrote:

Hello,

Where can one find descriptions of Throwable, Error,&  Exception? (I mean, how 
do you even know they exist?) I could finally guess the constructor must have a 
string parameter used for error output.

Also, is it possible to implicitely reuse the superclass's constructor? I had 
to write:

class E : Exception {
 this (string msg) {
 super(msg) ;
 }
}

E.this performs nothing new. But without it, I get a compiler error:
 trial.d(7): Error: constructor trial.E.this no match for implicit super() 
call in constructor
Isn't the constructor inherited like other attributes?

Finally, is it possible to customize the error message construction, using eg 
tostring? A big issue is that, currently, an exception's message is computed at 
construction time, even if the exception will never be thrown, or more 
ccommonly never be output -- because it is caught by a 'catch' clause. In some 
cases, constructing the message can be costly; some programming schemes may 
throw huge numbers of exceptions, all caught (or nearly all).
Example: in a parsing library, pattern match methods throw an instance of 
MatchFailure when matching fails. When there is a pattern choice, there may be 
numerous failures for each success. MatchFailure is just a clean way of 
signaling this fact (*): each failure exception is caught at a higher level to 
allow trying the alternative patterns. Since error messages can be rather 
complicated, constructing them uselessly would multiply parsing time by a 
rather big factor (in my case, ~ X 30!).
I guess tostring is the right feature for this: it would return the exception's textual 
form, ie the message. (For information, this is how Python works.) I tried to use it, but 
it seems to be simply ignored. What is the func/method that constructs the text of an 
exception, eg what is implicitely called by "writeln(e);"?


Denis

(*) This is exactly the programming pattern somewhere explained in D docs: 
throw an exception instead of returning a fake value used as failure flag.
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com





Re: [D1,static array] fill static multidimensional array.

2010-10-22 Thread %u
== Quote from spir (denis.s...@gmail.com)'s article
> How does this method perform? would you be kind enough to include it in you
> r tests, %u? Even if it's slightly slower than "(cast(int[n1*n2*n3])arr)[]
> = e;", I would use it because I find it cleaner.

But it does take up more memory (assuming you don't generate the arrays every 
fill).
And, it performs worse than the casts (with this array length).
If you really want to investigate, I suggest disassembling ;)

Here is the added code:
int[50][50] arr2;
int[50] arr1;

timer.start();
for(int i = 0; i < 10_000; i++) {
arr1[] = i;
arr2[] = arr1;
arr3[] = arr2;
}
timer.stop();
writefln("123 : ",timer.periodCount());

And killing almost every process on my computer the new timings are:
[x][y][z]   : 37..
[x][y][]: 38..
cast(int[*]): 28..
cast(int[1]): 28..
123 : 36..
fill: 28..

> Is D reflexive enough to know at runtime the number of dimensions of an arr
> ay and their sizes, and write a generic
> arrayDeepFill(arr, element);
> where element is the "terminal" value? Or maybe
> arrayDeepFill(arr, element, [sizes]);

Yeah, wouldn't that be nice ?!
arr.Fill(elem);

:D

template BaseType(T: T[]) { alias BaseType!(T) BaseType; }
template BaseType(T) { alias T BaseType; }

void Fill(T,E)(T arr, E elem) {
static assert( isStaticArray!(T) );
static assert( is( BaseType!(T) == E ) );
(cast(E[arr.sizeof / elem.sizeof])arr)[] = elem;
}


[D1,__traits] D1 has __traits?

2010-10-22 Thread %u
http://www.digitalmars.com/d/1.0/template.html
..
assert(__traits(isRef, x[i]));
..
static assert(!__traits(compiles, min(3, y) = 10));

These won't compile with my D1 :(


Re: [D1,__traits] D1 has __traits?

2010-10-22 Thread Jacob Carlborg

On 2010-10-22 20:57, %u wrote:

http://www.digitalmars.com/d/1.0/template.html
..
assert(__traits(isRef, x[i]));
..
static assert(!__traits(compiles, min(3, y) = 10));

These won't compile with my D1 :(


D1 neither has __traits or auto ref. I guess that the section was 
accidentally put in the D1 specification.


--
/Jacob Carlborg


Re: [D1,__traits] D1 has __traits?

2010-10-22 Thread %u
== Quote from Jacob Carlborg (d...@me.com)'s article
> On 2010-10-22 20:57, %u wrote:
> > http://www.digitalmars.com/d/1.0/template.html
> > ..
> > assert(__traits(isRef, x[i]));
> > ..
> > static assert(!__traits(compiles, min(3, y) = 10));
> >
> > These won't compile with my D1 :(
> D1 neither has __traits or auto ref. I guess that the section was
> accidentally put in the D1 specification.
filed