Re: C to D: please help translate this weird macro

2023-09-23 Thread Ki Rill via Digitalmars-d-learn
On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'm trying to translate the Nuklear GUI library to D [here](https://

Re: C to D: please help translate this weird macro

2023-09-22 Thread Ki Rill via Digitalmars-d-learn
On Thursday, 21 September 2023 at 16:50:51 UTC, Imperatorn wrote: On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'

Re: C to D: please help translate this weird macro

2023-09-22 Thread Ki Rill via Digitalmars-d-learn
On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven wrote: The 1st argument of `getMember` can just be T, like the original macro. The 2nd argument needs to be a compile-time string. Also the `char*` cast needs to apply to `ptr` before subtracting the offset AFAICS. So reordering

Re: C to D: please help translate this weird macro

2023-09-21 Thread Imperatorn via Digitalmars-d-learn
On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'm trying to translate the Nuklear GUI library to D [here](https://

Re: C to D: please help translate this weird macro

2023-09-21 Thread user1234 via Digitalmars-d-learn
On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven wrote: (Untested) There might be a `need this` error

Re: C to D: please help translate this weird macro

2023-09-21 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven wrote: return cast(T*)(cast(void*)(cast(char*)ptr - __traits(getMember, T, member).offsetof))); There's a trailing `)` that needs removing. Also pretty sure it can be simplified to: return cast(T*)(cast(char*)ptr

Re: C to D: please help translate this weird macro

2023-09-21 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 21 September 2023 at 02:57:07 UTC, Ki Rill wrote: On Thursday, 21 September 2023 at 02:23:32 UTC, Ki Rill wrote: wrote: [...] Translated it to this eventually: ```D auto nk_container_of(P, T)(P ptr, T type, const(char)* member) { return cast(T*)(cast(void*)(cast(char*)

Re: C to D: please help translate this weird macro

2023-09-20 Thread Ki Rill via Digitalmars-d-learn
On Thursday, 21 September 2023 at 02:23:32 UTC, Ki Rill wrote: wrote: [...] Translated it to this eventually: ```D auto nk_container_of(P, T)(P ptr, T type, const(char)* member) { return cast(T*)(cast(void*)(cast(char*) (ptr - __traits(getMember, type, member).offsetof))); } ```

Re: C to D: please help translate this weird macro

2023-09-20 Thread Ki Rill via Digitalmars-d-learn
On Wednesday, 20 September 2023 at 17:14:41 UTC, Dejan Lekic wrote: [...] NK_CONTAINER_OF should probably be translated to: `cast(T*)((cast(void*)ptr - __traits(getMember, T, member).offsetof))` PS. I did not invent this. My original idea was far worse than this. - It was suggested on IRC b

Re: C to D: please help translate this weird macro

2023-09-20 Thread Dejan Lekic via Digitalmars-d-learn
On Wednesday, 20 September 2023 at 13:55:14 UTC, Ki Rill wrote: On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'm

Re: C to D: please help translate this weird macro

2023-09-20 Thread ryuukk_ via Digitalmars-d-learn
On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'm trying to translate the Nuklear GUI library to D [here](https://

Re: C to D: please help translate this weird macro

2023-09-20 Thread Dejan Lekic via Digitalmars-d-learn
On Wednesday, 20 September 2023 at 13:55:14 UTC, Ki Rill wrote: On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'm

Re: C to D: please help translate this weird macro

2023-09-20 Thread Ki Rill via Digitalmars-d-learn
On Wednesday, 20 September 2023 at 13:53:08 UTC, Ki Rill wrote: Here is the macro: ```C #define NK_CONTAINER_OF(ptr,type,member)\ (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member))) ``` I'm trying to translate the Nuklear GUI library to D [here](https://

Re: C to D convertor

2021-08-24 Thread Виталий Фадеев via Digitalmars-d-learn
On Tuesday, 24 August 2021 at 11:52:45 UTC, Dennis wrote: On Saturday, 21 August 2021 at 08:14:22 UTC, Виталий Фадеев wrote: Any more ? CPP2D https://github.com/lhamot/CPP2D Dennis, thank!

Re: C to D convertor

