BitArray in custom class initialize it in ctor with arguments

2014-03-22 Thread MarisaLovesUsAll

Hi! I need to use array of bits in my custom class. I tried to
add std.bitmanip.BitArray as field, but got a strange error. What
am I doing wrong?

import std.bitmanip;
class Scene
{
 BitArray collisionMap;
 this(int w, int h)
 {
 collisionMap.init([0,0,0]);
 }
}

C:\D\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d
line 606
Error: btr cannot be interpreted at compile time, because it has
no available source code


If I use collisionMap.length(w*h), I get:

C:\D\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d
line 554
Error: non-constant expression [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,


And if I use BitArray in default ctor ( this() ), or in other
function in class, or in main function, all is OK. But I need
only ctor with arguments that initialize the BitArray with '0'
and sets 'w*h' length.

DMD version 2.065.0

Sorry for bad English.


Re: Ranges/algorithms for aggregation

2014-03-22 Thread bearophile

Luís Marques:

It would swap elements, like sort, so it doesn't need to put 
them anywhere, just permute them. The advantage is this:


Input: [7, 3, 7, 1, 1, 1, 1]
Output sort: [1, 1, 1, 1, 3, 7, 7]
Output groupSort: [3, 7, 7, 1, 1, 1, 1]


I think to swap items it must know where to swap them to. And you 
can't do that efficiently if the groups are unsorted.


Bye,
bearophile


Re: BitArray in custom class initialize it in ctor with arguments

2014-03-22 Thread bearophile

MarisaLovesUsAll:


Hi! I need to use array of bits in my custom class. I tried to
add std.bitmanip.BitArray as field, but got a strange error. 
What

am I doing wrong?

import std.bitmanip;
class Scene
{
 BitArray collisionMap;
 this(int w, int h)
 {
 collisionMap.init([0,0,0]);
 }
}

C:\D\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d
line 606
Error: btr cannot be interpreted at compile time, because it has
no available source code


To me this program compiles:


import std.bitmanip;

class Scene {
 BitArray collisionMap;
 this(int w, int h) {
 collisionMap.init([0,0,0]);
 }
}
void main() {}


Errors like btr cannot be interpreted at compile time mean that 
you are trying to run at compile-time something that can only run 
at run-time. So are you using enums or static immutables or 
static constructors or something like that?


Bye,
bearophile


Re: Ada-Style Range Types with Value Range Propagation in Arithmetic

2014-03-22 Thread bearophile

Nordlöw:


I cracked it:


This is important stuff. So if you find that you can't do 
something in D, consider asking for it in the main D newsgroup 
and in Bugzilla.


Bye,
bearophile


Re: to! converting 1D to 2D array

2014-03-22 Thread Marc Schütz

On Friday, 14 March 2014 at 19:24:21 UTC, Chris Williams wrote:
In D, an array is a struct (struct Array), with an address and 
a length value. A multi-dimensional array is an Array with an 
address pointing to an array of Arrays. So with an int[2][2] 
array, you have a layout like:


@1000 Array(address=1016, length=2)
@1016 [Array(address=1048, length=2),Array(address=1056, 
length=2)]

@1048 [1,2]
@1056 [3,4]


This is not true; you need to distinguish between fixed-size and 
dynamically sized arrays. It's better to call the latter slices 
to avoid the confusion.


Fixed-size arrays are value types, there are no 
references/addresses involved in storing them. So this:


int[2][2] a;

always has the following layout:

@1000 [1,2]
@1008 [3,4]

You layout would be correct for this example:

int[][] = [[1,2],[3,4]];


More updated question about Sleep/msleep/usleep

2014-03-22 Thread BeschBesch

Hello,

I know there are severel threads concerning Wait, Pause, 
Sleep, a.s.o
but most of them were created in 2007 or similiar. It seems D has 
changed a lot because when I try to call sleep/msleep (example), 
the compiler cannot find it (ERROR: undefined indentifier).


import std.stdio;
///  other imports as well

import std.c.time;

main_bock()
{
 init and things

sleep(1); // not found

std.c.time.sleep(2); // not found as well

std.c.time.msleep(1000); // nope...


/// continued code
}

So, is there a newer version? I get a hint for thread.sleep but 
this may be a member function. Also, i suppose I MAY use Sleep() 
from windows.h, if there is no other option.


Re: More updated question about Sleep/msleep/usleep

2014-03-22 Thread Adam D. Ruppe

import core.thread;

void main() {

}


Re: More updated question about Sleep/msleep/usleep

2014-03-22 Thread Adam D. Ruppe

This will work:

import core.thread;

void main() {
 Thread.sleep(5.seconds);
}


sleep is a member of Thread, but it is static so you can easily 
call it to sleep the current thread.


The argument is a Duration, which is most easily constructed with 
the .seconds, .msecs, .minutes, etc. helper functions like I did 
here.


Re: Function to print a diamond shape

2014-03-22 Thread Jay Norwood
The computation times of different methods can differ a lot.   
How do you suggest to measure this effectively without the 
overhead of the write and writeln output?   Would a count of 
11 and stubs like below be reasonable, or would there be 
something else that would  prevent the optimizer from getting too 
aggressive?


void writelnx(T...)(T args)
{
}
void writex(T...)(T args)
{
}



shouldn't this work? / how to make it work?

2014-03-22 Thread captaindet

pls see example code below.

the two 'test' templates work fine by themselves. if, however, in the same 
module, the eponymous template does not work anymore. instead the compiler 
seems to try instantiating the variadic template.

a) why? for how i understand it, this should not happen as
o they have very different signatures, i.e. completely incompatible argument lists 
( zero vs 1 arguments)
o the string parameter is more specialized than the take-all tuple

