Re: Prevent self-comparison without taking the address

2024-07-26 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 25 July 2024 at 15:40:29 UTC, Nick Treleaven wrote: On Thursday, 25 July 2024 at 15:06:35 UTC, IchorDev wrote: I think your function most likely has a safe interface, so it can be marked as `@trusted` as-per [the spec](https://dlang.org/spec/function.html#safe-interfaces). Just t

Re: Prevent self-comparison without taking the address

2024-07-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, July 25, 2024 4:50:04 AM MDT Dom DiSc via Digitalmars-d-learn wrote: > I have copied some source from C++, where the following pattern > is common (operator== already renamed): > > ```d > @safe: > struct S > { > bool opEquals(const ref S x) const > { >if(&x==&this) ret

Re: Prevent self-comparison without taking the address

2024-07-25 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 25 July 2024 at 15:06:35 UTC, IchorDev wrote: I think your function most likely has a safe interface, so it can be marked as `@trusted` as-per [the spec](https://dlang.org/spec/function.html#safe-interfaces). Just to mention that with -dip1000, taking the address of variables is

Re: Prevent self-comparison without taking the address

2024-07-25 Thread IchorDev via Digitalmars-d-learn
On Thursday, 25 July 2024 at 14:05:50 UTC, Dom DiSc wrote: As he said: no. It's only true for @safe: No they did not, they specifically said that my assertion holds true for all other [function attributes](https://dlang.org/spec/attribute.html#function-attributes). This does not tell me anyth

Re: Prevent self-comparison without taking the address

2024-07-25 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 25 July 2024 at 13:20:59 UTC, IchorDev wrote: On Thursday, 25 July 2024 at 13:01:53 UTC, Dennis wrote: That's true for the other function attributes, but `@safe:` actually does penetrate scopes The spec doesn’t mention this at all! Is this the case for any other `AttributeSpecifi

Re: Prevent self-comparison without taking the address

2024-07-25 Thread IchorDev via Digitalmars-d-learn
On Thursday, 25 July 2024 at 13:01:53 UTC, Dennis wrote: That's true for the other function attributes, but `@safe:` actually does penetrate scopes The spec doesn’t mention this at all! Is this the case for any other `AttributeSpecifier` declarations?

Re: Prevent self-comparison without taking the address

2024-07-25 Thread Dennis via Digitalmars-d-learn
On Thursday, 25 July 2024 at 11:46:29 UTC, IchorDev wrote: Also just so you know, placing `@safe:` there will not affect the contents of `S`. It has to be inside the struct declaration to affect its contents. That's true for the other function attributes, but `@safe:` actually does penetrate

Re: Prevent self-comparison without taking the address

2024-07-25 Thread IchorDev via Digitalmars-d-learn
On Thursday, 25 July 2024 at 10:50:04 UTC, Dom DiSc wrote: ```d @safe: struct S{ ``` Also just so you know, placing `@safe:` there will not affect the contents of `S`. It has to be inside the struct declaration to affect its contents.

Re: Prevent self-comparison without taking the address

2024-07-25 Thread IchorDev via Digitalmars-d-learn
On Thursday, 25 July 2024 at 10:50:04 UTC, Dom DiSc wrote: Can I replace this pattern with ```(x is this)``` or are there some special cases where this doen't work? When a parameter is `ref` it is treated as a value type (i.e. it has value semantics), even though it is a reference; therefore a

Prevent self-comparison without taking the address

2024-07-25 Thread Dom DiSc via Digitalmars-d-learn
I have copied some source from C++, where the following pattern is common (operator== already renamed): ```d @safe: struct S { bool opEquals(const ref S x) const { if(&x==&this) return true; ... } } ``` Can I replace this pattern with ```(x is this)``` or are there some sp

Re: Is comparison of shared data thread-safe?

2023-03-17 Thread bauss via Digitalmars-d-learn
On Thursday, 16 March 2023 at 12:32:34 UTC, Nick Treleaven wrote: With -preview=nosharedaccess, I get: int y = 2; shared int x = y; // OK assert(x == 2); // no error y = x; // error So for the assignment to y, reading x is an error and atomicLoad should be used instead. But is

Re: Is comparison of shared data thread-safe?

2023-03-16 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 16 March 2023 at 12:32:34 UTC, Nick Treleaven wrote: With -preview=nosharedaccess, I get: int y = 2; shared int x = y; // OK assert(x == 2); // no error y = x; // error This also does not error: ```d bool b = x == 3; ``` Filed: https://issues.dlang.org/show_bug.cg

Is comparison of shared data thread-safe?

2023-03-16 Thread Nick Treleaven via Digitalmars-d-learn
With -preview=nosharedaccess, I get: int y = 2; shared int x = y; // OK assert(x == 2); // no error y = x; // error So for the assignment to y, reading x is an error and atomicLoad should be used instead. But is it an oversight that reading x in the assert is not an error? I

Re: Comparison of multidimensional associative arrays

2023-02-08 Thread Anonymouse via Digitalmars-d-learn
On Wednesday, 8 February 2023 at 19:04:15 UTC, Alexander Zhirov wrote: [...] I would write a data structure and use struct members to reason about things, but that's probably just preference. ``` import std; struct DatabaseEntry { int id = -1; string deleted; string name; t

Re: Comparison of multidimensional associative arrays

2023-02-08 Thread Alexander Zhirov via Digitalmars-d-learn
On Wednesday, 8 February 2023 at 19:32:22 UTC, Ali Çehreli wrote: This should do it: [...] Yes, it works! I'll try it tomorrow on a large array of data. Thank you very much! This turns out to be a simple loop with a comparison of the existence of a key (whether it is included in an arr

Re: Comparison of multidimensional associative arrays

2023-02-08 Thread Ali Çehreli via Digitalmars-d-learn
On 2/8/23 11:04, Alexander Zhirov wrote: > That is, the result is arrays of table B that are missing OR not equal > to arrays in table A. This should do it: alias MyType = string[string][int]; // 'a' is subtracted from 'b' MyType difference(MyType b, MyType a) { MyType result; foreach

Re: Comparison of multidimensional associative arrays

2023-02-08 Thread Alexander Zhirov via Digitalmars-d-learn
On Wednesday, 8 February 2023 at 18:57:00 UTC, Anonymouse wrote: Can you explain how you determine how/if two entries are different? I apologize. I have not written, in fact, what I need to get. Array `A` ```d [ 4:["id":"4", "deleted":"f", "name":"6.2"], 3:["id":"3", "deleted":"f", "n

Re: Comparison of multidimensional associative arrays

2023-02-08 Thread Anonymouse via Digitalmars-d-learn
On Wednesday, 8 February 2023 at 17:55:03 UTC, Alexander Zhirov wrote: Not an easy task for me, maybe you can advise your compact solution. There are two associative arrays of type `string[string][int]`. It is necessary to find the differences and return them when comparing: Can you explain h

Re: Comparison of multidimensional associative arrays

2023-02-08 Thread Alexander Zhirov via Digitalmars-d-learn
On Wednesday, 8 February 2023 at 18:08:40 UTC, Ali Çehreli wrote: Just because this sounds complicated, I hope the data structure can be designed differently to be more friendly to this operation. (?) Ali This is the result of an SQL query. Roughly speaking, I need to compare the result of

Re: Comparison of multidimensional associative arrays

2023-02-08 Thread Ali Çehreli via Digitalmars-d-learn
On 2/8/23 09:55, Alexander Zhirov wrote: > the differences Is it considered a difference if a key exists but the value is different? Or is that an error case if you encounter that? > return them when comparing: The representation seems difficult as well. When given this: > 6:["id":"6",

Comparison of multidimensional associative arrays

2023-02-08 Thread Alexander Zhirov via Digitalmars-d-learn
Not an easy task for me, maybe you can advise your compact solution. There are two associative arrays of type `string[string][int]`. It is necessary to find the differences and return them when comparing: ```d [ 6:["id":"6", "deleted":"f", "name":"6.2_test"], 5:["id":"5", "deleted":"f"

Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread matheus. via Digitalmars-d-learn
On Sunday, 13 November 2022 at 17:10:23 UTC, DLearner wrote: ... The slight generalisation shown at bottom also worked. However, is there a way of avoiding the for-loop? ... I don't have too much knowledge in D, but I think so. (My main language is C). Well, one way to make things "better"

Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread DLearner via Digitalmars-d-learn
On Sunday, 13 November 2022 at 16:11:17 UTC, matheus. wrote: [...] You should add the code below after "auto B = A.dup;": B[0].Txt = A[0].Txt.dup; The "Txt" property inside B is still referencing A without the above. Matheus. Thank you - your suggestion worked. The slight generalisati

Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread Siarhei Siamashka via Digitalmars-d-learn
On Sunday, 13 November 2022 at 15:45:40 UTC, DLearner wrote: ```D struct test_struct { char[] Txt; } test_struct[] A; auto B = A.dup; ``` But A is not an `int[]` in your new example. You need a "deep copy" and I can see that similar questions had been asked in this forum bef

Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread matheus. via Digitalmars-d-learn
On Sunday, 13 November 2022 at 15:45:40 UTC, DLearner wrote: On Sunday, 13 November 2022 at 14:39:26 UTC, Siarhei Siamashka wrote: On Sunday, 13 November 2022 at 14:28:45 UTC, DLearner wrote: Creating a step 1.5: ``` int[] B = A; ``` ```D auto B = A.dup; ``` This will create a copy of A rath

Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread DLearner via Digitalmars-d-learn
On Sunday, 13 November 2022 at 14:39:26 UTC, Siarhei Siamashka wrote: On Sunday, 13 November 2022 at 14:28:45 UTC, DLearner wrote: Creating a step 1.5: ``` int[] B = A; ``` ```D auto B = A.dup; ``` This will create a copy of A rather than referencing to the same buffer in memory. Tested:

Re: Comparison of two 'dynamic arrays'.

2022-11-13 Thread Siarhei Siamashka via Digitalmars-d-learn
On Sunday, 13 November 2022 at 14:28:45 UTC, DLearner wrote: Creating a step 1.5: ``` int[] B = A; ``` ```D auto B = A.dup; ``` This will create a copy of A rather than referencing to the same buffer in memory.

Comparison of two 'dynamic arrays'.

2022-11-13 Thread DLearner via Digitalmars-d-learn
Hi Program has two steps: 1. Creates an array (int[] A), whose size and element values are only known at run-time. Then: 2. The elements (but not the array size) are further processed in a way that may or may not alter their value. Requirement: to identify the indices (if any) of the elemen

Re: Compiler switch for integer comparison/promotion to catch a simple error

2022-05-30 Thread Dom Disc via Digitalmars-d-learn
On Sunday, 29 May 2022 at 01:35:23 UTC, frame wrote: Is there a compiler switch to catch this kind of error? ```d ulong v = 1; writeln(v > -1); ``` IMHO the compiler should bail a warning if it sees a logic comparison between signed and unsigned / different integer sizes. There is 50% cha

Re: Compiler switch for integer comparison/promotion to catch a simple error

2022-05-30 Thread matheus via Digitalmars-d-learn
On Monday, 30 May 2022 at 13:15:12 UTC, bauss wrote: Good luck convincing Walter that this is a mistake :) I don't think this is a matter of convincing or changing the behavior, I think that a flag for this case (If not exist) should be added as a warning. A language where some people use t

Re: Compiler switch for integer comparison/promotion to catch a simple error

2022-05-30 Thread frame via Digitalmars-d-learn
On Monday, 30 May 2022 at 13:15:12 UTC, bauss wrote: Good luck convincing Walter that this is a mistake :) Well, I'm not talking about this is a mistake, just a C-thing I think. I wouldn't even ask him about that since it's in the spec. If I could I would just clone a DMD build, disable out

Re: Compiler switch for integer comparison/promotion to catch a simple error

2022-05-30 Thread bauss via Digitalmars-d-learn
On Sunday, 29 May 2022 at 01:35:23 UTC, frame wrote: Is there a compiler switch to catch this kind of error? ```d ulong v = 1; writeln(v > -1); ``` IMHO the compiler should bail a warning if it sees a logic comparison between signed and unsigned / different integer sizes. There is 50% cha

Re: Compiler switch for integer comparison/promotion to catch a simple error

2022-05-29 Thread matheus via Digitalmars-d-learn
On Sunday, 29 May 2022 at 01:35:23 UTC, frame wrote: Is there a compiler switch to catch this kind of error? ```d ulong v = 1; writeln(v > -1); ``` IMHO the compiler should bail a warning if it sees a logic comparison between signed and unsigned / different integer sizes. There is 50% cha

Compiler switch for integer comparison/promotion to catch a simple error

2022-05-28 Thread frame via Digitalmars-d-learn
Is there a compiler switch to catch this kind of error? ```d ulong v = 1; writeln(v > -1); ``` IMHO the compiler should bail a warning if it sees a logic comparison between signed and unsigned / different integer sizes. There is 50% chance that a implicit conversion was not intended.

Re: Lexicographical object comparison by selected members of a struct

2021-08-21 Thread Tejas via Digitalmars-d-learn
On Saturday, 21 August 2021 at 13:45:59 UTC, Ali Çehreli wrote: On 8/21/21 1:31 AM, Tejas wrote: > I was more impressed that you found that hack in the first place I can't take credit. :) 'static foreach' had that difference since its inception. The spec says "If a new scope is desired for ea

Re: Lexicographical object comparison by selected members of a struct

2021-08-21 Thread Ali Çehreli via Digitalmars-d-learn
On 8/21/21 1:31 AM, Tejas wrote: > I was more impressed that you found that hack in the first place I can't take credit. :) 'static foreach' had that difference since its inception. The spec says "If a new scope is desired for each expansion, use another set of braces:" https://dlang.org/s

Re: Lexicographical object comparison by selected members of a struct

2021-08-21 Thread Tejas via Digitalmars-d-learn
On Saturday, 21 August 2021 at 06:58:47 UTC, Ali Çehreli wrote: On 8/20/21 11:19 PM, Tejas wrote: [...] Yes. 'static foreach' does not introduce scope, which can be pretty useful. For example, one can define functions at module scope. The subtle differences between 'static foreach' and '(

Re: Lexicographical object comparison by selected members of a struct

2021-08-21 Thread Ali Çehreli via Digitalmars-d-learn
On 8/20/21 11:19 PM, Tejas wrote: On Saturday, 21 August 2021 at 06:03:33 UTC, Ali Çehreli wrote: On 8/20/21 10:37 PM, Alexandru Ermicioi wrote: [...] Cool! Much better. :) I could not do [...] Did you use that double curly bracket in `static foreach` so that you don't get error for decl

