Optimized swapping of ref array elements.
This commit is contained in:
parent
872f3a4ab1
commit
8d86c6ed84
1 changed files with 7 additions and 0 deletions
|
|
@ -1744,6 +1744,13 @@ proc swap*[T](a, b: var T) {.magic: "Swap", noSideEffect.}
|
||||||
## swaps the values `a` and `b`. This is often more efficient than
|
## swaps the values `a` and `b`. This is often more efficient than
|
||||||
## ``tmp = a; a = b; b = tmp``. Particularly useful for sorting algorithms.
|
## ``tmp = a; a = b; b = tmp``. Particularly useful for sorting algorithms.
|
||||||
|
|
||||||
|
when not defined(js) and not defined(booting):
|
||||||
|
template swapRefsInArray*{swap(arr[a], arr[b])}(arr: openarray[ref], a, b: int) =
|
||||||
|
# Optimize swapping of array elements if they are refs. Default swap
|
||||||
|
# implementation will cause unsureAsgnRef to be emitted which causes
|
||||||
|
# unnecessary slow down in this case.
|
||||||
|
swap(cast[ptr pointer](addr arr[a])[], cast[ptr pointer](addr arr[b])[])
|
||||||
|
|
||||||
template `>=%` *(x, y: untyped): untyped = y <=% x
|
template `>=%` *(x, y: untyped): untyped = y <=% x
|
||||||
## treats `x` and `y` as unsigned and compares them.
|
## treats `x` and `y` as unsigned and compares them.
|
||||||
## Returns true iff ``unsigned(x) >= unsigned(y)``.
|
## Returns true iff ``unsigned(x) >= unsigned(y)``.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue