Re: Reading a structured binary file?

2013-08-06 Thread H. S. Teoh
On Tue, Aug 06, 2013 at 06:48:12AM +0200, Jesse Phillips wrote:
[...]
 The only way I'm seeing to advance through the file is to keep an
 index on where you're currently reading from. This actually works
 perfect for the FileRange I mentioned in the previous post. Though
 I'm not familiar with how mmfile manages its memory, but hopefully
 there isn't buffer reuse or storing the slice could be overridden
 (not an issue for value data, but string data).

I don't know about D's Mmfile, but AFAIK, it maps directly to the OS
mmap(), which basically maps a portion of your program's address space
to the data on the disk. Meaning that the memory is managed by the OS,
and addresses will not change from under you.

In the underlying physical memory, pages may get swapped out and reused,
but this is invisible to your program, since referencing them will cause
the OS to swap the pages back in, so you'll never end up with invalid
pointers. The worst that could happen is the I/O performance hit
associated with swapping. Such is the utility of virtual memory.


T

-- 
Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG


Re: Getting number of messages in MessageBox

2013-08-06 Thread Marek Janukowicz
Ali Çehreli wrote:

 On 08/05/2013 04:18 PM, Marek Janukowicz wrote:
 I'm using std.concurrency message passing and I'd like to check which
 thread might be a bottleneck. The easiest would be check number of
 messages piled up for each of them, but I could not find a way to do
 that. Is it possible? Every detail about MessageBox seems to be hidden...
 
 Would setMaxMailboxSize() be helpful?
 
 void setMaxMailboxSize(Tid tid, size_t messages, bool function(Tid)
 onCrowdingDoThis);
 
 You can set a limit (perhaps that is lower than normal operation) and
 report whenever a particular Tid's MessageBox gets full.

Well, while this could help a little, it's not really what I'm looking for. 
I'd like to dump message counts for various threads periodically and see how 
it fluctuates over time. With the solution you propose I could only check 
how often a certain limit is hit.

-- 
Marek Janukowicz


Re: Reading a structured binary file?

2013-08-06 Thread Jonathan M Davis
On Monday, August 05, 2013 23:04:58 H. S. Teoh wrote:
 On Tue, Aug 06, 2013 at 06:48:12AM +0200, Jesse Phillips wrote:
 [...]
 
  The only way I'm seeing to advance through the file is to keep an
  index on where you're currently reading from. This actually works
  perfect for the FileRange I mentioned in the previous post. Though
  I'm not familiar with how mmfile manages its memory, but hopefully
  there isn't buffer reuse or storing the slice could be overridden
  (not an issue for value data, but string data).
 
 I don't know about D's Mmfile, but AFAIK, it maps directly to the OS
 mmap(), which basically maps a portion of your program's address space
 to the data on the disk. Meaning that the memory is managed by the OS,
 and addresses will not change from under you.
 
 In the underlying physical memory, pages may get swapped out and reused,
 but this is invisible to your program, since referencing them will cause
 the OS to swap the pages back in, so you'll never end up with invalid
 pointers. The worst that could happen is the I/O performance hit
 associated with swapping. Such is the utility of virtual memory.

