Skip to content

Commit c64a6cc

Browse files
authored
Deprecate rol and ror in favor of circshift methods (#23404)
1 parent 6210e24 commit c64a6cc

File tree

8 files changed

+56
-150
lines changed

8 files changed

+56
-150
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ Deprecated or removed
372372
* `filter` and `filter!` on dictionaries now pass a single `key=>value` pair to the
373373
argument function, instead of two arguments ([#17886]).
374374

375+
* `rol`, `rol!`, `ror`, and `ror!` have been deprecated in favor of specialized methods for
376+
`circshift`/`circshift!` ([#23404]).
377+
375378
* `Base.SparseArrays.SpDiagIterator` has been removed ([#23261]).
376379

377380
* The tuple-of-types form of `cfunction`, `cfunction(f, returntype, (types...))`, has been deprecated
@@ -1269,3 +1272,4 @@ Command-line option changes
12691272
[#23207]: https://github.com/JuliaLang/julia/issues/23207
12701273
[#23233]: https://github.com/JuliaLang/julia/issues/23233
12711274
[#23342]: https://github.com/JuliaLang/julia/issues/23342
1275+
[#23404]: https://github.com/JuliaLang/julia/issues/23404

base/abstractarraymath.jl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ 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 tuple or
182+
vector giving the amount to shift in each dimension, or an integer to shift only in the
183+
first dimension.
183184
184185
# Examples
185186
```jldoctest
@@ -203,6 +204,30 @@ julia> circshift(b, (-1,0))
203204
3 7 11 15
204205
4 8 12 16
205206
1 5 9 13
207+
208+
julia> a = BitArray([true, true, false, false, true])
209+
5-element BitArray{1}:
210+
true
211+
true
212+
false
213+
false
214+
true
215+
216+
julia> circshift(a, 1)
217+
5-element BitArray{1}:
218+
true
219+
true
220+
true
221+
false
222+
false
223+
224+
julia> circshift(a, -1)
225+
5-element BitArray{1}:
226+
true
227+
false
228+
false
229+
true
230+
true
206231
```
207232
208233
See also [`circshift!`](@ref).

base/bitarray.jl

Lines changed: 10 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,145 +1478,24 @@ details and examples.
14781478
"""
14791479
(>>>)(B::BitVector, i::Int) = (i >=0 ? B >> unsigned(i) : B << unsigned(-i))
14801480

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

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

16211500
## count & find ##
16221501

base/deprecated.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,13 @@ export hex2num
16681668
@deprecate convert(::Type{String}, v::Vector{UInt8}) String(v)
16691669
@deprecate convert(::Type{S}, g::UTF8proc.GraphemeIterator) where {S<:AbstractString} convert(S, g.s)
16701670

1671+
# Issue #19923
1672+
@deprecate ror circshift
1673+
@deprecate ror! circshift!
1674+
@deprecate rol(B, i) circshift(B, -i)
1675+
@deprecate rol!(dest, src, i) circshift!(dest, src, -i)
1676+
@deprecate rol!(B, i) circshift!(B, -i)
1677+
16711678
# issue #5148, PR #23259
16721679
# warning for `const` on locals should be changed to an error in julia-syntax.scm
16731680

base/exports.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,6 @@ export
638638
# bitarrays
639639
falses,
640640
flipbits!,
641-
rol,
642-
rol!,
643-
ror,
644-
ror!,
645641
trues,
646642

647643
# dequeues

base/multidimensional.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ circshift!(dest::AbstractArray, src, ::Tuple{}) = copy!(dest, src)
933933
"""
934934
circshift!(dest, src, shifts)
935935
936-
Circularly shift the data in `src`, storing the result in
936+
Circularly shift, i.e. rotate, the data in `src`, storing the result in
937937
`dest`. `shifts` specifies the amount to shift in each dimension.
938938
939939
The `dest` array must be distinct from the `src` array (they cannot

doc/src/stdlib/arrays.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ and can be converted to/from the latter via `Array(bitarray)` and `BitArray(arra
178178

179179
```@docs
180180
Base.flipbits!
181-
Base.rol!
182-
Base.rol
183-
Base.ror!
184-
Base.ror
185181
```
186182

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

test/bitarray.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,21 +1040,20 @@ timesofar("binary comparison")
10401040
@test isequal(b1 >>> m, [ falses(m); b1[1:end-m] ])
10411041
@test isequal(b1 << -m, b1 >> m)
10421042
@test isequal(b1 >>> -m, b1 << m)
1043-
@test isequal(rol(b1, m), [ b1[m+1:end]; b1[1:m] ])
1044-
@test isequal(ror(b1, m), [ b1[end-m+1:end]; b1[1:end-m] ])
1045-
@test isequal(ror(b1, m), rol(b1, -m))
1046-
@test isequal(rol(b1, m), ror(b1, -m))
1043+
@test isequal(circshift(b1, -m), [ b1[m+1:end]; b1[1:m] ])
1044+
@test isequal(circshift(b1, m), [ b1[end-m+1:end]; b1[1:end-m] ])
1045+
@test isequal(circshift(b1, m), circshift(b1, m - length(b1)))
10471046
end
10481047

10491048
b = bitrand(v1)
10501049
i = bitrand(v1)
10511050
for m = [rand(1:v1), 63, 64, 65, 191, 192, 193, v1-1]
10521051
j = rand(1:m)
1053-
b1 = ror!(i, b, j)
1054-
i1 = ror!(b, j)
1052+
b1 = circshift!(i, b, j)
1053+
i1 = circshift!(b, j)
10551054
@test b1 == i1
1056-
b2 = rol!(i1, b1, j)
1057-
i2 = rol!(b1, j)
1055+
b2 = circshift!(i1, b1, -j)
1056+
i2 = circshift!(b1, -j)
10581057
@test b2 == i2
10591058
end
10601059
end

0 commit comments

Comments
 (0)