2021-08-24 Thread Dennis via Digitalmars-d-learn
On Saturday, 21 August 2021 at 08:14:22 UTC, Виталий Фадеев wrote: Any more ? CPP2D https://github.com/lhamot/CPP2D

Re: C to D convertor

2021-08-21 Thread Виталий Фадеев via Digitalmars-d-learn
On Saturday, 21 August 2021 at 08:59:55 UTC, evilrat wrote: On Saturday, 21 August 2021 at 08:14:22 UTC, Виталий Фадеев wrote: I know, i know... It not possible, but part of the C code we can to convert to the D. Show me, please, solutions, projects, tools, scripts, docs. Can you give the link

Re: C to D convertor

2021-08-21 Thread evilrat via Digitalmars-d-learn
On Saturday, 21 August 2021 at 08:14:22 UTC, Виталий Фадеев wrote: I know, i know... It not possible, but part of the C code we can to convert to the D. Show me, please, solutions, projects, tools, scripts, docs. Can you give the link ? `htod` is 1. Any more ? dstep https://code.dlang.org/pac

Re: C++ to D - recursion with std.variant

2015-04-05 Thread Dennis Ritchie via Digitalmars-d-learn
On Sunday, 5 April 2015 at 09:48:01 UTC, thedeemon wrote: On Friday, 3 April 2015 at 16:46:08 UTC, Dennis Ritchie wrote: Hi, Is it possible to write on D recursion using std.variant? Using Algebraic from std.variant and some additional templates: http://dpaste.dzfl.pl/65afd3a7ce52 (taken from

Re: C++ to D - recursion with std.variant

2015-04-05 Thread thedeemon via Digitalmars-d-learn
On Friday, 3 April 2015 at 16:46:08 UTC, Dennis Ritchie wrote: Hi, Is it possible to write on D recursion using std.variant? Using Algebraic from std.variant and some additional templates: http://dpaste.dzfl.pl/65afd3a7ce52 (taken from this thread: http://forum.dlang.org/thread/yidovyrczgdiveq

Re: C++ to D

2015-04-02 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 1 April 2015 at 17:51:40 UTC, John Colvin wrote: Don't really see the point. Here's a neat thing that's definitely cheating because although it stores the results in the type system, the arithmetic is done in constant-folding: struct Integer(int a){} template Value(T) { stati

Re: C++ to D

2015-04-01 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 1 April 2015 at 17:51:40 UTC, John Colvin wrote: On Wednesday, 1 April 2015 at 17:03:34 UTC, Dennis Ritchie wrote: On Wednesday, 1 April 2015 at 15:22:10 UTC, John Colvin wrote: Compile Time Function Evaluation (CTFE) is a very powerful tool to avoid having to enter in to all tha

Re: C++ to D

2015-04-01 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 1 April 2015 at 17:03:34 UTC, Dennis Ritchie wrote: On Wednesday, 1 April 2015 at 15:22:10 UTC, John Colvin wrote: Compile Time Function Evaluation (CTFE) is a very powerful tool to avoid having to enter in to all that C++ style mess. Yes, CTFE in D really cool. Thanks. I need t

Re: C++ to D

2015-04-01 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 1 April 2015 at 15:22:10 UTC, John Colvin wrote: Compile Time Function Evaluation (CTFE) is a very powerful tool to avoid having to enter in to all that C++ style mess. Yes, CTFE in D really cool. Thanks. I need to implement arithmetic (addition / subtraction) only use the type

Re: C++ to D

2015-04-01 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 1 April 2015 at 13:59:10 UTC, Dennis Ritchie wrote: You can do this: import std.typetuple; //helper for staticReduce template Alias(alias a) { alias Alias = a; } // staticReduce should really be in std.typetuple, or // the soon to arrive std.meta package. template static

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Ivan Kazmenko: arr.map !(to !(string)) .join (" ") .writeln; I suggest to not put a space before the bang (!), because it's confusing for me. Also, "arr.map !(to !(string))" is better written "arr.map!text". But even better is to use the range formatting of writefln, a

Re: C# to D