mmap is awesome. It makes handling large files _way_ easier, especially when 
you have to worry about performance. It was a huge performance boost for one 
of our video recorder programs where I work when we switched to using mmap on 
it (this device is recording multiple video streams from cameras 24/7, and 
performance is critical). Trying to do what mmap does on your own is 
incredibly bug-prone and bound to be worse for performance (since you're doing 
it instead of the kernel). One of our older products tries to do it on its own 
(probably because the developers didn't know about mmap), and it's a royal 
mess.

- Jonathan M Davis


Re: tuple parameter fwd

2013-08-06 Thread kdmult

On Tuesday, 6 August 2013 at 05:07:51 UTC, Ali Çehreli wrote:

On 08/05/2013 09:51 PM, kdmult wrote:

On Monday, 5 August 2013 at 18:45:49 UTC, kdmult wrote:

class A(P...) {}

class B(R = A!P, P...) {}

P... should be rightmost, so how can I use it in the 
preceding parameter?


The default parameter value doesn't make sense as it's shown 
above.

Actually I want to port C++ code which looks like below.

template typename P1
class A1 {};
template typename P1, typename R=A1P1 
class B1 {}

template typename P1, typename P2
class A2 {};
template typename P1, typename P2, typename R=A2P1,P2 
class B2 {}

and so on.

I would use the variadic template parameters. However, I have 
no idea

how it can be done. Please advise.

Thanks.


Direct translation seems to work:

class A1(P1) {}
class B1(P1, R = A1!P1) {}

class A2(P1, P2) {}
class B2(P1, P2, R = A2!(P1, P2)) {}

void main()
{
auto a1 = new A1!int;
auto b1d = new B1!int;
auto b1 = new B1!(int, A1!double);
auto a2 = new A2!(int, double);
auto b2d = new B2!(int, double);
auto b2 = new B2!(int, double, A2!(char, short));
}

Ali


Is there a way to use variadic template/class parameter instead
of P1, P2, ..., PN ? If it is I could create 2 templated classes
instead of all that copy/pasted classes with P1, P2, etc
parameters.

Thanks.


Re: Which option is faster...

2013-08-06 Thread Jonathan M Davis
On Monday, August 05, 2013 15:59:23 jicman wrote:
 Greetings!
 
 I have this code,
 
 foreach (...)
 {
 
if (std.string.tolower(fext[0]) == doc ||
  std.string.tolower(fext[0]) == docx ||
  std.string.tolower(fext[0]) == xls ||
  std.string.tolower(fext[0]) == xlsx ||
  std.string.tolower(fext[0]) == ppt ||
  std.string.tolower(fext[0]) == pptx)
 continue;
 }
 
 foreach (...)
 {
if (std.string.tolower(fext[0]) == doc)
  continue;
if (std.string.tolower(fext[0]) == docx)
  continue;
if (std.string.tolower(fext[0]) == xls)
  continue;
if (std.string.tolower(fext[0]) == xlsx)
  continue;
if (std.string.tolower(fext[0]) == ppt)
  continue;
if (std.string.tolower(fext[0]) == pptx)
 continue;
...
...
 }

As others have pointed out, the two code snippets are semantically identical, 
and there's a decent chance that the compiler will generate identical code for 
both. It wouldn't surprise me if the first snippet resulted in more concise 
assembly code, but there shouldn't be any performance difference between the 
two. But the first snippet is cleaner, so between those two, you should use 
that.

Of course, as others have pointed out, it would be far more efficient to simply 
calculate std.string.tolower(fext[0]) once and re-use the result, which should 
be a very large performance gain here (assuming that the code around it 
doesn't dwarf it in cost). However, another possibility that I don't think 
anyone has pointed out is std.string.icmp, which will do case-insensitive 
comparison, meaning that you could do

if(fext[0].icmp(doc) == 0 || fext[0].icmp(docx) == 0 || ...)

and that might be more efficient as it avoids having to allocate a new string 
when fext[0] isn't all lowercase already. However, you would have to benchmark 
the code to be sure, since depending on the typical input, the code around 
this code, and how many times this code gets called, calling toLower once 
could be more efficient or using icmp could be more efficient, since toLower 
allocates whereas icmp doesn't, but icmp does more comparisons that a simple 
==. But if you really care about efficiency, you should try both ways and see 
which is faster for your use case.

- Jonathan M Davis


Re: Getting number of messages in MessageBox

2013-08-06 Thread Gabi

On Tuesday, 6 August 2013 at 06:15:20 UTC, Marek Janukowicz wrote:

Ali Çehreli wrote:


On 08/05/2013 04:18 PM, Marek Janukowicz wrote:
I'm using std.concurrency message passing and I'd like to 
check which
thread might be a bottleneck. The easiest would be check 
number of
messages piled up for each of them, but I could not find a 
way to do
that. Is it possible? Every detail about MessageBox seems to 
be hidden...


Would setMaxMailboxSize() be helpful?

void setMaxMailboxSize(Tid tid, size_t messages, bool 
function(Tid)

onCrowdingDoThis);

You can set a limit (perhaps that is lower than normal 
operation) and

report whenever a particular Tid's MessageBox gets full.


Well, while this could help a little, it's not really what I'm 
looking for.
I'd like to dump message counts for various threads 
periodically and see how
it fluctuates over time. With the solution you propose I could 
only check

how often a certain limit is hit.


Why not go for the trivial solution - just increase/decrease a 
counter for each push/pop message?


Re: Getting number of messages in MessageBox

2013-08-06 Thread Marek Janukowicz
Gabi wrote:

 Why not go for the trivial solution - just increase/decrease a
 counter for each push/pop message?

Yeah, that's most likely what I'll end up with, but it's a pity such 
information exists and I only can't access it because someone decided to 
make it private... or should I file a bug (or rather feature request) about 
it?

-- 
Marek Janukowicz


Re: Getting number of messages in MessageBox

2013-08-06 Thread dennis luehring

Am 06.08.2013 09:30, schrieb Marek Janukowicz:

Gabi wrote:


Why not go for the trivial solution - just increase/decrease a
counter for each push/pop message?


Yeah, that's most likely what I'll end up with, but it's a pity such
information exists and I only can't access it because someone decided to
make it private... or should I file a bug (or rather feature request) about
it?


the question is do the published counter then needs locking - and make 
it slow for all - just for beeing getable?




Re: Getting number of messages in MessageBox

2013-08-06 Thread Marek Janukowicz
dennis luehring wrote:
 the question is do the published counter then needs locking - and make
 it slow for all - just for beeing getable?

Good point - but I believe the code operating on the counter is synchronized 
already, so synchronized getter would not really slow things down.

-- 
Marek Janukowicz


X11 XSynchronize() definition in D

2013-08-06 Thread andrea9940
Hi, I'm working with the X11 library available from 
https://github.com/D-Programming-Deimos/libX11
If I try to call XSynchronize(display, True) the compilation 
fails with Error: function deimos.X11.Xlib.XSynchronize 
(_XDisplay*) is not callable using argument types (_XDisplay*, 
int)


I am sure the arguments are correct (see http://goo.gl/8Hzn8s for 
example) so I think there is a conversion problem between the C 
and D definition of the function:


--- Xlib.h
extern int (*XSynchronize(
Display*/* display */,
Bool/* onoff */
))(
Display*/* display */
);

--- Xlib.d
extern int function(
Display*/* display */,
Bool/* onoff */
)XSynchronize(
Display*/* display */
);


Also I can't understand why the C version is not extern int 
XSynchronize(Display*,

Bool); which would be the simplest definition.


Re: Getting number of messages in MessageBox

2013-08-06 Thread Gabi

On Tuesday, 6 August 2013 at 07:47:10 UTC, Marek Janukowicz wrote:

dennis luehring wrote:
the question is do the published counter then needs locking - 
and make

it slow for all - just for beeing getable?


Good point - but I believe the code operating on the counter is 
synchronized
already, so synchronized getter would not really slow things 
down.


I agree that this is much needed feature.
In python for example, the threaded Queue class has qsize() 
method that returns the APPROX size of the queue. Without it  my 
life would be much harder-I use it frequently.


Re: X11 XSynchronize() definition in D

2013-08-06 Thread bearophile

andrea9940:


extern int function(
Display*/* display */,
Bool/* onoff */
)XSynchronize(
Display*/* display */
);


Try extern(C)?

Bye,
bearophile


Re: Getting number of messages in MessageBox

2013-08-06 Thread Sean Kelly
On Aug 5, 2013, at 4:18 PM, Marek Janukowicz ma...@janukowicz.net wrote:

 I'm using std.concurrency message passing and I'd like to check which thread 
 might be a bottleneck. The easiest would be check number of messages piled 
 up for each of them, but I could not find a way to do that. Is it possible? 
 Every detail about MessageBox seems to be hidden...

Not currently.  It wouldn't be hard to add a function to get the count used by 
setMaxMailboxSize though.  I'll make a note to add it.

Re: X11 XSynchronize() definition in D

2013-08-06 Thread andrea9940

On Tuesday, 6 August 2013 at 08:21:26 UTC, bearophile wrote:

Try extern(C)?


It's not a linkage error, it's a syntax one


Re: tuple parameter fwd

2013-08-06 Thread Ali Çehreli

On 08/05/2013 11:45 AM, kdmult wrote:

 Hi,

 I would like to use a tuple template parameter in the default value of
 another template parameter in the same class declaration as follows.

 class A(P...) {}

 class B(R = A!P, P...) {}

 P... should be rightmost, so how can I use it in the preceding parameter?

 Thanks.

Do you mean that when P[0] is already an instance of A, then R should be 
P[0]. Otherwise, R should be A!P? If so, the following works:


class A(P...)
{
pragma(msg, \nA:  ~ P.stringof);
}

class BImpl(R, P...)
{
pragma(msg, R:  ~ R.stringof ~ , P:  ~ P.stringof);
}

template B(P...)
{
static if (is (P[0] == A!U, U)) {
/* The first type is already an A instance. Accept the types as 
is. */

alias B = BImpl!P;

} else {
/* The first type is not an A instance. Inject one. */
alias B = BImpl!(A!P, P);
}
}

void main()
{
auto b0 = new B!(int, double, char);
auto b1 = new B!(A!long, int, double);
}

Ali



Re: X11 XSynchronize() definition in D

2013-08-06 Thread Ali Çehreli

On 08/06/2013 01:01 AM, andrea9940 wrote:

 Hi, I'm working with the X11 library available from
 https://github.com/D-Programming-Deimos/libX11
 If I try to call XSynchronize(display, True) the compilation fails with
 Error: function deimos.X11.Xlib.XSynchronize (_XDisplay*) is not
 callable using argument types (_XDisplay*, int)

 I am sure the arguments are correct (see http://goo.gl/8Hzn8s for
 example) so I think there is a conversion problem between the C and D
 definition of the function:

 --- Xlib.h
 extern int (*XSynchronize(
  Display*/* display */,
  Bool/* onoff */
 ))(
  Display*/* display */
 );

I have written the following stubs for C:

typedef void Display;
typedef int Bool;

extern int (*XSynchronize(
Display*/* display */,
Bool/* onoff */
))(
Display*/* display */
);

typedef int(*PreviousAfterFunction)(Display*);

int previous_after_foo(Display* display)
{
return 0;
}

PreviousAfterFunction XSynchronize(Display* display, Bool onoff)
{
return previous_after_foo;
}

int main()
{
int (*paf)(Display*) = XSynchronize(0, 1);
paf(0);
}

And then I have written the direct translation in D, which works as well:

struct Display
{}

alias PreviousAfterFunction = int function(Display*);

int previousAfterFoo(Display *)
{
return 0;
}

PreviousAfterFunction XSynchronize(Display*, bool)
{
return previousAfterFoo;
}

void main()
{
PreviousAfterFunction paf = XSynchronize(null, 1);
paf(null);
}

 --- Xlib.d
 extern int function(
  Display*/* display */,
  Bool/* onoff */
 )XSynchronize(
  Display*/* display */
 );

It looks like Xlib.d got it backwards: XSynchronize takes two parameters 
and returns a function pointer that takes a single parameter. It should 
be the following:


int function(Display*) XSynchronize(Display*, bool);

 Also I can't understand why the C version is not extern int
 XSynchronize(Display*,
  Bool); which would be the simplest definition.

That's different. That would be a function taking two parameters and 
returning int. However, XSynchronize takes two parameters and return a 
function pointer (that takes one parameter and returns an int).


Ali



Re: X11 XSynchronize() definition in D

2013-08-06 Thread David
 struct Display
 {}

D supports opaque structs/pointers

struct Display;

Now Display has to be used as pointer only or the compiler complains
e.g. about

Display display;



Re: X11 XSynchronize() definition in D

2013-08-06 Thread andrea9940

@Ali Çehreli
Thank you for the explanation. I'll send a pull request to fix 
Xlib.d


Re: Get attributes of type by string name

2013-08-06 Thread Jacob Carlborg

On 2013-08-02 03:02, JS wrote:

how can I get the UDA's of a type that I only know by name and only in a
CTFE.

I would like to loop over an array of names passed to a me(I don't know
their contents beforehand) and get the attributes.

I've tried to use a mixin but I can't get the mixin to work on the
string name...

e.g., mixin(alias a = __traits(getAttributes, ~type~););


You need to use a typetuple as well:

import std.typetuple;
enum attribute;

@attribute struct Foo {}

void main ()
{
alias a = TypeTuple!(__traits(getAttributes, mixin(Foo)));
pragma(msg, a);
}

--
/Jacob Carlborg


Re: Which option is faster...

2013-08-06 Thread jicman

On Tuesday, 6 August 2013 at 04:10:57 UTC, Andre Artus wrote:

On Monday, 5 August 2013 at 13:59:24 UTC, jicman wrote:


Greetings!

I have this code,

foreach (...)
{

 if (std.string.tolower(fext[0]) == doc ||
   std.string.tolower(fext[0]) == docx ||
   std.string.tolower(fext[0]) == xls ||
   std.string.tolower(fext[0]) == xlsx ||
   std.string.tolower(fext[0]) == ppt ||
   std.string.tolower(fext[0]) == pptx)
  continue;
}

foreach (...)
{
 if (std.string.tolower(fext[0]) == doc)
   continue;
 if (std.string.tolower(fext[0]) == docx)
   continue;
 if (std.string.tolower(fext[0]) == xls)
   continue;
 if (std.string.tolower(fext[0]) == xlsx)
   continue;
 if (std.string.tolower(fext[0]) == ppt)
   continue;
 if (std.string.tolower(fext[0]) == pptx)
  continue;
 ...
 ...
}

thanks.

josé


What exactly are you trying to do with this? I get the 
impression that there is an attempt at local optimization 
when broader approach could lead to better results.


For instance. Using the OS's facilities to filter (six 
requests, one each for *.doc, *.docx) could actually end up 
being a lot faster.


If you could give more detail about what you are trying to 
achieve then it could be possible to get better results.


The files are in a network drive and doing a list foreach *.doc, 
*.docx, etc. will be more expensive than getting the list of all 
the files at once and then processing them accordingly.


Re: tuple parameter fwd

2013-08-06 Thread kdmult

On Tuesday, 6 August 2013 at 09:53:33 UTC, Ali Çehreli wrote:
Do you mean that when P[0] is already an instance of A, then R 
should be P[0]. Otherwise, R should be A!P? If so, the 
following works:


class A(P...)
{
pragma(msg, \nA:  ~ P.stringof);
}

class BImpl(R, P...)
{
pragma(msg, R:  ~ R.stringof ~ , P:  ~ P.stringof);
}

template B(P...)
{
static if (is (P[0] == A!U, U)) {
/* The first type is already an A instance. Accept the 
types as is. */

alias B = BImpl!P;

} else {
/* The first type is not an A instance. Inject one. */
alias B = BImpl!(A!P, P);
}
}

void main()
{
auto b0 = new B!(int, double, char);
auto b1 = new B!(A!long, int, double);
}

Ali


Great! Thanks Ali.


Re: Which option is faster...

2013-08-06 Thread Andrej Mitrovic
On 8/5/13, H. S. Teoh hst...@quickfur.ath.cx wrote:
 If you really want optimal performance, use std.regex:

Yes and if you also want to bloat your executable to the point that
other parts of the system start slowing down.


Re: Which option is faster...

2013-08-06 Thread H. S. Teoh
On Tue, Aug 06, 2013 at 02:32:11PM +0200, jicman wrote:
 On Tuesday, 6 August 2013 at 04:10:57 UTC, Andre Artus wrote:
[...]
 What exactly are you trying to do with this? I get the impression
 that there is an attempt at local optimization when broader
 approach could lead to better results.
 
 For instance. Using the OS's facilities to filter (six requests,
 one each for *.doc, *.docx) could actually end up being a lot
 faster.
 
 If you could give more detail about what you are trying to achieve
 then it could be possible to get better results.
 
 The files are in a network drive and doing a list foreach *.doc,
 *.docx, etc. will be more expensive than getting the list of all the
 files at once and then processing them accordingly.

