Skip to content

Commit

Permalink
Optimize slice reverse (#5401)
Browse files Browse the repository at this point in the history
  • Loading branch information
larubujo authored and RX14 committed Dec 18, 2017
1 parent 2e59d49 commit c74c0bd
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/slice.cr
Expand Up @@ -212,13 +212,17 @@ struct Slice(T)
def reverse!
check_writable

i = 0
j = size - 1
while i < j
@pointer.swap i, j
i += 1
j -= 1
return self if size <= 1

p = @pointer
q = @pointer + size - 1

while p < q
p.value, q.value = q.value, p.value
p += 1
q -= 1
end

self
end

Expand Down

0 comments on commit c74c0bd

Please sign in to comment.