Re: What's the default implementation of opCmp/opEquals for structs?

2021-03-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/3/21 1:37 PM, Jack wrote: the one that the compiler defaults to when you don't provide one? There's no default opCmp. For opEquals, it's a memberwise comparison. -Steve

What's the default implementation of opCmp/opEquals for structs?

2021-03-03 Thread Jack via Digitalmars-d-learn
the one that the compiler defaults to when you don't provide one?

Re: implementing default opCmp

2020-11-19 Thread Ali Çehreli via Digitalmars-d-learn
On 11/19/20 6:12 AM, Steven Schveighoffer wrote: On 11/18/20 6:06 PM, ag0aep6g wrote: int opCmp(S other) { import std.typecons: tuple; return tuple(this.tupleof).opCmp(tuple(other.tupleof)); } Ah, excellent solution! I hadn't thought of that. -Steve

Re: implementing default opCmp

2020-11-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/18/20 6:06 PM, ag0aep6g wrote: On Wednesday, 18 November 2020 at 22:29:17 UTC, Steven Schveighoffer wrote: How do I do something really simple for opCmp? I tried this it didn't work: return this == other ? 0 :     this.tupleof < other.tupleof ? -1 : 1; std.typecons.Tuple has opCmp.

Re: implementing default opCmp

2020-11-19 Thread Steven Schveighoffer via Digitalmars-d-learn
opCmp. While this is useful for the compiler, there's no default I know of that is an easy one-liner. Here's a stab at a totally generic version that I haven't unit tested at all, except to verify that it works for your example struct S: auto cmp(T, U)(auto ref T lhs, auto ref U rhs

Re: implementing default opCmp

2020-11-18 Thread ag0aep6g via Digitalmars-d-learn
On Wednesday, 18 November 2020 at 22:29:17 UTC, Steven Schveighoffer wrote: How do I do something really simple for opCmp? I tried this it didn't work: return this == other ? 0 : this.tupleof < other.tupleof ? -1 : 1; std.typecons.Tuple has opCmp. So this works: int opCmp(S ot

Re: implementing default opCmp

2020-11-18 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 18 November 2020 at 22:29:17 UTC, Steven Schveighoffer wrote: I have a struct like this: struct S { int x; int y; } and I want a default comparison. The problem is, that comparison doesn't have a default, and requires I implement opCmp. While this is useful

implementing default opCmp

2020-11-18 Thread Steven Schveighoffer via Digitalmars-d-learn
I have a struct like this: struct S { int x; int y; } and I want a default comparison. The problem is, that comparison doesn't have a default, and requires I implement opCmp. While this is useful for the compiler, there's no default I know of that is an easy one-liner. The truth

Re: opCmp with and without const

2019-12-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, December 6, 2019 12:03:45 AM MST berni44 via Digitalmars-d-learn wrote: > In std.typecons, in Tuple there are two opCmp functions, that are > almost identical; they only differ by one being const and the > other not: > > int opCmp(R)(R rhs) > if (a

Re: opCmp with and without const

2019-12-06 Thread Basile B. via Digitalmars-d-learn
On Friday, 6 December 2019 at 07:03:45 UTC, berni44 wrote: In std.typecons, in Tuple there are two opCmp functions, that are almost identical; they only differ by one being const and the other not: int opCmp(R)(R rhs) if (areCompatibleTuples!(typeof(this), R

Re: opCmp with and without const

2019-12-05 Thread Paul Backus via Digitalmars-d-learn
a template this parameter [1] to have a new copy of opCmp generated for each qualified version of Tuple it's called with: int opCmp(R, this This)(R rhs) const if (areCompatibleTuples!(typeof(this), R, "<")) { ... } This may lead to binary bloat, though, since you can pote

opCmp with and without const

2019-12-05 Thread berni44 via Digitalmars-d-learn
In std.typecons, in Tuple there are two opCmp functions, that are almost identical; they only differ by one being const and the other not: int opCmp(R)(R rhs) if (areCompatibleTuples!(typeof(this), R, "<")) { static foreach (i; 0 ..

Re: opCmp with double values

2017-12-24 Thread drug via Digitalmars-d-learn
values are sufficiently larger than that hard-coded value. I think feqrel() will be useful there:    https://dlang.org/phobos/std_math.html#.feqrel Ali I'd do something like that (disclaimer - from memory): ```     int opCmp( T rhs ) const {     auto diff = curVal - rhs; auto epsilon

Re: opCmp with double values

2017-12-24 Thread drug via Digitalmars-d-learn
-coded value. I think feqrel() will be useful there:   https://dlang.org/phobos/std_math.html#.feqrel Ali I'd do something like that (disclaimer - from memory): ``` int opCmp( T rhs ) const { auto diff = curVal - rhs; auto epsilon = max(curVal.epsilon, rhs.e

Re: opCmp with double values

2017-12-24 Thread Ali Çehreli via Digitalmars-d-learn
On 12/24/2017 02:10 AM, kerdemdemir wrote: > if ( fabs(diff) < 0.0001 ) I can't answer your question but I know that the comparison above is wrong because it can be meaningful only if the values are sufficiently larger than that hard-coded value. I think feqrel() will be

opCmp with double values

2017-12-24 Thread kerdemdemir via Digitalmars-d-learn
In documentation and forums I found some example for overloading opCmp for int values. But I couldn't see any examples for double values. That is what I come up with my own: struct AdjustableVal ( T = double ) { this ( T initVal ) { curVal = initVal

Re: How to implement opCmp?

2017-06-13 Thread Patrick Schluter via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 16:49:14 UTC, H. S. Teoh wrote: On Tue, Jun 13, 2017 at 10:51:40AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] I think Andrei has a nice way to do opCmp for integers that's a simple subtraction and negation or something like

Re: How to implement opCmp?

2017-06-13 Thread Honey via Digitalmars-d-learn
s about the subject. I have to sit down and write it down. I agree. It's a thing also that can be optimized in unintuitive ways. I think Andrei has a nice way to do opCmp for integers that's a simple subtraction and negation or something like that. I observed that compiler optimizers are prett

Re: How to implement opCmp?

2017-06-13 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jun 13, 2017 at 10:51:40AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...] > I think Andrei has a nice way to do opCmp for integers that's a simple > subtraction and negation or something like that. [...] In theory, cmp(int x, int y) can be implemented simply as

Re: How to implement opCmp?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/11/17 11:24 AM, Honey wrote: On Friday, 9 June 2017 at 17:50:28 UTC, Honey wrote: Looking at the implementation of Tuple.opCmp, I'm not sure I'd bet on existence of opCmp for fundamental types: int opCmp(R)(R rhs) if (areCompatibleTuples!(typeof(this), R

Re: How to implement opCmp?

2017-06-11 Thread Honey via Digitalmars-d-learn
On Sunday, 11 June 2017 at 15:40:42 UTC, Honey wrote: On Sunday, 11 June 2017 at 15:24:30 UTC, Honey wrote: Doesn't it make sense to introduce another overload of cmp similar to Steve's doCmp [2] right at that spot? Moreover, it seems that std.algorithm.cmp should employ three way comparison

Re: How to implement opCmp?

2017-06-11 Thread Honey via Digitalmars-d-learn
On Sunday, 11 June 2017 at 15:24:30 UTC, Honey wrote: Doesn't it make sense to introduce another overload of cmp similar to Steve's doCmp [2] right at that spot? Moreover, it seems that std.algorithm.cmp should employ three way comparison as well. The current implementation int cmp(alias

Re: How to implement opCmp?

2017-06-11 Thread Honey via Digitalmars-d-learn
On Friday, 9 June 2017 at 17:50:28 UTC, Honey wrote: Looking at the implementation of Tuple.opCmp, I'm not sure I'd bet on existence of opCmp for fundamental types: int opCmp(R)(R rhs) if (areCompatibleTuples!(typeof(this), R, "<")) { forea

Re: How to implement opCmp?

2017-06-09 Thread Honey via Digitalmars-d-learn
On Friday, 9 June 2017 at 17:28:18 UTC, Steven Schveighoffer wrote: This is why I think such a function should exist in Phobos/druntime. I'm not 100% sure it doesn't already, but I couldn't find one... Looking at the implementation of Tuple.opCmp, I'm not sure I'd bet on existence of opCmp

Re: How to implement opCmp?

2017-06-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/9/17 11:33 AM, Honey wrote: Hi Steve! On Friday, 9 June 2017 at 15:12:42 UTC, Steven Schveighoffer wrote: If I were to write it, it would be something like: int doCmp(T)(auto ref T t1, auto ref T t2) { static if(is(typeof(t1.opCmp(t2 return t1.opCmp(t2); else

Re: How to implement opCmp?

2017-06-09 Thread drug via Digitalmars-d-learn
I re-read thoroughly and got it)

Re: How to implement opCmp?

2017-06-09 Thread drug via Digitalmars-d-learn
09.06.2017 18:12, Steven Schveighoffer пишет: int doCmp(T)(auto ref T t1, auto ref T t2) { static if(is(typeof(t1.opCmp(t2 return t1.opCmp(t2); else { if(t1 < t2) return -1; else if(t1 > t2) return 1; return 0; } } Isn't it enough to use just

Re: How to implement opCmp?

2017-06-09 Thread Honey via Digitalmars-d-learn
owever, this is less performant if the type defines opCmp (you are calling it twice). Calling opCmp twice on the same data is exactly what I tried to avoid. There really ought to be an opCmp for any type, which does the best thing available. I'm not sure if it exists. I agree it should

Re: How to implement opCmp?

2017-06-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/9/17 10:53 AM, Honey wrote: Hi everyone! Given struct Pair(T, U = T) { T f; U s; } what is the intended way to genrically implement opCmp for this struct? The naive approach struct Pair(T, U = T) { // [...] int opCmp(const Pair r) const { immutable c = f.opCmp(r.f

How to implement opCmp?

2017-06-09 Thread Honey via Digitalmars-d-learn
Hi everyone! Given struct Pair(T, U = T) { T f; U s; } what is the intended way to genrically implement opCmp for this struct? The naive approach struct Pair(T, U = T) { // [...] int opCmp(const Pair r) const { immutable c = f.opCmp(r.f); return c != 0 ? c : s.opCmp(r.s

Re: opCmp with structs

2015-11-10 Thread anonymous via Digitalmars-d-learn
On 07.11.2015 16:59, anonymous wrote: Wat. It even compiles with @safe. That's not good. Filed an issue: https://issues.dlang.org/show_bug.cgi?id=15315

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
On Saturday, 7 November 2015 at 14:36:25 UTC, Mike Parker wrote: So my general question is: why immutable variables shouldn't be able to be moved (inside an array)? To be pedantic, sort isn't actually moving anything. It's reassigning elements, i.e. a[1] = a[2]. The immutable member makes

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
On Saturday, 7 November 2015 at 00:19:56 UTC, Ali Çehreli wrote: Continuing from your hint: So, opCmp works but it is sort() that cannot move objects of ku around because of that immutable variable. So my general question is: why immutable variables shouldn't be able to be moved (inside

Re: opCmp with structs

2015-11-07 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 7 November 2015 at 11:48:56 UTC, Alex wrote: So my general question is: why immutable variables shouldn't be able to be moved (inside an array)? To be pedantic, sort isn't actually moving anything. It's reassigning elements, i.e. a[1] = a[2]. The immutable member makes it

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
Ok, ok... I see the direction, but I still miss something: From the point of view of "Programming in D", chapter 33.3, "Immutability of the slice vs the elements": I don't want to have an immutable slice but immutable elements. And your answers imply that sorting not only modify the slice

Re: opCmp with structs

2015-11-07 Thread BBaz via Digitalmars-d-learn
, here when sort() will have to move it'll move the reference: --- import std.stdio; import std.algorithm; struct ku { immutable int id; alias id this; this(int i) { id = i; } int opCmp(ref const ku rhs) const

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
On Saturday, 7 November 2015 at 10:24:03 UTC, BBaz wrote: void main() { ku*[] tt = [new ku(2),new ku(1)]; sort(tt); } Don't really like this ;) not because writeln(tt); doesn't work any more, but because I have to think about where to use pointers and where not...

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
Ok... found the solution. The technical at least. import std.algorithm; import std.range; void main(){ ku[] tt = [ku(2), ku(1)]; //writeln(tt); auto index3 = new size_t[tt.length]; makeIndex!("a.id < b.id")(tt, index3); auto ind = indexed(tt, index3);

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
Found something useful, here: http://dlang.org/phobos/std_algorithm_sorting.html#makeIndex with that I can achieve the following void main(){ ku[] tt = [ku(2), ku(1)]; //writeln(tt); auto index3 = new size_t[tt.length]; makeIndex!("a.id < b.id")(tt, index3);

Re: opCmp with structs

2015-11-07 Thread anonymous via Digitalmars-d-learn
On 07.11.2015 15:36, Mike Parker wrote: It's actually possible to use move one instance into another, though: void main() { import std.algorithm : move; ku k1 = ku(1); ku k2 = ku(2); k2.move(k1); assert(k1.id == 2); } Wat. It even compiles with @safe. That's not good.

Re: opCmp with structs

2015-11-06 Thread BBaz via Digitalmars-d-learn
On Friday, 6 November 2015 at 22:55:15 UTC, Alex wrote: Ok... the question is not silly any more... without 'immutable' it works. So, what am I missing? sorry, again a forum bug that stripped my answer: sort() fails because in the template constraint `hasAssignableElements` fails.

Re: opCmp with structs

2015-11-06 Thread BBaz via Digitalmars-d-learn
On Friday, 6 November 2015 at 22:55:15 UTC, Alex wrote: Ok... the question is not silly any more... without 'immutable' it works. So, what am I missing? sort() fails because in the template constraint `hasAssignableElements

Re: opCmp with structs

2015-11-06 Thread Ali Çehreli via Digitalmars-d-learn
On 11/06/2015 02:54 PM, Alex wrote: > I'm sure I'm doing a silly mistake somewhere, but why this doesn't work? > import std.stdio; > import std.algorithm; > > struct ku > { > immutable int id; > alias id this; > > this(int i) > { >

opCmp with structs

2015-11-06 Thread Alex via Digitalmars-d-learn
I'm sure I'm doing a silly mistake somewhere, but why this doesn't work? import std.stdio; import std.algorithm; struct ku { immutable int id; alias id this; this(int i) { id = i; } int opCmp(ref const ku rhs

Re: opCmp with structs

2015-11-06 Thread Alex via Digitalmars-d-learn
Ok... the question is not silly any more... without 'immutable' it works. So, what am I missing?

Re: On opCmp

2015-03-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 3/2/15 8:52 AM, Nordlöw wrote: On Friday, 27 February 2015 at 15:00:35 UTC, Steven Schveighoffer wrote: Hm... what about: return count rhs.count ? -1 : count rhs.count ? 1 : rank rhs.rank ? -1 : rank rhs.rank; Is this more efficient than my version? You said more compact, not more

Re: On opCmp

2015-03-02 Thread Nordlöw
On Friday, 27 February 2015 at 15:00:35 UTC, Steven Schveighoffer wrote: Hm... what about: return count rhs.count ? -1 : count rhs.count ? 1 : rank rhs.rank ? -1 : rank rhs.rank; Is this more efficient than my version?

Re: On opCmp

2015-02-27 Thread anonymous via Digitalmars-d-learn
On Friday, 27 February 2015 at 11:04:51 UTC, Nordlöw wrote: Is there a more compact way to describe the opCmp function in the following struct struct Hit { size_t count; // number of walkers that found this node NWeight rank; // rank (either minimum distance or maximum strength

Re: On opCmp

2015-02-27 Thread Steven Schveighoffer via Digitalmars-d-learn
On 2/27/15 6:04 AM, Nordlöw wrote: Is there a more compact way to describe the opCmp function in the following struct struct Hit { size_t count; // number of walkers that found this node NWeight rank; // rank (either minimum distance or maximum strength) auto opCmp(const Hit rhs

Re: On opCmp

2015-02-27 Thread Ali Çehreli via Digitalmars-d-learn
On 02/27/2015 03:04 AM, Nordlöw wrote: Is there a more compact way to describe the opCmp function in the following struct Please see: http://forum.dlang.org/thread/lnr99a$vvd$1...@digitalmars.com#post-lnr99a:24vvd:241:40digitalmars.com Ali

On opCmp

2015-02-27 Thread Nordlöw
Is there a more compact way to describe the opCmp function in the following struct struct Hit { size_t count; // number of walkers that found this node NWeight rank; // rank (either minimum distance or maximum strength) auto opCmp(const Hit rhs) const

Can't understand how to compare DateTime with opCmp

2015-02-01 Thread Suliman via Digitalmars-d-learn
I need to compare to DateTime. I looked at docs and found opCmp for DateTime type. The problem is that I can't understand how to use it. http://dlang.org/phobos/std_datetime.html#DateTime opCmp(in DateTime rhs); what is rhs? I am trying to do something like this: if( DateTime.opCmp(dtindb

Re: Can't understand how to compare DateTime with opCmp

2015-02-01 Thread Suliman via Digitalmars-d-learn
+ n.days solved my problem.

Re: Can't understand how to compare DateTime with opCmp

2015-02-01 Thread Rene Zwanenburg via Digitalmars-d-learn
On Sunday, 1 February 2015 at 15:04:39 UTC, Suliman wrote: I need to compare to DateTime. I looked at docs and found opCmp for DateTime type. The problem is that I can't understand how to use it. http://dlang.org/phobos/std_datetime.html#DateTime opCmp(in DateTime rhs); what is rhs? I am

Re: Can't understand how to compare DateTime with opCmp

2015-02-01 Thread FG via Digitalmars-d-learn
On 2015-02-01 at 16:04, Suliman wrote: opCmp(in DateTime rhs); what is rhs? RHS is probably short of right hand side, ie. the argument on the right side of the operator in a binary operator expression. In `a b` it would be b. I am trying to do something like this: if( DateTime.opCmp

Re: Can't understand how to compare DateTime with opCmp

2015-02-01 Thread Suliman via Digitalmars-d-learn
Thanks! Could anybody say how can I use roll if I need to date to date. For example I need to plus: DateTime currentdt = cast(DateTime)(Clock.currTime()); with another DateTime var foo.

Re: Can't understand how to compare DateTime with opCmp

2015-02-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, February 01, 2015 19:22:40 Suliman via Digitalmars-d-learn wrote: + n.days solved my problem. Roll is specifically for cases where you want one of the fields to increase or decrease without affecting the others (e.g. if you had a spin control in your GUI with a DateTime backing it and

Re: opCmp and unittest as a nested function

2014-04-09 Thread bearophile
Alexandr Druzhinin: http://dpaste.dzfl.pl/f7364d416cb2 Error appeared when I defined opCmp and now I can't understand what's the reason of the error. Why does tween need access to unittest? Try to use static struct instead of a struct. Bye, bearophile

Re: opCmp and unittest as a nested function

2014-04-09 Thread bearophile
/ 3.0, 0)); } opEquals/onHash/opCmp are minefields. I don't know why the D compiler doesn't add a large amount of errors and warnings to turns this minefield in something a bit safer. Also use std.math.feqrel to compare floating point values. Bye, bearophile

Re: opCmp and unittest as a nested function

2014-04-09 Thread Alexandr Druzhinin
09.04.2014 13:45, bearophile пишет: Alexandr Druzhinin: http://dpaste.dzfl.pl/f7364d416cb2 Error appeared when I defined opCmp and now I can't understand what's the reason of the error. Why does tween need access to unittest? Try to use static struct instead of a struct. Bye, bearophile

Re: opCmp and unittest as a nested function

2014-04-09 Thread Alexandr Druzhinin
); assert(tween(p2d0, p2d1, 1 / 3.0) == Point2D(1 / 3.0, 0)); } opEquals/onHash/opCmp are minefields. I don't know why the D compiler doesn't add a large amount of errors and warnings to turns this minefield in something a bit safer. It would be very nice. Also use std.math.feqrel to compare

Re: opCmp and unittest as a nested function

2014-04-09 Thread bearophile
Alexandr Druzhinin: Thank you! It works. But where can I read about this issue? Reading about this issue is not good. What you can read about is how unittests are implemented in D (as functions) and what's the difference between static structs and nonstatic ones when they are defined

Re: opCmp and unittest as a nested function

2014-04-09 Thread Alexandr Druzhinin
09.04.2014 14:13, bearophile пишет: Reading about this issue is not good. What you can read about is how unittests are implemented in D (as functions) and what's the difference between static structs and nonstatic ones when they are defined inside a function. Bye, bearophile I guess I should

Re: opCmp and unittest as a nested function

2014-04-09 Thread bearophile
Alexandr Druzhinin: I guess I should read how tepmlates work. No templates are involved in this code. Bye, bearophile

Re: opCmp and unittest as a nested function

2014-04-09 Thread Alexandr Druzhinin
09.04.2014 15:19, bearophile пишет: Alexandr Druzhinin: I guess I should read how tepmlates work. No templates are involved in this code. Bye, bearophile I mean that nested struct Point2D has additional pointer to frame and when compiler tries to instantiate template function 'tweet' it

What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread dnspies
To make a struct a valid key type, do I need to implement both opCmp and opEquals or just one or the other? It says on the page about associative arrays: The implementation may use either opEquals or opCmp or both. Does that mean it uses whichever one is user-defined (or both if they're both

Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread Jonathan M Davis
On Thursday, April 03, 2014 07:10:06 dnspies wrote: To make a struct a valid key type, do I need to implement both opCmp and opEquals or just one or the other? It says on the page about associative arrays: The implementation may use either opEquals or opCmp or both. Does that mean it uses

Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread monarch_dodra
On Thursday, 3 April 2014 at 10:15:46 UTC, Jonathan M Davis wrote: _Any_ type which overloads both opEquals and opCmp and does not make them match exactly is just plain broken. I disagree: If a.opEquals(b) is true, then a.opCmp(b) must be 0. If a.opCmp(b) is non-zero, then a.opEquals(b

Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread w0rp
On Thursday, 3 April 2014 at 10:42:33 UTC, monarch_dodra wrote: A correctly implemented AA would use opCmp to store objects in each bucket in cases of hash collisions, but still use opEqual in case of equivalence. I would add to that, try to use opCmp if it is available. It should

Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread dnspies
On Thursday, 3 April 2014 at 10:42:33 UTC, monarch_dodra wrote: On Thursday, 3 April 2014 at 10:15:46 UTC, Jonathan M Davis wrote: _Any_ type which overloads both opEquals and opCmp and does not make them match exactly is just plain broken. I disagree: If a.opEquals(b) is true

Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread Steven Schveighoffer
On Thu, 03 Apr 2014 06:42:32 -0400, monarch_dodra monarchdo...@gmail.com wrote: On Thursday, 3 April 2014 at 10:15:46 UTC, Jonathan M Davis wrote: _Any_ type which overloads both opEquals and opCmp and does not make them match exactly is just plain broken. I disagree: If a.opEquals(b

Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread monarch_dodra
On Thursday, 3 April 2014 at 16:47:05 UTC, Steven Schveighoffer wrote: On Thu, 03 Apr 2014 06:42:32 -0400, monarch_dodra monarchdo...@gmail.com wrote: On Thursday, 3 April 2014 at 10:15:46 UTC, Jonathan M Davis wrote: _Any_ type which overloads both opEquals and opCmp and does not make them

Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread Steven Schveighoffer
On Thu, 03 Apr 2014 14:45:40 -0400, monarch_dodra monarchdo...@gmail.com wrote: On Thursday, 3 April 2014 at 16:47:05 UTC, Steven Schveighoffer wrote: This can lead to false positives if opCmp(x, y) == 0 is assumed to mean equal. An example: if you used RBTree to store 2d points

Can opCmp return a 'long' instead of 'int'?

2014-02-16 Thread Saurabh Das
Hello, The call signature for opCmp in a struct is: struct S { int opCmp(ref const S s) const { ... } } int opCmp(ref const S s) const { return _val - s._val; } This works fine if _val is 'int'. However, if _val is 'long' then subtracting 2 longs may not result in an int - and therefore I

Re: Can opCmp return a 'long' instead of 'int'?

2014-02-16 Thread Timon Gehr
On 02/16/2014 02:59 PM, Saurabh Das wrote: This does compile and run correctly, but are there any hidden assumptions or requirements on the return value of opCmp that I should be aware of? Is there any reason that doing this may be not be wise? No, this is fine.

Re: Can opCmp return a 'long' instead of 'int'?

2014-02-16 Thread Timon Gehr
On 02/16/2014 04:13 PM, Timon Gehr wrote: On 02/16/2014 02:59 PM, Saurabh Das wrote: This does compile and run correctly, but are there any hidden assumptions or requirements on the return value of opCmp that I should be aware of? Is there any reason that doing this may be not be wise

Re: Can opCmp return a 'long' instead of 'int'?

2014-02-16 Thread bearophile
: struct Foo { int x, y; string s; int opCmp(in ref Foo r) { return cmpBuilder(x, r.x, y.abs, r.y.abs, s, r.s); } } Is this worth adding to Phobos? Bye, bearophile

Re: Can opCmp return a 'long' instead of 'int'?

2014-02-16 Thread Timon Gehr
, that are seen as pairs. Usage example: struct Foo { int x, y; string s; int opCmp(in ref Foo r) { return cmpBuilder(x, r.x, y.abs, r.y.abs, s, r.s); } } Is this worth adding to Phobos? Bye, bearophile IMO no (lots of repetition), but forwarding opCmp is: struct Foo{ int

Re: Can opCmp return a 'long' instead of 'int'?

2014-02-16 Thread Saurabh Das
On Sunday, 16 February 2014 at 15:19:08 UTC, Timon Gehr wrote: On 02/16/2014 04:13 PM, Timon Gehr wrote: On 02/16/2014 02:59 PM, Saurabh Das wrote: This does compile and run correctly, but are there any hidden assumptions or requirements on the return value of opCmp that I should be aware

Template constraints: opCmp and opUnary!++

2013-12-20 Thread Francesco Cattoglio
I'm trying to experiment a bit around the iota function. If I try to impose the following constraits: auto my_iota(B, E)(B begin, E end) if (is (typeof(++begin)) is (typeof(begin end))) {} Everything works as it should, but according to D Templates: A Tutorial book, you should not use

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Philippe Sigaud
fails to compile for both integers and my defined types. I read the D Templates: A Tutorial book and as far as I can tell ++B.init and B.init E.init doesn't look too much wrong, but I've not seen any constraint of this kind in phobos (using variables instead of types) so I was wondering if

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread monarch_dodra
On Friday, 20 December 2013 at 15:38:56 UTC, Francesco Cattoglio wrote: I'm trying to experiment a bit around the iota function. If I try to impose the following constraits: Everything works as it should, but according to D Templates: A Tutorial book, you should not use arguments in

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Timon Gehr
On 12/20/2013 05:40 PM, monarch_dodra wrote: That's normal, because T.init is not an lvalue. If you need an lvalue, we have `std.traits.lvalueOf!T` which you can use. is(typeof((T v){ /+ use v +/ })) I think this is a lot cleaner.

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Francesco Cattoglio
On Friday, 20 December 2013 at 16:40:23 UTC, monarch_dodra wrote: Everything works as it should, but according to D Templates: A Tutorial book, you should not use arguments in constraints. That's news to me. It seems strange to me too, but: page 69 on the PDF: Do not use argument in your

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Francesco Cattoglio
On Friday, 20 December 2013 at 17:18:01 UTC, Timon Gehr wrote: On 12/20/2013 05:40 PM, monarch_dodra wrote: That's normal, because T.init is not an lvalue. If you need an lvalue, we have `std.traits.lvalueOf!T` which you can use. is(typeof((T v){ /+ use v +/ })) I think this is a lot

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Philippe Sigaud
On Fri, Dec 20, 2013 at 6:33 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: Is there any difference between is(typeof(somecode)) and __traits(compiles, somecode)? I find the latter cleaner: its intent is more apparent. I use is(typeof()) only for really testing for type

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Philippe Sigaud
On Fri, Dec 20, 2013 at 6:31 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: On Friday, 20 December 2013 at 16:40:23 UTC, monarch_dodra wrote: Everything works as it should, but according to D Templates: A Tutorial book, you should not use arguments in constraints. That's news

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread monarch_dodra
On Friday, 20 December 2013 at 17:48:03 UTC, Philippe Sigaud wrote: On Fri, Dec 20, 2013 at 6:33 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: Is there any difference between is(typeof(somecode)) and __traits(compiles, somecode)? I find the latter cleaner: its intent is more

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread monarch_dodra
On Friday, 20 December 2013 at 17:18:01 UTC, Timon Gehr wrote: On 12/20/2013 05:40 PM, monarch_dodra wrote: That's normal, because T.init is not an lvalue. If you need an lvalue, we have `std.traits.lvalueOf!T` which you can use. is(typeof((T v){ /+ use v +/ })) I think this is a lot

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Timon Gehr
On 12/20/2013 09:42 PM, monarch_dodra wrote: On Friday, 20 December 2013 at 17:48:03 UTC, Philippe Sigaud wrote: On Fri, Dec 20, 2013 at 6:33 PM, Francesco Cattoglio francesco.cattog...@gmail.com wrote: Is there any difference between is(typeof(somecode)) and __traits(compiles, somecode)? I

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Timon Gehr
On 12/20/2013 10:57 PM, Timon Gehr wrote: Most non-trivial templates that use is(typeof(...)) in the constraint can be broken. (In the sense that it is possible to instantiate them even though their body does not compile.) Actually, it seems that the behaviour of DMD has changed in this

Re: Template constraints: opCmp and opUnary!++

2013-12-20 Thread Jakob Ovrum
On Friday, 20 December 2013 at 20:42:13 UTC, monarch_dodra wrote: AFAIK, there is no real difference, but is(typeof()) is more idiomatic in phobos. Most uses of `is(typeof())` were written before the `compiles` trait was introduced. Add the cargo cult to that, and it's no wonder that Phobos

Re: opCmp on a struct keyed by an array of bytes

2013-11-14 Thread Ali Çehreli
On 11/13/2013 07:46 PM, Charles Hixson wrote: On 11/12/2013 04:47 PM, bearophile wrote: Charles Hixson: I had tried return bytes.cmp(b.bytes); , but it didn't occur to me that the error meant I should have used a copy? Does this syntax mean that what's being compared is a dynamic array

Re: opCmp on a struct keyed by an array of bytes

2013-11-13 Thread Charles Hixson
On 11/12/2013 04:47 PM, bearophile wrote: Charles Hixson: I had tried return bytes.cmp(b.bytes); , but it didn't occur to me that the error meant I should have used a copy? Does this syntax mean that what's being compared is a dynamic array copy of the original static array? They are not

opCmp on a struct keyed by an array of bytes

2013-11-12 Thread Charles Hixson
Is there any better way to write the method than: (cmp doesn't seem to work, and byte arrays don't have an opCmp method.) int opCmp(ref const B24 b) const {for(int i = 0;i 24;i++) {if(bytes[i] b.bytes[i])return-1; if(bytes[i] b.bytes[i

Re: opCmp on a struct keyed by an array of bytes

2013-11-12 Thread Ali Çehreli
On 11/12/2013 01:06 PM, Charles Hixson wrote: Is there any better way to write the method than: (cmp doesn't seem to work, and byte arrays don't have an opCmp method.) int opCmp(ref const B24 b) const {for(int i = 0;i 24;i++) {if(bytes[i] b.bytes[i])return

Re: opCmp on a struct keyed by an array of bytes

2013-11-12 Thread bearophile
Ali Çehreli: int opCmp(ref const B24 b) const { return bytes[].cmp(b.bytes[]); Few months ago I have written a small rant about that. That little [] at the end of those arrays is not innocuous, it essentially throws away a very precious compile-time amount of information

Re: opCmp on a struct keyed by an array of bytes

2013-11-12 Thread Charles Hixson
On 11/12/2013 01:38 PM, Ali Çehreli wrote: On 11/12/2013 01:06 PM, Charles Hixson wrote: Is there any better way to write the method than: (cmp doesn't seem to work, and byte arrays don't have an opCmp method.) int opCmp(ref const B24 b) const {for(int i = 0;i 24;i

Re: opCmp on a struct keyed by an array of bytes

2013-11-12 Thread bearophile
Charles Hixson: I had tried return bytes.cmp(b.bytes); , but it didn't occur to me that the error meant I should have used a copy? Does this syntax mean that what's being compared is a dynamic array copy of the original static array? They are not copies, just slices. In case of doubts take

using opCmp

2013-07-17 Thread Jaehunt
Hi, I tried to compare two sets by opCmp, but I couldn't get correct outputs. Does anyone help me? I have same sets of set S1 = [1,2,3] and S2 = [1,2,3]. Output of S1S2 should be false because S1 is not a proper subset. However, S1=S2 should be true because S1 is a subset of S1. I can't

  1   2   >