Re: Is my T.init elaborate (eg: Not 0)

2014-05-06 Thread monarch_dodra via Digitalmars-d-learn

On Monday, 5 May 2014 at 21:54:33 UTC, Tobias Pankrath wrote:

On Monday, 5 May 2014 at 21:51:14 UTC, Tobias Pankrath wrote
I'm trying to find the same result, without a runtime check. 
Anybody know how? I'd write a function that tests the bits of 
a T.init copy, but reinterpreting is not allowed during 
CTFE...


Foreach member x of Type X of T, check if T.init.x equals 
X.init?


Ah, sorry, didn't read carefully. This is not what you want.


Actually, I think you're on to something. I can recursively 
iterate on all basic types in my T, and check they are all 0 or 
NULL. Should work.


Thanks!


Re: Is it possible to check if a type is an instance of a template?

2014-05-06 Thread Andrej Mitrovic via Digitalmars-d-learn
On 5/6/14, bearophile via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 There is now std.traits.isInstanceOf that could do what you need.

Someone resurrected a thread from 2011. Of course there's isInstanceOf
when I added it myself at the end of 2012.


Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread bearophile via Digitalmars-d-learn

Mark Isaacson:

I'm trying my hand at reading from standard input and having 
little luck. In particular, I would like to be able to do the 
rough equivalent of C++'s:


cin  myString;


There isn't always a 1:1 mapping between C++ and D. In D if you 
want a single word you usually read the whole line (with a 
byLine) and then you use split or splitter to take the single 
words.


Something like this (untested):

foreach (line; stdin.byLine) {
immutable words = line.chomp.idup.split;
}

Bye,
bearophile


Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread Mark Isaacson via Digitalmars-d-learn
Fair enough. I've done stuff like that in the past. I'm trying to 
implement a university project that was originally designed for 
C++ style I/O... and so where I'd have otherwise jumped at 
something like that from the beginning, my hands are slightly 
tied.


Suppose I'll make due/not fully comply with the spec.


Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread bearophile via Digitalmars-d-learn

Mark Isaacson:

Fair enough. I've done stuff like that in the past. I'm trying 
to implement a university project that was originally designed 
for C++ style I/O... and so where I'd have otherwise jumped at 
something like that from the beginning, my hands are slightly 
tied.


If you need/want to use the C++ style, even if it's not fully 
Phobos-idiomatic, probably you can invent some way to simulate it.


Bye,
bearophile


Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread Mark Isaacson via Digitalmars-d-learn
Indeed. However, doing so looks more painful than redefining my 
goals. Upon further examination it seems that I had more 
flexibility than I originally estimated. Besides, the real reason 
I'm implementing this project is just to practice for when I get 
to write production D code in a week anyway; I'd rather learn the 
idioms.


Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread bearophile via Digitalmars-d-learn

Mark Isaacson:

Indeed. However, doing so looks more painful than redefining my 
goals. Upon further examination it seems that I had more 
flexibility than I originally estimated. Besides, the real 
reason I'm implementing this project is just to practice for 
when I get to write production D code in a week anyway; I'd 
rather learn the idioms.


OK. Once you are done with this little project you can post a 
link here, I can review the code a little if you want.


Bye,
bearophile


Re: Implicit static-dynamic arr and modifying

2014-05-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Mon, 05 May 2014 22:16:58 -0400
Nick Sabalausky via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:

 On 5/5/2014 10:11 PM, Nick Sabalausky wrote:
  Is this kinds stuff a sane thing to do, or does it just work by
  accident?:
 
  void modify(ubyte[] dynamicArr)
  {
   dynamicArr[$-1] = 5;
  }
 
  void main()
  {
   ubyte[4] staticArr = [1,1,1,1];
   modify(staticArr);
   assert(staticArr == [1,1,1,5]);
  }

 Duh, it's just using a normal slice of the static array...

 // Roughly:
 dynamicArr.ptr = staticArr;
 dynamicArr.length = typeof(staticArr).sizeof;

 So all is well, and deliberately so. Pardon the noise.

It's definitely deliberate, though I think that it's a flaw in the language's
design. IMHO, static arrays should never be automatically sliced, but
unfortunately, changing that would break too much code at this point. The
biggest problem is the fact that it's inherently unsafe, though unfortunately,
the compiler currently considers it @safe:

