Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC : Implementation of rol! and ror! #9822

Merged
merged 1 commit into from
Feb 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1272,29 +1272,45 @@ end
(>>)(B::BitVector, i::Int32) = B >>> i
(>>)(B::BitVector, i::Integer) = B >>> i

function rol(B::BitVector, i::Integer)
n = length(B)
function rol!(dest::BitVector, src::BitVector, i::Integer)
length(dest) == length(src) || throw(ArgumentError("destination and source should be of same size"))
n = length(dest)
i %= n
i == 0 && return copy(B)
i < 0 && return ror(B, -i)
A = BitArray(n)
copy_chunks!(A.chunks, 1, B.chunks, i+1, n-i)
copy_chunks!(A.chunks, n-i+1, B.chunks, 1, i)
return A
i == 0 && return (src === dest ? src : copy!(dest, src))
i < 0 && return ror!(dest, src, -i)
Bc = (src === dest ? copy(src.chunks) : src.chunks)
copy_chunks!(dest.chunks, 1, Bc, i+1, n-i)
copy_chunks!(dest.chunks, n-i+1, Bc, 1, i)
return dest
end

function ror(B::BitVector, i::Integer)
n = length(B)
function rol!(B::BitVector, i::Integer)
return rol!(B, B, i)
end

function rol(B::BitVector, i::Integer)
return rol!(similar(B), B, i)
end

function ror!(dest::BitVector, src::BitVector, i::Integer)
length(dest) == length(src) || throw(ArgumentError("destination and source should be of same size"))
n = length(dest)
i %= n
i == 0 && return copy(B)
i < 0 && return rol(B, -i)
A = BitArray(n)
copy_chunks!(A.chunks, i+1, B.chunks, 1, n-i)
copy_chunks!(A.chunks, 1, B.chunks, n-i+1, i)
return A
i == 0 && return (src === dest ? src : copy!(dest, src))
i < 0 && return rol!(dest, src, -i)
Bc = (src === dest ? copy(src.chunks) : src.chunks)
copy_chunks!(dest.chunks, i+1, Bc, 1, n-i)
copy_chunks!(dest.chunks, 1, Bc, n-i+1, i)
return dest
end

#TODO: rol!, ror!
function ror!(B::BitVector, i::Integer)
return ror!(B, B, i)
end

function ror(B::BitVector, i::Integer)
return ror!(similar(B), B, i)
end

## countnz & find ##

Expand Down
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,9 @@ export
falses,
flipbits!,
rol,
rol!,
ror,
ror!,
trues,

# dequeues
Expand Down
24 changes: 20 additions & 4 deletions doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,29 @@ BitArrays

Performs a bitwise not operation on B. See :ref:`~ operator <~>`.

.. function:: rol(B::BitArray{1},i::Integer) -> BitArray{1}
.. function:: rol!(dest::BitArray{1}, src::BitArray{1}, i::Integer) -> BitArray{1}

Left rotation operator.
Performs a left rotation operation on ``src`` and put the result into ``dest``.

.. function:: ror(B::BitArray{1},i::Integer) -> BitArray{1}
.. function:: rol!(B::BitArray{1}, i::Integer) -> BitArray{1}

Right rotation operator.
Performs a left rotation operation on B.

.. function:: rol(B::BitArray{1}, i::Integer) -> BitArray{1}

Performs a left rotation operation.

.. function:: ror!(dest::BitArray{1}, src::BitArray{1}, i::Integer) -> BitArray{1}

Performs a right rotation operation on ``src`` and put the result into ``dest``.

.. function:: ror!(B::BitArray{1}, i::Integer) -> BitArray{1}

Performs a right rotation operation on B.

.. function:: ror(B::BitArray{1}, i::Integer) -> BitArray{1}

Performs a right rotation operation.

.. _stdlib-sparse:

Expand Down
12 changes: 12 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,18 @@ for m = [rand(1:v1)-1 0 1 63 64 65 191 192 193 v1-1]
@test isequal(rol(b1, m), ror(b1, -m))
end

b = bitrand(v1)
i = bitrand(v1)
for m = [rand(1:v1) 63 64 65 191 192 193 v1-1]
j = rand(1:m)
b1 = ror!(i, b, j)
i1 = ror!(b, j)
@test b1 == i1
b2 = rol!(i1, b1, j)
i2 = rol!(b1, j)
@test b2 == i2
end

timesofar("datamove")

## countnz & find ##
Expand Down