Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on is_sorted & is_sorted_until #34

Closed
samchon opened this issue May 3, 2019 · 2 comments
Closed

Error on is_sorted & is_sorted_until #34

samchon opened this issue May 3, 2019 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@samchon
Copy link
Owner

samchon commented May 3, 2019

Summary

  • TSTL Version: 2.1.2
  • Expected behavior: is_sorted & is_sorted_until requires only comparator like less
  • Actual behavior: They require not only comparator but also predicator (equal_to)

Code occuring the bug

Functions is_sorted and is_sorted_until returns invalid value when target elements are not atomic values but custom objects that have not defined the predicator (Object.equals) function.

import std = require("tstl");

function comparator(x: std.IPointer<number>, y: std.IPointer<number>): boolean
{
    return x.value < y.value;
}
function main(): void
{
    let pointers: std.Vector<std.IPointer<number>> = new std.Vector();
    for (let i: number = 0; i < 100; ++i)
        pointers.push_back({ value: std.randint(0, 10) });

    std.sort(pointers.begin(), pointers.end(), comparator);
    console.log( std.is_sorted(pointers.begin(), pointers.end(), comparator) );
        // MUST BE true
        // BUT PRINTS false
}
main();
@samchon samchon added bug Something isn't working enhancement New feature or request labels May 3, 2019
@samchon
Copy link
Owner Author

samchon commented May 3, 2019

if (!(equal_to(next.value, first.value) || comp(first.value, next.value)))

Functions is_sorted and is_sorted_until are using equal_to function. However, those functions do not provide the matched predicator function. The equal_to must be replaced to !comp(x, y) && !comp(y, x).

@samchon samchon added this to Patch in v2.1 Update May 3, 2019
@samchon
Copy link
Owner Author

samchon commented May 3, 2019

Fixed on v2.1.3 patch

@samchon samchon closed this as completed May 3, 2019
v2.1 Update automation moved this from Patch to Done May 3, 2019
@samchon samchon added this to Done in v2.2 Update May 7, 2019
@samchon samchon moved this from Done to In progress in v2.2 Update Jul 12, 2019
@samchon samchon moved this from In progress to Done in v2.2 Update Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
No open projects
v2.1 Update
  
Done
v2.2 Update
  
Done
Development

No branches or pull requests

1 participant