Re: WTF does "Enforcement failed" actually mean?

2015-09-30 Thread Ali Çehreli via Digitalmars-d-learn

On 09/30/2015 10:46 PM, Russel Winder via Digitalmars-d-learn wrote:

I have the code:

reduce!"a+b"(x)

where x is a int[] and I get an exception "Enforcement failed" at run
time. This gives me enough information to say ¿que?



It's coming from the following no-message enforce():

enforce(!r.empty);


https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L2481

You are using the no-seed version of reduce(), which uses the first 
element as seed, which means that the range cannot be empty.


Ali



WTF does "Enforcement failed" actually mean?

2015-09-30 Thread Russel Winder via Digitalmars-d-learn
I have the code:

reduce!"a+b"(x)

where x is a int[] and I get an exception "Enforcement failed" at run
time. This gives me enough information to say ¿que?

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: an example of parallel calculation of metrics

2015-09-30 Thread Jay Norwood via Digitalmars-d-learn
This compiles and appears to execute correctly, but if I 
uncomment the taskPool line I get a compile error message about 
wrong buffer type.  Am I breaking some rule for 
std.parallelism.amap?


import std.algorithm, std.parallelism, std.range;
import std.stdio;
import std.datetime;
import std.typecons;
import std.meta;

// define some input measurement sample tuples and output metric 
tuples
alias TR = Tuple!(double,"per_sec", double, "per_cycle", 
long,"raw");
alias TI = Tuple!(long, "proc_cyc", long, "DATA_RD", long, 
"DATA_WR", long, "INST_FETCH", long, "L1I_MISS", long, "L1I_HIT", 
long,"L1D_HIT", long, "L1D_MISS");
alias TO = Tuple!(TR,"L1_MISS", TR, "L1_HIT", TR,"DATA_ACC", 
TR,"ALL_ACC");

const double CYC_PER_SEC = 1_600_000_000;

// various metric definitions
// using Tuples with defined names for each member, and use the 
names here in the metrics.
TR met_l1_miss ( ref TI m){ TR rv; with(rv) with(m) { raw = 
L1I_MISS+L1D_MISS; per_cycle = cast(double)raw/proc_cyc; per_sec 
= per_cycle*CYC_PER_SEC;} return rv; }
TR met_l1_hit ( ref TI m){ TR rv; with(rv) with(m) { raw = 
L1I_HIT+L1D_HIT; per_cycle = cast(double)raw/proc_cyc; per_sec = 
per_cycle*CYC_PER_SEC;} return rv; }
TR met_data_acc ( ref TI m){ TR rv; with(rv) with(m) { raw = 
DATA_RD+DATA_WR; per_cycle = cast(double)raw/proc_cyc; per_sec = 
per_cycle*CYC_PER_SEC;} return rv; }
TR met_all_acc( ref TI m){ TR rv; with(rv) with(m) { raw = 
DATA_RD+DATA_WR+INST_FETCH; per_cycle = cast(double)raw/proc_cyc; 
per_sec = per_cycle*CYC_PER_SEC;} return rv; }


// a convenience to use all the metrics above as a list
alias Metrics = 
AliasSeq!(met_l1_miss,met_l1_hit,met_data_acc,met_all_acc);


void main(string[] argv)
{
auto samples = iota(1_00);
auto meas = new TI[samples.length];
auto results = new TO[samples.length];

// Initialize some values for the measured samples
foreach(i, ref m; meas){
		with(m){ proc_cyc = 1_000_000+i*2; DATA_RD = 1000+i; DATA_WR= 
2000+i; INST_FETCH=proc_cyc/2;

L1I_HIT= INST_FETCH-100; L1I_MISS=100;
L1D_HIT= DATA_RD+DATA_WR - 200; L1D_MISS=200;}
}

std.datetime.StopWatch sw;
sw.start();

ref TI getTerm(int i)
{
return meas[i];
}

	// compute the metric results for the above measured sample 
values in parallel

//taskPool.amap!(Metrics)(std.algorithm.map!getTerm(samples),results);

TR rv1 = met_l1_miss( meas[0]);
TR rv2 = met_l1_hit( meas[0]);
TR rv3 = met_data_acc( meas[0]);
TR rv4 = met_all_acc( meas[0]);

// how long did this take
long exec_ms = sw.peek().msecs;
writeln("measurements:", meas[0]);
writeln("rv1:", rv1);
writeln("rv2:", rv2);
writeln("rv3:", rv3);
writeln("rv4:", rv4);
writeln("results:", results[1]);
writeln("time:", exec_ms);

}



