Skip to content

Commit a2b3500

Browse files
committed
Deprecate ror and rol in favor of circshift methods
1 parent aa3d2be commit a2b3500

File tree

7 files changed

+51
-150
lines changed

7 files changed

+51
-150
lines changed

base/abstractarraymath.jl

+26-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ circshift(a::AbstractArray, shiftamt::DimsInteger) = circshift!(similar(a), a, s
178178
"""
179179
circshift(A, shifts)
180180
181-
Circularly shift the data in an array. The second argument is a vector giving the amount to
182-
shift in each dimension.
181+
Circularly shift, i.e. rotate, the data in an array. The second argument is a vector giving
182+
the amount to shift in each dimension.
183183
184184
# Examples
185185
```jldoctest
@@ -203,6 +203,30 @@ julia> circshift(b, (-1,0))
203203
3 7 11 15
204204
4 8 12 16
205205
1 5 9 13
206+
207+
julia> a = BitArray([true, true, false, false, true])
208+
5-element BitArray{1}:
209+
true
210+
true
211+
false
212+
false
213+
true
214+
215+
julia> circshift(a, 1)
216+
5-element BitArray{1}:
217+
true
218+
true
219+
true
220+
false
221+
false
222+
223+
julia> circshift(a, -1)
224+
5-element BitArray{1}:
225+
true
226+
false
227+
false
228+
true
229+
true
206230
```
207231
208232
See also [`circshift!`](@ref).

base/bitarray.jl

+10-131
Original file line numberDiff line numberDiff line change
@@ -1474,145 +1474,24 @@ details and examples.
14741474
"""
14751475
(>>>)(B::BitVector, i::Int) = (i >=0 ? B >> unsigned(i) : B << unsigned(-i))
14761476

1477-
"""
1478-
rol!(dest::BitVector, src::BitVector, i::Integer) -> BitVector
1479-
1480-
Performs a left rotation operation on `src` and puts the result into `dest`.
1481-
`i` controls how far to rotate the bits.
1482-
"""
1483-
function rol!(dest::BitVector, src::BitVector, i::Integer)
1484-
length(dest) == length(src) || throw(ArgumentError("destination and source should be of same size"))
1485-
n = length(dest)
1486-
i %= n
1487-
i == 0 && return (src === dest ? src : copy!(dest, src))
1488-
i < 0 && return ror!(dest, src, -i)
1489-
Bc = (src === dest ? copy(src.chunks) : src.chunks)
1490-
copy_chunks!(dest.chunks, 1, Bc, i+1, n-i)
1491-
copy_chunks!(dest.chunks, n-i+1, Bc, 1, i)
1492-
return dest
1493-
end
1494-
1495-
"""
1496-
rol!(B::BitVector, i::Integer) -> BitVector
1497-
1498-
Performs a left rotation operation in-place on `B`.
1499-
`i` controls how far to rotate the bits.
1500-
"""
1501-
rol!(B::BitVector, i::Integer) = rol!(B, B, i)
1502-
1503-
"""
1504-
rol(B::BitVector, i::Integer) -> BitVector
1505-
1506-
Performs a left rotation operation, returning a new `BitVector`.
1507-
`i` controls how far to rotate the bits.
1508-
See also [`rol!`](@ref).
1509-
1510-
# Examples
1511-
```jldoctest
1512-
julia> A = BitArray([true, true, false, false, true])
1513-
5-element BitArray{1}:
1514-
true
1515-
true
1516-
false
1517-
false
1518-
true
1519-
1520-
julia> rol(A,1)
1521-
5-element BitArray{1}:
1522-
true
1523-
false
1524-
false
1525-
true
1526-
true
1527-
1528-
julia> rol(A,2)
1529-
5-element BitArray{1}:
1530-
false
1531-
false
1532-
true
1533-
true
1534-
true
1535-
1536-
julia> rol(A,5)
1537-
5-element BitArray{1}:
1538-
true
1539-
true
1540-
false
1541-
false
1542-
true
1543-
```
1544-
"""
1545-
rol(B::BitVector, i::Integer) = rol!(similar(B), B, i)
1546-
1547-
"""
1548-
ror!(dest::BitVector, src::BitVector, i::Integer) -> BitVector
1549-
1550-
Performs a right rotation operation on `src` and puts the result into `dest`.
1551-
`i` controls how far to rotate the bits.
1552-
"""
1553-
function ror!(dest::BitVector, src::BitVector, i::Integer)
1477+
function circshift!(dest::BitVector, src::BitVector, i::Integer)
15541478
length(dest) == length(src) || throw(ArgumentError("destination and source should be of same size"))
15551479
n = length(dest)
15561480
i %= n
15571481
i == 0 && return (src === dest ? src : copy!(dest, src))
1558-
i < 0 && return rol!(dest, src, -i)
15591482
Bc = (src === dest ? copy(src.chunks) : src.chunks)
1560-
copy_chunks!(dest.chunks, i+1, Bc, 1, n-i)
1561-
copy_chunks!(dest.chunks, 1, Bc, n-i+1, i)
1483+
if i > 0 # right
1484+
copy_chunks!(dest.chunks, i+1, Bc, 1, n-i)
1485+
copy_chunks!(dest.chunks, 1, Bc, n-i+1, i)
1486+
else # left
1487+
i = -i
1488+
copy_chunks!(dest.chunks, 1, Bc, i+1, n-i)
1489+
copy_chunks!(dest.chunks, n-i+1, Bc, 1, i)
1490+
end
15621491
return dest
15631492
end
15641493

1565-
"""
1566-
ror!(B::BitVector, i::Integer) -> BitVector
1567-
1568-
Performs a right rotation operation in-place on `B`.
1569-
`i` controls how far to rotate the bits.
1570-
"""
1571-
ror!(B::BitVector, i::Integer) = ror!(B, B, i)
1572-
1573-
"""
1574-
ror(B::BitVector, i::Integer) -> BitVector
1575-
1576-
Performs a right rotation operation on `B`, returning a new `BitVector`.
1577-
`i` controls how far to rotate the bits.
1578-
See also [`ror!`](@ref).
1579-
1580-
# Examples
1581-
```jldoctest
1582-
julia> A = BitArray([true, true, false, false, true])
1583-
5-element BitArray{1}:
1584-
true
1585-
true
1586-
false
1587-
false
1588-
true
1589-
1590-
julia> ror(A,1)
1591-
5-element BitArray{1}:
1592-
true
1593-
true
1594-
true
1595-
false
1596-
false
1597-
1598-
julia> ror(A,2)
1599-
5-element BitArray{1}:
1600-
false
1601-
true
1602-
true
1603-
true
1604-
false
1605-
1606-
julia> ror(A,5)
1607-
5-element BitArray{1}:
1608-
true
1609-
true
1610-
false
1611-
false
1612-
true
1613-
```
1614-
"""
1615-
ror(B::BitVector, i::Integer) = ror!(similar(B), B, i)
1494+
circshift!(B::BitVector, i::Integer) = circshift!(B, B, i)
16161495

16171496
## countnz & find ##
16181497

base/deprecated.jl

+7
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,13 @@ export hex2num
17041704
@deprecate convert(::Type{String}, v::Vector{UInt8}) String(v)
17051705
@deprecate convert(::Type{S}, g::UTF8proc.GraphemeIterator) where {S<:AbstractString} convert(S, g.s)
17061706

1707+
# Issue #19923
1708+
@deprecate ror circshift
1709+
@deprecate ror! circshift!
1710+
@deprecate rol(B, i) circshift(B, -i)
1711+
@deprecate rol!(dest, src, i) circshift!(dest, src, -i)
1712+
@deprecate rol!(B, i) circshift!(B, -i)
1713+
17071714
# issue #5148, PR #23259
17081715
# warning for `const` on locals should be changed to an error in julia-syntax.scm
17091716

base/exports.jl

-4
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,6 @@ export
643643
# bitarrays
644644
falses,
645645
flipbits!,
646-
rol,
647-
rol!,
648-
ror,
649-
ror!,
650646
trues,
651647

652648
# dequeues

base/multidimensional.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ circshift!(dest::AbstractArray, src, ::Tuple{}) = copy!(dest, src)
946946
"""
947947
circshift!(dest, src, shifts)
948948
949-
Circularly shift the data in `src`, storing the result in
949+
Circularly shift, i.e. rotate, the data in `src`, storing the result in
950950
`dest`. `shifts` specifies the amount to shift in each dimension.
951951
952952
The `dest` array must be distinct from the `src` array (they cannot

doc/src/stdlib/arrays.md

-4
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,6 @@ and can be converted to/from the latter via `Array(bitarray)` and `BitArray(arra
179179

180180
```@docs
181181
Base.flipbits!
182-
Base.rol!
183-
Base.rol
184-
Base.ror!
185-
Base.ror
186182
```
187183

188184
## [Sparse Vectors and Matrices](@id stdlib-sparse-arrays)

test/bitarray.jl

+7-8
Original file line numberDiff line numberDiff line change
@@ -1035,21 +1035,20 @@ timesofar("binary comparison")
10351035
@test isequal(b1 >>> m, [ falses(m); b1[1:end-m] ])
10361036
@test isequal(b1 << -m, b1 >> m)
10371037
@test isequal(b1 >>> -m, b1 << m)
1038-
@test isequal(rol(b1, m), [ b1[m+1:end]; b1[1:m] ])
1039-
@test isequal(ror(b1, m), [ b1[end-m+1:end]; b1[1:end-m] ])
1040-
@test isequal(ror(b1, m), rol(b1, -m))
1041-
@test isequal(rol(b1, m), ror(b1, -m))
1038+
@test isequal(circshift(b1, -m), [ b1[m+1:end]; b1[1:m] ])
1039+
@test isequal(circshift(b1, m), [ b1[end-m+1:end]; b1[1:end-m] ])
1040+
@test isequal(circshift(b1, m), circshift(b1, m - length(b1)))
10421041
end
10431042

10441043
b = bitrand(v1)
10451044
i = bitrand(v1)
10461045
for m = [rand(1:v1), 63, 64, 65, 191, 192, 193, v1-1]
10471046
j = rand(1:m)
1048-
b1 = ror!(i, b, j)
1049-
i1 = ror!(b, j)
1047+
b1 = circshift!(i, b, j)
1048+
i1 = circshift!(b, j)
10501049
@test b1 == i1
1051-
b2 = rol!(i1, b1, j)
1052-
i2 = rol!(b1, j)
1050+
b2 = circshift!(i1, b1, -j)
1051+
i2 = circshift!(b1, -j)
10531052
@test b2 == i2
10541053
end
10551054
end

0 commit comments

Comments
 (0)