-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend opaque companion context to inlined code
The GADT bounds set in an opaque companion also need to be established for any inlined code coming from the companion. Test case in opaque-immutable-array.scala.
- Loading branch information
Showing
4 changed files
with
68 additions
and
12 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
File renamed without changes.
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,43 @@ | ||
object ia { | ||
|
||
import java.util.Arrays | ||
|
||
opaque type IArray[A1] = Array[A1] | ||
|
||
object IArray { | ||
@inline final def initialize[A](body: => Array[A]): IArray[A] = body | ||
|
||
@inline final def size[A](ia: IArray[A]): Int = ia.length | ||
@inline final def get[A](ia: IArray[A], i: Int): A = ia(i) | ||
|
||
// return a sorted copy of the array | ||
def sorted[A <: AnyRef : math.Ordering](ia: IArray[A]): IArray[A] = { | ||
val arr = Arrays.copyOf(ia, ia.length) | ||
scala.util.Sorting.quickSort(arr) | ||
arr | ||
} | ||
|
||
// use a standard java method to search a sorted IArray. | ||
// (note that this doesn't mutate the array). | ||
def binarySearch(ia: IArray[Long], elem: Long): Int = | ||
Arrays.binarySearch(ia, elem) | ||
} | ||
|
||
// same as IArray.binarySearch but implemented by-hand. | ||
// | ||
// given a sorted IArray, returns index of `elem`, | ||
// or a negative value if not found. | ||
def binaryIndexOf(ia: IArray[Long], elem: Long): Int = { | ||
var lower: Int = 0 | ||
var upper: Int = IArray.size(ia) | ||
while (lower <= upper) { | ||
val middle = (lower + upper) >>> 1 | ||
val n = IArray.get(ia, middle) | ||
|
||
if (n == elem) return middle | ||
else if (n < elem) lower = middle + 1 | ||
else upper = middle - 1 | ||
} | ||
-lower - 1 | ||
} | ||
} |