Regex start/end position of match?

2015-09-30 Thread Gerald via Digitalmars-d-learn
I'm using the std.regex API as part of Linux GUI grep utility I'm 
trying to create. I've got the GUI going fine using gtkd, the 
code to iterate over files (wow that was succinct in D, very 
impressive!), and getting matches via regex using the matchAll 
function.


I'm stuck though on how to get the start/end index of a match? 
Looking at RegexMatch, I don't see an obvious way to get this? 
I'll admit that coming from Java I'm not very comfortable with 
the Range concept in D, however I read the Range and Regex D 
documentation as well as Andrei's article on ranges referenced in 
the docs and I'm still not seeing what I'm missing.


By comparison, the Java API includes the start/end match index in 
the MatchResult interface. I have a feeling I'm not grokking 
something fundamental about Ranges in D, what am I missing?


BTW in case anyone asks, the reason I'm looking for this is to 
highlight the matches when displaying matching lines in the GUI.


Re: Checking that a template parameter is an enum

2015-09-30 Thread Fusxfaranto via Digitalmars-d-learn

On Thursday, 1 October 2015 at 00:04:18 UTC, Nordlöw wrote:
How do I check that a template parameter is a CT-value or an 
enum symbol?


I want this to restrict the following template:

/** Returns: true iff all values $(D V) are the same. */
template allSame(V...)  // TODO restrict to values only
{
static if (V.length <= 1)
enum bool allSame = true;
else
enum bool allSame = V[0] == V[1] && allSame!(V[1..$]);
}

unittest
{
static assert(!allSame!(41, 42));
static assert(allSame!(42, 42, 42));
}


std.traits to the rescue!

http://dlang.org/phobos/std_traits.html#isExpressions

Using isExpressions!V as a template constraint looks like the 
behavior you're looking for.


Checking that a template parameter is an enum

2015-09-30 Thread Nordlöw via Digitalmars-d-learn
How do I check that a template parameter is a CT-value or an enum 
symbol?


I want this to restrict the following template:

/** Returns: true iff all values $(D V) are the same. */
template allSame(V...)  // TODO restrict to values only
{
static if (V.length <= 1)
enum bool allSame = true;
else
enum bool allSame = V[0] == V[1] && allSame!(V[1..$]);
}

unittest
{
static assert(!allSame!(41, 42));
static assert(allSame!(42, 42, 42));
}



Re: address of overloaded function

2015-09-30 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 30 September 2015 at 22:48:03 UTC, Freddy wrote:
How do you take the address of a specific overloaded function. 
This won't compile


You can write a helper function that uses __traits(getOverloads) 
and searches them for the right signature:


http://dlang.org/traits.html#getOverloads



address of overloaded function

2015-09-30 Thread Freddy via Digitalmars-d-learn
How do you take the address of a specific overloaded function. 
This won't compile

---
import std.range;

void main()
{
ForwardAssignable!int range;
int delegate() @property get = &range.front;
void delegate(int) @property set = &range.front;
}
---


Re: an example of parallel calculation of metrics

2015-09-30 Thread Jay Norwood via Digitalmars-d-learn
On Wednesday, 30 September 2015 at 22:24:25 UTC, Jay Norwood 
wrote:

// various metric definitions
// the Tuples could also define names for each member and use 
the names here in the metrics.

long met1( TI m){ return m[0] + m[1] + m[2]; }
long met2( TI m){ return m[1] + m[2] + m[3]; }
long met3( TI m){ return m[0] - m[1] + m[2]; }
long met4( TI m){ return m[0] + m[1] - m[2]; }



should use reference parameters here:
long met1( ref TI m){ return m[0] + m[1] + m[2]; }
long met2( ref TI m){ return m[1] + m[2] + m[3]; }
long met3( ref TI m){ return m[0] - m[1] + m[2]; }
long met4( ref TI m){ return m[0] + m[1] - m[2]; }





an example of parallel calculation of metrics

2015-09-30 Thread Jay Norwood via Digitalmars-d-learn
This is something I'm playing with for work. We do this a lot, 
capture counter events for some number of on-chip performance 
counters, compute some metrics, display the outputs. This seems 
ideal for the application.


import std.algorithm, std.parallelism, std.range;
import std.stdio;
import std.datetime;
import std.typecons;
import std.meta;

// define some input measurement sample tuples and output metric 
tuples

alias TI = Tuple!(long, long, long, long, long);
alias TO = Tuple!(long, long, long, long);

// various metric definitions
// the Tuples could also define names for each member and use the 
names here in the metrics.

