Re: GStreamer and D

2017-06-15 Thread Russel Winder via Digitalmars-d-learn
On Thu, 2017-06-15 at 19:27 +, Jay Norwood via Digitalmars-d-learn
wrote:
> wow! I hadn't tried this gtkd library before. I was hunting for 
> the gstreamer in particular.

Welcome to the group of people using GStreamer from D. I suspect I may
be the only other member of that club.

> The hello_world alsa-sink audio example failed on Windows.  The 
> debugger indicates no sink, which I guess is reasonable.

Ah the word Windows. I use only Debian Sid and Fedora Rawhide, with
occasional descent into some ancient version of MacOS (Apple refuse to
upgrade older laptops, some agist excuse ;-) so I can't help with
anything platform specific relating to Windows. 

Both Debian Sid and Fedora Rawhide have packages for GStreamer and LDC
so installation is very easy.

> With very little effort, though, I converted the hello_world 
> example to generate a video test pattern and use vidoeconvert and 
> autovideosink, and that popped up right away on Windows in a 64 
> bit build ... so, nice going!
> 
> The gstreamer example built without error in msvc 2013 with 
> visualD and DMD32 D Compiler v2.073.2

DMD is not packaged for Debian, but there is D-Apt so it is available,
or Fedora, so LDC tends to be the "go to" D compiler. I haven't tried
the example codes, but My GStreamer programs in D work nicely.

I am just starting to rewrite Me TV from C++14 to D.

-- 
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: Help with an algorithm!

2017-06-15 Thread Ivan Kazmenko via Digitalmars-d-learn

On Thursday, 15 June 2017 at 13:41:07 UTC, MGW wrote:
On Thursday, 15 June 2017 at 13:16:24 UTC, CRAIG DILLABAUGH 
wrote:


The purpose - search of changes in file system.
Sorting is a slow operation as well as hashing. Creation of a 
tree, is equally in sorting.

So far the best result:

foreach(str; m2) {
bool fFind; int j;
foreach(int i, s; m1) {
if(str == s) { fFind = true; j = i; break; }
}
if(!fFind) { rez ~= str; }
else   {m1[j] = m1[$-1]; m1.length = m1.length - 1; }
}


Ugh.  This can work as slow as length-of-m1 *multiplied* by 
length-of-m2.  For 5,000,000 strings, it is 5,000,000 * 5,000,000 
= 25,000,000,000,000.  Granted, if you run it very often, the 
arrays are almost equal, and it's closer to linear.  But once 
there are substantial changes between two consecutive runs, this 
approach is seriously screwed.


Sorting would work in length-of-array * log(length-of-array).  
For 5,000,000 strings, it is 5,000,000 * 23 = 115,000,000.  This 
is ~217,391 times better than your method above.  May be a bit 
slower because of long common prefixes.  Anyway, a couple of 
seconds at most.  How fast you need it to be?  Did you actually 
try it?


Ivan Kazmenko.



Re: GStreamer and D

2017-06-15 Thread Jay Norwood via Digitalmars-d-learn
wow! I hadn't tried this gtkd library before. I was hunting for 
the gstreamer in particular.


The hello_world alsa-sink audio example failed on Windows.  The 
debugger indicates no sink, which I guess is reasonable.


With very little effort, though, I converted the hello_world 
example to generate a video test pattern and use vidoeconvert and 
autovideosink, and that popped up right away on Windows in a 64 
bit build ... so, nice going!


The gstreamer example built without error in msvc 2013 with 
visualD and DMD32 D Compiler v2.073.2





Re: Implementing interfaces using alias this

2017-06-15 Thread Jesse Phillips via Digitalmars-d-learn
On Wednesday, 14 June 2017 at 09:34:27 UTC, Balagopal Komarath 
wrote:
Why doesn't this work? The Test!Duck type has a void quack() 
method but the compiler says it is not implemented.


You question was answered, but you can do this:

--
interface IDuck
{
void quack();
}

struct Duck
{
void quack()
{
import std.stdio : writeln;
writeln("Quack");
}
}


void main()
{
Duck d;
import std.experimental.typecons : wrap;
IDuck i = wrap!IDuck(d);
i.quack();
}
-

Please note that you can't wrap to an interface which returns 
itself


interface IDuck {
IDuck clone();
}

It fails because the struct would return a Duck and wouldn't wrap 
it to an IDuck. This is only a limitation on wrap because I 
haven't written the detection and appropriate wrapping function 
call.


Re: Help with an algorithm!

2017-06-15 Thread CRAIG DILLABAUGH via Digitalmars-d-learn

On Thursday, 15 June 2017 at 13:41:07 UTC, MGW wrote:
On Thursday, 15 June 2017 at 13:16:24 UTC, CRAIG DILLABAUGH 
wrote:


The purpose - search of changes in file system.
Sorting is a slow operation as well as hashing. Creation of a 
tree, is equally in sorting.

So far the best result:

string[] rez;

foreach(str; m2) {
bool fFind; int j;
foreach(int i, s; m1) {
if(str == s) { fFind = true; j = i; break; }
}
if(!fFind) { rez ~= str; }
else   {m1[j] = m1[$-1]; m1.length = m1.length - 1; }
}

//  rez => rezult

How to parallel on thred?


radix sort is O(N) time, which is as fast as you can hope. But 
given your specific problem domain (the strings are paths) an 
initial radix sort step likely won't gain you much, as everything 
is going to be sorted into a small subset of the buckets. So I 
guess you can scrap that idea.


Knowing that your strings are actually file paths I think 
building some sort of tree structure over M1 wouldn't be 
unreasonable. You say go two or three levels deep on your 
directory structure (ie nodes are labelled with directory name) 
and use that to split M1 into buckets. If some bucket has too 
many entries you could apply this recursively. Since you are only 
building a constant number of levels and the number of nodes is 
not likely to be too large you should do much better than N log N 
* c time for this step.


