www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11409] New: Array element-wise comparison

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11409

           Summary: Array element-wise comparison
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: nobody puremagic.com
        ReportedBy: daniel350 bigpond.com



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 01 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11409


Iain Buclaw <ibuclaw ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |ibuclaw ubuntu.com
         Resolution|                            |INVALID



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11409





 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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11409





 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11409






 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]. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 01 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11409




For others that may find this later.

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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 01 2013