C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Manifold comparison operator

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Wed, 27 Sep 2023 11:25:49 +0200
wt., 26 wrz 2023 o 17:09 Lénárd Szolnoki via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> On Tue, 2023-09-26 at 12:29 +0200, Fabio Alemagna via Std-Proposals
> wrote:
> > Il giorno mar 26 set 2023 alle ore 11:13 Bjorn Reese
> > <breese_at_[hidden]> ha scritto:
> > >
> > > On 9/24/23 16:05, Fabio Alemagna via Std-Proposals wrote:
> > >
> > > > It seems to be working fine, but comments and corrections are
> > > > greatly welcome.
> > >
> > > Here is a simplification that uses std::apply:
> > >
> > > https://godbolt.org/z/9bn4jn5v7
> >
> > That looks indeed neater at some levels, but you did away with the
> > type check, which renders the construct not able to deal with a
> > heterogeneous set of value types any longer.
> >
> > However, meanwhile I came up with a revised version that uses
> > policies
> > and free functions rather than subclasses, to achieve the same syntax
> > but with a leaner and more composable implementation.
> >
> > https://godbolt.org/z/4Wv7rq4se
>
> I don't know how to interpret `all_of(a, b) < any_of(c, d)` at a
> glance.
>
> Is this "for every x in {a, b} exists y in {c, d} so that x < y", or is
> it "there exists a y in {c, d} so that for every x in {a, b} x < y".
>
> I think these two are probably equivalent if a,b,c,d have a total
> order, otherwise I don't think they necessarily are.
>
> For the partial order where a < c and b < d are true and other
> comparisons are false the first statement is true and the second
> statement is false.
>
> all_of(a,b) == any_of(a,b) is a similar example.
>

Better question is what is result of:
```
one_of(1, 2) == one_of(1, 1, 2)
```
I would expect that is should be symmetrical but on grouping of
what is consider "one" then it can give `false` or `true`:
```
1 == one_of(1, 1, 2) // false
2 == one_of(1, 1, 2) // true
// -> true
```
vs
```
one_of(1, 2) == 1 //true
one_of(1, 2) == 1 //true
one_of(1, 2) == 2 //true
// -> false
```

I think in this case the correct answer should be always `false`.
We should consider this as a matrix of comparison results, and each
`one_of`, `any_of`, `all_of`, `none_of` define how we group results.
And some groupings have bigger precedence,
like for `a * b + c * d` we first calculate both `*` then `+`
in case of `a * b * c * d` order for natural numbers does not matter.
I think this priority should be:
```
all_of
none_of
any_of
one_of
```
If both sides are the same operator then we treat all as one group.
e.g. for
```
//`any_of(a,b,c) op all_of(d,e,f)` same as `all_of(a,b,c) op any_of(d,e,f)`
((a op d) && (a op e) && (a op f)) || ((b op d) && (b op e) && (b op
f)) || ((c op d) && (c op e) && (c op f))

//`any_of((a,b,c) op any_of(d,e,f)`
(a op d) || (a op e) || (a op f) || (b op d) || (b op e) || (b op f)
|| (c op d) || (c op e) || (c op f)
```



BTW, aside of better `if` this function interact greatly with `std::find` like:
```
std::find(b, e, any_of(1, 2));
```
Or with itself:
```
foo > any_of(one_of(1, 2), one_of(4, 5)) //-> foo is in [1,2) or [4,5) range
```


> Cheers,
> Lénárd
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-09-27 09:26:01