https://issues.dlang.org/show_bug.cgi?id=8838

- Jonathan M Davis


The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread FrankLike via Digitalmars-d-learn

Hi,everyone,

I find the The writeln()  function's args  can't be [一 ,二]?
why?

Thank you.

Frank.


Re: The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread JR via Digitalmars-d-learn

On Tuesday, 6 May 2014 at 09:43:10 UTC, FrankLike wrote:

Hi,everyone,

I find the The writeln()  function's args  can't be [一 ,二]?
why?

Thank you.

Frank.


The problem is that you have a wide-character comma (,) there.

This works:

void main() {
writeln([一, 二]);
}


Re: Global variables read at compile time?

2014-05-06 Thread Suliman via Digitalmars-d-learn
I have got same error. I need to pass in instance of class 
constant, but got error Error: static variable  cannot be read 
at compile


http://www.everfall.com/paste/id.php?1mc9mb9cxyie

When I had create instance of class in main, and create 
confvarible above it all worked, but when I had moved it's in 
module I got error.


Re: Need help with movement from C to D

2014-05-06 Thread Andrey via Digitalmars-d-learn

On Monday, 5 May 2014 at 17:55:37 UTC, Meta wrote:



enum offsetof(T, string field) = mixin(type.stringof ~ . ~ 
field ~ .offsetof);


To ensure that a syntactically valid symbol is passed as the 
type.


Interestingly, but this code doesn't compile:

enum offsetof(typenfield) = mixin(type.stringof ~ .offsetof);

writeln(offsetof!(StrToBob.bob));



Simple matching on a range

2014-05-06 Thread bearophile via Digitalmars-d-learn

Is it a good idea to add a function like this to Phobos?

This is just a first draft of the idea.


void main() {
import std.stdio, std.algorithm, std.range, range_matcher;

auto primes = iota(2, uint.max)
  .filter!(x = iota(2, x)
.all!(t = x % t != 0));

auto twinPrimes = primes.rangeMatch!(q{x, x + 2},
 q{ [x, x + 2] });
twinPrimes.take(20).writeln;

immutable arr = [1, 2, 1, 3, 5, 3];
// All items of arr that appear twice or more:
arr.rangeMatch!(q{x, $_, x}, q{ x }).writeln;
}


Output:

[[3, 5], [5, 7], [11, 13], [17, 19], [29, 31], [41, 43], [59, 
61], [71, 73], [101, 103], [107, 109]]

[1, 3]


rangeMatch takes three arguments, the first is a range of items. 
The second is a string that encodes what to match, and the third 
is a string that generates the output from the matched data.


So q{x, x + 2} matches any value x followed by x + 2. This is the 
definition of twin prime, if the input sequence is of primes.



q{x, $__, x} matches any x, followed by 1 or more other items, 
followed by the same x.

$_ means 0 or more.

Bye,
bearophile


Re: Need help with movement from C to D

2014-05-06 Thread via Digitalmars-d-learn

On Tuesday, 6 May 2014 at 11:09:37 UTC, Andrey wrote:

On Monday, 5 May 2014 at 17:55:37 UTC, Meta wrote:



enum offsetof(T, string field) = mixin(type.stringof ~ . ~ 
field ~ .offsetof);


To ensure that a syntactically valid symbol is passed as the 
type.


Interestingly, but this code doesn't compile:

enum offsetof(typenfield) = mixin(type.stringof ~ .offsetof);

writeln(offsetof!(StrToBob.bob));


That's because `StrToBob.bob.stringof` is bob. You can use this 
instead:


import std.traits;
enum offsetof(alias typenfield) =
mixin(fullyQualifiedName!(typenfield) ~ .offsetof);


Re: Create many objects using threads

2014-05-06 Thread hardcoremore via Digitalmars-d-learn

On Tuesday, 6 May 2014 at 03:26:52 UTC, Ali Çehreli wrote:

On 05/05/2014 04:32 PM, Caslav Sabani wrote:

 So basically using threads in D for creating multiple
instances of class is
 actually slower.

Not at all! That statement can be true only in certain 
programs. :)


Ali



