Deprecate smart binary search (#7745)
* deprecate smartBinarySearch * changelog entry
This commit is contained in:
parent
9735bb46be
commit
f94fafff9b
2 changed files with 21 additions and 14 deletions
|
|
@ -63,6 +63,9 @@
|
||||||
fields.
|
fields.
|
||||||
- ``system.SomeReal`` is now called ``SomeFloat`` for consistency and
|
- ``system.SomeReal`` is now called ``SomeFloat`` for consistency and
|
||||||
correctness.
|
correctness.
|
||||||
|
- ``algorithm.smartBinarySearch`` and ``algorithm.binarySearch`` is
|
||||||
|
now joined in ``binarySearch``. ``smartbinarySearch`` is now
|
||||||
|
deprecated.
|
||||||
|
|
||||||
### Language additions
|
### Language additions
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,21 +69,25 @@ proc reversed*[T](a: openArray[T]): seq[T] =
|
||||||
|
|
||||||
proc binarySearch*[T](a: openArray[T], key: T): int =
|
proc binarySearch*[T](a: openArray[T], key: T): int =
|
||||||
## binary search for `key` in `a`. Returns -1 if not found.
|
## binary search for `key` in `a`. Returns -1 if not found.
|
||||||
var b = len(a)
|
if ((a.len - 1) and a.len) == 0:
|
||||||
while result < b:
|
# when `a.len` is a power of 2, a faster div can be used.
|
||||||
var mid = (result + b) div 2
|
var step = a.len div 2
|
||||||
if a[mid] < key: result = mid + 1
|
while step > 0:
|
||||||
else: b = mid
|
if a[result or step] <= key:
|
||||||
if result >= len(a) or a[result] != key: result = -1
|
result = result or step
|
||||||
|
step = step shr 1
|
||||||
|
if a[result] != key: result = -1
|
||||||
|
else:
|
||||||
|
var b = len(a)
|
||||||
|
while result < b:
|
||||||
|
var mid = (result + b) div 2
|
||||||
|
if a[mid] < key: result = mid + 1
|
||||||
|
else: b = mid
|
||||||
|
if result >= len(a) or a[result] != key: result = -1
|
||||||
|
|
||||||
proc smartBinarySearch*[T](a: openArray[T], key: T): int =
|
proc smartBinarySearch*[T](a: openArray[T], key: T): int {.deprecated.} =
|
||||||
## ``a.len`` must be a power of 2 for this to work.
|
## **Deprecated since version 0.18.1**; Use ``binarySearch`` instead.
|
||||||
var step = a.len div 2
|
binarySearch(a,key)
|
||||||
while step > 0:
|
|
||||||
if a[result or step] <= key:
|
|
||||||
result = result or step
|
|
||||||
step = step shr 1
|
|
||||||
if a[result] != key: result = -1
|
|
||||||
|
|
||||||
const
|
const
|
||||||
onlySafeCode = true
|
onlySafeCode = true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue