-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Sort.relativeComparator
for specifying a comparator with `compa…
…re` (#25821) Adds the `relativeComparator` interface to the Sort module. This deprecates the old way of defining comparators with `compare` methods. Implements part of #25553 Testing - [x] built and checked docs - [x] paratest with/without comm [Reviewed by @lydia-duncan]
- Loading branch information
Showing
39 changed files
with
426 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use Sort; | ||
|
||
// deprecation, missing keyPartComparator | ||
record R1 { | ||
proc compare(x,y) do return if x<y then -1 else if y<x then 1 else 0; | ||
proc keyPart(elt, i) do return (if i > 0 then -1 else 0, elt); | ||
} | ||
|
||
// this is an error, keyPart is wrong | ||
record R2: keyPartComparator { | ||
proc compare(x,y) do return if x<y then -1 else if y<x then 1 else 0; | ||
proc keyPart(elt, i) do return (if i > 0 then -1 else 0, elt); | ||
} | ||
|
||
// this is correct | ||
record R3: keyPartComparator { | ||
proc compare(x,y) do return if x<y then -1 else if y<x then 1 else 0; | ||
proc keyPart(elt, i) do | ||
return (if i > 0 then keyPartStatus.pre else keyPartStatus.returned, elt); | ||
} | ||
|
||
|
||
config type comparator; | ||
|
||
use Random; | ||
var arr: [1..1000] int; | ||
|
||
fillRandom(arr); | ||
writeln("isSorted before ", isSorted(arr, comparator=new comparator())); | ||
// this line won't get the deprecation warning (already resolved in isSorted) | ||
sort(arr, comparator=new comparator()); | ||
writeln("isSorted after ", isSorted(arr, comparator=new comparator())); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-scomparator=R1 # compareAndKeyPart.deprecation.good | ||
-scomparator=R2 # compareAndKeyPart.error.good | ||
-scomparator=R3 # compareAndKeyPart.good |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
compareAndKeyPart.chpl:29: warning: Defining a comparator with both a 'compare' method and a 'keyPart' without implementing the keyPartComparator interface is deprecated. Please implement the keyPartComparator interface (i.e. 'record R1: relativeComparator'). | ||
compareAndKeyPart.chpl:32: warning: Defining a comparator with both a 'compare' method and a 'keyPart' without implementing the keyPartComparator interface is deprecated. Please implement the keyPartComparator interface (i.e. 'record R1: relativeComparator'). | ||
isSorted before false | ||
isSorted after true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
compareAndKeyPart.chpl:29: error: The keyPart method in R2 must return a tuple with element 0 of type keyPartStatus when used with int(64) elements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
isSorted before false | ||
isSorted after true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
isSorted before false | ||
isSorted after true | ||
isSorted before false | ||
isSorted after true | ||
isSorted before false | ||
isSorted after true |
Oops, something went wrong.