mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Merge pull request #2273 from ap29600/core_slice_rotate_fix
Fix `core:slice.rotate_left`
This commit is contained in:
+17
-15
@@ -73,24 +73,26 @@ ptr_rotate :: proc(left: int, mid: ^$T, right: int) {
|
|||||||
left, mid, right := left, mid, right
|
left, mid, right := left, mid, right
|
||||||
|
|
||||||
// TODO(bill): Optimization with a buffer for smaller ranges
|
// TODO(bill): Optimization with a buffer for smaller ranges
|
||||||
if left >= right {
|
for left > 0 && right > 0 {
|
||||||
for {
|
if left >= right {
|
||||||
ptr_swap_non_overlapping(ptr_sub(mid, right), mid, right)
|
for {
|
||||||
mid = ptr_sub(mid, right)
|
ptr_swap_non_overlapping(ptr_sub(mid, right), mid, right * size_of(T))
|
||||||
|
mid = ptr_sub(mid, right)
|
||||||
|
|
||||||
left -= right
|
left -= right
|
||||||
if left < right {
|
if left < right {
|
||||||
break
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
for {
|
||||||
for {
|
ptr_swap_non_overlapping(ptr_sub(mid, left), mid, left * size_of(T))
|
||||||
ptr_swap_non_overlapping(ptr_sub(mid, left), mid, left)
|
mid = ptr_add(mid, left)
|
||||||
mid = ptr_add(mid, left)
|
|
||||||
|
|
||||||
right -= left
|
right -= left
|
||||||
if right < left {
|
if right < left {
|
||||||
break
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,8 +218,10 @@ rotate_left :: proc(array: $T/[]$E, mid: int) {
|
|||||||
n := len(array)
|
n := len(array)
|
||||||
m := mid %% n
|
m := mid %% n
|
||||||
k := n - m
|
k := n - m
|
||||||
p := raw_data(array)
|
// FIXME: (ap29600) this cast is a temporary fix for the compiler not matching
|
||||||
ptr_rotate(mid, ptr_add(p, mid), k)
|
// [^T] with $P/^$T
|
||||||
|
p := cast(^E)raw_data(array)
|
||||||
|
ptr_rotate(m, ptr_add(p, m), k)
|
||||||
}
|
}
|
||||||
rotate_right :: proc(array: $T/[]$E, k: int) {
|
rotate_right :: proc(array: $T/[]$E, k: int) {
|
||||||
rotate_left(array, -k)
|
rotate_left(array, -k)
|
||||||
@@ -515,4 +517,4 @@ dot_product :: proc(a, b: $S/[]$T) -> (r: T, ok: bool)
|
|||||||
enumerated_array :: proc(ptr: ^$T) -> []intrinsics.type_elem_type(T)
|
enumerated_array :: proc(ptr: ^$T) -> []intrinsics.type_elem_type(T)
|
||||||
where intrinsics.type_is_enumerated_array(T) {
|
where intrinsics.type_is_enumerated_array(T) {
|
||||||
return ([^]intrinsics.type_elem_type(T))(ptr)[:len(T)]
|
return ([^]intrinsics.type_elem_type(T))(ptr)[:len(T)]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user