Re: What wrong did i do? (key in hashtable is always null)

2009-12-30 Thread bearophile
The Anh Tran: > This is just a small D exercise. I port c++ knucleotide from > shootout.alioth.debian.org This was my version, maybe it solves some of your problems: http://shootout.alioth.debian.org/debian/benchmark.php?test=knucleotide&lang=gdc&id=2 I haven't used my dlibs here, so for example

What wrong did i do? (key in hashtable is always null)

2009-12-30 Thread The Anh Tran
This is just a small D exercise. I port c++ knucleotide from shootout.alioth.debian.org Issue 1: If i manually listing hashtable contents, the key does exist in that ht. But (key in hash_table) always yield null. Worse, if i use: "auto val = ht[key]", an exception is thrown. Problem code is fr

Re: Is there a reason for default-int?

2009-12-30 Thread Ary Borenszweig
Don wrote: BCS wrote: Hello Ary, Don wrote: Phil Deets wrote: On Mon, 28 Dec 2009 16:18:46 -0500, Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that ma

Memory RAM usage

2009-12-30 Thread bearophile
Today PCs have lot of RAM, so reducing the memory used by a program may seem not so important, but practice shows that reducing the memory used by a program reduces the cache traffic and increases cache coherence, so the code runs faster (I have recently seen a 13X speedup in a not synthetic pro

Re: c to d: types + struct/union alignment

2009-12-30 Thread bearophile
Michael P.: >Also, would short unsigned int be a 'ushort' then?< In D ushort is an unsigned integer 16 bits long. While I think in C short unsigned int is not guaranteed to be 16 bit wide. The same is true for unsigned short / uint. Bye, bearophile

Re: c to d: types + struct/union alignment

2009-12-30 Thread Michael P.
BCS Wrote: > Hello Michael P., > > > I'm converting some C headers into D, and had some questions. > > 1. What is the equivalent of a 'short int' in D? > > e.g: > > struct ScePspSRect { > > short int x; > > short int y; > > short int w; > > short int h; > > } > > short > > > 3. > > type

Re: c to d: types + struct/union alignment

2009-12-30 Thread BCS
Hello Michael P., I'm converting some C headers into D, and had some questions. 1. What is the equivalent of a 'short int' in D? e.g: struct ScePspSRect { short int x; short int y; short int w; short int h; } short 3. typedef uint8_t u8; typede

c to d: types + struct/union alignment

2009-12-30 Thread Michael P.
I'm converting some C headers into D, and had some questions. 1. What is the equivalent of a 'short int' in D? e.g: struct ScePspSRect { short int x; short int y; short int w; short int h; } 2. If I have this union in C: typedef union ScePsp

Re: To init or not to init?

2009-12-30 Thread Don
Stewart Gordon wrote: Don wrote: I think we could get the behaviour you want by changing the definition of T.init. I think it should ALWAYS be a bug to read an uninitialized variable; All variables in D are initialized, unless overridden with a void initializer. But what would "ALWAYS be

To init or not to init? (was: floating point verification using is?)

2009-12-30 Thread Stewart Gordon
Don wrote: I think we could get the behaviour you want by changing the definition of T.init. I think it should ALWAYS be a bug to read an uninitialized variable; All variables in D are initialized, unless overridden with a void initializer. But what would "ALWAYS be a bug" mean? (a) Trig

Re: converting a byte array to a struct array?

2009-12-30 Thread Trass3r
Steven Schveighoffer schrieb: You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose. And the answer (created by bearophile): http://codepad.org/8HnF62s2 Yeah, I know but if you got that data out of some file like the resource sec

Re: converting a byte array to a struct array?

2009-12-30 Thread Trass3r
grauzone schrieb: First the anti-feature: [0xAB, ...] will yield an int[], not a ubyte[] (I guess ubyte[] is what you're expecting). If you cast two arrays, the compiler will reinterpret cast all data. The result won't be what you intended. Yeah I already encountered that problem. That's why

Re: Is there a reason for default-int?

2009-12-30 Thread Don
BCS wrote: Hello Ary, Don wrote: Phil Deets wrote: On Mon, 28 Dec 2009 16:18:46 -0500, Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense?

Re: floating point verification using is?

2009-12-30 Thread Don
Steven Schveighoffer wrote: On Mon, 28 Dec 2009 21:52:24 -0500, Don wrote: Steven Schveighoffer wrote: Whether you like it or not, the runtime must initialize it to something, even if the value means "uninitialized". The key question is: is it valid to read an uninitialized variable? I wou

Re: Is there a reason for default-int?

2009-12-30 Thread BCS
Hello Ary, Don wrote: Phil Deets wrote: On Mon, 28 Dec 2009 16:18:46 -0500, Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? C++ abandone

Re: Is there a reason for default-int?

2009-12-30 Thread BCS
Hello Don, Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? There's now an Error type in the compiler. It's gradually filtering its way throug

Re: define new types within templates

2009-12-30 Thread BCS
Hello teo, There was a way to define new types within templates and I think that I have seen that demonstrated here in the newsgroups, but cannot find it now. Can someone help me please? I would like to do something like this: template MyTemplate(T) { struct T ~ "Struct" // define FooStruct s

Re: define new types within templates

2009-12-30 Thread grauzone
teo wrote: There was a way to define new types within templates and I think that I have seen that demonstrated here in the newsgroups, but cannot find it now. Can someone help me please? I would like to do something like this: template MyTemplate(T) { struct T ~ "Struct" // define FooStruc

Re: converting a byte array to a struct array?

2009-12-30 Thread grauzone
Trass3r wrote: I got some RGB palette in a byte array which I'd like to convert or "map" to an RGB struct array, isn't this easily possible without using dozens of struct constructors? RGB[256] PALETTE = cast(RGB[256]) [ 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0xCF, 0x4B, 0x07, 0xBF, 0x43

define new types within templates

2009-12-30 Thread teo
There was a way to define new types within templates and I think that I have seen that demonstrated here in the newsgroups, but cannot find it now. Can someone help me please? I would like to do something like this: template MyTemplate(T) { struct T ~ "Struct" // define FooStruct structure

Re: define new types within templates

2009-12-30 Thread Phil Deets
On Tue, 29 Dec 2009 17:44:18 -0500, teo wrote: There was a way to define new types within templates and I think that I have seen that demonstrated here in the newsgroups, but cannot find it now. Can someone help me please? I would like to do something like this: template MyTemplate(T) { s

Re: converting a byte array to a struct array?

2009-12-30 Thread Steven Schveighoffer
On Tue, 29 Dec 2009 10:37:43 -0500, Steven Schveighoffer wrote: On Tue, 29 Dec 2009 10:03:50 -0500, Trass3r wrote: Steven Schveighoffer schrieb: What is RGB's structure? I would expect something like this to work: RGB[256] PALETTE = [ {0x00, 0x00, 0x00}, {0xE3, 0x53, 0x00}, ... ]; Assu

Re: property/opAddAssign problem

2009-12-30 Thread Steven Schveighoffer
On Tue, 29 Dec 2009 13:44:18 -0500, teo wrote: On Tue, 29 Dec 2009 09:12:15 -0500, Steven Schveighoffer wrote: Eventually, marking Prop as a @property should also work (haven't tested it, it may work now): @property A Prop() { return a; } ... b.Prop += 3; // compiles -Steve Unfortunat

Re: property/opAddAssign problem

2009-12-30 Thread teo
On Tue, 29 Dec 2009 09:12:15 -0500, Steven Schveighoffer wrote: > On Tue, 29 Dec 2009 09:10:14 -0500, Steven Schveighoffer > wrote: > >> On Tue, 29 Dec 2009 09:01:38 -0500, teo >> wrote: >> >>> Consider following: >>> >>> class A >>> { >>> int m; >>> void opAddAssign(int n) { m += n; } >>>

Re: Is there a reason for default-int?

2009-12-30 Thread Ary Borenszweig
Don wrote: Phil Deets wrote: On Mon, 28 Dec 2009 16:18:46 -0500, Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? C++ abandoned default

Re: Is there a reason for default-int?

2009-12-30 Thread Simen kjaeraas
On Tue, 29 Dec 2009 07:37:42 +0100, Don wrote: Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? There's now an Error type in the compile

Re: converting a byte array to a struct array?

2009-12-30 Thread Steven Schveighoffer
On Tue, 29 Dec 2009 10:03:50 -0500, Trass3r wrote: Steven Schveighoffer schrieb: What is RGB's structure? I would expect something like this to work: RGB[256] PALETTE = [ {0x00, 0x00, 0x00}, {0xE3, 0x53, 0x00}, ... ]; Assuming RGB is a struct of 3 ubyte members... Yeah, simply 3 ubytes.

Re: converting a byte array to a struct array?

2009-12-30 Thread Trass3r
Steven Schveighoffer schrieb: What is RGB's structure? I would expect something like this to work: RGB[256] PALETTE = [ {0x00, 0x00, 0x00}, {0xE3, 0x53, 0x00}, ... ]; Assuming RGB is a struct of 3 ubyte members... Yeah, simply 3 ubytes. But IIRC I read that struct literals will be removed i

Re: property/opAddAssign problem

2009-12-30 Thread Steven Schveighoffer
On Tue, 29 Dec 2009 09:10:14 -0500, Steven Schveighoffer wrote: On Tue, 29 Dec 2009 09:01:38 -0500, teo wrote: Consider following: class A { int m; void opAddAssign(int n) { m += n; } } class B { A a; this() { a = new A; } A Prop() { return a; } } void main() { B b = new B

Re: converting a byte array to a struct array?

2009-12-30 Thread Steven Schveighoffer
On Tue, 29 Dec 2009 07:56:04 -0500, Trass3r wrote: I got some RGB palette in a byte array which I'd like to convert or "map" to an RGB struct array, isn't this easily possible without using dozens of struct constructors? RGB[256] PALETTE = cast(RGB[256]) [ 0x00, 0x00, 0x00, 0xE3, 0x5

Re: property/opAddAssign problem

2009-12-30 Thread Steven Schveighoffer
On Tue, 29 Dec 2009 09:01:38 -0500, teo wrote: Consider following: class A { int m; void opAddAssign(int n) { m += n; } } class B { A a; this() { a = new A; } A Prop() { return a; } } void main() { B b = new B; b.Prop += 3; } I get a compilation error (dmd v2.037): test.d(1

Re: converting a byte array to a struct array?

2009-12-30 Thread Trass3r
Just remembered the old casting hack: RGB[] PALETTE = (cast(RGB*) cast(ubyte[]) [...])[0..256]; It works but is there perhaps another nicer possibility?

property/opAddAssign problem

2009-12-30 Thread teo
Consider following: class A { int m; void opAddAssign(int n) { m += n; } } class B { A a; this() { a = new A; } A Prop() { return a; } } void main() { B b = new B; b.Prop += 3; } I get a compilation error (dmd v2.037): test.d(17): Error: 'b.Prop' is not a scalar, it is a A() test

converting a byte array to a struct array?

2009-12-30 Thread Trass3r
I got some RGB palette in a byte array which I'd like to convert or "map" to an RGB struct array, isn't this easily possible without using dozens of struct constructors? RGB[256] PALETTE = cast(RGB[256]) [ 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00, 0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ... doe

Re: floating point verification using is?

2009-12-30 Thread Jérôme M. Berger
Don wrote: Steven Schveighoffer wrote: That value should always be consistent. There has to be a way to verify that the value was set properly. Otherwise, how do you verify things like communication protocols or expectations for runtime functions? I think that sending an uninitialized vari

Re: floating point verification using is?

2009-12-30 Thread Steven Schveighoffer
On Mon, 28 Dec 2009 21:52:24 -0500, Don wrote: Steven Schveighoffer wrote: Whether you like it or not, the runtime must initialize it to something, even if the value means "uninitialized". The key question is: is it valid to read an uninitialized variable? I would hope the compiler would

Re: Is there a reason for default-int?

2009-12-30 Thread Don
Phil Deets wrote: On Mon, 28 Dec 2009 16:18:46 -0500, Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? C++ abandoned default int. I think

Re: Is there a reason for default-int?

2009-12-30 Thread Don
Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? There's now an Error type in the compiler. It's gradually filtering its way through the com

Re: struct construction (how?)

2009-12-30 Thread Don
Ali Çehreli wrote: bearophile wrote: > Ali Çehreli: >> auto s = S(1, 2); Doesn't work for structs that have opCall (or maybe an opCall with matching parameters to that use). > And by the way, that's the idiomatic way to initialize a struct in D. Excellent! That's the way I have chosen and

Re: Is there a reason for default-int?

2009-12-30 Thread Phil Deets
On Mon, 28 Dec 2009 16:18:46 -0500, Simen kjaeraas wrote: Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? C++ abandoned default int. I think it would be bes

Re: floating point verification using is?

2009-12-30 Thread Don
Steven Schveighoffer wrote: On Mon, 28 Dec 2009 06:51:30 -0500, Don wrote: Stewart Gordon wrote: Steven Schveighoffer wrote: Are you still working on this? :) I think this proves my point. The compiler does not provide an easy way to compare floats bitwise, so this means convoluted hard

Re: struct construction (how?)

2009-12-30 Thread bearophile
Ali Çehreli: > As 'this' is not available in static member functions; you mean Yes, of course, I am sorry. You know about D as much as me or more, so it's time for me to shut up :-) Bye, bearophile

Re: struct construction (how?)

2009-12-30 Thread Ali Çehreli
bearophile wrote: > Ali Çehreli: > >> >> auto s = S(1, 2); >> >> Doesn't work for structs that have opCall (or maybe an opCall with >> matching parameters to that use). > > Try: > static S opCall(int x, int y) { this.x = x; this.y = y; } As 'this' is not available in static member functions; you

Re: struct members not default initialized?

2009-12-30 Thread Ali Çehreli
bearophile wrote: > This prints 42 0.00 with the latest dmd2 and 42 nan with an older dmd1. You are unlucky then... ;) The following program leaves random values in s.d with dmd 2.037. Here is one output: 42 -0.008821 > It can be a bug: Yes it is! :) http://d.puremagic.com/issues/sh

Is there a reason for default-int?

2009-12-30 Thread Simen kjaeraas
Apart from C legacy, is there a reason to assume anything we don't know what is, is an int? Shouldn't the compiler instead say 'unknown type' or something else that makes sense? -- Simen

Re: struct construction (how?)

2009-12-30 Thread Steven Schveighoffer
On Mon, 28 Dec 2009 10:40:58 -0500, Ali Çehreli wrote: One issue remains, which prompted me to open this thread in the first place: I wanted to experiment with defining opCall for that struct: struct S { int x; int y; const int opCall(int p0, int p1) { return p

Re: struct construction (how?)

2009-12-30 Thread bearophile
Ali Çehreli: > >> auto s = S(1, 2); > > Doesn't work for structs that have opCall (or maybe an opCall with > matching parameters to that use). Try: static S opCall(int x, int y) { this.x = x; this.y = y; } > I think the {} should still default initialize the > remaining members (like C and

Re: D2: Template as an alias to another template instance

2009-12-30 Thread Stanislav Blinov
Steven Schveighoffer wrote: [irrelevant text cut out] ...I'm having some difficulties. Consider this code: --- struct Templ(int N,T){} template Templ1(T) { alias Templ!(1,T) Templ1; } void foo2(T)(Templ1!T t){} void main() { Templ1!(float) t1f1; foo2(t1f1); // this doesn't com

Re: struct construction (how?)

2009-12-30 Thread Ali Çehreli
bearophile wrote: > Ali Çehreli: >> auto s = S(1, 2); Doesn't work for structs that have opCall (or maybe an opCall with matching parameters to that use). > And by the way, that's the idiomatic way to initialize a struct in D. Excellent! That's the way I have chosen and have been using in my

Re: D2: Template as an alias to another template instance

2009-12-30 Thread Steven Schveighoffer
On Thu, 24 Dec 2009 12:45:41 -0500, Stanislav Blinov wrote: Hello, I'm trying to make a template that would be an alias to another template instance (sort of a shortcut), but I'm having some difficulties. Consider this code: --- import std.stdio; struct Templ(int N,T){} alias Templ!

Re: floating point verification using is?

2009-12-30 Thread Steven Schveighoffer
On Mon, 28 Dec 2009 06:51:30 -0500, Don wrote: Stewart Gordon wrote: Steven Schveighoffer wrote: Are you still working on this? :) I think this proves my point. The compiler does not provide an easy way to compare floats bitwise, so this means convoluted hard-to-write code. When we ha

Re: floating point verification using is?

2009-12-30 Thread Don
Stewart Gordon wrote: Steven Schveighoffer wrote: Are you still working on this? :) I think this proves my point. The compiler does not provide an easy way to compare floats bitwise, so this means convoluted hard-to-write code. When we have two operators that do equality -- and one of tho

Re: struct construction (how?)

2009-12-30 Thread bearophile
Ali Çehreli: > auto s = S(1, 2); And by the way, that's the idiomatic way to initialize a struct in D. If you want to statically initialize it, there are other ways I've shown you. Bye, bearophile

Re: Runtime error when accessing static data in a DLL

2009-12-30 Thread Don
Phil Deets wrote: On Thu, 24 Dec 2009 13:10:14 -0500, Phil Deets wrote: On Thu, 24 Dec 2009 12:49:42 -0500, Richard Webb wrote: Sounds like you might be running into this: http://d.puremagic.com/issues/show_bug.cgi?id=3342 Thanks for the link. That is probably my problem since I'm runnin

Re: struct construction (how?)

2009-12-30 Thread bearophile
Ali Çehreli: > S s = { 1 }; > > Not good; because we may forget to provide initializers and the > remaining members are not even default initialized. All fields get initialized, unless explicitly told to not be initialized (and sometimes they get initialized even if you explicitly tell them t

Re: floating point verification using is?

2009-12-30 Thread Stewart Gordon
Steven Schveighoffer wrote: Are you still working on this? :) I think this proves my point. The compiler does not provide an easy way to compare floats bitwise, so this means convoluted hard-to-write code. When we have two operators that do equality -- and one of those means bitwise compar

struct construction (how?)

2009-12-30 Thread Ali Çehreli
Coming from C++ and reading the D2 spec, I realized that I don't understand struct construction yet. :) I am playing with dmd 2.037. Considering struct S { int x; int y; } which may have a this(int x, int y) defined: 1) S s = { 1 }; Not good; because we may forget to provide i