-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from yukuku/fix-footnote-xref-binary-search
Fix binary search on footnotes and xref: arrays of arif should be tre…
- Loading branch information
Showing
6 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
30 changes: 30 additions & 0 deletions
30
AlkitabYes2/src/main/java/yuku/alkitab/yes2/util/unsignedBinarySearch.kt
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,30 @@ | ||
package yuku.alkitab.yes2.util | ||
|
||
/** | ||
* Treat [a] as a sorted array of unsigned ints and do binary search on it. | ||
* | ||
* @param key element to look for, treated as an unsigned int. | ||
*/ | ||
fun unsignedIntBinarySearch(a: IntArray, key: Int): Int { | ||
var low = 0 | ||
var high = a.size - 1 | ||
|
||
while (low <= high) { | ||
val mid = (low + high) ushr 1 | ||
val midVal = a[mid] | ||
|
||
val cmp = uintCompare(midVal, key) | ||
if (cmp < 0) { | ||
low = mid + 1 | ||
} else if (cmp > 0) { | ||
high = mid - 1 | ||
} else { | ||
// key found | ||
return mid | ||
} | ||
} | ||
|
||
return -(low + 1) // key not found. | ||
} | ||
|
||
private fun uintCompare(v1: Int, v2: Int): Int = (v1 xor Int.MIN_VALUE).compareTo(v2 xor Int.MIN_VALUE) |
65 changes: 65 additions & 0 deletions
65
AlkitabYes2/src/test/java/yuku/alkitab/yes2/util/UnsignedBinarySearchKtTest.kt
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,65 @@ | ||
@file:OptIn(ExperimentalUnsignedTypes::class) | ||
|
||
package yuku.alkitab.yes2.util | ||
|
||
import org.junit.Assert.assertArrayEquals | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class UnsignedBinarySearchKtTest { | ||
|
||
private val allPositiveArray = IntArray(100) { it + 10 } | ||
private val allNegativeArray = IntArray(100) { (0xc000_0000L + it.toLong()).toInt() } | ||
private val mixedSignArray = IntArray(100) { it } + IntArray(100) { (0xc000_0000L + it.toLong()).toInt() } | ||
private val skipArray = IntArray(100) { it * 2 } + IntArray(100) { (0xc000_0000L + (it * 2).toLong()).toInt() } | ||
|
||
|
||
@Before | ||
fun checkSorted() { | ||
// all arrays under test must be sorted | ||
fun assertSorted(a: IntArray) { | ||
val u = UIntArray(a.size) { a[it].toUInt() } | ||
assertArrayEquals(u.toTypedArray(), u.sorted().toTypedArray()) | ||
} | ||
|
||
assertSorted(allPositiveArray) | ||
assertSorted(allNegativeArray) | ||
assertSorted(mixedSignArray) | ||
} | ||
|
||
@Test | ||
fun unsignedBinarySearch() { | ||
for (i in allPositiveArray.indices) { | ||
assertEquals(i, unsignedIntBinarySearch(allPositiveArray, i + 10)) | ||
} | ||
assertEquals(0.inv(), unsignedIntBinarySearch(allPositiveArray, 0)) | ||
assertEquals(100.inv(), unsignedIntBinarySearch(allPositiveArray, -1)) | ||
assertEquals(100.inv(), unsignedIntBinarySearch(allPositiveArray, -2)) | ||
assertEquals(100.inv(), unsignedIntBinarySearch(allPositiveArray, 400)) | ||
|
||
for (i in allNegativeArray.indices) { | ||
assertEquals(i, unsignedIntBinarySearch(allNegativeArray, allNegativeArray[i])) | ||
} | ||
assertEquals(0.inv(), unsignedIntBinarySearch(allNegativeArray, 0)) | ||
assertEquals(100.inv(), unsignedIntBinarySearch(allNegativeArray, -1)) | ||
assertEquals(100.inv(), unsignedIntBinarySearch(allNegativeArray, -2)) | ||
assertEquals(0.inv(), unsignedIntBinarySearch(allNegativeArray, 400)) | ||
|
||
for (i in mixedSignArray.indices) { | ||
assertEquals(i, unsignedIntBinarySearch(mixedSignArray, mixedSignArray[i])) | ||
} | ||
assertEquals(0, unsignedIntBinarySearch(mixedSignArray, 0)) | ||
assertEquals(200.inv(), unsignedIntBinarySearch(mixedSignArray, -1)) | ||
assertEquals(200.inv(), unsignedIntBinarySearch(mixedSignArray, -2)) | ||
// in the middle | ||
assertEquals(100.inv(), unsignedIntBinarySearch(mixedSignArray, 400)) | ||
|
||
for (i in skipArray.indices) { | ||
assertEquals(i, unsignedIntBinarySearch(skipArray, skipArray[i])) | ||
} | ||
assertEquals(0, unsignedIntBinarySearch(skipArray, 0)) | ||
assertEquals(2.inv(), unsignedIntBinarySearch(skipArray, 3)) | ||
assertEquals(103.inv(), unsignedIntBinarySearch(skipArray, (0xc000_0000L + 5L).toInt())) | ||
} | ||
} |