Freeing memory allocated at C function

2012-03-23 Thread Pedro Lacerda
I'm using some C functions like these: char *str = allocateNewString(); And this: Object *obj = constructObject(); // etc freeObject(obj); Do I need to free the memory in both cases? Can I someway register them on GC?

Re: Freeing memory allocated at C function

2012-03-23 Thread Ali Çehreli
On 03/22/2012 11:27 PM, Pedro Lacerda wrote: I'm using some C functions like these: char *str = allocateNewString(); And this: Object *obj = constructObject(); // etc freeObject(obj); Do I need to free the memory in both cases? Can I someway register them on GC?

Re: Vector operations optimization.

2012-03-23 Thread James Miller
On 23 March 2012 18:57, Comrad comrad.karlov...@googlemail.com wrote: On Thursday, 22 March 2012 at 10:43:35 UTC, Trass3r wrote: What is the status at the moment? What compiler and with which compiler flags I should use to achieve maximum performance? In general gdc or ldc. Not sure how

Template constraint and specializations

2012-03-23 Thread Ed McCardell
Is there a way to write a template constraint that matches any specialization of a given type? For example can the following be done without having to write out every combination of feature1 and feature2: class Foo(bool feature1, bool feature2) { ... } void useFoo(T)(T foo) if (is(T

Re: Template constraint and specializations

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Ed McCardell edmcc...@hotmail.com wrote: Is there a way to write a template constraint that matches any specialization of a given type? Nope. But there are simple workarounds: class Foo(bool feature1, bool feature2) { enum _isFoo = true; } template isFoo(T) { enum bool isFoo =

Re: Template constraint and specializations

2012-03-23 Thread Ed McCardell
On 03/23/2012 04:14 AM, Andrej Mitrovic wrote: On 3/23/12, Ed McCardelledmcc...@hotmail.com wrote: Is there a way to write a template constraint that matches any specialization of a given type? Nope. But there are simple workarounds: class Foo(bool feature1, bool feature2) { enum _isFoo =

Re: Vector operations optimization.

2012-03-23 Thread Dmitry Olshansky
On 23.03.2012 9:57, Comrad wrote: On Thursday, 22 March 2012 at 10:43:35 UTC, Trass3r wrote: What is the status at the moment? What compiler and with which compiler flags I should use to achieve maximum performance? In general gdc or ldc. Not sure how good vectorization is though, esp.

Re: Vector operations optimization.

2012-03-23 Thread Trass3r
The flags you want are -O, -inline -release. If you don't have those, then that might explain some of the slow down on slicing, since -release drops a ton of runtime checks. -noboundscheck option can also speed up things.

Re: Template constraint and specializations

2012-03-23 Thread bearophile
Andrej Mitrovic: Nope. But there are simple workarounds: Why isn't something similar to this working? import std.traits: Unqual; class Foo(bool feature1, bool feature2) {} template isFoo(T) { static if (is(Unqual!T Unused : Foo!Features, Features...)) { enum isFoo = true; }

Calling c shared library

2012-03-23 Thread simendsjo
Forgive my programming 101 question :) I want to call a method from a precompiled shared library: // c header void f(void); // my d file extern(C) void f(); void main() {} $ dmd mydfile.d libphobos2.a(deh2_33a_525.o): In function `_D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable':

Re: Vector operations optimization.

2012-03-23 Thread Comrad
On Friday, 23 March 2012 at 10:48:55 UTC, Dmitry Olshansky wrote: On 23.03.2012 9:57, Comrad wrote: On Thursday, 22 March 2012 at 10:43:35 UTC, Trass3r wrote: What is the status at the moment? What compiler and with which compiler flags I should use to achieve maximum performance? In

Re: Vector operations optimization.

2012-03-23 Thread Comrad
On Friday, 23 March 2012 at 11:20:59 UTC, Trass3r wrote: The flags you want are -O, -inline -release. If you don't have those, then that might explain some of the slow down on slicing, since -release drops a ton of runtime checks. -noboundscheck option can also speed up things. dmd is

Re: Calling c shared library

2012-03-23 Thread simendsjo
On Fri, 23 Mar 2012 15:04:48 +0100, simendsjo simend...@gmail.com wrote: Forgive my programming 101 question :) I want to call a method from a precompiled shared library: // c header void f(void); // my d file extern(C) void f(); void main() {} $ dmd mydfile.d libphobos2.a(deh2_33a_525.o):

string[] to char**

2012-03-23 Thread simendsjo
What's the best way to convert char** from string[]?

Re: string[] to char**

2012-03-23 Thread bearophile
On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote: What's the best way to convert char** from string[]? This is one way to do it: import std.algorithm, std.array, std.string, core.stdc.stdio; void main() { auto data = [red, yellow, green]; immutable(char)** p =

Re: Template constraint and specializations

2012-03-23 Thread Philippe Sigaud
On Fri, Mar 23, 2012 at 10:17, Ed McCardell edmcc...@hotmail.com wrote: Is there a way to write a template constraint that matches any specialization of a given type? Nope. But there are simple workarounds: class Foo(bool feature1, bool feature2) { enum _isFoo = true; } template

Re: string[] to char**

2012-03-23 Thread Ali Çehreli
On 03/23/2012 08:48 AM, simendsjo wrote: What's the best way to convert char** from string[]? In C, char** communicates transfer of ownership. Is that what you are trying to do? Are you going to pass a slice to a C function to be filled in by that C function? Such functions usually assign

Re: Template constraint and specializations

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Philippe Sigaud philippe.sig...@gmail.com wrote: testFoo is a function that accepts any Foo!( ... ) for any ... The second line tests it on a value of type T (T.init). That can't work. For a Foo!int your code will expand like so: // original code void tester(Args...)(Foo!Args

Re: string[] to char**

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, bearophile bearophileh...@lycos.com wrote: This is one way to do it: immutable(char)** p = array(map!toStringz(data)).ptr; This is asked so frequently that I think we could consider adding it to Phobos.

Re: Template constraint and specializations

2012-03-23 Thread Philippe Sigaud
On Fri, Mar 23, 2012 at 21:27, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: That can't work. For a Foo!int your code will expand like so: See for yourself: ? It works for me: template isBar(T) { enum isBar = __traits(compiles, {

Re: Template constraint and specializations

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Philippe Sigaud philippe.sig...@gmail.com wrote: It works for me Yes but check the isA template. It seems there's something causing a nested variadic template to fail. This won't work in a template constraint (it returns false): template isA(alias Foo) { template isA(T) {

Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
Does someone have a map implementation that maintains the insertion order of the keys? E.g.: string[string] map; map[foo] = x; map[bar] = y; When iterating over map keys I want to visit foo first, then bar, and so on.

Confused by refusal to expand template

2012-03-23 Thread H. S. Teoh
Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() { S s; s.func(abc); //

Re: Confused by refusal to expand template

2012-03-23 Thread Timon Gehr
On 03/23/2012 11:52 PM, H. S. Teoh wrote: Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() {

Re: Confused by refusal to expand template

2012-03-23 Thread David
Am 23.03.2012 23:52, schrieb H. S. Teoh: Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() {

Re: Confused by refusal to expand template

2012-03-23 Thread Timon Gehr
On 03/23/2012 11:58 PM, David wrote: Am 23.03.2012 23:52, schrieb H. S. Teoh: Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() { S s; s.func(abc); // This is line 44 } This refuses to compile: test2.d(44): Error: template test2.S.func(K)

Re: Freeing memory allocated at C function

2012-03-23 Thread Pedro Lacerda
On Fri, Mar 23, 2012 at 3:43 AM, Ali Çehreli acehr...@yahoo.com wrote: You can register on GC if you wrap the resources in a class. Then the class object's destructor would call the clean up code. The problem is, it is undeterministic when the destructor will be called, or will it be called

Re: Map with maintained insertion order

2012-03-23 Thread Jonathan M Davis
On Friday, March 23, 2012 23:48:51 Andrej Mitrovic wrote: Does someone have a map implementation that maintains the insertion order of the keys? E.g.: string[string] map; map[foo] = x; map[bar] = y; When iterating over map keys I want to visit foo first, then bar, and so on. I can't

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: Does someone have a map implementation that maintains the insertion order of the keys? Here's a sloppy implementation: module test; import std.stdio; import std.traits; template KeyType(V : V[K], K) if

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Jonathan M Davis jmdavisp...@gmx.com wrote: I can't think of any data structure that does that off the top of my head Java has it and they call it a LinkedHashMap: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashMap.html That _does_ require having two data

Re: Map with maintained insertion order

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 06:07:42PM -0700, Jonathan M Davis wrote: On Friday, March 23, 2012 23:48:51 Andrej Mitrovic wrote: Does someone have a map implementation that maintains the insertion order of the keys? E.g.: string[string] map; map[foo] = x; map[bar] = y; When

Re: Map with maintained insertion order

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 06:33:54PM -0700, H. S. Teoh wrote: On Fri, Mar 23, 2012 at 06:07:42PM -0700, Jonathan M Davis wrote: On Friday, March 23, 2012 23:48:51 Andrej Mitrovic wrote: Does someone have a map implementation that maintains the insertion order of the keys? E.g.:

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: On 3/23/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: Does someone have a map implementation that maintains the insertion order of the keys? Here's a sloppy implementation: I forgot to make opIndex ref, but also the

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: static if (isArray!Value) { template isValue(T) { enum bool isValue = is(typeof( { Value v; T t; v = t; } )) || is(BaseElement!Value == T); } } Correction, it's: template isValue(T) { enum bool isValue =

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: Correction, it's: template isValue(T) { enum bool isValue = is(typeof( { Value v; T t; v = t; } )) || is(typeof( { BaseElement!Value v; T t; v = t; } )) || is(BaseElement!Value == T); } Last check not needed, so: