Date: Sun, 14 Sep 2025 20:12:29 +0200
Thank you. This is a great example. I am not even sure what the correct set
intersection definition for weak ordering should be!
If an element is in the intersection if it there exist elements a in A and
b in B with equiv(a,b) == true, then the answer should be {1, 2, 3, 4}
If an element has to be "the same value" and not only equivalent, the
answer is {2,3}
In math we could define the intersection on "equivalence
<https://dict.leo.org/german-english/equivalence> class
<https://dict.leo.org/german-english/class>es[math. sic]", not helpful here
i believe.
Is it enough if we take one arbitrary represantant of "equivalence
<https://dict.leo.org/german-english/equivalence> class
<https://dict.leo.org/german-english/class>es[math. sic]", then {1}, {2},
{3} and {4} would be a valid answer.
set_intersection might be broken for weakly ordering.
Am So., 14. Sept. 2025 um 19:29 Uhr schrieb Ell <ell_se_at_[hidden]>:
> On 9/14/25 20:08, Peter Neiss wrote:
> > Ok. I have not thought about overlapping input ranges. Classical
> > aliasing problem. Modifying the first range at a later position, through
> > the second maybe makes the range unsorted. But when I try to construct
> > an example, the sorting seems to make this impossible and it still works
> > out correctly.
> > I am not familiar with weak comparators and when they are allowed in the
> > stl. Is sorting by a weak comparator enough for a range to be sorted ?
> > Please give an example that breaks with overlapping input ranges. If
> > this is a problem inplace_set_intersection is a good idea, otherwise i
> > would prefer changing the precondition on std::set_intersection.
> In the extreme case, you can consider all values equivalent:
>
> #include <array>
> #include <span>
> #include <algorithm>
> #include <print>
>
> int main () {
> std::array in = {1, 2, 3, 4};
>
> auto in1 = std::span (in).subspan (0, 3);
> auto in2 = std::span (in).subspan (1, 3);
>
> // all values are equivalent
> auto comp = [] (auto...) { return false; };
>
> auto test = [&] (const char* name, auto&& out) {
> std::ranges::set_intersection (
> in1, in2,
> out.begin (),
> comp
> );
>
> std::println ("{}: {}", name, out);
> };
>
> test ("out-of-place ", std::array<int, 3> {});
> test ("in-place (in1)", in1);
> test ("in-place (in2)", in2);
> }
>
> Output:
>
> out-of-place : [1, 2, 3]
> in-place (in1): [1, 2, 3]
> in-place (in2): [1, 1, 1]
>
> https://godbolt.org/z/xzKqq3nrT
>
intersection definition for weak ordering should be!
If an element is in the intersection if it there exist elements a in A and
b in B with equiv(a,b) == true, then the answer should be {1, 2, 3, 4}
If an element has to be "the same value" and not only equivalent, the
answer is {2,3}
In math we could define the intersection on "equivalence
<https://dict.leo.org/german-english/equivalence> class
<https://dict.leo.org/german-english/class>es[math. sic]", not helpful here
i believe.
Is it enough if we take one arbitrary represantant of "equivalence
<https://dict.leo.org/german-english/equivalence> class
<https://dict.leo.org/german-english/class>es[math. sic]", then {1}, {2},
{3} and {4} would be a valid answer.
set_intersection might be broken for weakly ordering.
Am So., 14. Sept. 2025 um 19:29 Uhr schrieb Ell <ell_se_at_[hidden]>:
> On 9/14/25 20:08, Peter Neiss wrote:
> > Ok. I have not thought about overlapping input ranges. Classical
> > aliasing problem. Modifying the first range at a later position, through
> > the second maybe makes the range unsorted. But when I try to construct
> > an example, the sorting seems to make this impossible and it still works
> > out correctly.
> > I am not familiar with weak comparators and when they are allowed in the
> > stl. Is sorting by a weak comparator enough for a range to be sorted ?
> > Please give an example that breaks with overlapping input ranges. If
> > this is a problem inplace_set_intersection is a good idea, otherwise i
> > would prefer changing the precondition on std::set_intersection.
> In the extreme case, you can consider all values equivalent:
>
> #include <array>
> #include <span>
> #include <algorithm>
> #include <print>
>
> int main () {
> std::array in = {1, 2, 3, 4};
>
> auto in1 = std::span (in).subspan (0, 3);
> auto in2 = std::span (in).subspan (1, 3);
>
> // all values are equivalent
> auto comp = [] (auto...) { return false; };
>
> auto test = [&] (const char* name, auto&& out) {
> std::ranges::set_intersection (
> in1, in2,
> out.begin (),
> comp
> );
>
> std::println ("{}: {}", name, out);
> };
>
> test ("out-of-place ", std::array<int, 3> {});
> test ("in-place (in1)", in1);
> test ("in-place (in2)", in2);
> }
>
> Output:
>
> out-of-place : [1, 2, 3]
> in-place (in1): [1, 2, 3]
> in-place (in2): [1, 1, 1]
>
> https://godbolt.org/z/xzKqq3nrT
>
Received on 2025-09-14 18:12:48