long met1( TI m){ return m[0] + m[1] + m[2]; }
long met2( TI m){ return m[1] + m[2] + m[3]; }
long met3( TI m){ return m[0] - m[1] + m[2]; }
long met4( TI m){ return m[0] + m[1] - m[2]; }

// a convenience to use all the metrics above as a list
alias Metrics = AliasSeq!(met1,met2,met3,met4);

void main(string[] argv)
{
auto samples = iota(1_000);
auto meas = new TI[samples.length];
auto results = new TO[samples.length];

// Initialize some values for the measured samples
foreach(i, ref m; meas){
m[0] = i;
m[1] = i+1;
m[2] = i+2;
m[3] = i+3;
m[4] = i+4;
}

std.datetime.StopWatch sw;
sw.start();

ref TI getTerm(int i)
{
return meas[i];
}

	// compute the metric results for the above measured sample 
values in parallel

taskPool.amap!(Metrics)(std.algorithm.map!getTerm(samples),results);

// how long did this take
long exec_ms = sw.peek().msecs;
writeln("results:", results);
writeln("time:", exec_ms);

}




Re: Range of variables

2015-09-30 Thread anonymous via Digitalmars-d-learn

On Wednesday, 30 September 2015 at 20:11:56 UTC, Freddy wrote:

Is there a way to make a range of a variables lazily?
---
int var1;
int var2;
void func()
{
int var3;
auto range = /*range of var1,var2,var3*/ ;
}
---


There's std.range.only which gives you a range over the arguments 
you pass without allocating an array for them. Not sure what you 
mean by "lazily".


http://dlang.org/phobos/std_range.html#only


Range of variables

2015-09-30 Thread Freddy via Digitalmars-d-learn

Is there a way to make a range of a variables lazily?
---
int var1;
int var2;
void func()
{
int var3;
auto range = /*range of var1,var2,var3*/ ;
}
---


Re: How to do unittests

2015-09-30 Thread qsdf via Digitalmars-d-learn

On Wednesday, 30 September 2015 at 14:20:28 UTC, Namal wrote:
On Wednesday, 30 September 2015 at 13:03:52 UTC, Rikki 
Cattermole wrote:

On 01/10/15 1:59 AM, Namal wrote:

Hello,

can someone give me a complete example please how to do 
unittests? I
tried this with the example from german wikipedia, but the 
flag

-unittest didn't make any difference.


Example file with loads of unittests: 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/uri.d


If you were to compile it e.g. dmd uri.d it won't be much use 
(unittest wise). You will need to dmd -unittest uri.d to 
compile them in. Don't forget to do the same for your main 
function. When you run the final executable the tests will 
execute before your main function does.


can't I do unittest in the main?


D unit tests are like a stack of free functions. You put them 
separatly.



when there's a main: dmd -unittest a.d
--
module a;
void main(){}

unittest{}
--


when there is no main: (like std.uri): dmd -main -unittest a.d
--
module a;
unittest{}
--

the -main switch adds a dummy main function so that the output 
can be executed.


But most of the time you'll think that nothing happens because 
the tests succeed...


Re: How to do unittests

2015-09-30 Thread Namal via Digitalmars-d-learn
On Wednesday, 30 September 2015 at 13:03:52 UTC, Rikki Cattermole 
wrote:

On 01/10/15 1:59 AM, Namal wrote:

Hello,

can someone give me a complete example please how to do 
unittests? I

tried this with the example from german wikipedia, but the flag
-unittest didn't make any difference.


Example file with loads of unittests: 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/uri.d


If you were to compile it e.g. dmd uri.d it won't be much use 
(unittest wise). You will need to dmd -unittest uri.d to 
compile them in. Don't forget to do the same for your main 
function. When you run the final executable the tests will 
execute before your main function does.


can't I do unittest in the main?


Re: How to do unittests

2015-09-30 Thread Rikki Cattermole via Digitalmars-d-learn

On 01/10/15 1:59 AM, Namal wrote:

Hello,

can someone give me a complete example please how to do unittests? I
tried this with the example from german wikipedia, but the flag
-unittest didn't make any difference.


Example file with loads of unittests: 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/uri.d


If you were to compile it e.g. dmd uri.d it won't be much use (unittest 
wise). You will need to dmd -unittest uri.d to compile them in. Don't 
forget to do the same for your main function. When you run the final 
executable the tests will execute before your main function does.


How to do unittests

2015-09-30 Thread Namal via Digitalmars-d-learn

Hello,

