Re: Map with maintained insertion order
On Fri, 23 Mar 2012 23:48:51 +0100, 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. http://bazaar.launchpad.net/~michael-rynn-500/d2-xml/trunk/view/head:/alt/ arraymap.d I took and adapted the idea from Ultimate++ library code. Ultimate++ is worth a try, even if it is C++. arraymap.d implements a hashmap, that will maintain insertion order, but only if no deletions are made. Each added key-value pair is appended into a separate key and values array. Also maintained is an array of hash_t. Also maintained is an array of index integers. Matching hash keys are related by the linked list method (no pointers). So starting with an empty map, each insertion appends the key and value array storage. But a key removal will create a hole, and the hole is added to the free links list. The hash_t array points to one of the linked list entries which indexes the key/value array. arraymap.d would appear to have the property, that the hash values cannot be mistaken as aliased pointers by the GC, because they can be stored in a non-scanned array. In the same module is a template version copycat implementation of druntime AA, aahash.d with some customizations that I have played around with, like being able to use char[] for lookups to string keys (but not for op_put). aahash module requires hashutil.d and blockheap. arraymap requires hashutil.d In performance tests the arraymap.d was slower than the D runtime look alike, but acceptable. I think it has scope for improvement in coding and performance.
Rewrite of std.range docs (Was: Re: Making sense of ranges)
On Sat, Mar 24, 2012 at 06:19:32PM +, Stewart Gordon wrote: > The documentation for std.range states > > http://dlang.org/phobos/std_range.html > "This module defines the notion of range (by the membership tests > isInputRange, isForwardRange, isBidirectionalRange, > isRandomAccessRange), range capability tests (such as hasLength or > hasSlicing), and a few useful range incarnations." > > But that intro doesn't describe what a range actually is. [...] In a previous post long lost inside a rather large thread some time ago, I proposed a rewrite of the docs of std.range to make it clearer, at least on a basic level, what a range is, why we should care, and what the module offers. This thread has further convinced me that std.range's docs *need* this rewrite. So here's my first attempt at it: https://github.com/quickfur/phobos/tree/stdrange_docs It's not complete yet; I still have a few more range-creation templates to cover, and then list the auxiliary functions, etc., before it's ready for merging into Phobos. But I thought I should put it up for review here first, in case people have some comments/suggestions or further things that should be put into the docs. Better to put it all in a single pull request than waste the Phobos maintainers' time with multiple updates to the docs. So, comments, suggestions, flames, etc., are hereby solicited. :-) T -- If lightning were to ever strike an orchestra, it'd always hit the conductor first.
Re: opDispatch(string name, E...) (E e) question.
On 03/26/12 00:58, bls wrote: > Thanks Artur, > > On 03/25/2012 03:18 PM, Artur Skawina wrote: >> On 03/25/12 22:45, bls wrote: >>> How do I "call" opDispatch(string name, E...)(E elements) ? >>> What I want to archive is to call f.i. fm.list with an arbitrary number of >>> arguments without using >>> >>> fm.list(1, "abc", 4L, 3.33); >>> >>> Instead I would prefer >>> fm.list = (1, "abc", 4L, 3.33); >>> >>> Is this somehow possible ? >> >> Well, you can do >> >> template ID(A...) { alias A ID; } >> >> fm.list = ID!(1, "abc", 4L, 3.33); >> >> but is that any better than your first version above?... > Not sure... > > Maybe if we rename it > fm.list = Values!(1,2,true); > > But I does not work.. Seems I am not able to figure out the opDispatch > signature. import std.stdio; template Values(A...) { alias A Values; } struct FlexMap { template opDispatch(string key) { auto opDispatch(VS...)(VS vs) { foreach (v; vs) writefln("%s: (%s)%s;", key, typeof(v).stringof, v); } } } void main() { auto fm = FlexMap(); fm.list = Values!(1, "abc", 4L, 3.33); } artur
Re: opDispatch(string name, E...) (E e) question.
On Sun, 25 Mar 2012 22:45:57 +0200, bls wrote: How do I "call" opDispatch(string name, E...)(E elements) ? What I want to archive is to call f.i. fm.list with an arbitrary number of arguments without using fm.list(1, "abc", 4L, 3.33); Instead I would prefer fm.list = (1, "abc", 4L, 3.33); Is this somehow possible ? No. However, this is: fm.list = tuple( 1, "abc", 4L, 3.33 ); In that case, you probably want to use the overload opDispatch(string name, E...)(Tuple!E elements) and mark the other: opDispatch(string name, E...)(E element) if ( !(E.length == 1 && isTuple!(E[0]))) tuple, Tuple, and isTuple are found in std.typecons. import std.variant; import std.conv; auto fm = FlexMap(); fm.ten = 10; fm.ten = ["Ten", "Zehn", "Tein"]; fm.list = [20, 10, 2, 2, 44 ] ; fm.list = "Hello opDispatch"; struct FlexMap { Variant[] [string] map; Variant[] opDispatch(string name)() { return map[name]; } Variant[] opDispatch(string name, E...)(E elements) { foreach(element; elements) map[name] ~= to!Variant(element); return map[name]; } Variant[] opDispatch(string name, T) (T t) { map[name] ~= to!Variant(t); return map[name]; } } Another question : How do I bring in : opDispatch(string name, T) (T[] t) into FlexMap ? It's possible to do with template constraints: import std.traits : isArray; void opDispatch(string name, T)(T t) if ( isArray!T ) { // Array specific. } void opDispatch(string name, T)(T t) if ( !isArray!T) { // Single element. } One could also use static if: Variant[] opDispatch(string name, T)(T t) { static if ( is( T U : U[] ) ) { map[name] ~= to! } else { // Whatever you want to do differently here. } }
Re: opDispatch(string name, E...) (E e) question.
Thanks Artur, On 03/25/2012 03:18 PM, Artur Skawina wrote: On 03/25/12 22:45, bls wrote: How do I "call" opDispatch(string name, E...)(E elements) ? What I want to archive is to call f.i. fm.list with an arbitrary number of arguments without using fm.list(1, "abc", 4L, 3.33); Instead I would prefer fm.list = (1, "abc", 4L, 3.33); Is this somehow possible ? Well, you can do template ID(A...) { alias A ID; } fm.list = ID!(1, "abc", 4L, 3.33); but is that any better than your first version above?... Not sure... Maybe if we rename it fm.list = Values!(1,2,true); But I does not work.. Seems I am not able to figure out the opDispatch signature. Think I will rewrite it without using opDispatch. artur
Re: opDispatch(string name, E...) (E e) question.
On 03/25/12 22:45, bls wrote: > How do I "call" opDispatch(string name, E...)(E elements) ? > What I want to archive is to call f.i. fm.list with an arbitrary number of > arguments without using > > fm.list(1, "abc", 4L, 3.33); > > Instead I would prefer > fm.list = (1, "abc", 4L, 3.33); > > Is this somehow possible ? Well, you can do template ID(A...) { alias A ID; } fm.list = ID!(1, "abc", 4L, 3.33); but is that any better than your first version above?... artur
Re: opDispatch(string name, E...) (E e) question.
On 03/25/2012 02:59 PM, James Miller wrote: Ok, so looking here:http://dlang.org/function.html, I have determined that, if you are using Variant arrays (though I'm not sure if you can do that using literals...) you can use the syntax from this example: Thanks James.. will give it tomorrow a new try. At least Variant[] va = [1, 2.3222, "abc"]; is not working. Guess I have to give up the opDispatch() thing and create it a bit more traditional :) Bjoern oh, beside you mean : opDispatch(string name, T) (T[] t...)
Re: opDispatch(string name, E...) (E e) question.
On 26 March 2012 10:34, bls wrote: > > (T) (T[] t) AND (T) (T t) seems not to work. Ok, so looking here: http://dlang.org/function.html, I have determined that, if you are using Variant arrays (though I'm not sure if you can do that using literals...) you can use the syntax from this example: int test() { return sum(1, 2, 3) + sum(); // returns 6+0 } int func() { int[3] ii = [4, 5, 6]; return sum(ii); // returns 15 } int sum(int[] ar ...) { int s; foreach (int x; ar) s += x; return s; } You'll probably need to do some experimentation to figure out how Variant fits into that properly, but it shouldn't be too hard. Also, remember that opDispatch takes the name of the function as the last parameter, so watch out for that. -- James Miller
Re: opDispatch(string name, E...) (E e) question.
On 03/25/2012 02:04 PM, James Miller wrote: On 26 March 2012 09:45, bls wrote: How do I "call" opDispatch(string name, E...)(E elements) ? What I want to archive is to call f.i. fm.list with an arbitrary number of arguments without using fm.list(1, "abc", 4L, 3.33); Instead I would prefer fm.list = (1, "abc", 4L, 3.33); You can use @property on opDispatch to use setter/getter notation, however I wouldn't rely on that functionality long-term if you want to keep the same function-call syntax (since -property flag is supposed to enforce proper parenthesis use on `@property`s). fm.list = (1, "abc", 4L, 3.33); I'm hoping you mean `fm.list = [1, "abc", 4L, 3.33];` I think that using the right template parameters, you can use the same code for (T...)(T el) and (T)(T[]), I just can't remember what that is... Ouch, yep, I mean [1, "abc", 4L, 3.33] But I have no clue how to implement it. Another question : How do I bring in : opDispatch(string name, T) (T[] t) -- James Miller (T) (T[] t) AND (T) (T t) seems not to work. snip struct FlexMap { Variant[] [string] map; Variant[] opDispatch(string name)() { return map[name]; } Variant[] opDispatch(string name, E...)(E elements) { foreach(element; elements) map[name] ~= to!Variant(element); return properties[name]; } Variant[] opDispatch(string name, T) (T t) { map[name] ~= to!Variant(t); return map[name]; } // No go Variant[] opDispatch(string name, T) (T[] t) {} }
Re: opDispatch(string name, E...) (E e) question.
On 26 March 2012 09:45, bls wrote: > How do I "call" opDispatch(string name, E...)(E elements) ? > What I want to archive is to call f.i. fm.list with an arbitrary number of > arguments without using > > fm.list(1, "abc", 4L, 3.33); > > Instead I would prefer > fm.list = (1, "abc", 4L, 3.33); You can use @property on opDispatch to use setter/getter notation, however I wouldn't rely on that functionality long-term if you want to keep the same function-call syntax (since -property flag is supposed to enforce proper parenthesis use on `@property`s). > fm.list = (1, "abc", 4L, 3.33); I'm hoping you mean `fm.list = [1, "abc", 4L, 3.33];` I think that using the right template parameters, you can use the same code for (T...)(T el) and (T)(T[]), I just can't remember what that is... > Another question : > How do I bring in : > > opDispatch(string name, T) (T[] t) -- James Miller
opDispatch(string name, E...) (E e) question.
How do I "call" opDispatch(string name, E...)(E elements) ? What I want to archive is to call f.i. fm.list with an arbitrary number of arguments without using fm.list(1, "abc", 4L, 3.33); Instead I would prefer fm.list = (1, "abc", 4L, 3.33); Is this somehow possible ? import std.variant; import std.conv; auto fm = FlexMap(); fm.ten = 10; fm.ten = ["Ten", "Zehn", "Tein"]; fm.list = [20, 10, 2, 2, 44 ] ; fm.list = "Hello opDispatch"; struct FlexMap { Variant[] [string] map; Variant[] opDispatch(string name)() { return map[name]; } Variant[] opDispatch(string name, E...)(E elements) { foreach(element; elements) map[name] ~= to!Variant(element); return map[name]; } Variant[] opDispatch(string name, T) (T t) { map[name] ~= to!Variant(t); return map[name]; } } Another question : How do I bring in : opDispatch(string name, T) (T[] t) into FlexMap ? TIA, Bjoern
Re: GC collecting "too much"..
On Sunday, 25 March 2012 at 19:15:05 UTC, simendsjo wrote: I'm doing some coding against a c library, and Ds GC keeps collecting c owned objects (I think - disabling the GC makes everything work) Three alternative solutions: - Allocate from the C heap the memory that C will need to use, and free it manually or in a struct destructor (RAII) or with scope(exit). - Keep a pointer to the D-GC memory in the D code too. - In core.memory there are ways to disable scanning of a memory zone. Maybe it's usable for your purposes too. Bye, bearophile
GC collecting "too much"..
I'm doing some coding against a c library, and Ds GC keeps collecting c owned objects (I think - disabling the GC makes everything work) But how can I figure out what the GC is (read: I am) fucking up? I have some to!string(c_struct_field) and format("%s", c_struct_field) and field = c_fields[i] etc etc
Re: std.json
On 03/25/2012 12:50 PM, Andrea Fontana wrote: Hope it's clear... import std.json; import std.stdio; void main(string args[]) { JSONValue json = parseJSON(q"EOS { "key" : { "subkey1" : "str_val", "subkey2" : [1,2,3], "subkey3" : 3.1415 } } EOS"); writeln(json.object["key"].object["subkey1"].str); writeln(json.object["key"].object["subkey2"].array[1].integer); writeln(json.object["key"].object["subkey3"].type == JSON_TYPE.FLOAT); } On Sunday, 25 March 2012 at 15:26:31 UTC, AaronP wrote: Could I get a "hello, world" example of parsing json? The docs look simple enough, but I could still use an example. Ah. That's perfect, thanks. :)
Re: std.json
Hope it's clear... import std.json; import std.stdio; void main(string args[]) { JSONValue json = parseJSON(q"EOS { "key" : { "subkey1" : "str_val", "subkey2" : [1,2,3], "subkey3" : 3.1415 } } EOS"); writeln(json.object["key"].object["subkey1"].str); writeln(json.object["key"].object["subkey2"].array[1].integer); writeln(json.object["key"].object["subkey3"].type == JSON_TYPE.FLOAT); } On Sunday, 25 March 2012 at 15:26:31 UTC, AaronP wrote: Could I get a "hello, world" example of parsing json? The docs look simple enough, but I could still use an example.
Problem with receiveOnly and classes
Hello, I need to pass objects of a hierarchy between threads and I have some troubles. The sample code below displays: Unknown B.fun() I do not understand why an object of type A is fetched as a Variant, while a object of type B is received correctly. Instead of (A a){a.fun();} I also tried (immutable(A) a){a.fun();} which resulted in core.exception.AssertError@/usr/include/d/dmd/phobos/std/variant.d(286): immutable(A) Any idea? I am using dmd 2.057 with linux. Thanks import std.stdio; import std.concurrency; class A{ public: void fun()const{ writeln("A.fun()"); } }; class B : A{ public: override void fun()const{ writeln("B.fun()"); } }; void producer(Tid t){ auto a = new immutable(A); auto b = new immutable(B); t.send(a); t.send(b); } void main(){ auto t = spawn(&producer, thisTid); while(1){ receive( (A a){a.fun();}, (Variant v){writeln("Unknown");} ); } }
Re: GUI library
On 2012-03-25 17:22, Kevin Cox wrote: I would reccomend Qt as well. You will get native cross-platform widgets with great performance. I am not sure how far QtD is but I know it once had a lot of development on it. I don't think Qt is uses the native drawing operations on Mac OS X. -- /Jacob Carlborg
std.json
Could I get a "hello, world" example of parsing json? The docs look simple enough, but I could still use an example.
Re: GUI library
On Sun, Mar 25, 2012 at 11:13 AM, Jacob Carlborg wrote: > On 2012-03-25 15:04, Tyro[17] wrote: > >> Is there one available for use with D2 on MAC OS X? >> >> Thanks, >> Andrew >> > > * QtD - Bindings to Qt. Use the native drawing operations of the operating > system (I think). Available on all platforms. Not sure if this is developed > any more. > I would reccomend Qt as well. You will get native cross-platform widgets with great performance. I am not sure how far QtD is but I know it once had a lot of development on it.
Re: GUI library
On 2012-03-25 15:04, Tyro[17] wrote: Is there one available for use with D2 on MAC OS X? Thanks, Andrew I think these are the choices on Mac OS X: * gtkD - Bindings to GTK. Does not use the native drawing operations of the operating system. Available on all platforms. http://dsource.org/projects/gtkd * QtD - Bindings to Qt. Use the native drawing operations of the operating system (I think). Available on all platforms. Not sure if this is developed any more. http://dsource.org/projects/qtd * wxD - Bindings to wxWidgets. Use the native drawing operations of the operating system. Available on all platforms. Not sure of the status. http://wxd.sourceforge.net/ It would also be possible to use Cocoa, as you do with Objective-C, but that wouldn't be very practically. There's also a DMD fork that directly supports interfacing with Objective-C: http://michelf.com/projects/d-objc/ -- /Jacob Carlborg
GUI library
Is there one available for use with D2 on MAC OS X? Thanks, Andrew
Parser (not parser generators) and ctfe
Does it exists a working ctfe parser for xml or ini or something suitable for config file? Something that import() file and reads values at compile times. For example: On my (php) website I use an optimized c++ backend (compiled as php module). Profiling code I see that website wastes a lot of time on opening, reading and parsing config files (a couple of files where i read hosts, ports, etc..). If these value was compiled as consts it would be a great boost...
Re: trouble calling function from windows dll
While I was playing around some more I noticed that the optimize flag causes my program to give an access violation while normal compilation doesn't give any error whatsoever. is this an old bug or have I stumbled upon a new bug?
Re: trouble calling function from windows dll
Turns out it indeed got the right function pointer and that function is getting called correctly. What I was trying to do however was forking a process within another. One of the things I needed to do was unmapping the base module from memory from one of the exe's, align correctly and then write the second executable in the memory of the first one. NtUnmapViewOfSection seems to fail with error code STATUS_NOT_MAPPED_VIEW. I've found out this error appears on certain kinds of executable and one's written in D seem to be one of those. Is there any reason windows complains that the main module containing the code section is not mapped?
Re: trouble calling function from windows dll
I did not import ntoskrnl.lib because I'm trying to do everything in user mode and there I have access to ntdll.dll which contains ntunmapviewofsection. Thats why I started using implib to create an ntdll.dll import library but I couldn't get it to work. It's good to know that it actually returns the right function pointer. Now I only have to find where the real problem is :) Thank you for your help.
Re: trouble calling function from windows dll
On Saturday, 24 March 2012 at 19:11:38 UTC, maarten van damme wrote: hi, I'm trying to call NtUnmapViewOfSection from ntdll.dll. According to the msdn docs it should look like NTSTATUS NtUnmapViewOfSection( __in HANDLE ProcessHandle, __in_opt PVOID BaseAddress ); I tried to call it by simply declaring extern(Windows) uint NtUnmapViewOfSection(HANDLE hProcess,PVOID baseAddress); But now I get Error 42: Symbol Undefined _NtUnmapViewOfSection@8 I've also tried using GetProcAddress cast(uint function(HANDLE hProcess,PVOID address))GetProcAddress(Runtime.loadLibrary("ntdll.dll"), "NtUnmapViewOfSection") but when I looked at GetLastError I get error 127 (specified procedure could not be found) and the function doesn't work. It's likely I'm missing something easy here, I just can't figure out what it is. Someone knows what it is? Actually, Runtime.loadLibrary should return the function pointer correctly. Your call to GetLastError() is returning 127 because Runtime.loadLibrary itself calls GetProcAddress to see if it contains a GC-related function (which will fail if it's not a D DLL). Maarten
Re: trouble calling function from windows dll
On Saturday, 24 March 2012 at 19:11:38 UTC, maarten van damme wrote: hi, I'm trying to call NtUnmapViewOfSection from ntdll.dll. According to the msdn docs it should look like NTSTATUS NtUnmapViewOfSection( __in HANDLE ProcessHandle, __in_opt PVOID BaseAddress ); I tried to call it by simply declaring extern(Windows) uint NtUnmapViewOfSection(HANDLE hProcess,PVOID baseAddress); But now I get Error 42: Symbol Undefined _NtUnmapViewOfSection@8 Did you import ntoskrnl.lib? I've also tried using GetProcAddress cast(uint function(HANDLE hProcess,PVOID address))GetProcAddress(Runtime.loadLibrary("ntdll.dll"), "NtUnmapViewOfSection") but when I looked at GetLastError I get error 127 (specified procedure could not be found) and the function doesn't work. It's likely I'm missing something easy here, I just can't figure out what it is. Someone knows what it is? Runtime.loadLibrary is the problem. Use the Win32 LoadLibrary instead. Maarten