Re: Reflection in D

2017-01-27 Thread rumbu via Digitalmars-d-learn

On Saturday, 28 January 2017 at 07:10:27 UTC, medhi558 wrote:

I have a last question, currently i use :
if(lc.name.indexOf("protocol.messages") != -1)

To know if the class is a NetworkMessage, Would be possible to 
do this


if(lc is NetworkMessage)


Sorry for my English, i speak french.


if (auto nm = cast(NetworkMessage)lc)
{
  //do something with nm
}




Re: Reflection in D

2017-01-27 Thread medhi558 via Digitalmars-d-learn

I have a last question, currently i use :
if(lc.name.indexOf("protocol.messages") != -1)

To know if the class is a NetworkMessage, Would be possible to do 
this


if(lc is NetworkMessage)


Sorry for my English, i speak french.


Re: Reflection in D

2017-01-27 Thread medhi558 via Digitalmars-d-learn

I develop a game server. Currently I use a switch :

import protocol.messages.connection.Message1;
import protocol.messages.connection.Message2;
import protocol.messages.queues.Message3;
import ..

import protocol.messages.NetworkMessage;

class ProtocolMessageManager
{
public static NetworkMessage GetInstance(uint id)
{
  switch(id)
  {
 case Message1.Id:
 return new Message1();
 case Message2.Id:
 return new Message2();
 .

 default:
  return null;
  }
}
}

what I want to do :

import protocol.messages.NetworkMessage;

class ProtocolMessageManager
{
private static TypeInfo_Class[uint] m_types;

public static void Init()
{
foreach(mod; ModuleInfo)
{
foreach(TypeInfo_Class lc; mod.localClasses)
{
if(lc.name.indexOf("protocol.messages") != -1)
{
NetworkMessage c = 
cast(NetworkMessage)lc.create();

ProtocolMessageManager.m_types[c.MessageId] = lc;
}
}

}
}

public static NetworkMessage GetInstance(string id)
{
auto v = (id in ProtocolMessageManager.m_types);
if (v !is null)
	 return 
cast(NetworkMessage)ProtocolMessageManager.m_types[id].create();

else
 return null;
}
}


Re: Partial arrays reclaimed?

2017-01-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 27 January 2017 at 23:36:58 UTC, Stefan Koch wrote:

It should reclaim the memory from the beginning of the array.


I doubt the current implementation will though, you should check.


Concurrent containers

2017-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn

Does phobos offer concurrent containers?

I couldn't find one at http://dlang.org/phobos/std_container.html

Any other in the D land?

Arun


Re: Partial arrays reclaimed?

2017-01-27 Thread Stefan Koch via Digitalmars-d-learn

On Friday, 27 January 2017 at 23:22:17 UTC, Nick Sabalausky wrote:

Suppose an array is being used like a FIFO:

---
T[] slice;

// Add:
slice ~= T();

// Remove:
slice = slice[1..$];
---

Assuming of course there's no other references to the memory, 
as this gets used, does the any of the memory from the removed 
elements ever get GC'd?


Also, if this is a long-running process, isn't there a 
potential danger in the array just marching through the address 
space and running out of room? (ie either running out of of 
continuous space, or hitting 0xFFF)


It should reclaim the memory from the beginning of the array.
But your could just have written a small tool that does this and 
look at the memory use over time.
Generally a good idea, to perform experiments with things you are 
interested in.


And there is always the risk of running out of memory on machines 
which have no mmu.




Partial arrays reclaimed?

2017-01-27 Thread Nick Sabalausky via Digitalmars-d-learn

Suppose an array is being used like a FIFO:

---
T[] slice;

// Add:
slice ~= T();

// Remove:
slice = slice[1..$];
---

Assuming of course there's no other references to the memory, as this 
gets used, does the any of the memory from the removed elements ever get 
GC'd?


Also, if this is a long-running process, isn't there a potential danger 
in the array just marching through the address space and running out of 
room? (ie either running out of of continuous space, or hitting 0xFFF)


Re: D package for optimizing/compressing images

2017-01-27 Thread Ali Çehreli via Digitalmars-d-learn

On 01/27/2017 03:03 AM, aberba wrote:

Are there any dub package for compressing images uploaded through web
forms? Cropping/resizing may also come in handy.

I want one for a vibe.d project.


I have a minimal MagickWand binding if you want to use or extend:

  https://github.com/acehreli/alibum/blob/master/magickwand.d

Ali



Re: D package for optimizing/compressing images

2017-01-27 Thread aberba via Digitalmars-d-learn

On Friday, 27 January 2017 at 19:14:19 UTC, Jesse Phillips wrote:

On Friday, 27 January 2017 at 11:03:15 UTC, aberba wrote:
Are there any dub package for compressing images uploaded 
through web forms? Cropping/resizing may also come in handy.


I want one for a vibe.d project.


I don't know of any D projects. If I were doing this I'd be 
running the vibe.d server on linux and just use common linux 
utilities for photo manipulation on the commandline.


Unfortunately, I will be using Heroku which is a PaaS, so no 
access to shell.


Re: Reflection in D

2017-01-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 27 January 2017 at 21:02:13 UTC, medhi558 wrote:
Hello, I would like to know if it is possible to recover all 
classes in the project in D.


Yes, `foreach(mod; ModuleInfo) foreach(lc; mod.localClasses)`... 
but why do you want it? That facility is kinda limited in doing 
many things with.


Reflection in D

2017-01-27 Thread medhi558 via Digitalmars-d-learn
Hello, I would like to know if it is possible to recover all 
classes in the project in D.


Example in c# :

Assembly asm = Assembly.GetAssembly(typeof(MyClass));

foreach (Type type in asm.GetTypes())
{
}


Re: D package for optimizing/compressing images

2017-01-27 Thread Jesse Phillips via Digitalmars-d-learn

On Friday, 27 January 2017 at 11:03:15 UTC, aberba wrote:
Are there any dub package for compressing images uploaded 
through web forms? Cropping/resizing may also come in handy.


I want one for a vibe.d project.


I don't know of any D projects. If I were doing this I'd be 
running the vibe.d server on linux and just use common linux 
utilities for photo manipulation on the commandline.


Re: Bug in documentation or misunderstanding it?

2017-01-27 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 26, 2017 at 06:47:21PM +, Suliman via Digitalmars-d-learn wrote:
> On Thursday, 26 January 2017 at 18:42:29 UTC, Suliman wrote:
> > On Thursday, 26 January 2017 at 17:52:24 UTC, H. S. Teoh wrote:
> > > On Thu, Jan 26, 2017 at 05:38:59PM +, Suliman via
> > > Digitalmars-d-learn wrote:
> > > > I read docs and can't understand what's wrong. Or I am do not
> > > > understand it, or there is come mistake.
> > > > 
> > > > Let's look at function
> > > > https://dlang.org/phobos/std_stdio.html#.File.byLine
> > > > 
> > > > auto byLine(Terminator = char, Char = char)(KeepTerminator
> > > > keepTerminator = No.keepTerminator, Terminator terminator =
> > > > '\x0a')
> > > > 
> > > > what does mean first groups of scope: (Terminator = char, Char =
> > > > char) ?
> > > 
> > > Those are compile-time parameters. You specify them in a
> > > compile-time argument list using the !(...) construct, for
> > > example:
> > > 
> > >   auto lines = File("myfile.txt")
> > >   .byLine!(dchar, char)(Yes.keepTerminator, '\u263a');
> > > 
> > > 
> > > T
> > 
> > So I am right about others items about for example that `=` is optional?
> 
> Why this code is work: `file.byLine(KeepTerminator.no, 'm')`

Yes, the `=` means the parameter has a default value. This applies to
both compile-time parameters and runtime parameters. So:

file.byLine(KeepTerminator.no);

is the same as:

file.byLine!(char, char)(No.keepTerminator, '\x0a');


T

-- 
There are 10 kinds of people in the world: those who can count in binary, and 
those who can't.


Re: A matter of propiety

