Re: Creating D bindings for a C library
On Tuesday, 24 November 2015 at 23:22:24 UTC, Joseph Rushton Wakeling wrote: On Tuesday, 24 November 2015 at 23:14:14 UTC, Joseph Rushton Wakeling wrote: I'm considering creating some D bindings for a C library. I should probably clarify that I know what to do assuming I have to write all the bindings manually. However, as the library has quite a lot of header files, I'd really much rather auto-convert as much as possible, hence the questions above :-) There are some binding generator the most two famous being htod and dstep: http://wiki.dlang.org/List_of_Bindings#Binding_generators
Creating D bindings for a C library
Hello all, I'm considering creating some D bindings for a C library. Before I start, I was wondering if anyone could advise me on the current state-of-the-art in automatically converting C headers to .d or .di files; it's a long time since I've looked at anything to do with this and the interfacing-with-C page in the D reference doesn't mention any tools. Thanks in advance for any advice, Best wishes, -- Joe
Re: Creating D bindings for a C library
On Tuesday, 24 November 2015 at 23:14:14 UTC, Joseph Rushton Wakeling wrote: I'm considering creating some D bindings for a C library. I should probably clarify that I know what to do assuming I have to write all the bindings manually. However, as the library has quite a lot of header files, I'd really much rather auto-convert as much as possible, hence the questions above :-)
Re: AA behaves differently in CTFE?
On 11/24/15 2:43 PM, Bastiaan Veelo wrote: On Tuesday, 24 November 2015 at 14:23:38 UTC, Steven Schveighoffer wrote: If CTFE associative arrays perform differently, then that is a bug. I am not sure if this is the case, but you should file a bug anyway, someone can take a look at it. If you can narrow it down to a minimal case where it breaks down, that would be helpful. -Steve Thanks. After narrowing it down further I discovered a mistake in my code, which caused undesired behaviour dependent on the order in which elements on an AA are traversed by foreach. As it turns out, the order in which foreach traverses elements of an AA at compile time differs from the order at run time. So yes, associative arrays behave differently, but it is hardly a bug since "in a foreach loop the order in which the elements are iterated is unspecified." [1] [1] http://dlang.org/hash-map.html Ah yes, that is very true. Thanks for looking at it further! If you need traversal to be consistent, there is std.container.rbtree, but it's not very friendly to use as a map. -Steve
Re: AA behaves differently in CTFE?
On Tuesday, 24 November 2015 at 14:23:38 UTC, Steven Schveighoffer wrote: If CTFE associative arrays perform differently, then that is a bug. I am not sure if this is the case, but you should file a bug anyway, someone can take a look at it. If you can narrow it down to a minimal case where it breaks down, that would be helpful. -Steve Thanks. After narrowing it down further I discovered a mistake in my code, which caused undesired behaviour dependent on the order in which elements on an AA are traversed by foreach. As it turns out, the order in which foreach traverses elements of an AA at compile time differs from the order at run time. So yes, associative arrays behave differently, but it is hardly a bug since "in a foreach loop the order in which the elements are iterated is unspecified." [1] [1] http://dlang.org/hash-map.html
Re: Something about Chinese Disorder Code
Am Tue, 24 Nov 2015 17:08:33 + schrieb BLM768: > On Tuesday, 24 November 2015 at 09:48:45 UTC, magicdmer wrote: > > I display chinese string like: > > > > auto str = "你好,世界" > > writeln(str) > > > > and The display is garbled。 > > > > some windows api like MessageBoxA ,if string is chinese, it > > displays disorder code too > > > > i think i must use WideCharToMultiByte to convert it , is there > > any other answer to solve this question simplely > > You can also try using a wide string literal, i.e. "你好,世界"w. The > suffix forces the string to use 16-bit characters. > > The garbled display from writeln might be related to your console > settings. If you aren't using the UTF-8 codepage in cmd.exe, that > would explain why the text appears garbled. Unfortunately, > Windows has some significant bugs with UTF-8 in the console. This is really our problem. We pretend the output terminal is UTF-8 without providing any means to configure the terminal or converting the output to the terminal's encoding. All OSs provide conversion API that works for the most part (assuming normalization form D for example), but we just don't use them. Most contributers were on Linux or English Windows system where the issue is not obvious. But even in Europe łáö will come out garbled. Now this is mostly not an issue with modern Linux and OS X as the default is UTF-8, but some people refuse to change to variable length encodings and Windows has always been preferring fixed length encodings. The proper way to solve this for the OP in a cross-platform way is to replace writeln with something that detects whether the output is a terminal and then converts the string to something that will display correctly, which can be a simple wchar[] on Windows or the use of iconv on Linux. Ketmar is currently using iconv to print D string on his Linux set up for Cyrillic KIO-8U and I think I used it in some code as well. It is not perfect, but will make most printable strings readable. ICU I think is the only library that gets it 100% correct with regards to normalization and offering you different error concealment methods. -- Marco
Re: Communicating with external processes in D
On Monday, 23 November 2015 at 20:02:16 UTC, Cameron Reid wrote: I'm rather new to D, so apologies if this is a silly question: I'd like to be able to fork a number of instances of a process, write to their stdins and read from their stdouts in parallel. That is, I want to write some data to the stdin of all these processes and collect lines written to their stdouts as soon as they're emitted by the process. I tried for quite a while last night using permutations of pipeProcess and i/o redirection but nothing did exactly what I was looking for. Is such a thing possible? If so, where might I go to educate myself? pipeProcess will give you the input/output pair (and believe the output is accessible at the available time the program writes a line). To publish the same input to multiple pipes will be something have to create along with a receiver of multiple outputs. That is to say, you can do it, but managing read/write is on you.
Re: How to use D parallel functions/library
On 24.11.2015 19:49, Bishop120 wrote: I figured this would be a simple parallel foreach function with an iota range of sizeX and just making int X declared inside the function so that I didnt have to worry about shared variable but I cant get around the alive++ reduction and I dont understand enough about D's reduction/parallel library. Any ideas? Thanks in advance for yalls patience and assistance! I'm not sure what you're asking. Are you maybe looking for core.atomic.atomicOp? Example: import core.atomic: atomicOp; import std.parallelism: parallel; import std.range: iota; import std.stdio: writeln; void main() { int x = 0; shared int y = 0; foreach(i; parallel(iota(100_000))) { ++x; y.atomicOp!"+="(1); } writeln(x); /* usually less than 100_000 */ writeln(y); /* 100_000 */ }
Re: Something about Chinese Disorder Code
On 25/11/15 1:47 AM, Meta wrote: On Tuesday, 24 November 2015 at 09:52:21 UTC, Rikki Cattermole wrote: On 24/11/15 10:48 PM, magicdmer wrote: I display chinese string like: auto str = "你好,世界" writeln(str) and The display is garbled。 some windows api like MessageBoxA ,if string is chinese, it displays disorder code too i think i must use WideCharToMultiByte to convert it , is there any other answer to solve this question simplely *A windows API functions are for ASCII only. You need the *W varients which are unicode. For this you could use std.conv : to. wstring text = "my str".to!wstring; Or at least I'm pretty sure that will work for you. I'm pretty sure you can just do: wstring text = "my string"; Or auto text = "my string"w; The second one is correct yes. I'm just assuming that it isn't compiled into the executable.
switch with enum
I have seen a code a while ago, but even by looking at documentation, I couldn't have found anything about it. Let's say I have defined an enum; enum Status: ubyte{ Busy = 1, Active = 2 } and received a ubyte value from user. ubyte userValue; I want to switch over userValue, but that should depend on Status. switch( userValue ){ ... } What I mean is that compiler should enforce values of enum "Status" to be declared in switch as it would be done with "final switch", but as you can guess, user might enter a value that is not defined by Status. Thus, I should be able to enter the case "default" as well. I remember it something like switch( userValue ) with( Status ){...}, but not sure about it. Maybe it was D1 code. Is there anything like this currently?
Re: switch with enum
On 11/24/15 10:51 PM, tcak wrote: I have seen a code a while ago, but even by looking at documentation, I couldn't have found anything about it. Let's say I have defined an enum; enum Status: ubyte{ Busy = 1, Active = 2 } and received a ubyte value from user. ubyte userValue; I want to switch over userValue, but that should depend on Status. switch( userValue ){ } What I mean is that compiler should enforce values of enum "Status" to be declared in switch as it would be done with "final switch", but as you can guess, user might enter a value that is not defined by Status. Thus, I should be able to enter the case "default" as well. All final switch does is ensure you are covering all possible enums. It assumes that the value is already a valid enum value. If you did final switch on userValue, it would require you handle all 256 possible values for ubyte. So you would have to cast first. I remember it something like switch( userValue ) with( Status ){...}, but not sure about it. Maybe it was D1 code. Is there anything like this currently? What this does (and yes, it should work) is make it so you don't have to type "Status.Busy" within your case statements. You can just type "Busy". That's all. -Steve
Re: Something about Chinese Disorder Code
On Tuesday, 24 November 2015 at 19:41:12 UTC, Marco Leise wrote: Am Tue, 24 Nov 2015 17:08:33 + schrieb BLM768: [...] thank you for your answers. I solved it. windows console like: fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china"); auto str = "你好,世界"; writeln(str); MessageBox like: const wchar *wstring = toUTF16z("你好,世界"); MessageBoxW(null,wstring,wstring,0);
Re: switch with enum
On Wednesday, 25 November 2015 at 03:59:01 UTC, Steven Schveighoffer wrote: On 11/24/15 10:51 PM, tcak wrote: I have seen a code a while ago, but even by looking at documentation, I couldn't have found anything about it. Let's say I have defined an enum; enum Status: ubyte{ Busy = 1, Active = 2 } and received a ubyte value from user. ubyte userValue; I want to switch over userValue, but that should depend on Status. switch( userValue ){ } What I mean is that compiler should enforce values of enum "Status" to be declared in switch as it would be done with "final switch", but as you can guess, user might enter a value that is not defined by Status. Thus, I should be able to enter the case "default" as well. All final switch does is ensure you are covering all possible enums. It assumes that the value is already a valid enum value. If you did final switch on userValue, it would require you handle all 256 possible values for ubyte. So you would have to cast first. I remember it something like switch( userValue ) with( Status ){...}, but not sure about it. Maybe it was D1 code. Is there anything like this currently? What this does (and yes, it should work) is make it so you don't have to type "Status.Busy" within your case statements. You can just type "Busy". That's all. -Steve As far as I see, "default" case is not allowed when final switch is used. From compiler developer's perspective, it is meaningful and I can understand, but thinking about use cases as I have given an example, this limitation prevents writing "tight" code. (That is the term I could have found to express I am trying to say).
Re: How to use D parallel functions/library
On Tuesday, 24 November 2015 at 18:49:25 UTC, Bishop120 wrote: I figured this would be a simple parallel foreach function with an iota range of sizeX and just making int X declared inside the function so that I didnt have to worry about shared variable but I cant get around the alive++ reduction and I dont understand enough about D's reduction/parallel library. Any ideas? Thanks in advance for yalls patience and assistance! Incrementing often the same variable from different parallel threads is a very bad idea in terms of performance. I would suggest counting number of alive cells for each row independently (in a local non-shared variable) and storing it to an array (one value per row), then after the loop sum them up. auto aliveCellsPerRow = new int[N]; foreach(i; iota(N).parallel) { int aliveHere; //...process a row... aliveCellsPerRow[i] = aliveHere; } alive = aliveCellsPerRow.sum; Then everything will be truly parallel, correct and fast.
convertion of type tuple to enum
What is the best way to do subj? I did ``` import std.array: array; import std.typetuple: TypeTuple; import std.typecons: tuple; import std.traits: EnumMembers; struct Foo {} struct Bar {} struct FooBar {} struct Baz {} string convertTypeTupleToEnum(Types...)() { string s = "enum Kind { "; foreach(T; Types) { s ~= T.stringof ~ ", "; } s ~= "}"; return s; } alias Types = TypeTuple!(Foo, Bar, FooBar, Baz); void main() { mixin(convertTypeTupleToEnum!(Types)); assert(EnumMembers!Kind.tuple.array == [ Kind.Foo, Kind.Bar, Kind.FooBar, Kind.Baz]); } ``` (also here http://dpaste.dzfl.pl/bc6f19770f85). Is there a way to do it without string mixin? Thanks!
How to use D parallel functions/library
Hey everyone. A new D learner here. So far I love D and how much better its working than C++. One thing I like doing is parallel functions so with C++ using OMP. Right now Im trying to figure out how to do Conways Game of Life in D in parallel. Serially D is much faster than C++ so I feel fairly confident that it should be faster using D's parallelism library. In C++ with OMP its pretty easy to do a parallel for with a private and a reduction variable but I am having problems understanding how to do this in D. Heres the meat of my parallel code for the Game of Life. Can yall help me understand how to convert this to D? //Iterate through 2d matrix ignoring the border cells (starting at 1 and going to matrix size) #pragma omp for private (x) reduction (+:alive) schedule (dynamic) for (int i = 1; i <= sizeX; i++) { for (int j = 1; j <= sizeY; j++) { //Set X to 0... sumerize all 8 of X's neighbors including border cells x = 0; x += matrixA[i - 1][j] + matrixA[i + 1][j] + matrixA[i][j - 1] + matrixA[i][j + 1] + matrixA[i - 1][j - 1] + matrixA[i - 1][j + 1] + matrixA[i + 1][j - 1] + matrixA[i + 1][j + 1]; //If cell is alive if (matrixA[i][j] == true) { //Cell dies if it doesnot have 2 or 3 neighbors if (x < 2 || x > 3) { matrixB[i][j] = false; } //Mark cell as alive in matrix B else { matrixB[i][j] = true; alive++; } } //If cell is not alive else { //Cell becomes alive if it has exactly 3 neighbors if (x == 3) { //Mark cell alive in matrix B matrixB[i][j] = true; alive++; } } } } The Matrices are bools since its only alive or dead. I keep track of the number of alive cells so that I can see at a glance if things are working correctly since the same seed run the same number of iterations will always have the same outcome. For simplicity sake imagine that the matrices are 2002 x 2002. The reason they are extra rows and columns is so that I can do wrap around but thats not relevant here. I figured this would be a simple parallel foreach function with an iota range of sizeX and just making int X declared inside the function so that I didnt have to worry about shared variable but I cant get around the alive++ reduction and I dont understand enough about D's reduction/parallel library. Any ideas? Thanks in advance for yalls patience and assistance! Thomas
DUB, Platform specifications and dependencies
Actually I'm trying to setup dub to not grab a dependency on Windows ( https://github.com/Zardoz89/DEDCPU-16/blob/master/dub.sdl ) : name "dedcpu" authors "Luis Panadero Guardeño" targetType "none" license "BSD 3-clause" description "DCPU-16 tools" subPackage { name "lem1802" description "Visual LEM1802 font editor" targetType "executable" targetName "lem1802" excludedSourceFiles "src/bconv.d" excludedSourceFiles "src/ddis.d" dependency "gtk-d:gtkd" version="~>3.2.0" platform="posix" libs "gtkd" platform="windows" } ... How ever, running dub on Windows (tested on two different machines), ignores platform specification for gtk-d dependency . What I'm doing wrong ?
Re: Something about Chinese Disorder Code
On Tuesday, 24 November 2015 at 09:48:45 UTC, magicdmer wrote: I display chinese string like: auto str = "你好,世界" writeln(str) and The display is garbled。 some windows api like MessageBoxA ,if string is chinese, it displays disorder code too i think i must use WideCharToMultiByte to convert it , is there any other answer to solve this question simplely You can also try using a wide string literal, i.e. "你好,世界"w. The suffix forces the string to use 16-bit characters. The garbled display from writeln might be related to your console settings. If you aren't using the UTF-8 codepage in cmd.exe, that would explain why the text appears garbled. Unfortunately, Windows has some significant bugs with UTF-8 in the console.
Re: AA behaves differently in CTFE?
On Tuesday, 24 November 2015 at 07:54:24 UTC, Bastiaan Veelo wrote: This results TRUE at compile time, but FALSE at run time. Sorry, that should be the reverse: TRUE at run time, FALSE at compile time. Compile time exhibits the unexpected behaviour.
Re: Something about Chinese Disorder Code
On 24/11/15 10:48 PM, magicdmer wrote: I display chinese string like: auto str = "你好,世界" writeln(str) and The display is garbled。 some windows api like MessageBoxA ,if string is chinese, it displays disorder code too i think i must use WideCharToMultiByte to convert it , is there any other answer to solve this question simplely *A windows API functions are for ASCII only. You need the *W varients which are unicode. For this you could use std.conv : to. wstring text = "my str".to!wstring; Or at least I'm pretty sure that will work for you.
Re: Something about Chinese Disorder Code
On Tuesday, 24 November 2015 at 09:48:45 UTC, magicdmer wrote: I display chinese string like: auto str = "你好,世界" writeln(str) and The display is garbled。 some windows api like MessageBoxA ,if string is chinese, it displays disorder code too i think i must use WideCharToMultiByte to convert it , is there any other answer to solve this question simplely MessageBoxA <- A is ANSI. You should try with MessageBoxW, for unicode I guess.
Something about Chinese Disorder Code
I display chinese string like: auto str = "你好,世界" writeln(str) and The display is garbled。 some windows api like MessageBoxA ,if string is chinese, it displays disorder code too i think i must use WideCharToMultiByte to convert it , is there any other answer to solve this question simplely
Re: AA behaves differently in CTFE?
On 11/24/15 2:59 AM, Bastiaan Veelo wrote: On Tuesday, 24 November 2015 at 07:54:24 UTC, Bastiaan Veelo wrote: This results TRUE at compile time, but FALSE at run time. Sorry, that should be the reverse: TRUE at run time, FALSE at compile time. Compile time exhibits the unexpected behaviour. If CTFE associative arrays perform differently, then that is a bug. I am not sure if this is the case, but you should file a bug anyway, someone can take a look at it. If you can narrow it down to a minimal case where it breaks down, that would be helpful. -Steve
Re: Something about Chinese Disorder Code
On Tuesday, 24 November 2015 at 09:52:21 UTC, Rikki Cattermole wrote: On 24/11/15 10:48 PM, magicdmer wrote: I display chinese string like: auto str = "你好,世界" writeln(str) and The display is garbled。 some windows api like MessageBoxA ,if string is chinese, it displays disorder code too i think i must use WideCharToMultiByte to convert it , is there any other answer to solve this question simplely *A windows API functions are for ASCII only. You need the *W varients which are unicode. For this you could use std.conv : to. wstring text = "my str".to!wstring; Or at least I'm pretty sure that will work for you. I'm pretty sure you can just do: wstring text = "my string"; Or auto text = "my string"w;