can someone give me a complete example please how to do 
unittests? I tried this with the example from german wikipedia, 
but the flag -unittest didn't make any difference.


Re: Threading Questions

2015-09-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, September 29, 2015 22:38:42 Johannes Pfau via Digitalmars-d-learn 
wrote:
> Am Tue, 29 Sep 2015 15:10:58 -0400
> schrieb Steven Schveighoffer :
>
> >
> > > 3) Why do I have to pass a "Mutex" to "Condition"? Why can't I just
> > > pass an "Object"?
> >
> > An object that implements the Monitor interface may not actually be a
> > mutex. For example, a pthread_cond_t requires a pthread_mutex_t to
> > operate properly. If you passed it anything that can act like a lock,
> > it won't work. So the Condition needs to know that it has an actual
> > Mutex, not just any lock-like object.
> >
> > I think I advocated in the past to Sean that Condition should provide
> > a default ctor that just constructs a mutex, but it doesn't look like
> > that was done.
> >
>
> But you'll need access to the Mutex in user code as well. And often you
> use multiple Conditions with one Mutex so a Condition doesn't really
> own the Mutex.
>
> > >
> > > 4) Will D's Condition ever experience spurious wakeups?
> >
> > What do you mean by "spurious"? If you notify a condition, anything
> > that is waiting on it can be woken up. Since the condition itself is
> > user defined, there is no way for the actual Condition to verify you
> > will only be woken up when it is satisfied.
> >
> > In terms of whether a condition could be woken when notify *isn't*
> > called, I suppose it's possible (perhaps interrupted by a signal?).
> > But I don't know why it would matter -- per above you should already
> > be checking the condition while within the lock.
>
> Spurious wakeup is a common term when talking about posix conditions
> and it does indeed mean a wait() call can return without ever calling
> notify():
> https://en.wikipedia.org/wiki/Spurious_wakeup
> http://stackoverflow.com/questions/8594591/why-does-pthread-cond-wait-have-spurious-wakeups
>
> And yes, this does happen for core.sync.condition as well. As a result
> you'll always have to check in a loop:
>
> synchronized(mutex)
> {
> while(some_flag_or_expression)
> {
> cond.wait();
> }
> }
>
> -
> synchronized(mutex)
> {
> some_flag_or_expression = true;
> cond.notify();
> }

What I took from the answers to that SO question was that in general, it
really doesn't matter whether a condition variable has spurious wakeups.
You're going to have to check that the associated bool is true when you wake
up anyway. Maybe without spurious wakeups, it wouldn't be required if only
one thread was waiting for the signal, but you'd almost certainly still need
an associated bool in case it becomes true prior to waiting. In addition, if
you want to avoid locking up your program, it's ferquently the case that you
want a timed wait so that you can check whether the program is trying to
exit (or at least that the thread in question is being terminated), and
you'd need a separate bool in that case as well so that you can check
whether the condition has actually been signaled. So, ultimately, while
spurious wakeups do seem wrong from a correctness perspective, when you look
at what a condition variable needs to do, it usually doesn't matter that
spurious wakeups exist, and a correctly used condition variable will just
handle spurious wakeups as a side effect of how it's used.

- Jonathan M Davis


Re: Why getting private member fails using getMember trait in a template?

2015-09-30 Thread Marc Schütz via Digitalmars-d-learn
On Wednesday, 30 September 2015 at 07:57:59 UTC, Atila Neves 
wrote:
On Tuesday, 29 September 2015 at 09:40:41 UTC, Alexandru 
Ermicioi wrote:
On Saturday, 26 September 2015 at 10:10:39 UTC, Alexandru 
Ermicioi wrote:

Suppose we have, two modules:

module testOne;

[...]


So, is this behavior correct?
If yes, then why?


Yes, because private members aren't accessible from another 
module. If they need to be accessed, then they need to be 
public.


Atila


As a workaround, you should be able to determine the index of the 
member (i.e. the how-many-th member it is in your struct/class), 
and then use .tupleof to access it, which circumvents access 
checks.


Re: Why getting private member fails using getMember trait in a template?

2015-09-30 Thread Atila Neves via Digitalmars-d-learn
On Tuesday, 29 September 2015 at 09:40:41 UTC, Alexandru Ermicioi 
wrote:
On Saturday, 26 September 2015 at 10:10:39 UTC, Alexandru 
Ermicioi wrote:

Suppose we have, two modules:

module testOne;

[...]


So, is this behavior correct?
If yes, then why?


Yes, because private members aren't accessible from another 
module. If they need to be accessed, then they need to be public.


Atila