2017-01-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 27 January 2017 at 16:30:14 UTC, WhatMeWorry wrote:

But it seems like I'm committing some great sin by doing this.



meh i see no problem with it.

Just another quick question: is the module that holds the 
main() function just the

same as any other module?


It is the same as any other module. I say it should be named the 
same as any other (including a two-part name, `module foo;` is 
likely to clash if you ever do two libraries, i almost always use 
`module myproject.foo;` instead), but all the exact same rules 
apply.


A matter of propiety

2017-01-27 Thread WhatMeWorry via Digitalmars-d-learn


module_common
import app; // Ugly?  Bad?  Better way?
common_func()
{
static if (compileTimeFlag1)
codeBlockA
static if (compileTimeFlag2)
codeBlockB
static if (compileTimeFlag3)
codeBlockC
}

I want to have many stand-alone programs that calls the common 
function.
Each app.d is its own separate directory so I can use the same 
name.


app.d
static bool compileTimeFlag1 = true;
common_func()

app.d
static bool compileTimeFlag1 = true;
static bool compileTimeFlag2 = true;
common_func()

app.d
static bool compileTimeFlag1 = true;
static bool compileTimeFlag2 = true;
static bool compileTimeFlag3 = true;
common_func()


I've got this working, but only if I add an "import app;" to 
module_common.

Otherwise I get, "Error: undefined identifier compileTimeFlags

I want to keep all the compileTimeFlags in the main modules. 
Seems like the

most logical place to put them.

But it seems like I'm committing some great sin by doing this. 
Maybe it's my C++ days,
but it just seems wrong to have a module import the main module? 
Is there a more elegant

approach?


Just another quick question: is the module that holds the main() 
function just the
same as any other module?  Or is there some secret sauce?  
(Besides the point of entry)



Thanks.


Re: D idom for removing array elements

2017-01-27 Thread cym13 via Digitalmars-d-learn

On Friday, 27 January 2017 at 15:39:57 UTC, cym13 wrote:

On Friday, 27 January 2017 at 08:30:41 UTC, Dukc wrote:

[...]


Note that if the set of values to be excluded isn't smaller 
than the haystack then using partition is way faster and your 
method is the slowest of all. If the order of the array matters 
stable partition can be used but is slower than the original 
method.


[...]


I forgot to say that the main reason to use partition is that 
it's easy to use it to remove the elements of the array in place:


 auto r = a.partition!(v => !b.canFind(v));
 a.length -= r.length;

It just puts the "bad" elements at the end and changes the length 
of the array.


Re: D idom for removing array elements

2017-01-27 Thread cym13 via Digitalmars-d-learn

On Friday, 27 January 2017 at 08:30:41 UTC, Dukc wrote:

On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote:

My method is much better for large arrays I tested here.


Trough, considering the size of the arrays the performance 
difference should be even greater, like 1000X better instead of 
15X better so it's definitely not that great.


Note that if the set of values to be excluded isn't smaller than 
the haystack then using partition is way faster and your method 
is the slowest of all. If the order of the array matters stable 
partition can be used but is slower than the original method.


Added test cases and benchmark results:


void partitionMethod() {
auto c = a.partition!(v => b.canFind(v));
// Sort to recover the order
assert(sort(c[0..4]).array == [1, 2, 5, 7]);
}

void stablePartitionMethod() {
auto c = a.partition!(v => b.canFind(v), 
SwapStrategy.stable);

assert(c[0..4] == [1, 2, 5, 7]);
}

benchmark!(originalMethod,
   WilsonMethod,
   myMethod,
   partitionMethod,
   stablePartitionMethod)(1)
.each!writeln;


With "a" of length 4000 and "b" of length 4000:

/*
int[] a = [1, 2, 3, 4, 5, 6, 7, 4].cycle.take(4000).array;
int[] b = [3, 4, 6].cycle.take(4000).array;
*/

TickDuration(51482830)
TickDuration(43634792)
TickDuration(1085159)  // myMethod is faster
TickDuration(44216820) // 3rd
TickDuration(42920567) // 2nd

