Skip to content

Commit

Permalink
Deprecate ror and rol in favor of rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Aug 23, 2017
1 parent 4962c12 commit cb83c73
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 149 deletions.
24 changes: 24 additions & 0 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ julia> rotate(b, (-1,0))
3 7 11 15
4 8 12 16
1 5 9 13
julia> a = BitArray([true, true, false, false, true])
5-element BitArray{1}:
true
true
false
false
true
julia> rotate(a, 1)
5-element BitArray{1}:
true
true
true
false
false
julia> rotate(a, -1)
5-element BitArray{1}:
true
false
false
true
true
```
See also [`rotate!`](@ref).
Expand Down
140 changes: 9 additions & 131 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1474,145 +1474,23 @@ details and examples.
"""
(>>>)(B::BitVector, i::Int) = (i >=0 ? B >> unsigned(i) : B << unsigned(-i))

"""
rol!(dest::BitVector, src::BitVector, i::Integer) -> BitVector
Performs a left rotation operation on `src` and puts the result into `dest`.
`i` controls how far to rotate the bits.
"""
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 (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

"""
rol!(B::BitVector, i::Integer) -> BitVector
Performs a left rotation operation in-place on `B`.
`i` controls how far to rotate the bits.
"""
rol!(B::BitVector, i::Integer) = rol!(B, B, i)

"""
rol(B::BitVector, i::Integer) -> BitVector
Performs a left rotation operation, returning a new `BitVector`.
`i` controls how far to rotate the bits.
See also [`rol!`](@ref).
# Examples
```jldoctest
julia> A = BitArray([true, true, false, false, true])
5-element BitArray{1}:
true
true
false
false
true
julia> rol(A,1)
5-element BitArray{1}:
true
false
false
true
true
julia> rol(A,2)
5-element BitArray{1}:
false
false
true
true
true
julia> rol(A,5)
5-element BitArray{1}:
true
true
false
false
true
```
"""
rol(B::BitVector, i::Integer) = rol!(similar(B), B, i)

"""
ror!(dest::BitVector, src::BitVector, i::Integer) -> BitVector
Performs a right rotation operation on `src` and puts the result into `dest`.
`i` controls how far to rotate the bits.
"""
function ror!(dest::BitVector, src::BitVector, i::Integer)
function rotate!(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 (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)
if i > 0 # right
copy_chunks!(dest.chunks, 1, Bc, i+1, n-i)
copy_chunks!(dest.chunks, n-i+1, Bc, 1, i)
else # left
copy_chunks!(dest.chunks, i+1, Bc, 1, n-i)
copy_chunks!(dest.chunks, 1, Bc, n-i+1, i)
end
return dest
end

"""
ror!(B::BitVector, i::Integer) -> BitVector
Performs a right rotation operation in-place on `B`.
`i` controls how far to rotate the bits.
"""
ror!(B::BitVector, i::Integer) = ror!(B, B, i)

"""
ror(B::BitVector, i::Integer) -> BitVector
Performs a right rotation operation on `B`, returning a new `BitVector`.
`i` controls how far to rotate the bits.
See also [`ror!`](@ref).
# Examples
```jldoctest
julia> A = BitArray([true, true, false, false, true])
5-element BitArray{1}:
true
true
false
false
true
julia> ror(A,1)
5-element BitArray{1}:
true
true
true
false
false
julia> ror(A,2)
5-element BitArray{1}:
false
true
true
true
false
julia> ror(A,5)
5-element BitArray{1}:
true
true
false
false
true
```
"""
ror(B::BitVector, i::Integer) = ror!(similar(B), B, i)
rotate!(B::BitVector, i::Integer) = rotate!(B, B, i)

## countnz & find ##

Expand Down
9 changes: 7 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1705,8 +1705,13 @@ export hex2num
@deprecate convert(::Type{S}, g::UTF8proc.GraphemeIterator) where {S<:AbstractString} convert(S, g.s)

# Issue #19923
@deprecate circshift rotate
@deprecate circshift! rotate!
@deprecate circshift rotate
@deprecate circshift! rotate!
@deprecate ror rotate
@deprecate ror! rotate!
@deprecate rol(B, i) rotate(B, -i)
@deprecate rol!(dest, src, i) rotate!(dest, src, -i)
@deprecate rol!(B, i) rotate!(B, -i)

# issue #5148, PR #23259
# warning for `const` on locals should be changed to an error in julia-syntax.scm
Expand Down
4 changes: 0 additions & 4 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,6 @@ export
# bitarrays
falses,
flipbits!,
rol,
rol!,
ror,
ror!,
trues,

# dequeues
Expand Down
4 changes: 0 additions & 4 deletions doc/src/stdlib/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ and can be converted to/from the latter via `Array(bitarray)` and `BitArray(arra

```@docs
Base.flipbits!
Base.rol!
Base.rol
Base.ror!
Base.ror
```

## [Sparse Vectors and Matrices](@id stdlib-sparse-arrays)
Expand Down
15 changes: 7 additions & 8 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1035,21 +1035,20 @@ timesofar("binary comparison")
@test isequal(b1 >>> m, [ falses(m); b1[1:end-m] ])
@test isequal(b1 << -m, b1 >> m)
@test isequal(b1 >>> -m, b1 << m)
@test isequal(rol(b1, m), [ b1[m+1:end]; b1[1:m] ])
@test isequal(ror(b1, m), [ b1[end-m+1:end]; b1[1:end-m] ])
@test isequal(ror(b1, m), rol(b1, -m))
@test isequal(rol(b1, m), ror(b1, -m))
@test isequal(rotate(b1, -m), [ b1[m+1:end]; b1[1:m] ])
@test isequal(rotate(b1, m), [ b1[end-m+1:end]; b1[1:end-m] ])
@test isequal(rotate(b1, m), rotate(b1, m - length(b1)))
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)
b1 = rotate!(i, b, j)
i1 = rotate!(b, j)
@test b1 == i1
b2 = rol!(i1, b1, j)
i2 = rol!(b1, j)
b2 = rotate!(i1, b1, -j)
i2 = rotate!(b1, -j)
@test b2 == i2
end
end
Expand Down

0 comments on commit cb83c73

Please sign in to comment.