b) how can i make it work?

cheers,
det

CODE:
=

import std.stdio;

// cannot be used with function arguments
template test(string str ){
enum test = template 'test' used with ~str;
}

// cannot be called with less than 2 function arguments
string test (T ...)( T strs )
if( 1strs.length )
{
return templated function 'test' called with ~strs[0]~ and ~strs[1];
}

// enum epo = test!one; // use eponymous template
// if commented in:
// Error: tuple T is used as a type
// in line 9 = string test (T ...)( T strs )

enum vari = test(two, three);   // use templated/variadic function

void main(string[] args)
{
// writeln( epo );
writeln( vari );
}


Re: shouldn't this work? / how to make it work?

2014-03-22 Thread anonymous

On Saturday, 22 March 2014 at 22:54:15 UTC, captaindet wrote:

pls see example code below.

the two 'test' templates work fine by themselves. if, however, 
in the same module, the eponymous template does not work 
anymore. instead the compiler seems to try instantiating the 
variadic template.


a) why? for how i understand it, this should not happen as
o they have very different signatures, i.e. completely 
incompatible argument lists ( zero vs 1 arguments)
o the string parameter is more specialized than the take-all 
tuple


b) how can i make it work?

cheers,
det

CODE:
=

import std.stdio;

// cannot be used with function arguments
template test(string str ){
enum test = template 'test' used with ~str;
}

// cannot be called with less than 2 function arguments
string test (T ...)( T strs )
if( 1strs.length )
{
	return templated function 'test' called with ~strs[0]~ and 
~strs[1];

}

// enum epo = test!one; // use eponymous template
// if commented in:
// Error: tuple T is used as a type
// in line 9 = string test (T ...)( T strs )

enum vari = test(two, three);	// use templated/variadic 
function


void main(string[] args)
{
// writeln( epo );
writeln( vari );
}


Looks like a bug to me.

By the way, the variadic template is eponymous, too: The template
declares a function with the same name.


Re: Function to print a diamond shape

2014-03-22 Thread Jay Norwood
I decided to redirect stdout to nul and print the stopwatch 
messages to stderr.

So, basically like this.

import std.stdio;
import std.datetime;
import std.cstream;

StopWatch sw;
sw.start();

measured code

sw.stop();
derr.writefln(time: , sw.peek().msecs, [ms]);

Then, windows results comparing two versions, this for n=2001, 
shows one form is about 3x faster when you redirect stdout to nul.


D:\diamond\diamond\diamond\Releasediamond 1nul
time: 15[ms]
time: 42[ms]








Re: Function to print a diamond shape

2014-03-22 Thread Ali Çehreli

On 03/22/2014 06:03 PM, Jay Norwood wrote:

 derr.writefln(time: , sw.peek().msecs, [ms]);

Cool. stderr should work too:

stderr.writefln(/* ... */);

Ali