In this case, the bottleneck most likely is in the network latency, so
optimizing string comparisons won't get you very far. You should instead
be focusing on how to improve the performance of network accesses.

What kind of processing are you doing with the filtered list of files?
If it's a complicated operation, you might want to consider having a
separate thread for fetching the list of files while work is being done
on them in the main thread.


T

-- 
Computers shouldn't beep through the keyhole.


apply function to parameters

2013-08-06 Thread Jack Applegame

In C++11 we can write:

templateclass... Args
void somefunc(Args... args {
...
}

templateclass T
T process(T p) {
...
}

template class... Args
void foo(Args... args) {
somefunc(process(args)...);
}

Is there a simple way to do this in D?


Re: apply function to parameters

2013-08-06 Thread Jack Applegame

In other words, I need something like this:

foo(magicTemplate!(f, a1, a2, a3...)) === foo(f(a1), f(a2), 
f(a3)...)


Re: apply function to parameters

2013-08-06 Thread bearophile

Jack Applegame:


Is there a simple way to do this in D?


somefunc(process(args));

Bye,
bearophile


Re: apply function to parameters

2013-08-06 Thread bearophile

Jack Applegame:


In other words, I need something like this:

foo(magicTemplate!(f, a1, a2, a3...)) === foo(f(a1), f(a2), 
f(a3)...)


Sorry, your second post was not yet present and I misunderstood 
your question.


Take a look at std.typetuple.staticMap

Bye,
bearophile


Re: Which option is faster...

2013-08-06 Thread Andre Artus

On Tuesday, 6 August 2013 at 12:32:13 UTC, jicman wrote:

On Tuesday, 6 August 2013 at 04:10:57 UTC, Andre Artus wrote:

On Monday, 5 August 2013 at 13:59:24 UTC, jicman wrote:


Greetings!

I have this code,

foreach (...)
{

if (std.string.tolower(fext[0]) == doc ||
  std.string.tolower(fext[0]) == docx ||
  std.string.tolower(fext[0]) == xls ||
  std.string.tolower(fext[0]) == xlsx ||
  std.string.tolower(fext[0]) == ppt ||
  std.string.tolower(fext[0]) == pptx)
 continue;
}

foreach (...)
{
if (std.string.tolower(fext[0]) == doc)
  continue;
if (std.string.tolower(fext[0]) == docx)
  continue;
if (std.string.tolower(fext[0]) == xls)
  continue;
if (std.string.tolower(fext[0]) == xlsx)
  continue;
if (std.string.tolower(fext[0]) == ppt)
  continue;
if (std.string.tolower(fext[0]) == pptx)
 continue;
...
...
}

thanks.

josé


What exactly are you trying to do with this? I get the 
impression that there is an attempt at local optimization 
when broader approach could lead to better results.


For instance. Using the OS's facilities to filter (six 
requests, one each for *.doc, *.docx) could actually end 
up being a lot faster.