Re: Lexicographical object comparison by selected members of a struct

2021-08-20 Thread Tejas via Digitalmars-d-learn
On Saturday, 21 August 2021 at 06:03:33 UTC, Ali Çehreli wrote: On 8/20/21 10:37 PM, Alexandru Ermicioi wrote: [...] Cool! Much better. :) I could not do [...] Did you use that double curly bracket in `static foreach` so that you don't get error for declaring stuff inside `static foreach`

Re: Lexicographical object comparison by selected members of a struct

2021-08-20 Thread Ali Çehreli via Digitalmars-d-learn
ypeof(this) that) const { // Comparison code per member, which would potentially return an // early 'false' result. static foreach (i; 0 .. members.length) {{ // mixin (makeCode(__traits(identifier, members[i]))); enum ident = __traits(identifier, members[i]);

Re: Lexicographical object comparison by selected members of a struct

2021-08-20 Thread Alexandru Ermicioi via Digitalmars-d-learn
fields for it and include only those fields and methods that have it into compariosn and hashing. Another problem that may arise is storage in local variable of fields before comparison, it might introduce unnecessary copy. Regards, Alexandru.

Re: Lexicographical object comparison by selected members of a struct

2021-08-20 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Saturday, 21 August 2021 at 05:34:59 UTC, Alexandru Ermicioi wrote: ... Also there is no need for mixing string code here. You can get the field using __traits(getMember, this, member).

Lexicographical object comparison by selected members of a struct

2021-08-20 Thread Ali Çehreli via Digitalmars-d-learn
Sometimes I need comparison operators that should consider only some members of a struct: struct S { int year; // Primary member int month;// Secondary member string[] values; // Irrelevant } I've been using the laziest tuple+tupleof solution in some of my st

Re: Testing for object property supporting "<" comparison

2021-05-11 Thread Chris Piker via Digitalmars-d-learn
On Wednesday, 12 May 2021 at 00:06:52 UTC, Paul Backus wrote: On Tuesday, 11 May 2021 at 19:42:34 UTC, Chris Piker wrote: std.traits.isOrderingComparable https://phobos.dpldocs.info/std.traits.isOrderingComparable.html Well I feel sheepish, don't know how I missed that one. Hey thanks for p

Re: Testing for object property supporting "<" comparison

2021-05-11 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 11 May 2021 at 19:42:34 UTC, Chris Piker wrote: My problem is that I don't know how to specify that properties must be comparable via "<". I took a look at the Traits module, but there are more things that are comparable then Arithmetic or Floating point types. Also, since the com

Testing for object property supporting "<" comparison

2021-05-11 Thread Chris Piker via Digitalmars-d-learn
Hi D I'm working on a bit of code that handles selecting one *.front from multiple range-ish objects. It's a select-or-drop algorithm for a data streaming service, the details aren't important. The algorithm takes a range of something I'll call "PriorityRange" objects. PriorityRange object

Re: Generic comparison

2020-11-10 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Tuesday, 10 November 2020 at 20:32:40 UTC, Paul Backus wrote: The compiler infers pure, @nogc, nothrow etc. for template functions automatically. It's actually better if you don't add them by hand. Ok, thanks :-).

Re: Generic comparison

2020-11-10 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 10 November 2020 at 20:32:40 UTC, Paul Backus wrote: alias isOrderingComparableWith(T, U) = __traits(compiles, (T t, U u) => t < u); My bad, should be `enum`, not `alias`.

Re: Generic comparison

2020-11-10 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 10 November 2020 at 17:19:09 UTC, Ola Fosheim Grøstad wrote: Interesting, so "auto ref T" is the go-to type specifier for generic code then? I guess I also should conditionally add things like pure, nogc, nothrow... I assume I would have to test the comparison o

Re: Generic comparison

2020-11-10 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
would have to test the comparison operator. I actually want to implement (low <= value) && (value < high) So I guess I need to test both. But how...? compiles-trait?

Re: Generic comparison

2020-11-10 Thread Paul Backus via Digitalmars-d-learn
f(x)" should only be evaluated once. Is it sufficient to use "scope ref" in parameters? I don't want to assume _anything_ about the definition of the types and the implementation of the comparison operator (can be overloaded). bool between(Value, Bound)(auto ref Value value, aut

Generic comparison

2020-11-10 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
ref" in parameters? I don't want to assume _anything_ about the definition of the types and the implementation of the comparison operator (can be overloaded).

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread frame via Digitalmars-d-learn
On Sunday, 8 November 2020 at 05:07:54 UTC, frame wrote: Something wrong with this library. Well, we need an edit function here. The library has no TCP_NODELAY flag enabled. This fix speeds it up to 10x and solves the 44ms problem to 400us https://github.com/mysql-d/mysql-native/pull/221/file

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread frame via Digitalmars-d-learn
On Saturday, 7 November 2020 at 12:29:46 UTC, Andre Pany wrote: Maybe mysql native is slower, but maybe this isn't the case, just the way performance measurements was done was incorrectly. I have tried mysql-native and it tooks way to long for simple things. My test setup is a remote linux vm

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread frame via Digitalmars-d-learn
On Saturday, 7 November 2020 at 16:37:22 UTC, Vino wrote: , Trying to improve the above code hence request your help on how to use array container instead of array, tried as per the example below but not working. I think you can just use a static array as ubyte[1024] if you want a fixed bu

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread Vino via Digitalmars-d-learn
On Saturday, 7 November 2020 at 15:26:48 UTC, frame wrote: On Saturday, 7 November 2020 at 14:57:39 UTC, Vino wrote: After further analysis we suspect that the issue is at the package std.net.curl the flow of the program is as below Establishing a new connection may vary in duration of +2

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread frame via Digitalmars-d-learn
On Saturday, 7 November 2020 at 14:57:39 UTC, Vino wrote: After further analysis we suspect that the issue is at the package std.net.curl the flow of the program is as below Establishing a new connection may vary in duration of +200ms or more.

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread Vino via Digitalmars-d-learn
On Saturday, 7 November 2020 at 14:38:35 UTC, Vino wrote: On Saturday, 7 November 2020 at 14:16:46 UTC, Vino wrote: On Saturday, 7 November 2020 at 12:29:46 UTC, Andre Pany wrote: On Friday, 6 November 2020 at 04:58:05 UTC, Vino wrote: [...] While doing performance measurements you should do

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread Vino via Digitalmars-d-learn
On Saturday, 7 November 2020 at 14:16:46 UTC, Vino wrote: On Saturday, 7 November 2020 at 12:29:46 UTC, Andre Pany wrote: On Friday, 6 November 2020 at 04:58:05 UTC, Vino wrote: [...] While doing performance measurements you should do each test multiple time (at least 5 times). There could b

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread Vino via Digitalmars-d-learn
On Saturday, 7 November 2020 at 12:29:46 UTC, Andre Pany wrote: On Friday, 6 November 2020 at 04:58:05 UTC, Vino wrote: [...] While doing performance measurements you should do each test multiple time (at least 5 times). There could be for example an effect that executing a db query the firs

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-07 Thread Andre Pany via Digitalmars-d-learn
On Friday, 6 November 2020 at 04:58:05 UTC, Vino wrote: Hi All, We recently tested the below components and the test results are as below, even though hunt-database is faster than mysql-native it is hard to use this package as it lacks on documentation, non of the example provided in the do

Re: Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-06 Thread frame via Digitalmars-d-learn
On Friday, 6 November 2020 at 04:58:05 UTC, Vino wrote: Component : mysql-native + asdf Executable size : 17 MB Execution time : 10 secs, 189 ms, 919 μs, and 3 hnsecs Component : hunt-database + asdf Executable size : 81 MB Execution time : 5 secs, 916 ms, 418 μs, and 3 hnsecs Interesting bu

Comparison : mysql-native + asdf and hunt-database + asdf

2020-11-05 Thread Vino via Digitalmars-d-learn
Hi All, We recently tested the below components and the test results are as below, even though hunt-database is faster than mysql-native it is hard to use this package as it lacks on documentation, non of the example provided in the documentation works, one has to go through the code and fi

Re: Does the default opEquals include padding in the comparison? If so, how can the problems that arise with C++ interoperabilty be solved?

2020-10-21 Thread Simon van Bernem via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 20:10:03 UTC, Paul Backus wrote: On Wednesday, 21 October 2020 at 19:23:43 UTC, Simon van Bernem wrote: The only explanation I can think of is that D memcmps the entire struct including the padding. Is this correct? If so, what can I do about this? Why doesn't

Re: Does the default opEquals include padding in the comparison? If so, how can the problems that arise with C++ interoperabilty be solved?

2020-10-21 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 19:23:43 UTC, Simon van Bernem wrote: The only explanation I can think of is that D memcmps the entire struct including the padding. Is this correct? If so, what can I do about this? Why doesn't the opEquals get modified appropriately even though the struct is

Does the default opEquals include padding in the comparison? If so, how can the problems that arise with C++ interoperabilty be solved?

2020-10-21 Thread Simon van Bernem via Digitalmars-d-learn
I ask this question because I chased a very very nasty bug all the way down, and I think I found the offender: I have a extern(C++) struct that contains an 8-byte integer followed by a 4-byte enum value. I came across two variables of that type, that are not equal by comparison (no opEquals

Re: Lexicographic comparison of arrays (of chars)

2020-08-02 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 22 January 2020 at 15:11:09 UTC, Jacob Carlborg wrote: BTW, why don't you implement `opCmp` with the built-in comparison operators. Those are going to get lower to a call to `__cmp`. Something like this: int opCmp()(const scope typeof(this) that) const @nogc { auto a =

Re: Overload comparison operators for "nullable" types

2020-08-01 Thread Oleg B via Digitalmars-d-learn
On Thursday, 30 July 2020 at 14:52:17 UTC, H. S. Teoh wrote: On Thu, Jul 30, 2020 at 01:41:05PM +, Oleg B via Digitalmars-d-learn wrote: [...] Logically we can compare versions, but what must return `opCmp` if one of versions has 'not comparible' state? [...] opCmp is allowed to return flo

Re: Overload comparison operators for "nullable" types

2020-07-30 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jul 30, 2020 at 01:41:05PM +, Oleg B via Digitalmars-d-learn wrote: [...] > Logically we can compare versions, but what must return `opCmp` if one of > versions has 'not comparible' state? [...] opCmp is allowed to return float; so you could return float.nan in this case. T -- "Rea

Overload comparison operators for "nullable" types

2020-07-30 Thread Oleg B via Digitalmars-d-learn
ugs if new user will use code (or old user forgot about this). Checking 'not comparible' state before `a <= b` is not intuitive too. `opBinary` can't overload comparison. We have same type of problem if wrap simple `double` into struct and try overload comparison (double.nan

Re: Beginner's Comparison Benchmark

2020-05-06 Thread p.shkadzko via Digitalmars-d-learn
On Tuesday, 5 May 2020 at 20:07:54 UTC, RegeleIONESCU wrote: [...] Python should be ruled out, this is not its war :) I have done benchmarks against NumPy if you are interested: https://github.com/tastyminerals/mir_benchmarks

Re: Beginner's Comparison Benchmark

2020-05-06 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, May 06, 2020 at 09:59:48AM +, welkam via Digitalmars-d-learn wrote: > On Tuesday, 5 May 2020 at 20:29:13 UTC, Steven Schveighoffer wrote: > > the optimizer recognizes what you are doing and changes your code > > to: > > > > writeln(1_000_000_001); > > > Oh yes a classic constant foldi

Re: Beginner's Comparison Benchmark

2020-05-06 Thread welkam via Digitalmars-d-learn
On Tuesday, 5 May 2020 at 20:29:13 UTC, Steven Schveighoffer wrote: the optimizer recognizes what you are doing and changes your code to: writeln(1_000_000_001); Oh yes a classic constant folding. The other thing to worry about is dead code elimination. Walter has a nice story where he sent

Re: Beginner's Comparison Benchmark

2020-05-05 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/5/20 4:07 PM, RegeleIONESCU wrote: Hello! I made a little test(counting to 1 billion by adding 1)to compare execution speed of a small counting for loop in C, D, Julia and Python. = The C version:

Beginner's Comparison Benchmark

2020-05-05 Thread RegeleIONESCU via Digitalmars-d-learn
Hello! I made a little test(counting to 1 billion by adding 1)to compare execution speed of a small counting for loop in C, D, Julia and Python. = The C version: |The D version: |The Julia versio

Re: Lexicographic comparison of arrays (of chars)

2020-01-22 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 22 January 2020 at 15:11:09 UTC, Jacob Carlborg wrote: int opCmp()(const scope typeof(this) that) const @nogc { auto a = this[]; auto b = that[]; return a < b ? -1 : (a > b); } -- /Jacob Carlborg I see. Good to know. Thanks

Re: Lexicographic comparison of arrays (of chars)

2020-01-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, January 22, 2020 7:50:01 AM MST Per Nordlöw via Digitalmars-d- learn wrote: > On Wednesday, 22 January 2020 at 10:19:38 UTC, Jacob Carlborg > > wrote: > > That looks like it's for internal use. There is a `compare` > > method in the `TypeInfo` of each type. > > Will that incur an extr

Re: Lexicographic comparison of arrays (of chars)

2020-01-22 Thread Jacob Carlborg via Digitalmars-d-learn
thod calls involved. Seems to be one of the initial call to `compare` and then one for each element of the array. BTW, why don't you implement `opCmp` with the built-in comparison operators. Those are going to get lower to a call to `__cmp`. Something like this: int opCmp()(const sc

Re: Lexicographic comparison of arrays (of chars)

2020-01-22 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 22 January 2020 at 10:19:38 UTC, Jacob Carlborg wrote: That looks like it's for internal use. There is a `compare` method in the `TypeInfo` of each type. Will that incur an extra runtime cost compared to __cmp?

Re: Lexicographic comparison of arrays (of chars)

2020-01-22 Thread Jacob Carlborg via Digitalmars-d-learn
On Wednesday, 22 January 2020 at 08:44:15 UTC, Per Nordlöw wrote: I just found import core.internal.array.comparison : __cmp; I presume that is a better alternative if Phobos' independence is desired. That looks like it's for internal use. There is a `compare` method in the `TypeInfo`

Re: Lexicographic comparison of arrays (of chars)

2020-01-22 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 22 January 2020 at 08:30:55 UTC, Per Nordlöw wrote: is a suitable definition of `opCmp` for `SSOString` in terms of independence of Phobos' `std.algorithm.comparison.cmp`. I just found import core.internal.array.comparison : __cmp; I presume that is a better alternative if P

Lexicographic comparison of arrays (of chars)

2020-01-22 Thread Per Nordlöw via Digitalmars-d-learn
I've implement a small size optimized `string` at https://github.com/nordlow/phobos-next/blob/c35fa4052738af0cd7ad39a9fa715b5ec29c7bba/src/nxt/sso_string.d I'm now wondering whether or not its definition of comparison at https://github.com/nordlow/phobos

Re: wstring comparison is failing

2019-09-24 Thread Vladimir Panteleev via Digitalmars-d-learn
On Tuesday, 24 September 2019 at 21:40:47 UTC, Brett wrote: The only issue is that buggy dynamic code can result if someone compares the two and it will fail silently. But, you don't know if the static array actually contains a null-terminated string (in which case the comparison is a bu

Re: wstring comparison is failing

2019-09-24 Thread Brett via Digitalmars-d-learn
On Tuesday, 24 September 2019 at 00:29:05 UTC, Vladimir Panteleev wrote: On Monday, 23 September 2019 at 23:22:14 UTC, Brett wrote: I guess you are probably right... I was thinking that it would compare up to a null terminator. Seems kinda buggy... maybe the compiler needs to give a warning? Af

Re: wstring comparison is failing

2019-09-23 Thread Vladimir Panteleev via Digitalmars-d-learn
On Monday, 23 September 2019 at 23:22:14 UTC, Brett wrote: I guess you are probably right... I was thinking that it would compare up to a null terminator. Seems kinda buggy... maybe the compiler needs to give a warning? After all, compared a fixed size array with a dynamic array then will almos

Re: wstring comparison is failing

2019-09-23 Thread Jonathan M Davis via Digitalmars-d-learn
thing is that szExeFile is a static > >> wchar array... which shouldn't effect the comparison. > > > > are you sure about that? > > > > with a static size, the lengths of the two sides won't match... > > I guess you are probably right... I was thi

Re: wstring comparison is failing

2019-09-23 Thread Brett via Digitalmars-d-learn
On Monday, 23 September 2019 at 20:45:00 UTC, destructionator wrote: On Mon, Sep 23, 2019 at 08:38:03PM +, Brett via Digitalmars-d-learn wrote: The only thing is that szExeFile is a static wchar array... which shouldn't effect the comparison. are you sure about that? with a static

Re: wstring comparison is failing

2019-09-23 Thread Vladimir Panteleev via Digitalmars-d-learn
array... which shouldn't effect the comparison. If you're comparing a static array, you're also comparing the bytes after the NUL character. Both of those conversions will give you a wstring with the length of the entire array, regardless of and including any NUL characters, so t

Re: wstring comparison is failing

2019-09-23 Thread Adam D. Ruppe via Digitalmars-d-learn
On Mon, Sep 23, 2019 at 08:38:03PM +, Brett via Digitalmars-d-learn wrote: > The only thing is that szExeFile is a static > wchar array... which shouldn't effect the comparison. are you sure about that? with a static size, the lengths of the two sides won't match...

wstring comparison is failing

2019-09-23 Thread Brett via Digitalmars-d-learn
cast(wstring)entry.szExeFile == Name to!wstring(entry.szExeFile) == Name These all fail. The strings are clearly the same. I can compare char by char and it works. latest D 2.088. The only thing is that szExeFile is a static wchar array... which shouldn't effect the comparison.

Re: == comparison of string literals, and their usage

2019-04-07 Thread diniz via Digitalmars-d-learn
pare the pointers and be done, just make sure to document how it operates. Depending on the circumstances I'd throw in some asserts that do actual strings comparison to verify the program logic. To add onto this. Here is an example why it's important to compare the length as well:    

Re: == comparison of string literals, and their usage

2019-04-07 Thread bauss via Digitalmars-d-learn
ow it operates. Depending on the circumstances I'd throw in some asserts that do actual strings comparison to verify the program logic. To add onto this. Here is an example why it's important to compare the length as well: string a = "hello"; string b = a[0 .. 3]; assert(a.ptr == b.ptr); assert(a.length != b.length);

Re: == comparison of string literals, and their usage

2019-04-06 Thread diniz via Digitalmars-d-learn
ow it operates. Depending on the circumstances I'd throw in some asserts that do actual strings comparison to verify the program logic. Thank you very much! And yes, properly documenting is also important to me. -- diniz {la vita e estranj}

Re: == comparison of string literals, and their usage

2019-04-06 Thread lithium iodate via Digitalmars-d-learn
e array references as in string a = "hello"; string b = a; assert(a is b); assert(a[] is b[]); Of course, if the strings are never sliced, you can just compare the pointers and be done, just make sure to document how it operates. Depending on the circumstances I'd throw in some asserts that do actual strings comparison to verify the program logic.

Re: == comparison of string literals, and their usage

2019-04-06 Thread diniz via Digitalmars-d-learn
Le 06/04/2019 à 16:07, AltFunction1 via Digitalmars-d-learn a écrit : On Friday, 5 April 2019 at 14:49:50 UTC, diniz wrote: Hello, Since literal strings are interned (and immutable), can I count on the fact that they are compared (==) by pointer? No. "==" performs a full array

Re: == comparison of string literals, and their usage

2019-04-06 Thread AltFunction1 via Digitalmars-d-learn
On Friday, 5 April 2019 at 14:49:50 UTC, diniz wrote: Hello, Since literal strings are interned (and immutable), can I count on the fact that they are compared (==) by pointer? No. "==" performs a full array comparison and "is" is apparently simplified at compile t

== comparison of string literals, and their usage

2019-04-05 Thread diniz via Digitalmars-d-learn
Hello, Since literal strings are interned (and immutable), can I count on the fact that they are compared (==) by pointer? Context: The use case is a custom lexer for a custom language. I initially wanted to represent lexeme classes by a big enum 'LexClass'. However, this makes me write 3 ti

Re: Filter out consecutive elements based on comparison between adiacent two

2019-01-31 Thread Langer via Digitalmars-d-learn
On Thursday, 31 January 2019 at 13:25:21 UTC, Alex wrote: On Thursday, 31 January 2019 at 13:18:39 UTC, Langer wrote: Hi all, Ditto... something like: filter!((a,b) => a.foo != b.foo) Thank you! but... why not ´´´ arr.uniq ´´´ thank you, uniq is perfect.

Re: Filter out consecutive elements based on comparison between adiacent two

2019-01-31 Thread Alex via Digitalmars-d-learn
On Thursday, 31 January 2019 at 13:18:39 UTC, Langer wrote: Hi all, Ditto... something like: filter!((a,b) => a.foo != b.foo) Thank you! but... why not ´´´ arr.uniq ´´´

Re: Filter out consecutive elements based on comparison between adiacent two

2019-01-31 Thread Alex via Digitalmars-d-learn
On Thursday, 31 January 2019 at 13:18:39 UTC, Langer wrote: Hi all, Ditto... something like: filter!((a,b) => a.foo != b.foo) Thank you! I would say: ´´´ arr.group.map!(el => el[0]) ´´´

  1   2   3   >