With "a" of length 40 and "b" of length 3:

/*
int[] a = [1, 2, 3, 4, 5, 6, 7, 4].cycle.take(40).array;
int[] b = [3, 4, 6];
*/

TickDuration(312038) // 2nd
TickDuration(541912)
TickDuration(606883)
TickDuration(190662) // partitionMethod is way faster
TickDuration(418751) // 3rd



Re: Problems compiling sqlite-d

2017-01-27 Thread Stefan Koch via Digitalmars-d-learn

On Friday, 27 January 2017 at 12:21:29 UTC, Nestor wrote:

On Friday, 27 January 2017 at 12:06:33 UTC, Stefan Koch wrote:

On Friday, 27 January 2017 at 12:04:06 UTC, Stefan Koch wrote:


I take it you build without dub ?
Have you specified source/sqlite.d on your compile 
commandline ?


That was supposed to say.
sqlite-d/source/sqlited.d

Please feel free to post here or contact me directly regarding 
the usage of sqlite-d.


Yes I was building withoug dub. What I did was simply:

copy data.db to sqlite-d/source
cd to sqlite-d/source
copy api_user.d to z1_app.d
modify z1_app.d (as shown before)
compile z1_app without additional parameters.

Shouldn't it work?

This was with dmd v2.072.2 on Windows 7 SP1 x86-64


You have to compile the library with your app.
or better yet use dub
replace app.d with your app.d and run dub


Re: Problems compiling sqlite-d

2017-01-27 Thread Nestor via Digitalmars-d-learn

On Friday, 27 January 2017 at 12:06:33 UTC, Stefan Koch wrote:

On Friday, 27 January 2017 at 12:04:06 UTC, Stefan Koch wrote:


I take it you build without dub ?
Have you specified source/sqlite.d on your compile commandline 
?


That was supposed to say.
sqlite-d/source/sqlited.d

Please feel free to post here or contact me directly regarding 
the usage of sqlite-d.


Yes I was building withoug dub. What I did was simply:

copy data.db to sqlite-d/source
cd to sqlite-d/source
copy api_user.d to z1_app.d
modify z1_app.d (as shown before)
compile z1_app without additional parameters.

Shouldn't it work?

This was with dmd v2.072.2 on Windows 7 SP1 x86-64


Re: Problems compiling sqlite-d

2017-01-27 Thread Stefan Koch via Digitalmars-d-learn

On Friday, 27 January 2017 at 12:04:06 UTC, Stefan Koch wrote:


I take it you build without dub ?
Have you specified source/sqlite.d on your compile commandline ?


That was supposed to say.
sqlite-d/source/sqlited.d

Please feel free to post here or contact me directly regarding 
the usage of sqlite-d.





Re: Problems compiling sqlite-d

2017-01-27 Thread Stefan Koch via Digitalmars-d-learn

On Friday, 27 January 2017 at 12:01:30 UTC, Nestor wrote:

Hi,

I was trying to use https://github.com/UplinkCoder/sqlite-d

Unfortunately even something as simple as this doesn´t compile 
(at least on Windows):


import std.stdio, sqlited;

void main(string[] args) {
  string filename = (args.length == 2 ? args[1] : "data.db");
  Database db = Database(filename);
}

See the error:
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
z1_app.obj(z1_app)
 Error 42: Symbol Undefined _D7sqlited8Database6__initZ
z1_app.obj(z1_app)
 Error 42: Symbol Undefined 
_D7sqlited8Database6__ctorMFNcAyabZS7sqlited8Database

Error: linker exited with status 107814472

Is there any other native D implementation of sqlite reader?


I take it you build without dub ?
Have you specified source/sqlite.d on your compile commandline ?



Problems compiling sqlite-d

2017-01-27 Thread Nestor via Digitalmars-d-learn

Hi,

I was trying to use https://github.com/UplinkCoder/sqlite-d

Unfortunately even something as simple as this doesn´t compile 
(at least on Windows):


import std.stdio, sqlited;

void main(string[] args) {
  string filename = (args.length == 2 ? args[1] : "data.db");
  Database db = Database(filename);
}