If you could give more detail about what you are trying to 
achieve then it could be possible to get better results.


The files are in a network drive and doing a list foreach 
*.doc, *.docx, etc. will be more expensive than getting the 
list of all the files at once and then processing them 
accordingly.


Again, what are you trying to achieve?
Your statement is not necessarily true, for a  myriad of reasons, 
but it entirely depends on what you want to do.
I would reiterate Dennis Luehring's reply, why are you not 
benching? It seems like you are guessing at what the problems 
are, that's hardly ever useful.
One of the first rules of network optimization  is to reduce the 
amount od data, that normally means filtering.at the server, the 
next thing is coarse grained is better than fine (BOCTAOE/L).


Re: X11 XSynchronize() definition in D

2013-08-06 Thread Ali Çehreli

On 08/06/2013 03:22 AM, David wrote: struct Display
 {}

 D supports opaque structs/pointers

 struct Display;

 Now Display has to be used as pointer only or the compiler complains
 e.g. about

 Display display;

Yes and I am sure that is how Xlib.d has it.

However, remembering a limitation that we have discovered a couple of 
weeks ago, I intentionally did not make it opaque, in case somebody 
would try toString() on it:


  http://forum.dlang.org/post/ksc2ln$f1i$1...@digitalmars.com

I have been to lazy to bring this topic up on the main newsgroup before 
opening an enhancement request.


Ali



Re: apply function to parameters

2013-08-06 Thread Jack Applegame

On Tuesday, 6 August 2013 at 14:44:18 UTC, bearophile wrote:

Take a look at std.typetuple.staticMap

staticMap works with types, and I don't know how it can help.



Re: apply function to parameters

2013-08-06 Thread Dicebot

On Tuesday, 6 August 2013 at 15:51:28 UTC, Jack Applegame wrote:

On Tuesday, 6 August 2013 at 14:44:18 UTC, bearophile wrote:

Take a look at std.typetuple.staticMap

staticMap works with types, and I don't know how it can help.


Not really. Anything that can be provided to template argument 
list can be stored in TypeTuple and thus processed by staticMap. 
Name is very confusing here.


Re: X11 XSynchronize() definition in D

2013-08-06 Thread Ali Çehreli

On 08/06/2013 08:46 AM, Ali Çehreli wrote:

 On 08/06/2013 03:22 AM, David wrote: struct Display
   {}
  
   D supports opaque structs/pointers
  
   struct Display;
  
   Now Display has to be used as pointer only or the compiler complains
   e.g. about
  
   Display display;

 Yes and I am sure that is how Xlib.d has it.

 However, remembering a limitation that we have discovered a couple of
 weeks ago, I intentionally did not make it opaque, in case somebody
 would try toString() on it:

I am confused and being confusing... I think nobody should apply 
toString() on an opaque type as it is supposed to dereference the actual 
object.


http://forum.dlang.org/post/ksc2ln$f1i$1...@digitalmars.com

I will continue on that thread.

Ali



Re: apply function to parameters

2013-08-06 Thread Jack Applegame

On Tuesday, 6 August 2013 at 16:03:07 UTC, Dicebot wrote:

On Tuesday, 6 August 2013 at 15:51:28 UTC, Jack Applegame wrote:

On Tuesday, 6 August 2013 at 14:44:18 UTC, bearophile wrote:

Take a look at std.typetuple.staticMap

staticMap works with types, and I don't know how it can help.


Not really. Anything that can be provided to template argument 
list can be stored in TypeTuple and thus processed by 
staticMap. Name is very confusing here.

Yes. Types, literals and aliases. But this doesn't help.

I wrote very ugly, but working solution. I don't like it, but I 
can't invent something better. :(