But what does exactly means that Garbage Collector blocks? What
does it blocks and in which way?


And can I use threads to create multiple instance faster or that 
is just not possible?




Thanks



Re: The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread FrankLike via Digitalmars-d-learn

The problem is that you have a wide-character comma (,) there.

This works:

void main() {
writeln([一, 二]);
}


No,I mean the execute result is error.That doesn't get the [一, 
二],but get the [涓C,浜?].


Why?

Thank you.

Frank.


Re: Global variables read at compile time?

2014-05-06 Thread Ali Çehreli via Digitalmars-d-learn

On 05/06/2014 03:16 AM, Suliman wrote:

 When I had create instance of class in main, and create confvarible
 above it all worked, but when I had moved it's in module I got error.

There is module 'static this()' for such runtime initialization:

Config config;

static this()
{
config = new Config(confname);
if (config.isconfigexists())
writeln(config exist);
else
writeln(config do not exists);
}

void main()
{}

Ali



Re: Need help with movement from C to D

2014-05-06 Thread Artur Skawina via Digitalmars-d-learn
I'm not sure why you'd want to wrap the .offsetof expression in
a template, but it can easily be done like this:

   enum offsetOf(alias A, string S) = mixin(A.~S~.offsetof);


Keep in mind that D's offsetof is flawed - if the object does not
contain the requested member, but implicitly converts to another one
that does have such field then the expression compiles, but yields
a bogus value. Eg

   struct S { int a, b, c; S2 s2; alias s2 this; }
   struct S2 { int d, e; }
   static assert(S.e.offsetof==4); // Oops.

artur


Re: Need help with movement from C to D

2014-05-06 Thread bearophile via Digitalmars-d-learn

Artur Skawina:

Keep in mind that D's offsetof is flawed - if the object does 
not contain the requested member, but implicitly converts to 
another one that does have such field then the expression

compiles, but yields a bogus value. Eg

   struct S { int a, b, c; S2 s2; alias s2 this; }
   struct S2 { int d, e; }
   static assert(S.e.offsetof==4); // Oops.


Is this in Bugzilla?

Bye,
bearophile


Re: Global variables read at compile time?

2014-05-06 Thread Suliman via Digitalmars-d-learn
Thanks! But is there any other solution? I am thinking that I am 
trying to specify config name by wrong way...


Re: The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread via Digitalmars-d-learn

On Tuesday, 6 May 2014 at 13:35:57 UTC, FrankLike wrote:

The problem is that you have a wide-character comma (,) there.

This works:

   void main() {
   writeln([一, 二]);
   }


No,I mean the execute result is error.That doesn't get the 
[一, 二],but get the [涓C,浜?].


Why?

Thank you.

Frank.


It works for me (Linux). If you're on Windows, it could have 
something to do with Windows' handling of Unicode, but I don't 
know enough about that to help you. There were posts about this 
in this newsgroup, maybe you can find them, or someone else 
remembers and can tell you directly...


Re: Need help with movement from C to D

2014-05-06 Thread via Digitalmars-d-learn
On Tuesday, 6 May 2014 at 14:25:01 UTC, Artur Skawina via 
Digitalmars-d-learn wrote:

I'm not sure why you'd want to wrap the .offsetof expression in
a template, but it can easily be done like this:

   enum offsetOf(alias A, string S) = mixin(A.~S~.offsetof);


Great, that's even shorter.

Somehow I was fixated on converting the symbol to a string first, 
but of course the name is directly available in the mixin:


enum offsetof(alias typenfield) = 
mixin(typenfield.offsetof);


Re: The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread Regan Heath via Digitalmars-d-learn

On Tue, 06 May 2014 15:48:44 +0100, Marc Schütz schue...@gmx.net wrote:


On Tuesday, 6 May 2014 at 13:35:57 UTC, FrankLike wrote:

The problem is that you have a wide-character comma (,) there.

This works:

   void main() {
   writeln([一, 二]);
   }


No,I mean the execute result is error.That doesn't get the [一,  
二],but get the [涓C,浜?].


Why?

Thank you.

Frank.


It works for me (Linux). If you're on Windows, it could have something  
to do with Windows' handling of Unicode, but I don't know enough about  
that to help you. There were posts about this in this newsgroup, maybe  
you can find them, or someone else remembers and can tell you directly...


