Reducing array.length triggers reallocation

2015-12-27 Thread milentin via Digitalmars-d-learn
I've just started learning D and noticed a bug, but wanted to 
confirm it here before reporting it.


According to spec: "If the new array length is shorter, the array 
is not reallocated, and no data is copied. It is equivalent to 
slicing the array". Contradicted by a trivial program:


void main() {
int[] arr;
arr.length = 7;
arr.length = 6; // not ok -- allocation
int[] slice = arr[0..5]; // ok -- no allocation
}

---
dmd -profile=gc test.d
(DMD32 D Compiler v2.069.2)
---
bytes allocated, allocations, type, function, file:line
 28   1 int[] D main test.d:3
 24   1 int[] D main test.d:4


Re: What's wrong with my debugger?

2015-12-27 Thread Rainer Schuetze via Digitalmars-d-learn



On 24.12.2015 14:57, Chris wrote:

On Thursday, 24 December 2015 at 09:30:24 UTC, Rainer Schuetze wrote:

In the locals window, mago displays all instances of variables, but
with the same value (which might be some uninitialized value of a
different declaration than expected). The Visual Studio debug engine
shows different values, though.


Hi, thanks for the reply but not actually, no, if I change the debug
engine to VS and rebuild I get the same behaviour.


I suspect that the variables are part of a closure. There is bad and 
incomplete debug information emitted by dmd in this case.


Re: How to use GDC to get .a file on Linux?

2015-12-27 Thread tcak via Digitalmars-d-learn

On Sunday, 27 December 2015 at 15:19:21 UTC, FrankLike wrote:

Hi,
   Now I need get the .a file on Linux,target system is ARM.
   If you use gcc ,you will use the 'ar' to get .a file,
but how to do by GDC ?
And how to  get the execute file by .a file and .d file?

Thank you.


I couldn't have understood your question very well, but some 
information is here.


You create .a static library file with "-lib" flag while 
compiling. Yesterday I did it.


dmd mylib.d -lib

This will generate mylib.a.

You can later use this static library while compiling another d 
code.


dmd main.d mylib.a

Pass the .a file directly as it is another source.

I have never tried these with GDC, but I don't think it is much 
different at all.


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread SimonN via Digitalmars-d-learn

On Sunday, 27 December 2015 at 16:41:10 UTC, TheDGuy wrote:
It looks like the debugger is not working correctly because i 
changed the code to this:

[...]
and the same problem appears.


I can't watch youtube here. What numbers does your input 
generate? Which 'if' doesn't fire? What results would you like 
instead?


-- Simon


Re: specifying an auto array type

2015-12-27 Thread Jay Norwood via Digitalmars-d-learn

On Sunday, 27 December 2015 at 07:40:55 UTC, Ali Çehreli wrote:


It looks like you need map(), not each():

import std.algorithm;
import std.typecons;
import std.array;

void main() {
auto a = [ 1, 2 ];
auto arr = a.map!(e => tuple(2 * e, e * e)).array;

static assert(is(typeof(arr) == Tuple!(int, int)[]));
}

Ali


ok, thanks.  This does work, using the uint i ahead of the map 
statement.


uint i=0;
auto arr = samples[].map!(a => 
tuple!("sample","f1","f2","f3")(i++,f1(a),f2(a),f3(a))).array;

writeln(arr);