auto apply(alias p, alias f, Args...)(Args args) {
auto impl(uint num, Head, Tail...)(Head head, Tail tail) {
static if(num == 0) return p(head, tail);
else return impl!(num - 1)(tail, f(head));
}
return impl!(args.length)(args);
}

apply!(fun1, fun2)(arg1, arg2, ...) === fun1(fun2(arg1), 
fun2(arg2), ...)


offtop: Dicebot, do you speak russian?


Re: apply function to parameters

2013-08-06 Thread Artur Skawina
On 08/06/13 18:03, Dicebot wrote:
 On Tuesday, 6 August 2013 at 15:51:28 UTC, Jack Applegame wrote:
 On Tuesday, 6 August 2013 at 14:44:18 UTC, bearophile wrote:
 Take a look at std.typetuple.staticMap
 staticMap works with types, and I don't know how it can help.
 
 Not really. Anything that can be provided to template argument list can be 
 stored in TypeTuple and thus processed by staticMap. Name is very confusing 
 here.

The problem is more that staticMap requires a *template*, not a
function. Another one is that doing it like that (ie storing the
intermediate results in a tuple or struct) creates unnecessary
copies -  which isn't really an issue for small PODs, because the
compiler optimizes most of the overhead away, but can have a
significant cost when handling types w/ nontrivial cpctors etc.

Anyway, there are several ways to do this in D. One example:

   void foo(Args...)(Args args) {
   mixin(evalExpMap!(q{ somefunc(%...); }, q{ process(%s) }, args));
 // == `somefunc(process(args[0]), process(args[1]), etc);`
   }

   // or

   void foo(Args...)(Args args) {
   auto result = mixin(evalExpMap!(q{ somefunc(%...) }, q{ process(%s) }, 
args));
   //...
   }

   // and the helper:

   template evalExpMap(string C, string F, A...) {
  enum evalExpMap = {
 import std.array, std.conv;
 string s, l;
 static if (is(typeof(A))) alias B = typeof(A);
else   alias B = A;
 foreach (I, _; B) {
auto r = replace( replace(F, %s, A[I].stringof),
 %d, to!string(I));
l ~= (I?, :) ~ r;
s ~=   r ~ ;\n;
 }
 return replace(replace(C, %...;, s), %..., l);
  }();
   }

Not as simple as the C++ equivalent. But not that much more
complicated and *much* more powerful.

artur


Re: Logging and tracing in D

2013-08-06 Thread evilrat

On Tuesday, 6 August 2013 at 04:35:39 UTC, Andre Artus wrote:
What is the recommended approach for adding logging and tracing 
to D apps?

Is there a library for it?


custom very simple yet powerful logging can be achieved with 
templates and debug specifier, something like this:


module logger;
import std.datetime;
import std.stdio : writefln;

template log(T) {
 auto timeString = Clock.currTime().toISOExtString();
 writefln(log message from(%s)[+%s]: %s, __FUNCTION__, 
timeString, T);

}


then in ur code just place with debug specifier:

import logger;

void main {
 debug log!my stuff;
}


and you get pretty formatted message in console(add log file 
writer if needed), and you get smart logging for free, in that 
way it will be generated only for debug mode(in release it will 
be skipped because of debug specifier).


Re: Logging and tracing in D

2013-08-06 Thread Andre Artus

-- snip--
and you get pretty formatted message in console(add log file 
writer if needed), and you get smart logging for free, in 
that way it will be generated only for debug mode(in release it 
will be skipped because of debug specifier).


Thanks, can I take it that there is no official library for this 
kind of thing then? I would think it generally useful. Perhaps it 
can be considered for inclusion into Phobos.


Re: Logging and tracing in D

2013-08-06 Thread H. S. Teoh
On Tue, Aug 06, 2013 at 07:22:11PM +0200, Andre Artus wrote:
 -- snip--
 and you get pretty formatted message in console(add log file
 writer if needed), and you get smart logging for free, in that
 way it will be generated only for debug mode(in release it will be
 skipped because of debug specifier).
 
 Thanks, can I take it that there is no official library for this
 kind of thing then? I would think it generally useful. Perhaps it
 can be considered for inclusion into Phobos.

This is strange. I clearly remember some time ago that there was a
logging module in the Phobos review queue. But after the initial
feedback, I haven't heard anything from it since. What's going on? Has
it been abandoned?


T

-- 
Those who've learned LaTeX swear by it. Those who are learning LaTeX swear at 
it. -- Pete Bleackley


Re: Logging and tracing in D

2013-08-06 Thread David
Am 06.08.2013 19:22, schrieb Andre Artus:
 -- snip--
 and you get pretty formatted message in console(add log file writer if
 needed), and you get smart logging for free, in that way it will be
 generated only for debug mode(in release it will be skipped because of
 debug specifier).
 
 Thanks, can I take it that there is no official library for this kind of
 thing then? I would think it generally useful. Perhaps it can be
 considered for inclusion into Phobos.

There is std.log (proposed), it doesn't work, I don't like its API.

I wrote my own logger (consider it MIT licensed)
https://github.com/Dav1dde/BraLa/blob/master/brala/utils/log.d

I am not 100% happy how it turned out, but I haven't come around to
change it, but it works pretty well.


Re: Logging and tracing in D

2013-08-06 Thread Andre Artus

David wrote:
Am 06.08.2013 19:22, schrieb Andre Artus:

-- snip--
and you get pretty formatted message in console(add log file 
writer if
needed), and you get smart logging for free, in that way it 
will be
generated only for debug mode(in release it will be skipped 
because of

debug specifier).


Thanks, can I take it that there is no official library for 
this kind of
thing then? I would think it generally useful. Perhaps it can 
be

considered for inclusion into Phobos.


There is std.log (proposed), it doesn't work, I don't like its 
API.


I take it that it hasn't been documented yet, at least I could 
not find it on dlang.org.



David:
I wrote my own logger (consider it MIT licensed)
https://github.com/Dav1dde/BraLa/blob/master/brala/utils/log.d

I am not 100% happy how it turned out, but I haven't come 
around to

change it, but it works pretty well.


Cool, thanks, I'll check it out.


Re: Logging and tracing in D

2013-08-06 Thread H. S. Teoh
On Tue, Aug 06, 2013 at 08:54:48PM +0200, Andre Artus wrote:
 David wrote:
 Am 06.08.2013 19:22, schrieb Andre Artus:
 -- snip--
 and you get pretty formatted message in console(add log file writer
 if needed), and you get smart logging for free, in that way it
 will be generated only for debug mode(in release it will be skipped
 because of debug specifier).
 
 Thanks, can I take it that there is no official library for this
 kind of thing then? I would think it generally useful. Perhaps it
 can be considered for inclusion into Phobos.
 
 There is std.log (proposed), it doesn't work, I don't like its API.
 
 I take it that it hasn't been documented yet, at least I could not
 find it on dlang.org.
[...]

Sounds like it's not merged into the official codebase yet.


T

-- 
Always remember that you are unique. Just like everybody else. -- despair.com


Re: Getting number of messages in MessageBox

2013-08-06 Thread Dmitry Olshansky

06-Aug-2013 03:18, Marek Janukowicz пишет:

I'm using std.concurrency message passing and I'd like to check which thread
might be a bottleneck. The easiest would be check number of messages piled
up for each of them, but I could not find a way to do that. Is it possible?
Every detail about MessageBox seems to be hidden...



This is sadly intentional. The reasons must be due to some efficient 
concurrent queues not being able to produce reliable item count if at all.


However this seems at odds with setMaxMailboxSize ...

--
Dmitry Olshansky


Re: Logging and tracing in D

2013-08-06 Thread David
Am 06.08.2013 20:54, schrieb Andre Artus:
 David wrote:
 Am 06.08.2013 19:22, schrieb Andre Artus:
 -- snip--
 and you get pretty formatted message in console(add log file writer if
 needed), and you get smart logging for free, in that way it will be
 generated only for debug mode(in release it will be skipped because of
 debug specifier).

 Thanks, can I take it that there is no official library for this kind of
 thing then? I would think it generally useful. Perhaps it can be
 considered for inclusion into Phobos.

 There is std.log (proposed), it doesn't work, I don't like its API.
 
 I take it that it hasn't been documented yet, at least I could not find
 it on dlang.org.

It's currently in the review queue: http://wiki.dlang.org/Review_Queue
but marked on hold/suspended.


Re: Logging and tracing in D

2013-08-06 Thread Andre Artus

-- snip --
I take it that it hasn't been documented yet, at least I could 
not find

it on dlang.org.


It's currently in the review queue: 
http://wiki.dlang.org/Review_Queue

but marked on hold/suspended.


Thanks, I'll check it out later.


Is it possible using reflection or similar to extract only public method names from classes?

2013-08-06 Thread Gary Willoughby
Is it possible using reflection or similar to extract only public 
method names from classes? I'm thinking how i would go about 
writing a unit test/mocking framework, investigating how i can 
gather information about such things before i manipulate them.


Re: Getting number of messages in MessageBox

2013-08-06 Thread Sean Kelly
On Aug 6, 2013, at 1:27 PM, Dmitry Olshansky dmitry.o...@gmail.com wrote:

 06-Aug-2013 03:18, Marek Janukowicz пишет:
 I'm using std.concurrency message passing and I'd like to check which thread
 might be a bottleneck. The easiest would be check number of messages piled
 up for each of them, but I could not find a way to do that. Is it possible?
 Every detail about MessageBox seems to be hidden...
 
 
 This is sadly intentional. The reasons must be due to some efficient 
 concurrent queues not being able to produce reliable item count if at all.
 
 However this seems at odds with setMaxMailboxSize ...

The lack of a function to get the current mailbox size is mostly an oversight.  
It would be easy to return the same message count used by setMaxMailboxSize.  
This isn't an exact count, since that would be inefficient as you say, but it's 
a reasonable approximation.  So I wouldn't use the returned message count as a 
pivotal part of a protocol design, for example, but it would be fine for 
determining whether a consumer is being overwhelmed by requests.

That said, with the possible exception of setMaxMailboxSize allowing one thread 
to affect the behavior of another thread's mailbox, I think the current 
functionality should all extend to interprocess messaging without alteration.  
I'm not sure that this could be done for a getMailboxSize call.  Certainly not 
with any semblance of efficiency anyway.  But perhaps it's reasonable to have 
some subset of functionality that only works on local threads so long as the 
core messaging API doesn't have such limitations.

Re: Logging and tracing in D

2013-08-06 Thread Jesse Phillips

On Tuesday, 6 August 2013 at 18:54:50 UTC, Andre Artus wrote:

David wrote:
There is std.log (proposed), it doesn't work, I don't like its 
API.


I take it that it hasn't been documented yet, at least I could 
not find it on dlang.org.