IIRC you need to type chcp 65001 and set the command prompt to the  
Lucida font...


R

--
Using Opera's revolutionary email client: http://www.opera.com/mail/


Re: Need help with movement from C to D

2014-05-06 Thread Artur Skawina via Digitalmars-d-learn
On 05/06/14 16:45, via Digitalmars-d-learn wrote:
 On Tuesday, 6 May 2014 at 14:25:01 UTC, Artur Skawina via Digitalmars-d-learn 
 wrote:
 I'm not sure why you'd want to wrap the .offsetof expression in
 a template, but it can easily be done like this:

enum offsetOf(alias A, string S) = mixin(A.~S~.offsetof);
 
 Great, that's even shorter.
 
 Somehow I was fixated on converting the symbol to a string first, but of 
 course the name is directly available in the mixin:
 
 enum offsetof(alias typenfield) = mixin(typenfield.offsetof);

I didn't realize that worked, but it does. So...

   enum offsetOf(alias A) = A.offsetof;


But I have no idea why anybody would want to wrap this trivial
expression like that. 

And, I have no idea if the, hmm, /unconventional/ D offsetof semantics
are in the bugzilla. It's not really a bug, but a design mistake...

artur


Re: Need help with movement from C to D

2014-05-06 Thread bearophile via Digitalmars-d-learn

Artur Skawina:

And, I have no idea if the, hmm, /unconventional/ D offsetof 
semantics
are in the bugzilla. It's not really a bug, but a design 
mistake...


Design mistakes are valid bugzilla entries. At worst the bad 
behavior could be documented. But often it's possible to fix the 
design too, with a small breaking change.


Bye,
bearophile


Re: Implicit static-dynamic arr and modifying

2014-05-06 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, May 06, 2014 at 01:06:14AM -0700, Jonathan M Davis via 
Digitalmars-d-learn wrote:
 On Mon, 05 May 2014 22:16:58 -0400
 Nick Sabalausky via Digitalmars-d-learn
 digitalmars-d-learn@puremagic.com wrote:
 
  On 5/5/2014 10:11 PM, Nick Sabalausky wrote:
   Is this kinds stuff a sane thing to do, or does it just work by
   accident?:
  
   void modify(ubyte[] dynamicArr)
   {
dynamicArr[$-1] = 5;
   }
  
   void main()
   {
ubyte[4] staticArr = [1,1,1,1];
modify(staticArr);
assert(staticArr == [1,1,1,5]);
   }
 
  Duh, it's just using a normal slice of the static array...
 
  // Roughly:
  dynamicArr.ptr = staticArr;
  dynamicArr.length = typeof(staticArr).sizeof;
 
  So all is well, and deliberately so. Pardon the noise.
 
 It's definitely deliberate, though I think that it's a flaw in the
 language's design. IMHO, static arrays should never be automatically
 sliced, but unfortunately, changing that would break too much code at
 this point. The biggest problem is the fact that it's inherently
 unsafe, though unfortunately, the compiler currently considers it
 @safe:
 
 https://issues.dlang.org/show_bug.cgi?id=8838
[...]

A particularly pernicious instance of this hole is the following:

class C {
int[] data;
this(int[] args...) @safe {
data = args;
}
}

C f() @safe {
return new C(1,2,3);
}

void main() {
import std.stdio;
writeln(f().data); // on my system, writes garbage
}

Exercise for the reader: spot the bug.


T

-- 
English has the lovely word defenestrate, meaning to execute by
throwing someone out a window, or more recently to remove Windows from
a computer and replace it with something useful. :-) -- John Cowan


Re: Implicit static-dynamic arr and modifying

2014-05-06 Thread bearophile via Digitalmars-d-learn

H. S. Teoh:


Exercise for the reader: spot the bug.


https://issues.dlang.org/show_bug.cgi?id=5212
https://issues.dlang.org/show_bug.cgi?id=11657

Bye,
bearophile


Re: Create many objects using threads

2014-05-06 Thread Kapps via Digitalmars-d-learn

On Monday, 5 May 2014 at 22:11:39 UTC, Ali Çehreli wrote:

On 05/05/2014 02:38 PM, Kapps wrote:

 I think that the GC actually blocks when
 creating objects, and thus multiple threads creating
instances would not
 provide a significant speedup, possibly even a slowdown.

Wow! That is the case. :)

 You'd want to benchmark this to be certain it helps.

I did:

import std.range;
import std.parallelism;

class C
{}

void foo()
{
auto c = new C;
}

void main(string[] args)
{
enum totalElements = 10_000_000;

if (args.length  1) {
foreach (i; iota(totalElements).parallel) {
foo();
}

} else {
foreach (i; iota(totalElements)) {
foo();
}
}
}

Typical run on my system for -O -noboundscheck -inline:

$ time ./deneme parallel

real0m4.236s
user0m4.325s
sys 0m9.795s

$ time ./deneme

real0m0.753s
user0m0.748s
sys 0m0.003s

Ali


Huh, that's a much, much, higher impact than I'd expected.
I tried with GDC as well (the one in Debian stable, which is 
unfortunately still 2.055...) and got similar results. I also 
tried creating only totalCPUs threads and having each of them 
create NUM_ELEMENTS / totalCPUs objects rather than risking that 
each creation was a task, and it still seems to be the same.


Using malloc and emplace instead of new D, results are about 50% 
faster for single-threadeded and ~3-4 times faster for 
multi-threaded (4 cpu 8 thread machine, Linux 64-bit). The 
multi-threaded version is still twice as slow though. On my 
Windows laptop (with the program compiled for 32-bit), it did not 
make a significant difference and the multi-threaded version is 
still 4 times slower.


That being said, I think most malloc implementations while being 
thread-safe, usually use locks or do not scale well.


Code:
import std.range;
import std.parallelism;
import std.datetime;
import std.stdio;
import core.stdc.stdlib;
import std.conv;

class C {}

void foo() {
//auto c = new C;
enum size = __traits(classInstanceSize, C);
void[] mem = malloc(size)[0..size];
emplace!C(mem);
}

void createFoos(size_t count) {
foreach(i; 0 .. count) {
foo();
}
}

void main(string[] args) {
StopWatch sw = StopWatch(AutoStart.yes);
enum totalElements = 10_000_000;
if (args.length = 1) {
foreach (i; iota(totalElements)) {
foo();
}
} else if(args[1] == tasks) {
foreach (i; parallel(iota(totalElements))) {
foo();
}
} else if(args[1] == parallel) {
for(int i = 0; i  totalCPUs; i++) {
taskPool.put(task(createFoos, totalElements / 
totalCPUs));

}
taskPool.finish(true);
} else
writeln(Unknown argument ', args[1], '.);
sw.stop();
writeln(cast(Duration)sw.peek);
}

Results (Linux 64-bit):
shardsoft:~$ dmd -O -inline -release test.d
shardsoft:~$ ./test
552 ms, 729 μs, and 7 hnsecs
shardsoft:~$ ./test
532 ms, 139 μs, and 5 hnsecs
shardsoft:~$ ./test tasks
1 sec, 171 ms, 126 μs, and 4 hnsecs
shardsoft:~$ ./test tasks
1 sec, 38 ms, 468 μs, and 6 hnsecs
shardsoft:~$ ./test parallel
1 sec, 146 ms, 738 μs, and 2 hnsecs
shardsoft:~$ ./test parallel
1 sec, 268 ms, 195 μs, and 3 hnsecs





Re: Global variables read at compile time?

2014-05-06 Thread Ali Çehreli via Digitalmars-d-learn

On 05/06/2014 07:40 AM, Suliman wrote:

Thanks! But is there any other solution? I am thinking that I am trying
to specify config name by wrong way...


Sorry, I don't understand what exactly you are trying to do. :( Is 
config file a compile-time concept? Do you want to read it at compile time?


You can also include the contents of a file at compile-time:

   import (somefile.d)

The compiler will read the file and insert its contents where the import 
statement appears.


On the other hand, if it is available only at run-time, you obviously 
have to read it at run time.


Ali



Re: Create many objects using threads

2014-05-06 Thread Kapps via Digitalmars-d-learn

On Tuesday, 6 May 2014 at 15:56:11 UTC, Kapps wrote:

On Monday, 5 May 2014 at 22:11:39 UTC, Ali Çehreli wrote:

On 05/05/2014 02:38 PM, Kapps wrote:

 I think that the GC actually blocks when
 creating objects, and thus multiple threads creating
instances would not
 provide a significant speedup, possibly even a slowdown.

Wow! That is the case. :)

 You'd want to benchmark this to be certain it helps.

I did:

import std.range;
import std.parallelism;

class C
{}

void foo()
{
   auto c = new C;
}

void main(string[] args)
{
   enum totalElements = 10_000_000;

   if (args.length  1) {
   foreach (i; iota(totalElements).parallel) {
   foo();
   }

   } else {
   foreach (i; iota(totalElements)) {
   foo();
   }
   }
}

Typical run on my system for -O -noboundscheck -inline:

$ time ./deneme parallel

real0m4.236s
user0m4.325s
sys 0m9.795s

$ time ./deneme

real0m0.753s
user0m0.748s
sys 0m0.003s

Ali


Huh, that's a much, much, higher impact than I'd expected.
I tried with GDC as well (the one in Debian stable, which is 
unfortunately still 2.055...) and got similar results. I also 
tried creating only totalCPUs threads and having each of them 
create NUM_ELEMENTS / totalCPUs objects rather than risking 
that each creation was a task, and it still seems to be the 
same.


snip


I tried with using an allocator that never releases memory, 
rounds up to a power of 2, and is lock-free. The results are 
quite a bit better.


shardsoft:~$ ./test
1 sec, 47 ms, 474 μs, and 4 hnsecs
shardsoft:~$ ./test
1 sec, 43 ms, 588 μs, and 2 hnsecs
shardsoft:~$ ./test tasks
692 ms, 769 μs, and 8 hnsecs
shardsoft:~$ ./test tasks
692 ms, 686 μs, and 8 hnsecs
shardsoft:~$ ./test parallel
691 ms, 856 μs, and 9 hnsecs
shardsoft:~$ ./test parallel
690 ms, 22 μs, and 3 hnsecs

I get similar results on my laptop (which is much faster than the 
results I got on it using DMD's malloc):

test

1 sec, 125 ms, and 847 ╬╝s

test

1 sec, 125 ms, 741 ╬╝s, and 6 hnsecs


test tasks

556 ms, 613 ╬╝s, and 8 hnsecs

test tasks

552 ms and 287 ╬╝s


test parallel

554 ms, 542 ╬╝s, and 6 hnsecs

test parallel

551 ms, 514 ╬╝s, and 9 hnsecs


Code:
http://pastie.org/9146326

Unfortunately it doesn't compile with the ancient version of gdc 
available in Debian, so I couldn't test with that. The results 
should be quite a bit better since core.atomic would be faster. 
And frankly, I'm not sure if the allocator actually works 
properly, but it's just for testing purposes anyways.




Re: Global variables read at compile time?

2014-05-06 Thread Suliman via Digitalmars-d-learn
I am tying to hardcode name of config file name. Then I would 
read and parse it.


Re: Reading a single whitespace-separated word from stdin

2014-05-06 Thread Mark Isaacson via Digitalmars-d-learn


An exceptionally generous offer! May take you up on that. Thank 
you :).


Re: Create many objects using threads

2014-05-06 Thread Ali Çehreli via Digitalmars-d-learn

On 05/06/2014 05:46 AM, hardcoremore wrote:

 But what does exactly means that Garbage Collector blocks? What
 does it blocks and in which way?

I know this much: The current GC that comes in D runtime is a 
single-threaded GC (aka a stop-the-world GC), meaning that all threads 
are stopped when the GC is running a garbage collection cycle.


 And can I use threads to create multiple instance faster or that is just
 not possible?

My example program that did nothing but constructed objects on the GC 
heap cannot be an indicator of the performance of all multi-threaded 
programs. In real programs there will be computation-intensive parts; 
there will be parts blocked on I/O; etc. There is no way of knowing 
without measuring.


Ali



Re: Read Complete File to Array of Lines

2014-05-06 Thread Suliman via Digitalmars-d-learn

I am trying to write simple parser, that split text to
key value

name = david
lastname = wood

here is my code:

foreach (line; readText(confname).splitLines())
{
writeln(line);
foreach (str; split(line, =))
{
writeln(str);   
}
}

now it's look like my code are put at str something wrong. I 
tried to do writeln(str[0]) but it's put there only first letters 
from first row.


Also could anybody help me to find example how create structure 
and than fill it's with value field.


Register allocation algorithm

2014-05-06 Thread asmman via Digitalmars-d-learn
I'm working on small compiler to understand these stuff and maybe 
get involved with the D compiler. I wrote a front-end to a C-like 
language and now I'm working on the code generator. To be more 
specific, in the register allocation phase. I was using a old and 
one where I put everything on stack because I thought it could be 
too complex for now but then I found something that seems a 
beginner like me would implement. From wikipedia articles I was 
able to implement below algorithm. I did some search, but it 
didn't provide much useful contents to myself, much probably 
because I don't have a math background and compiler development 
baggage. From what I understood how algorithms works I've 
implemented but I have a couple of questions. There's a lot of 
people here that knows a lot about compilers. Questions:


Data:
the code generator has only two registers available, they are RO 
and R1.

Numbers are 32-bit.
operations: load, push, pop, add, sub, div, mul. They are 
x86-like instructions.


I think that's all.

How should I design the get_reg() function?
I was using a stack-based to hold registers, but changed to 
current one (very very simple): it does keep the previously 
register returned in the function and return the inverse of, 
eg, if previously was R0 does return R1 if R1 does return R0.
It does init always with R0 register to result of expression 
always be in R0. But if I run out of registers, one register is 
pushed on stack and result might don't live in R0. So, how do I 
chose the register to result expression always be in it? A 
working code example could be very great!
I also want to someone tell me if is correct my 
label()/sethiUllman() implementation.


I translated directly this code from C/C++(since post non-D code 
one could say it doesn't make much sense) that's the language I'm 
using and keep it in the C-way as possible, ie, not using too 
much D features because I want to translate it back to my C one 
when I get it working. It's because D compiler is written in C++ 
and then I want to be able read dmd compiler source code, in case 
I get involved to, what I really want to.


Thanks in advance.


Here's the code:

http://pastebin.com/vP0XtyVi (pastebin version, in case of found 
it's more readable)


import std.stdio;

enum Type {
number,
id,
add,
sub,
mul,
div,
push,
pop,
none
}

enum Reg {
none,
r0,
r1
}

class AST {
AST left;
AST right;
Type type;
Reg reg;
int n;

this(AST l, AST r, Type t) {
left = l;
right = r;
type = t;
n = 0;
}
}

class BinExpression : AST {
this(AST l, AST r, Type t) {
super(l, r, t);
}
}

class Identifier : AST {
string name;

this(string nm) {
super(null, null, Type.id);
name = nm;
}
}

class Number : AST {
int number;

this(int n) {
super(null, null, Type.number);
number = n;
}
}


Reg[] regs = [ Reg.r0, Reg.r1 ];
const int regs_num = regs.sizeof / int.sizeof;
int C = 0;

void main() {
// ((2 + 2) + (2 + 2)) + ((2 + 2) + (2 + 2))
// t1 = 2 + 2
// t2 = 2 + 2
// t3 = t1 + t2
// t4 = 2 + 2
// t5 = 2 + 2
// t6 = t4 + t5
// t7 = t3 + t6
Number a = new Number(2);
Number b = new Number(2);

Number c = new Number(2);
Number d = new Number(2);

Number e = new Number(2);
Number f = new Number(2);

Number g = new Number(2);
Number h = new Number(2);


// t1 = 2 + 2
BinExpression t1 = new BinExpression(a, b, Type.add);
// t2 = 2 + 2
BinExpression t2 = new BinExpression(c, d, Type.add);
// t3 = t1 + t2
BinExpression t3 = new BinExpression(t1, t2, Type.add);
// t4 = 2 + 2
BinExpression t4 = new BinExpression(e, f, Type.add);
// t5 = 2 + 2
BinExpression t5 = new BinExpression(g, h, Type.add);
// t5 = t3 + t4
BinExpression t6 = new BinExpression(t4, t5,Type.add);
BinExpression t7 = new BinExpression(t3, t6, Type.add);
label(t7);
gen(t7);
}

Reg get_reg() { 
if(C == regs.sizeof / int.sizeof) {
//writeln(out of registers!);
C = 0;
}

return regs[C++];
}

void gen(AST ast) {
if(ast.left !is null  ast.right !is null) {
int l = ast.left.n;
int r = ast.right.n;

if(l = regs_num  r = regs_num) {
gen(ast.right);
ast.n -= 1;
//Reg r2 = ast.right.reg;
emit_operation(Type.push, ast.right.reg);

Re: Implicit static-dynamic arr and modifying

2014-05-06 Thread Rene Zwanenburg via Digitalmars-d-learn

On Tuesday, 6 May 2014 at 02:17:06 UTC, Nick Sabalausky wrote:

So all is well, and deliberately so. Pardon the noise.


IMO it's not. I once had a particularly nasty bug because of this:

struct S
{
@safe:
string str;

this(string data)
{
import std.digest.md;
str = md5Of(data).toHexString(); // Oops...
}
}


Atom text editor

2014-05-06 Thread Joshua Niehus via Digitalmars-d-learn

FYI:

If anyone is using GitHub's text editor Atom and would like
basic D syntax highlighting:

apm init --package ~/.atom/packages/language-d --convert
https://github.com/textmate/d.tmbundle

https://atom.io/docs/v0.94.0/converting-a-text-mate-bundle


Re: The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread FrankLike via Digitalmars-d-learn

On Tuesday, 6 May 2014 at 15:03:11 UTC, Regan Heath wrote:
On Tue, 06 May 2014 15:48:44 +0100, Marc Schütz 
schue...@gmx.net wrote:



On Tuesday, 6 May 2014 at 13:35:57 UTC, FrankLike wrote:
The problem is that you have a wide-character comma (,) 
there.


This works:

  void main() {
  writeln([一, 二]);
  }


No,I mean the execute result is error.That doesn't get the 
[一, 二],but get the [涓C,浜?].


Why?

Thank you.

Frank.


It works for me (Linux). If you're on Windows, it could have 
something to do with Windows' handling of Unicode, but I don't 
know enough about that to help you. There were posts about 
this in this newsgroup, maybe you can find them, or someone 
else remembers and can tell you directly...


IIRC you need to type chcp 65001 and set the command prompt 
to the Lucida font...


R


No,it's error.My OS is windows 7,chcp 936. SimpleChinese.
I use the 'go language' to test
'fmt.println(一, 二)'
Then  execute result is :一, 二.
It's ok.
But D is error:涓C,浜?.

Why?

Thank you.

Frank.




Re: The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread Ali Çehreli via Digitalmars-d-learn

On 05/06/2014 04:56 PM, FrankLike wrote:

 On Tuesday, 6 May 2014 at 15:03:11 UTC, Regan Heath wrote:

 IIRC you need to type chcp 65001 and set the command prompt to the
 Lucida font...

 R

 No,it's error.My OS is windows 7,chcp 936. SimpleChinese.
 I use the 'go language' to test

I don't know how go works but code page 936 is not Unicode. D's output 
on the other hand, is Unicode.


 'fmt.println(一, 二)'
 Then  execute result is :一, 二.
 It's ok.

It means that the go program's output is in code page 936.

 But D is error:涓C,浜?.

That is understandable: Since the console is set to 936, it interprets D 
program's UTF-8 output incorrectly.


Please do what Regan Heath says and test again:

1) Set the code page to 65001

2) Use a font that includes your Unicode characters

Ali



Re: The writeln() function's args can't be [一 ,二]?

2014-05-06 Thread FrankLike via Digitalmars-d-learn


That is understandable: Since the console is set to 936, it 
interprets D program's UTF-8 output incorrectly.


Please do what Regan Heath says and test again:

1) Set the code page to 65001

2) Use a font that includes your Unicode characters

Ali


Thank you.

I modify  it by the 'Regedit'(my OS is windows 7 x64):
open Regedit, find 'HKEY_CURRENT_USER\Console'
find the CodePage,Double Click,
then modify the type to 'decimal system',and modify the value to 
65001.

That's all.

Thank you,everyone.

Frank.