D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11409 - Array element-wise comparison
Summary: Array element-wise comparison
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-11-01 02:37 UTC by daniel
Modified: 2015-06-09 05:14 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description daniel 2013-11-01 02:37:19 UTC
void main() {
	auto a = [0, 2, 0];
	auto b = [1, 1, 1];

	// add 2 to ALL elements in C
	auto c = a.dup;
	c[] += 2;
	assert(c == [2, 4, 2]);

	// add ALL elements in B to D
	auto d = a.dup;
	d[] += b[];
	assert(d == [1, 3, 1]);

	// checks ALL elements in A are less than B
	assert(!(a < b)); // Error ...
}


As is shown above, there is an inconsistency in how array element-wise comparisons are handled in D.
The logical conclusion (IMHO) after using the array vector operations that mutated all elements in the lhs, is that the operation would check that the comparison was true for (again) **ALL** elements of the arrays.

Instead, it appears to only check for the first element for which the predicate holds true, and then stops.

This is really weird, especially when the following holds:

assert([2, 0, 0] > [1, 1, 1]); 
assert([0, 2, 0] < [1, 1, 1]); 
assert([0, 0, 2] < [1, 1, 1]); 

I couldn't find any relevant documentation on how this was supposed to work, so these are just best guesses support with data.
Comment 1 Iain Buclaw 2013-11-01 03:32:37 UTC
Your assumption is not quite right. This is the loop comparisons goes off:

for (size_t u = 0; u < len; u++)
{
    int result = s1[u] - s2[u];
    if (result)
        return result;
}


And in you examples:
---
Code:      assert([2, 0, 0] > [1, 1, 1]);
---
Generates: assert(compare(s1, s2) > 0);
---
Returns:   result = 2 - 1;  =>  return 1

---
Code:      assert([0, 2, 0] < [1, 1, 1]); 
---
Generates: assert(compare(s1, s2) < 0);
---
Returns:   result = 0 - 1;  =>  return -1;

---
Code:      assert([0, 0, 2] < [1, 1, 1]);
---
Generates: assert(compare(s1, s2) < 0);
---
Returns:   result = 0 - 1;  =>  return -1;



My advise to you would be to compare a *SORTED* array.

Marking as invalid because this is working as expected.
Comment 2 daniel 2013-11-01 03:44:39 UTC
(In reply to comment #1)
> Your assumption is not quite right. This is the loop comparisons goes off:
> 
> for (size_t u = 0; u < len; u++)
> {
>     int result = s1[u] - s2[u];
>     if (result)
>         return result;
> }
> 
> 
> And in you examples:
> ---
> Code:      assert([2, 0, 0] > [1, 1, 1]);
> ---
> Generates: assert(compare(s1, s2) > 0);
> ---
> Returns:   result = 2 - 1;  =>  return 1
> 
> ---
> Code:      assert([0, 2, 0] < [1, 1, 1]); 
> ---
> Generates: assert(compare(s1, s2) < 0);
> ---
> Returns:   result = 0 - 1;  =>  return -1;
> 
> ---
> Code:      assert([0, 0, 2] < [1, 1, 1]);
> ---
> Generates: assert(compare(s1, s2) < 0);
> ---
> Returns:   result = 0 - 1;  =>  return -1;
> 
> 
> 
> My advise to you would be to compare a *SORTED* array.
> 
> Marking as invalid because this is working as expected.

Why would I want to compare a sorted array?
I want to compare two different arrays?

I see no reason why a built-in comparison would assume the array would be sorted?
This is working as it is implemented, but I wouldn't say it is working as expected. Not by a long shot.

Is there any reference for this behaviour?
Comment 3 daniel 2013-11-01 03:56:03 UTC
(In reply to comment #1)
> Your assumption is not quite right. This is the loop comparisons goes off:
> 
> for (size_t u = 0; u < len; u++)
> {
>     int result = s1[u] - s2[u];
>     if (result)
>         return result;
> }

I understand that is more or less what the loop was going off, and I am saying, that is inconsistent behaviour.
It also makes no sense as a comparison, because as long as the arrays aren't equal, it will always only compare the first element!?

What the?
What kind of comparison is that for a set of operations that is on 'arrays', not sorted data structures.
Comment 4 Iain Buclaw 2013-11-01 04:16:51 UTC
(In reply to comment #3)
> (In reply to comment #1)
> > Your assumption is not quite right. This is the loop comparisons goes off:
> > 
> > for (size_t u = 0; u < len; u++)
> > {
> >     int result = s1[u] - s2[u];
> >     if (result)
> >         return result;
> > }
> 
> I understand that is more or less what the loop was going off, and I am saying,
> that is inconsistent behaviour.
> It also makes no sense as a comparison, because as long as the arrays aren't
> equal, it will always only compare the first element!?
> 
> What the?
> What kind of comparison is that for a set of operations that is on 'arrays',
> not sorted data structures.

Take by way of example, how you compare two words.  You'd say that betty comes before hello, thus "betty" < "hello" is true.

In the same logic, [0,2,0] comes before [1,1,1], thus [0,2,0] < [1,1,1].
Comment 5 daniel 2013-11-01 06:58:18 UTC
For others that may find this later.

The default comparison is the equivalent to a http://www.cplusplus.com/reference/algorithm/lexicographical_compare/.