= output
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(0, 
3, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(1, 
6, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(2, 
9, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(3, 
12, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(4, 
15, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(5, 
18, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(6, 
21, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(7, 
24, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(8, 
27, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(9, 
30, 0.7, 0.8)


=
However, I was trying to use each!, with the intention of then 
moving to parallel processing by samples blocks. My guess is this 
would be more efficient than using parallel map or amap, which 
would parallel process by function application, if I understand 
correctly.


It isn't clear to me from the examples if something like below 
can be rewritten to use the chained calls.


foreach(i, ref elem; taskPool.parallel(samples, 100))



Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

On Sunday, 27 December 2015 at 16:39:18 UTC, SimonN wrote:

On Sunday, 27 December 2015 at 16:01:37 UTC, TheDGuy wrote:

Sry:
if((x1 < 0) & (x2 >= 0)){


This looks like a bug, with & instead of &&.

-- Simon


It looks like the debugger is not working correctly because i 
changed the code to this:


if(discriminant > 0){
double x1 = (-b - sqrt(discriminant)) / (2.0*a);
double x2 = (-b + sqrt(discriminant)) / (2.0*a);
if((x1 >= 0) && (x2 >= 0)){
return x1;
}
if((x1 < 0) && (x2 >= 0)){
return x2;
}
return -1.0;
} else{
return -1.0;
}

and the same problem appears.


Re: How to use GDC to get .a file on Linux?

2015-12-27 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 27 December 2015 at 15:19:21 UTC, FrankLike wrote:

Hi,
   Now I need get the .a file on Linux,target system is ARM.
   If you use gcc ,you will use the 'ar' to get .a file,
but how to do by GDC ?
And how to  get the execute file by .a file and .d file?

Thank you.


Just use ar on the generated object files the same way you would 
if you were using gcc.


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

On Sunday, 27 December 2015 at 18:03:16 UTC, Bubbasaur wrote:

On Sunday, 27 December 2015 at 15:53:55 UTC, TheDGuy wrote:

Any idea what i am doing wrong?
https://www.youtube.com/watch?v=j_VCa-5VeP8


Tell me one thing, what is the value returned?


Well It's working here: http://dpaste.dzfl.pl/18b27ea26b08

Maybe you would like to change the code above to look like 
yours and see what happens?


Bubba.


Okay, thanks. It actually returned the right value but it looked 
like as if it would jump into the 2nd if-statement (as you can 
see on the video). Strange...


Variable below zero but if statement doesn't grab?

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

Any idea what i am doing wrong?
https://www.youtube.com/watch?v=j_VCa-5VeP8


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread SimonN via Digitalmars-d-learn

On Sunday, 27 December 2015 at 16:01:37 UTC, TheDGuy wrote:

Sry:
if((x1 < 0) & (x2 >= 0)){


This looks like a bug, with & instead of &&.

-- Simon


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

On Sunday, 27 December 2015 at 16:48:44 UTC, SimonN wrote:

On Sunday, 27 December 2015 at 16:41:10 UTC, TheDGuy wrote:
It looks like the debugger is not working correctly because i 
changed the code to this:

[...]
and the same problem appears.


I can't watch youtube here. What numbers does your input 
generate? Which 'if' doesn't fire? What results would you like 
instead?


-- Simon


I don't understand why my program goes into the if statement if 
the debugger shows, that the variable "discriminant" is below 
zero even though:


"if(discriminant > 0)"?


Re: How to instantiate a map with multiple functions

2015-12-27 Thread karthikeyan via Digitalmars-d-learn

On Sunday, 27 December 2015 at 02:21:11 UTC, Ali Çehreli wrote:

On 12/26/2015 05:26 PM, Karthikeyan wrote:

> if I need to map on a array of tuples will that work with the
tuple being
> unpacked or do I need to get it as single element and do
unpacking myself?

Unfortunately, there is no automatic unpacking of tuples.

The only exception that I know is when tuples are elements of a 
range (but not a proper slice, in which case the first element 
is the automatic element index).


import std.stdio;
import std.typecons;
import std.range;
import std.algorithm;

void main() {
auto range = 5.iota.map!(i => tuple(2 * i, i * i));

// automatic tuple expansion:
foreach (twice, square; range) {
writefln("twice: %s, square: %s", twice, square);
}
}

Prints:

twice: 0, square: 0
twice: 2, square: 1
twice: 4, square: 4
twice: 6, square: 9
twice: 8, square: 16

The problem is when the same elements are inside a slice:

import std.stdio;
import std.typecons;
import std.range;
import std.algorithm;

void main() {
auto range = [ tuple(0, 0), tuple(2, 1) ];

foreach (twice, square; range) {
writefln("twice: %s, square: %s", twice, square);
}
}

Now 'twice' is the automatic index, and 'square' is the entire 
element (i.e. the tuple):


twice: 0, square: Tuple!(int, int)(0, 0)
twice: 1, square: Tuple!(int, int)(2, 1)

Ali


I took a local copy of the std.algorithm.iteration as 
myiteration.d and used it for debugging. Commenting out the 
following two lines make this work 
https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L461-L463. I don't know why that fails. I filed an issue at the bug tracker.


Is it possible to elegantly create a range over a binary heap?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn
I have a binary tree storing ints implemented using an array. The 
internal state looks like this:


8,7,6,4,1,3,5,2

When extracting this data, it is returned as 8,7,6,5,4,3,2,1.

Is it possible to elegantly add a range on top of the internal 
state to return the correct value order I would expect when 
extracting? Is there an algorithm documented somewhere for doing 
this?


Re: How to use GDC to get .a file on Linux?

2015-12-27 Thread FrankLike via Digitalmars-d-learn

On Sunday, 27 December 2015 at 15:24:17 UTC, tcak wrote:

On Sunday, 27 December 2015 at 15:19:21 UTC, FrankLike wrote:

Hi,
   Now I need get the .a file on Linux,target system is ARM.
   If you use gcc ,you will use the 'ar' to get .a file,
but how to do by GDC ?
And how to  get the execute file by .a file and .d file?

Thank you.


I couldn't have understood your question very well, but some 
information is here.


You create .a static library file with "-lib" flag while 
compiling. Yesterday I did it.


dmd mylib.d -lib

This will generate mylib.a.

You can later use this static library while compiling another d 
code.


dmd main.d mylib.a

Pass the .a file directly as it is another source.

I have never tried these with GDC, but I don't think it is much 
different at all.
Now I will target file to ARM linux,and dmd can't get the file 
for ARM.




Is this an rvalue reference problem?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn

See the following code:

import std.stdio;

void foo(ref int x)
{
writefln("%s", x);
}

void main(string[] args)
{
int y = 0;
foo(y++);
}

When compiled it produces this error:

test.d(11): Error: function test.foo (ref int x) is not callable 
using argument types (int)


If I remove the post-increment of the y variable if works. Is 
this an rvalue reference issue? Would you expect this to work? 
Should the error message be a little more helpful?


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread SimonN via Digitalmars-d-learn

On Sunday, 27 December 2015 at 16:52:39 UTC, TheDGuy wrote:
I don't understand why my program goes into the if statement if 
the debugger shows, that the variable "discriminant" is below 
zero even though:


"if(discriminant > 0)"?


I have a hard time believing this. Does the problem persist if 
you swap out the entire control flow, beginning with that line, 
with the following?


if (discriminant > 0)
writeln("bigger than zero");
else
writeln("not entering the 'if'");


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread Bubbasaur via Digitalmars-d-learn

On Sunday, 27 December 2015 at 15:53:55 UTC, TheDGuy wrote:

Any idea what i am doing wrong?
https://www.youtube.com/watch?v=j_VCa-5VeP8


Tell me one thing, what is the value returned?


Well It's working here: http://dpaste.dzfl.pl/18b27ea26b08

Maybe you would like to change the code above to look like yours 
and see what happens?


Bubba.


Re: Is this an rvalue reference problem?

2015-12-27 Thread tsbockman via Digitalmars-d-learn
On Sunday, 27 December 2015 at 16:05:39 UTC, Gary Willoughby 
wrote:
If I remove the post-increment of the y variable if works. Is 
this an rvalue reference issue? Would you expect this to work?


This should work with *pre*-increment, but not post-increment. 
Post-increment works like this:


int y = 0;
foo(function(ref int val){
int old = val;
val += 1;
return old;
}(y));

`old` is just a local temporary, so it is not safe to return it 
by reference. Thus, it becomes an rvalue.


Re: Is this an rvalue reference problem?

2015-12-27 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 27 December 2015 at 16:05:39 UTC, Gary Willoughby 
wrote:

void foo(ref int x)
foo(y++);
If I remove the post-increment of the y variable if works. Is 
this an rvalue reference issue?


Yes, but more than that, what, exactly, would you expect from 
that? The order of operations with the postfix ++ operator and 
ref would probably lead to confusing results anyway, so better to 
disallow it and force you to be a bit more clear with the code.



Should the error message be a little more helpful?


Yeah, probably.

A ref in D is only allowed on lvalues... lvalue gets its name 
from being on the left side of an equal sign when assigning, as 
opposed to rvalues which are on the right side of the equal sign 
when assigning.


So if

y++ = 0;

doesn't compile, it will complain "y++ is not an lvalue" because 
it can't work on the left side of that assignment.


ref in D is a proxy for assignment which is why it has this 
requirement.


Re: Struct destructors not always called?

2015-12-27 Thread Jeremy DeHaan via Digitalmars-d-learn

On Sunday, 27 December 2015 at 18:47:52 UTC, Adam D. Ruppe wrote:
On Sunday, 27 December 2015 at 18:40:55 UTC, Jeremy DeHaan 
wrote:
I was playing around with some code today and I noticed that 
in some cases struct destructors are not called.


struct destructors are called when the struct ceases to exist 
in the program.


A global variable never ceases to exist as long as the program 
lives.


So are these left dangling or do they actually get cleaned up at 
the program exit?


Re: Is it possible to elegantly create a range over a binary heap?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn
On Sunday, 27 December 2015 at 17:23:35 UTC, Gary Willoughby 
wrote:
I have a binary tree storing ints implemented using an array. 
The internal state looks like this:


8,7,6,4,1,3,5,2

When extracting this data, it is returned as 8,7,6,5,4,3,2,1.

Is it possible to elegantly add a range on top of the internal 
state to return the correct value order I would expect when 
extracting? Is there an algorithm documented somewhere for 
doing this?


Some explanatory reference:

https://en.wikipedia.org/wiki/Binary_tree#Arrays


Re: Is this an rvalue reference problem?

2015-12-27 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 27 December 2015 at 18:54:25 UTC, Adam D. Ruppe wrote:
On Sunday, 27 December 2015 at 16:05:39 UTC, Gary Willoughby 
wrote:

[...]


Yes, but more than that, what, exactly, would you expect from 
that? The order of operations with the postfix ++ operator and 
ref would probably lead to confusing results anyway, so better 
to disallow it and force you to be a bit more clear with the 
code.


[...]


Thanks guys, I thought as much.


Re: Ranges: How to take N last of elements of range

2015-12-27 Thread Jakob Ovrum via Digitalmars-d-learn

On Saturday, 26 December 2015 at 05:31:59 UTC, Ur@nuz wrote:

On Friday, 25 December 2015 at 20:06:04 UTC, drug wrote:

25.12.2015 17:13, Ur@nuz пишет:

 [...]

You can do following http://dpaste.dzfl.pl/41c57f89a5a0
The reason of compile error is your using a range as a 
separator, change it to single symbol and it works. Also I 
used 'array' after 'take' to make range bidirectional to let 
next 'retro' works. And 'join' joins result to string with '.' 
as a separator.


Thanks for reply. This is useful


https://github.com/D-Programming-Language/phobos/pull/3855


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

On Sunday, 27 December 2015 at 16:00:34 UTC, jkpl wrote:

On Sunday, 27 December 2015 at 15:53:55 UTC, TheDGuy wrote:

Any idea what i am doing wrong?
https://www.youtube.com/watch?v=j_VCa-5VeP8


You could post the code also, personnaly I'm always almost at 2 
meters from my screen, with zoom, so I can't read the code...


Sry:

Vector3D v = ray.origin.sub(this.center).normalize();
double a = ray.direction.dot(ray.direction);
double b = 2.0 * ray.direction.dot(v);
double c = v.dot(v) - (this.radius * this.radius);
double discriminant = (b*b) - (4.0*a*c);
if(discriminant > 0){
double x1 = (-b - sqrt(discriminant)) / (2.0*a);
double x2 = (-b + sqrt(discriminant)) / (2.0*a);
if((x1 >= 0) && (x2 >= 0)){
return x1;
}
if((x1 < 0) & (x2 >= 0)){
return x2;
}
return -1.0;
}else{
return -1.0;
}


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread jkpl via Digitalmars-d-learn

On Sunday, 27 December 2015 at 15:53:55 UTC, TheDGuy wrote:

Any idea what i am doing wrong?
https://www.youtube.com/watch?v=j_VCa-5VeP8


You could post the code also, personnaly I'm always almost at 2 
meters from my screen, with zoom, so I can't read the code...


Re: Struct destructors not always called?

2015-12-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 27 December 2015 at 18:40:55 UTC, Jeremy DeHaan wrote:
I was playing around with some code today and I noticed that in 
some cases struct destructors are not called.


struct destructors are called when the struct ceases to exist in 
the program.


A global variable never ceases to exist as long as the program 
lives.


Re: Struct destructors not always called?

2015-12-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 27 December 2015 at 19:04:11 UTC, Jeremy DeHaan wrote:
So are these left dangling or do they actually get cleaned up 
at the program exit?


They are left dangling right now. You can clear it yourself by 
defining a `static ~this() { .destroy(your struct); }` somewhere 
in the module.


Maybe that should be done automatically but right now the 
assumption is those global structs are still available in the 
static dtors so they are considered alive right up to the program 
actually exiting... at which point no more code can run since the 
process has terminated


each! vs foreach parallel timings

2015-12-27 Thread Jay Norwood via Digitalmars-d-learn
I'm doing some re-writing and measuring.  The basic task is to 
take 10K samples (in struct S samples below) and calculate some 
metrics (just per sample for now).  It isn't evident to me how to 
write the parallel foreach in the same format as each!, so I just 
used the loop form that I understood.


Measured times below are for processing three simple metrics 100 
times on 10K samples. This parallel mode could be very useful in 
my work, which involves processing a bunch of hardware 
performance data.



This is on windows, corei5, DMD32 D Compiler v2.069.2, debug 
build.


each! time:59 ms
parallel! time:20 ms

import std.stdio;
import std.algorithm;
import std.conv;
import std.range;
import std.typecons;
import std.parallelism;
import std.array;
import std.traits;
import std.datetime;

struct S { int sn; ulong a; ulong b; ulong c; ulong d; double e; 
ulong f; ulong m1; double m2; double m3;}


void apply_metrics(int i,ref S s){
with(s){
m1 = a+b;
m2 = (c+d)/e;
m3 = (c+f)/e;
sn = i;
}
}

int main()
{

S[1] samples;
// initialize some values
foreach ( int i, ref s; samples){
int j=i+1;
with (s){
a=j; b=j*2; c=j*3; d=j*4; e=j*10; f=j*5;
}
}

auto sw = StopWatch(AutoStart.yes);
	// apply several functions on each  sample, also number the 
samples

foreach(j;iota(100))
samples[].each!((int i, ref a)=>apply_metrics(i,a));
writeln("each! time:", sw.peek().msecs, " ms");

auto sw2 = StopWatch(AutoStart.yes);
// do the same as above, but in parallel
foreach(j;iota(100))
foreach( i, ref a; parallel(samples[])){ apply_metrics(i,a);}
writeln("parallel! time:", sw2.peek().msecs, " ms");
return 0;
}


Re: Double precision?

2015-12-27 Thread Bubbasaur via Digitalmars-d-learn

On Sunday, 27 December 2015 at 20:06:53 UTC, TheDGuy wrote:

I don't know what my computer is doing today:
x and y are coordinates and if x is any number from 0 to 150 
the result of x/width is always zero


Dividing Integers will result in Integer:

int x = 10, width = 50;

writeln(x/width);   // Will be "0"
writeln(x/cast(double)width); // Will be 0.2

Check out: http://dpaste.dzfl.pl/8bcfb3d9c551


... Another video: https://youtu.be/Fysv2fOwtk4


Please dude, start using dpaste for that, it's a bit nonsense 
watching videos.


Bubba.


How to use GDC to get .a file on Linux?

2015-12-27 Thread FrankLike via Digitalmars-d-learn

Hi,
   Now I need get the .a file on Linux,target system is ARM.
   If you use gcc ,you will use the 'ar' to get .a file,
but how to do by GDC ?
And how to  get the execute file by .a file and .d file?

Thank you.



Struct destructors not always called?

2015-12-27 Thread Jeremy DeHaan via Digitalmars-d-learn
I was playing around with some code today and I noticed that in 
some cases struct destructors are not called.


for example:

impost std.stdio;

SomeStruct global;

void main()
{
   SomeStruct inMain;
   writeln(global.thing);
   writeln(inMain.thing);
   writeln(getSomeInt());
}

int getSomeInt()
{
static SomeStruct inner;
return inner.thing;
}

struct SomeStruct
{
int thing = 100;
~this()
{
writeln("destructor");
}



output is
100
100
100
destructor

Only inMain's destructor is ever called, or at least it is the 
only one that ever prints "destructor" to the console. Are there 
special rules for structs that I'm unaware of?


Double precision?

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

I don't know what my computer is doing today:

code:
double normalized_i = (x / width) - 0.5;
double normalized_j = (y / height) - 0.5;
writeln(x);
writeln(normalized_i);
writeln(y);
writeln(normalized_j);

x and y are coordinates and if x is any number from 0 to 150 the 
result of x/width is always zero, how can that be (width is never 
0)? Another video: https://youtu.be/Fysv2fOwtk4


Re: Reducing array.length triggers reallocation

2015-12-27 Thread Ali Çehreli via Digitalmars-d-learn

On 12/27/2015 02:09 AM, milentin wrote:
> I've just started learning D and noticed a bug, but wanted to confirm it
> here before reporting it.
>
> According to spec: "If the new array length is shorter, the array is not
> reallocated, and no data is copied. It is equivalent to slicing the
> array". Contradicted by a trivial program:
>
> void main() {
>  int[] arr;
>  arr.length = 7;
>  arr.length = 6; // not ok -- allocation
>  int[] slice = arr[0..5]; // ok -- no allocation
> }
>
> ---
> dmd -profile=gc test.d
> (DMD32 D Compiler v2.069.2)
> ---
> bytes allocated, allocations, type, function, file:line
>   28   1 int[] D main test.d:3
>   24   1 int[] D main test.d:4

I don't understand why that happens. I found one related bug:

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

I can understand that assignment to arr.length cannot be @nogc but I 
would expect a check against length so that there would be no allocation.


At least there are no copies and .ptr property of the array does not change.

[Several hours later...]

You know what... I bet there is no actual allocation at all. I think 
what happens is, the code calls GC.realloc(24) and realloc() does not do 
anything. However, it still reports to the profiler that there was an 
allocation (attempt).


Can someone verify that please. At least, can someone show where 
GC.realloc() source is.


Thank you,
Ali



Re: each! vs foreach parallel timings

2015-12-27 Thread Ali Çehreli via Digitalmars-d-learn

On 12/27/2015 04:17 PM, Jay Norwood wrote:

On Sunday, 27 December 2015 at 23:42:57 UTC, Ali Çehreli wrote:

That does not compile because i is size_t but apply_metrics() takes
an int. One solution is to call to!int:

foreach( i, ref a; parallel(samples[])){
apply_metrics(i.to!int,a);}



It builds for me still, and executes ok, but must be because size_t and
i are both 32 bits on Win32 build.



Makes sense. I would still prefer size_t and even leave it out for the 
lambda:


void apply_metrics(size_t i,ref S s){
// ...

sn = i.to!int;

// ...

samples[].each!((i, ref a)=>apply_metrics(i,a));

Ali



Re: each! vs foreach parallel timings

2015-12-27 Thread Jay Norwood via Digitalmars-d-learn

On Sunday, 27 December 2015 at 23:42:57 UTC, Ali Çehreli wrote:

On 12/27/2015 11:30 AM, Jay Norwood wrote:

>  samples[].each!((int i, ref a)=>apply_metrics(i,a));

Are you using an older compiler? That tuple expansion does not 
work any more at least with dmd v2.069.0 but you can use 
enumerate():



samples[].enumerate.each!(t=>apply_metrics(t[0].to!int,t[1]));


>  foreach( i, ref a; parallel(samples[])){
apply_metrics(i,a);}

That does not compile because i is size_t but apply_metrics() 
takes an int. One solution is to call to!int:


foreach( i, ref a; parallel(samples[])){ 
apply_metrics(i.to!int,a);}


To not answer your actual question, I don't think it's 
possible. :)


Ali


The code I posted was compiled with v2.069.2.  It isn't creating 
a tuple return value in this code. I'll re-check it.


Re: Reducing array.length triggers reallocation

2015-12-27 Thread Ivan Kazmenko via Digitalmars-d-learn

On Sunday, 27 December 2015 at 22:36:32 UTC, Ali Çehreli wrote:

[Several hours later...]

You know what... I bet there is no actual allocation at all. I 
think what happens is, the code calls GC.realloc(24) and 
realloc() does not do anything. However, it still reports to 
the profiler that there was an allocation (attempt).


Can someone verify that please. At least, can someone show 
where GC.realloc() source is.


Thank you,
Ali


I believe it boils down to calling gc.gc.reallocNoSync in 
druntime: 
https://github.com/D-Programming-Language/druntime/blob/master/src/gc/gc.d#L603 .


Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

On Sunday, 27 December 2015 at 22:51:27 UTC, Ali Çehreli wrote:

On 12/27/2015 07:53 AM, TheDGuy wrote:

Any idea what i am doing wrong?
https://www.youtube.com/watch?v=j_VCa-5VeP8


YouTube says that the video has been removed by the user. 
That's exactly the reason why I don't like even dpaste. There 
is no guarantee that such threads will be useful forever. :) 
For me, sample code should be right here, and it should be 
short enough to practically be here.


Ali


I deleted the video because the problem was solved, it was 
neither a problem with the single '&' nor with my code it just 
showed a strange behaviour while debugging but the function 
returned the right value. Sample code is in my second post.


Re: each! vs foreach parallel timings

2015-12-27 Thread Jay Norwood via Digitalmars-d-learn

On Sunday, 27 December 2015 at 23:42:57 UTC, Ali Çehreli wrote:
That does not compile because i is size_t but apply_metrics() 
takes an int. One solution is to call to!int:


foreach( i, ref a; parallel(samples[])){ 
apply_metrics(i.to!int,a);}




It builds for me still, and executes ok, but must be because 
size_t and i are both 32 bits on Win32 build.




Re: Variable below zero but if statement doesn't grab?

2015-12-27 Thread Ali Çehreli via Digitalmars-d-learn

On 12/27/2015 07:53 AM, TheDGuy wrote:

Any idea what i am doing wrong?
https://www.youtube.com/watch?v=j_VCa-5VeP8


YouTube says that the video has been removed by the user. That's exactly 
the reason why I don't like even dpaste. There is no guarantee that such 
threads will be useful forever. :) For me, sample code should be right 
here, and it should be short enough to practically be here.


Ali



Re: specifying an auto array type

2015-12-27 Thread Ali Çehreli via Digitalmars-d-learn

On 12/27/2015 08:42 AM, Jay Norwood wrote:

> However, I was trying to use each!, with the intention of then moving to
> parallel processing by samples blocks. My guess is this would be more
> efficient than using parallel map or amap, which would parallel process
> by function application, if I understand correctly.

There is also taskPool.asyncBuf but it made your code on the other 
thread with apply_metrics() even slower.


> It isn't clear to me from the examples if something like below can be
> rewritten to use the chained calls.
>
> foreach(i, ref elem; taskPool.parallel(samples, 100))

parallel() returns struct ParallelForeach, which is not an InputRange. 
That limits its usefulness. However, each() seems to work with types 
that provide opApply() (which ParallelForeach does) and the following 
works (in some cases):


some.range.chain.parallel.each!(/* ... */);

Ali



GTKD drawing area remains white even SourceRgb is changed

2015-12-27 Thread TheDGuy via Digitalmars-d-learn

My code:

http://dpaste.com/1X3E1HW

i store colors in the accumulator-array and draw them via 
"cr.rectangle()". Because i have some problems with the code i 
set the SourceRgb-color to a constant value but if i execute the 
program, the window remains white.


Re: each! vs foreach parallel timings

2015-12-27 Thread Ali Çehreli via Digitalmars-d-learn

On 12/27/2015 11:30 AM, Jay Norwood wrote:

>  samples[].each!((int i, ref a)=>apply_metrics(i,a));

Are you using an older compiler? That tuple expansion does not work any 
more at least with dmd v2.069.0 but you can use enumerate():


samples[].enumerate.each!(t=>apply_metrics(t[0].to!int,t[1]));

>  foreach( i, ref a; parallel(samples[])){ apply_metrics(i,a);}

That does not compile because i is size_t but apply_metrics() takes an 
int. One solution is to call to!int:


foreach( i, ref a; parallel(samples[])){ 
apply_metrics(i.to!int,a);}


To not answer your actual question, I don't think it's possible. :)

Ali



How to config the GDC on linux target for ARM linux?

2015-12-27 Thread FrankLike via Digitalmars-d-learn
Now I build a project for ARM linux on ubuntu 15.04 ,but build 
error.
I download the 'wiringPi' from http://wiringPi.com,convert the 
*.h to *.d.then build the 'aa.so' file:

#! /bin/sh
dfiles="max31855.d max5322.d mcp23008.d mcp23016.d mcp23016reg.d 
mcp23017.d mcp23s08.d mcp23s17.d mcp23x08.d mcp23x0817.d 
mcp3002.d mcp3004.d mcp3422.d mcp4802.d pcf8574.d pcf8591.d 
sn3218.d softPwm.d softServo.d softTone.d sr595.d wiringPi.d 
wiringPiI2C.d wiringPiSPI.d wiringSerial.d wiringShift.d 
wpiExtensions.d"


ofiles="drcSerial.o max31855.o max5322.o mcp23008.o mcp23016.o  
mcp23017.o mcp23s08.o mcp23s17.o mcp3002.o mcp3004.o mcp3422.o 
mcp4802.o pcf8574.o pcf8591.o piHiPri.o piThead.o sn3218.o 
softPwm.o softServo.o softTone.o sr595.o wiringPi.o wiringPiI2C.o 
wiringPiSPI.o wiringSerial.o wiringShift.o wpiExtensions.o"


 /opt/arm-unknown-linux-gnueabihf/bin/arm-linux-gnueabihf-gdc  -o 
aa.so $ofiels $dfiles  -shared

---my.d
import wiringPi;
import std.stdio;

void main()
{
writeln("start");
wiringPiSetup();
pinMode(0,OUTPUT);
while(1)
{
digitalWrite(0,HIGH);
delay(500);
digitalWrite(0,LOW);
delay(500);
}
return;
}
-build the my execute file
/opt/arm-unknown-linux-gnueabihf/bin/arm-linux-gnueabihf-gdc  -o 
my  my.d aa.so -I./wiringPi/WiringPi/


-now get the error:
my.d:1:8: error: module wiringPi is in file 'wiringPi.d' which 
cannot be read

 import wiringPi;
^
import path[0] = 
/opt/arm-unknown-linux-gnueabihf/lib/gcc/arm-unknown-linux-gnueabihf/5.2.0/include/d
-I copy the *.d to 
/opt/arm-unknown-linux-gnueabihf/lib/gcc/arm-unknown-linux-gnueabihf/5.2.0/include/d

it's ok ,but where is the file for config?
-get another error:
/tmp/cc7M1B9I.o: In function `_Dmain':
my.d:(.text+0x60): undefined reference to `wiringPiSetup'
my.d:(.text+0x6c): undefined reference to `pinMode'
my.d:(.text+0x84): undefined reference to `digitalWrite'
my.d:(.text+0x8c): undefined reference to `delay'
my.d:(.text+0x98): undefined reference to `digitalWrite'
my.d:(.text+0xa0): undefined reference to `delay'
collect2: error: ld returned 1 exit status
-end

I'm not known the GDC config file ,and maybe I use the error 
build .

Who can help me?
 thank you.





Re: Is it possible to elegantly create a range over a binary heap?

2015-12-27 Thread Ivan Kazmenko via Digitalmars-d-learn
On Sunday, 27 December 2015 at 20:01:47 UTC, Gary Willoughby 
wrote:
On Sunday, 27 December 2015 at 17:23:35 UTC, Gary Willoughby 
wrote:
I have a binary tree storing ints implemented using an array. 
The internal state looks like this:


8,7,6,4,1,3,5,2

When extracting this data, it is returned as 8,7,6,5,4,3,2,1.

Is it possible to elegantly add a range on top of the internal 
state to return the correct value order I would expect when 
extracting? Is there an algorithm documented somewhere for 
doing this?


Some explanatory reference:

https://en.wikipedia.org/wiki/Binary_tree#Arrays


If you implement a struct with range primitives over it, you can 
use it as a range.


See the second code example in std.container.binaryheap's docs at
http://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap.

Or do you mean you want to print variables in order without 
modifying the array?  Sounds like this would require at least N 
log N time and N additional memory for an N-element heap anyway 
(or quadratic time and constant memory).  So, you can just copy 
the array and exhaust the copied binary heap, getting the same 
asymptotic complexity: N log N time and N additional memory.


Ivan Kazmenko.


Re: Reducing array.length triggers reallocation

2015-12-27 Thread milentin via Digitalmars-d-learn

On Sunday, 27 December 2015 at 22:36:32 UTC, Ali Çehreli wrote:

I don't understand why that happens. I found one related bug:

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

I can understand that assignment to arr.length cannot be @nogc 
but I would expect a check against length so that there would 
be no allocation.


At least there are no copies and .ptr property of the array 
does not change.


[Several hours later...]

You know what... I bet there is no actual allocation at all. I 
think what happens is, the code calls GC.realloc(24) and 
realloc() does not do anything. However, it still reports to 
the profiler that there was an allocation (attempt).


Can someone verify that please. At least, can someone show 
where GC.realloc() source is.


Thank you,
Ali


Thanks for the feedback, I have opened 
https://issues.dlang.org/show_bug.cgi?id=15481.