2015-03-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 20:09:53 UTC, bearophile wrote: This is still not very efficient (perhaps the last sorting has to be stable): void main() { import std.stdio, std.algorithm, std.typecons, std.array; [7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8, 5, 8, 8]

Re: C# to D

2015-03-25 Thread Ivan Kazmenko via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 20:17:57 UTC, bearophile wrote: Ivan Kazmenko: (1) For me, the name of the function is obscure. Something like sortBy would be a lot easier to find than schwartzSort. I've asked to change the name of that function for years. But Andrei Alexandrescu is a adaman

Re: C# to D

2015-03-25 Thread Ivan Kazmenko via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 20:09:53 UTC, bearophile wrote: Dennis Ritchie: A more effective solution for C ++: #include #include #include int main() { using namespace ranges; auto rng = istream( std::cin ) | to_vector | action::sort | view::group_by( st

Re: C# to D

2015-03-25 Thread Ivan Kazmenko via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 20:02:20 UTC, Ivan Kazmenko wrote: Will file an issue soon. Here it is: https://issues.dlang.org/show_bug.cgi?id=14340 And another one, a 2.067 regression: https://issues.dlang.org/show_bug.cgi?id=14341

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Ivan Kazmenko: (1) For me, the name of the function is obscure. Something like sortBy would be a lot easier to find than schwartzSort. I've asked to change the name of that function for years. But Andrei Alexandrescu is a adamantly against changing that pet name he has chosen. This is irrat

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: A more effective solution for C ++: #include #include #include int main() { using namespace ranges; auto rng = istream( std::cin ) | to_vector | action::sort | view::group_by( std::equal_to() ) | copy | action::stab

Re: C# to D

2015-03-25 Thread Ivan Kazmenko via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 19:32:43 UTC, Dennis Ritchie wrote: On Wednesday, 25 March 2015 at 19:01:43 UTC, bearophile wrote: One solution: Thanks. On Wednesday, 25 March 2015 at 19:03:27 UTC, bearophile wrote: But calling "count" for each item is not efficient (in both C# and D). If you

Re: C# to D

2015-03-25 Thread Ivan Kazmenko via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 20:02:20 UTC, Ivan Kazmenko wrote: (2) The documentation says it is more efficient than the first version in the number of comparisons (verbose lambda with plain sort) [1], but I don't get how it is possible: unless we know than (not pred1(a,b)) and (not !pred1(a,

Re: C# to D

2015-03-25 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 19:01:43 UTC, bearophile wrote: One solution: Thanks. On Wednesday, 25 March 2015 at 19:03:27 UTC, bearophile wrote: But calling "count" for each item is not efficient (in both C# and D). If your array is largish, then you need a more efficient solution. A mo

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Ali Çehreli: Do you know the story about groupBy? It's a long messy story. Look for it with another name, like chunkBy or something like that. Bye, bearophile

Re: C# to D

2015-03-25 Thread Ali Çehreli via Digitalmars-d-learn
On 03/25/2015 12:01 PM, bearophile wrote: bearophile Do you know the story about groupBy? I see it in the documentation but my git head does not have it: http://dlang.org/phobos/std_algorithm_iteration.html#.groupBy Ali

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
.schwartzSort!(x => tuple(-arr.count!(y => y == x), x)) But calling "count" for each item is not efficient (in both C# and D). If your array is largish, then you need a more efficient solution. Bye, bearophile

Re: C# to D

2015-03-25 Thread bearophile via Digitalmars-d-learn
Dennis Ritchie: int[] arr = { 7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8, 5, 8, 8 }; Console.WriteLine(string.Join(" ", arr.OrderByDescending(x => arr.Count(y => y == x)).ThenBy(x => x))); // prints 1 1 1 1 1 3 3 3 3 3 5 5 5 5 8 8 8 2 2 7 7 0 One solutio

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 11 March 2015 at 18:10:55 UTC, FG wrote: And your point was...? I take it, "poor c++" is a hint. Don't compare apples to oranges. No, I forgot to remove from the foreign code.

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
Thank you very much, Ali Çehreli and FG.

Re: C++ to D

2015-03-11 Thread FG via Digitalmars-d-learn
On 2015-03-11 at 18:27, Dennis Ritchie wrote: The same without classes in Lisp: [...] And your point was...? I take it, "poor c++" is a hint. Don't compare apples to oranges.

Re: C++ to D

2015-03-11 Thread FG via Digitalmars-d-learn
On 2015-03-11 at 17:42, Dennis Ritchie wrote: On Wednesday, 11 March 2015 at 16:08:22 UTC, Kagamin wrote: A hash table? See http://dlang.org/hash-map.html That is, the input is a string and, depending on what word it contains, is called one of the three methods of the class that this line han

Re: C++ to D

2015-03-11 Thread Ali Çehreli via Digitalmars-d-learn
On 03/11/2015 07:33 AM, Dennis Ritchie wrote: How to rewrite this in D to the handler method for the input parameter was determined on average in O(1)? D's associative arrays are hash tables. The following program is as similar to the C++ one as possible: import std.stdio; import std.range;

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
The same without classes in Lisp: (define (foo) (let ((foo1 (lambda (s) s)) (foo2 (lambda (s) (list->string (reverse (string->list s) (foo3 (lambda (s) (string-append s ", " s " " (lambda (in) (match in ("first" foo1) ("second" foo2) (

Re: C++ to D

2015-03-11 Thread Dennis Ritchie via Digitalmars-d-learn
On Wednesday, 11 March 2015 at 16:08:22 UTC, Kagamin wrote: A hash table? See http://dlang.org/hash-map.html That is, the input is a string and, depending on what word it contains, is called one of the three methods of the class that this line handles. And this happens in average cons

Re: C++ to D

2015-03-11 Thread Kagamin via Digitalmars-d-learn
A hash table? See http://dlang.org/hash-map.html

Re: C to D conversion for function

2013-04-29 Thread Timon Gehr
On 04/29/2013 02:24 PM, Sumit Raja wrote: On Monday, 29 April 2013 at 11:50:21 UTC, Timon Gehr wrote: In case you want to preserve the attribute: struct av_printf_format{ int fmtpos, attrpos; } @av_printf_format(2, 3) void av_log_ask_for_sample(void* avc, const(char)* msg, ...); Thanks. Wha

Re: C to D conversion for function

2013-04-29 Thread Sumit Raja
On Monday, 29 April 2013 at 11:50:21 UTC, Timon Gehr wrote: In case you want to preserve the attribute: struct av_printf_format{ int fmtpos, attrpos; } @av_printf_format(2, 3) void av_log_ask_for_sample(void* avc, const(char)* msg, ...); Thanks. What does @av_printf_format(2, 3) do to the f

Re: C to D conversion for function

2013-04-29 Thread Timon Gehr
On 04/29/2013 02:17 PM, 1100110 wrote: ... What is the difference between const(char)*, and const(char*)? I have seen them used pretty much interchangeably... Are they? Somehow I don't think they are. Variables of type const(char)* can be mutated, while const(char*) cannot be. void main(){

Re: C to D conversion for function

2013-04-29 Thread 1100110
On 04/29/2013 06:50 AM, Timon Gehr wrote: > On 04/29/2013 12:57 PM, Sumit Raja wrote: >> Hi, >> >> I wanted some help in converting this >> >> void av_log_ask_for_sample(void *avc, const char *msg, ...) >> av_printf_format(2, 3); >> >> from C to D. >> >> I don't know what it means or is called in C

Re: C to D conversion for function

2013-04-29 Thread Timon Gehr
On 04/29/2013 12:57 PM, Sumit Raja wrote: Hi, I wanted some help in converting this void av_log_ask_for_sample(void *avc, const char *msg, ...) av_printf_format(2, 3); from C to D. I don't know what it means or is called in C to start with so I am a bit lost on what to search for. Thanks Su

Re: C++ to D: mutable

2011-03-25 Thread Jonathan M Davis
> Jonathan M Davis Wrote: > > > const int a=0; > > > *cast(int*)&a=1; > > > > There are so many reasons to cringe at that. Taking the address of a > > local variable is generally very dangerous. As long as the pointer > > doesn't escape and exist beyond the life the variable, then you're okay, > >

Re: C++ to D: mutable

2011-03-25 Thread Kagamin
Jonathan M Davis Wrote: > > const int a=0; > > *cast(int*)&a=1; > > There are so many reasons to cringe at that. Taking the address of a local > variable is generally very dangerous. As long as the pointer doesn't escape > and exist beyond the life the variable, then you're okay, but you often

Re: C++ to D: mutable

2011-03-24 Thread Jonathan M Davis
> Caligo Wrote: > > Greetings, > > > > I have a C++ class that I would like to rewrite it in D. The class > > has members that are declared as 'mutable'. How do I achieve the same > > effect in D? if not, what is recommended? > > const int a=0; > *cast(int*)&a=1; There are so many reasons to c

Re: C++ to D: mutable

2011-03-24 Thread Kagamin
Caligo Wrote: > Greetings, > > I have a C++ class that I would like to rewrite it in D. The class > has members that are declared as 'mutable'. How do I achieve the same > effect in D? if not, what is recommended? const int a=0; *cast(int*)&a=1;

Re: C++ to D: mutable

2011-03-24 Thread Bekenn
On 3/24/2011 12:23 AM, Caligo wrote: Greetings, I have a C++ class that I would like to rewrite it in D. The class has members that are declared as 'mutable'. How do I achieve the same effect in D? if not, what is recommended? You don't. Specific recommendations would depend on how the clas

Re: C++ to D: mutable

2011-03-24 Thread Jonathan M Davis
> Greetings, > > I have a C++ class that I would like to rewrite it in D. The class > has members that are declared as 'mutable'. How do I achieve the same > effect in D? if not, what is recommended? You don't - or at least it's generally inadvisable to try. Unlike C++, const in D is transitiv

Re: C++ to D: Help please

2011-02-24 Thread Jesse Phillips
Well I guess my point was to use a language you are very familiar with to obtain examples. There have been some attempts and such tutorials: http://www.prowiki.org/wiki4d/wiki.cgi?ComingFrom#SpecificLanguage But Java I think is the most thorough and out of date (pre-D1). There is also a porting

Re: C++ to D: Help please

2011-02-24 Thread Tyro[a.c.edwards]
On 2/25/2011 3:33 AM, Jesse Phillips wrote: Well using one language you aren't familiar with to learn another is probably not the best strategy. For one thing C++ uses namespaces and D uses modules. They are both about name-space but are very different approaches. If you want to learn about m

Re: C++ to D: Help please

2011-02-24 Thread Jesse Phillips
%u Wrote: > I have no problems converting small problems. Hence why I'm not > trying to convert small scripts. My problems are in understanding > the inner workings of multimodule programs, how to create them, > create the make file to use in comepiling them, and then since I'm > reading C/C++ (w

Re: C++ to D: Help please

2011-02-23 Thread %u
bearophile, You do have a point there, and I actually expected that response. I would have posted my attempt at implementation, but am unable to transfer info between the computer I'm typing this message on and the one I'm programming on at the moment. I have no problems converting small problems

Re: C++ to D: Help please

2011-02-23 Thread bearophile
%u: > I am hoping that one of you experienced programmers out > there could lend me a hand converting this small program to D. If you show specific problems some people here will try to help. But I think most people are not willing to translate a multi-module C++ program to D for you. If you ar

Re: c to d: types + struct/union alignment

2009-12-30 Thread bearophile
Michael P.: >Also, would short unsigned int be a 'ushort' then?< In D ushort is an unsigned integer 16 bits long. While I think in C short unsigned int is not guaranteed to be 16 bit wide. The same is true for unsigned short / uint. Bye, bearophile

Re: c to d: types + struct/union alignment

2009-12-30 Thread Michael P.
BCS Wrote: > Hello Michael P., > > > I'm converting some C headers into D, and had some questions. > > 1. What is the equivalent of a 'short int' in D? > > e.g: > > struct ScePspSRect { > > short int x; > > short int y; > > short int w; > > short int h; > > } > > short > > > 3. > > type

Re: c to d: types + struct/union alignment

2009-12-30 Thread BCS
Hello Michael P., I'm converting some C headers into D, and had some questions. 1. What is the equivalent of a 'short int' in D? e.g: struct ScePspSRect { short int x; short int y; short int w; short int h; } short 3. typedef uint8_t u8; typede