http://wiki.dlang.org/Review_Queue


Re: Is it possible using reflection or similar to extract only public method names from classes?

2013-08-06 Thread Marek Janukowicz
Gary Willoughby wrote:

 Is it possible using reflection or similar to extract only public
 method names from classes? I'm thinking how i would go about
 writing a unit test/mocking framework, investigating how i can
 gather information about such things before i manipulate them.

See traits: http://dlang.org/traits.html

Look for: getProtection, getVirtualFunctions, getVirtualMethods.

-- 
Marek Janukowicz


Re: Is it possible using reflection or similar to extract only public method names from classes?

2013-08-06 Thread H. S. Teoh
On Tue, Aug 06, 2013 at 11:41:35PM +0200, Gary Willoughby wrote:
 Is it possible using reflection or similar to extract only public
 method names from classes? I'm thinking how i would go about writing
 a unit test/mocking framework, investigating how i can gather
 information about such things before i manipulate them.

The following code demonstrates how you can do this:

import std.stdio;

class Base {
private int x;
public int y;

this() {}
private void privMethod() {}
public void method() {}
}

class Derived : Base {
public override void method() {}
public void derivedMethod() {}
private void privDerivedMethod() {}
}

void showAllMethods(C)(C obj) {
writeln(All members:);
foreach (field; __traits(allMembers, C)) {
static if (is(typeof(__traits(getMember, obj, field)) T 
== function)) {
auto prot = __traits(getProtection, 
__traits(getMember, obj, field));
writefln(\t(%s) %s, prot, field);
}
}
}

void showDerivedMethods(C)(C obj) {
writeln(\nDerived members:);
foreach (field; __traits(derivedMembers, C)) {
static if (is(typeof(__traits(getMember, obj, field)) T 
== function)) {
auto prot = __traits(getProtection, 
__traits(getMember, obj, field));
writefln(\t(%s) %s, prot, field);
}
}
}

string[] getPublicMethods(C)(C obj) {
string[] methods;
foreach (field; __traits(allMembers, C)) {
static if (is(typeof(__traits(getMember, obj, field)) 
== function) 
__traits(getProtection, 
__traits(getMember, obj, field)) == public)
{
methods ~= field;
}
}
return methods;
}

void main() {
auto d = new Derived();
showAllMethods(d);
showDerivedMethods(d);

writeln(All public methods:);
writeln(getPublicMethods(d));
}


The output is:

All members:
(public) method
(public) derivedMethod
(private) privDerivedMethod
(public) __ctor
(private) privMethod
(public) toString
(public) toHash
(public) opCmp
(public) opEquals
(public) factory

Derived members:
(public) method
(public) derivedMethod
(private) privDerivedMethod
(public) __ctor
All public methods:
[method, derivedMethod, __ctor, toString, toHash, opCmp, 
opEquals, factory]


Hope this helps!


T

-- 
Designer clothes: how to cover less by paying more.


Re: Which option is faster...

2013-08-06 Thread jicman

On Tuesday, 6 August 2013 at 14:49:42 UTC, Andre Artus wrote:

On Tuesday, 6 August 2013 at 12:32:13 UTC, jicman wrote:

On Tuesday, 6 August 2013 at 04:10:57 UTC, Andre Artus wrote:

On Monday, 5 August 2013 at 13:59:24 UTC, jicman wrote:


Greetings!

I have this code,

foreach (...)
{

if (std.string.tolower(fext[0]) == doc ||
 std.string.tolower(fext[0]) == docx ||
 std.string.tolower(fext[0]) == xls ||
 std.string.tolower(fext[0]) == xlsx ||
 std.string.tolower(fext[0]) == ppt ||
 std.string.tolower(fext[0]) == pptx)
continue;
}

foreach (...)
{
if (std.string.tolower(fext[0]) == doc)
 continue;
if (std.string.tolower(fext[0]) == docx)
 continue;
if (std.string.tolower(fext[0]) == xls)
 continue;
if (std.string.tolower(fext[0]) == xlsx)
 continue;
if (std.string.tolower(fext[0]) == ppt)
 continue;
if (std.string.tolower(fext[0]) == pptx)
continue;
...
...
}

thanks.

josé


What exactly are you trying to do with this? I get the 
impression that there is an attempt at local optimization 
when broader approach could lead to better results.


For instance. Using the OS's facilities to filter (six 
requests, one each for *.doc, *.docx) could actually end 
up being a lot faster.


If you could give more detail about what you are trying to 
achieve then it could be possible to get better results.


The files are in a network drive and doing a list foreach 
*.doc, *.docx, etc. will be more expensive than getting the 
list of all the files at once and then processing them 
accordingly.


Again, what are you trying to achieve?
Your statement is not necessarily true, for a  myriad of 
reasons, but it entirely depends on what you want to do.
I would reiterate Dennis Luehring's reply, why are you not 
benching? It seems like you are guessing at what the problems 
are, that's hardly ever useful.
One of the first rules of network optimization  is to reduce 
the amount od data, that normally means filtering.at the 
server, the next thing is coarse grained is better than fine 
(BOCTAOE/L).


It's a long story and I will return in a few months and give you 
the whole story, but right now, time is not on my side.  I have 
answers for all the questions you folks have asked, and I 
appreciate all the input.  I have the answer that I was looking 
for, so in a few months, I will come back and explain the whole 
story.  Thanks for all the response and suggestions.


jic