See the error:
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
z1_app.obj(z1_app)
 Error 42: Symbol Undefined _D7sqlited8Database6__initZ
z1_app.obj(z1_app)
 Error 42: Symbol Undefined 
_D7sqlited8Database6__ctorMFNcAyabZS7sqlited8Database

Error: linker exited with status 107814472

Is there any other native D implementation of sqlite reader?



Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn

On Friday, 27 January 2017 at 10:20:19 UTC, albert-j wrote:
I am also wondering why the standard library doesn't have 
convenience functions for this, e.g. like Java's removeAll? Now 
there's more typing than necessary for a relatively common task.


That might be a good addition considering how well it can be 
optimized compared to a naive implementation. Of course, D 
version of that would accept ranges as input, not only 
collections.


D package for optimizing/compressing images

2017-01-27 Thread aberba via Digitalmars-d-learn
Are there any dub package for compressing images uploaded through 
web forms? Cropping/resizing may also come in handy.


I want one for a vibe.d project.


Re: D idom for removing array elements

2017-01-27 Thread albert-j via Digitalmars-d-learn

On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote:


TickDuration(28085)
TickDuration(42868)
TickDuration(1509)



Thank you, this is very helpful. I am also wondering why the 
standard library doesn't have convenience functions for this, 
e.g. like Java's removeAll? Now there's more typing than 
necessary for a relatively common task.




Re: Parsing a UTF-16LE file line by line, BUG?

2017-01-27 Thread Era Scarecrow via Digitalmars-d-learn

On Friday, 27 January 2017 at 07:02:52 UTC, Jack Applegame wrote:

On Monday, 16 January 2017 at 14:47:23 UTC, Era Scarecrow wrote:
static char[1024*4] buffer;  //4k reusable buffer, NOT 
thread safe


Maybe I'm wrong, but I think it's thread safe. Because static 
mutable non-shared variables are stored in TLS.


 Perhaps, but fibers or other instances of sharing the buffer 
wouldn't be safe/reliable, at least not for long.


Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn

On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote:

My method is much better for large arrays I tested here.


Trough, considering the size of the arrays the performance 
difference should be even greater, like 1000X better instead of 
15X better so it's definitely not that great.




Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn

On Thursday, 26 January 2017 at 23:10:02 UTC, albert-j wrote:
Will it also work correctly and fast for arrays of custom 
objects? How should opCmp() be defined if objects don't have a 
meaningful ordering? The order of elements in the original 
array does not matter.


Two options: either define opCmp for the custom objects, or 
define ordering for the sort algorithm (see std.algorithm in 
phobos documentation)





Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn

On Friday, 27 January 2017 at 05:48:27 UTC, Stefan Koch wrote:

To me it looks rather slow.
please benchmark!


void main()
{   import std.stdio, std.algorithm, std.range, std.array, 
std.datetime;

int[] a = [1, 2, 3, 4, 5, 6, 7, 4].cycle.take(2000).array;
int[] b = [3, 4, 6].cycle.take(2000).array;

void originalMethod()
{   auto c = a.remove!(x => b.canFind(x));
assert(c[0 .. 4] == [1, 2, 5, 7]);
}

void WilsonMethod()
{   auto c = a.filter!(x => !b.canFind(x)).array;
assert(c[0 .. 4] == [1, 2, 5, 7]);
}

void myMethod()
{   auto sortedB = sort(b.dup);
auto c = a
.   filter!(i => !sortedB.contains(i))
.   array
;
assert(c[0 .. 4] == [1, 2, 5, 7]);
}

auto r = benchmark!(originalMethod, WilsonMethod, 
myMethod)(1);

foreach(result; r) result.writeln;
}

resulted in:
TickDuration(28085)
TickDuration(42868)
TickDuration(1509)

So, you were correct that the original method is better than 
Wilsons filter. My method is much better for large arrays I 
tested here. But what I think you were afraid of is that it 
needlessly wastes performance by allocating a new array, and I 
agree.


Also, as I said, it could be made O(n).