Then you search with the elements of M2. You should be able to do 
this in a multi-threaded way since once built, your data 
structure on M1 is read-only you could just split M2 over X 
threads and search. I am not an expert in this regard though, so 
perhaps someone better informed than I can chime in.


Since strings will tend to have long common prefix's Ivan's Trie 
idea would also work well.








Re: Help with an algorithm!

2017-06-15 Thread MGW via Digitalmars-d-learn

On Thursday, 15 June 2017 at 13:16:24 UTC, CRAIG DILLABAUGH wrote:

The purpose - search of changes in file system.
Sorting is a slow operation as well as hashing. Creation of a 
tree, is equally in sorting.

So far the best result:

string[] rez;

foreach(str; m2) {
bool fFind; int j;
foreach(int i, s; m1) {
if(str == s) { fFind = true; j = i; break; }
}
if(!fFind) { rez ~= str; }
else   {m1[j] = m1[$-1]; m1.length = m1.length - 1; }
}

//  rez => rezult

How to parallel on thred?


Re: Help with an algorithm!

2017-06-15 Thread CRAIG DILLABAUGH via Digitalmars-d-learn

On Thursday, 15 June 2017 at 11:48:54 UTC, Ivan Kazmenko wrote:

On Thursday, 15 June 2017 at 06:06:01 UTC, MGW wrote:
There are two arrays of string [] mas1, mas2; Size of each 
about 5M lines. By the size they different, but lines in both 
match for 95%. It is necessary to find all lines in an array 
of mas2 which differ from mas1. The principal criterion - 
speed. There are the 8th core processor and it is good to 
involve a multithreading.


The approaches which come to mind are:


clip

taking constant time.

Ivan Kazmenko.


As a follow up to this, if your alphabet is reasonably small 
perhaps could run radix sort based on the first few characters to 
split your arrays up into smaller subsets, and then use one of 
Ivan's suggestions within each subset.  Subset searches could be 
easily run in parallel.


Re: Help with an algorithm!

2017-06-15 Thread Ivan Kazmenko via Digitalmars-d-learn

On Thursday, 15 June 2017 at 06:06:01 UTC, MGW wrote:
There are two arrays of string [] mas1, mas2; Size of each 
about 5M lines. By the size they different, but lines in both 
match for 95%. It is necessary to find all lines in an array of 
mas2 which differ from mas1. The principal criterion - speed. 
There are the 8th core processor and it is good to involve a 
multithreading.


The approaches which come to mind are:

1. Sort both arrays, then traverse them in sorted order, like in 
merge step of merge sort:


sort (mas1);
sort (mas2);

size_t index1 = 0;
foreach (str2; mas2)
{
while (index1 < mas1.length && mas1[index1] < str2)
index1 += 1;
if (mas1[index1] != str2)
writeln (str2);
}

Sorting takes O (n log n * c) time, where n is the size of the 
arrays, and c is the expected time of two strings comparison when 
sorting.  The subsequent step is O (n * c) which is faster than 
sorting.


2. Hashing.  Just put the contents of the first array into a bool 
[string], and then, for each string from the second array, check 
whether it is contained in the associative array.  The time will 
be O (total length of all strings) multiplied by a moderate 
constant, unless the strings are designed specifically to 
generate hash collisions, in which case it will be slower.


3. Trie.  Similar to hashing, but the constant multipliers will 
be much higher unless the strings have large common prefixes.


Whether we can do faster depends on context.  For example, if the 
strings tend to all have long common prefixes, any string 
comparison will be slow, but otherwise it can be thought of as 
taking constant time.


Ivan Kazmenko.



Re: Implementing interfaces using alias this

2017-06-15 Thread Balagopal Komarath via Digitalmars-d-learn

On Thursday, 15 June 2017 at 07:12:56 UTC, Biotronic wrote:

Here however, is a solution that works for simple examples.



This is awesome. Very generic. Thanks.


Re: Implementing interfaces using alias this

2017-06-15 Thread Biotronic via Digitalmars-d-learn
On Wednesday, 14 June 2017 at 09:34:27 UTC, Balagopal Komarath 
wrote:

void main()
{
Test!Duck d;
}


As has been pointed out at length by others here, it's simply not 
how alias this is intended to work. I do see some arguments in 
favor of working that way, but I'm not sure what's the right 
solution.


Anyways, there is another solution - we could write a template 
that does the conversion for us. There is std.typecons.Proxy, 
which seems like a good fit, however it doesn't work quite the 
way we want. Here however, is a solution that works for simple 
examples. It might work for more complex examples as well, but I 
simply haven't tested that:


template duckImpl(alias a, TI, Fns...) {
static if (Fns.length > 0) {
mixin duckImpl!(a, TI, Fns[1..$]);

alias thisFn = Fns[0];
enum string fnName = __traits(identifier, thisFn);

mixin("ReturnType!thisFn "~fnName~"(Parameters!thisFn 
args) { return a."~fnName~"(args); }");

}
}

template duck(TI) if (is(TI == interface)) {
TI duck(T)(T t) {
import std.meta;
import std.traits;

template Functions(string s) {
alias Functions = MemberFunctionsTuple!(TI, s);
}

static class Result : TI {
private T payload;
this(T value) {
payload = value;
}
mixin duckImpl!(payload, TI, staticMap!(Functions, 
__traits(allMembers, TI)));

}
return new Result(t);
}
}

interface I {
void bar();
}

struct S {
void bar() {
import std.stdio;
writeln("OHAI");
}
}

unittest {
I i = S().duck!I;
i.bar();
}

--
  Biotronic