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

Deprecate conversion to Int for "non-obvious" index types #10458

Merged
merged 1 commit into from
Mar 12, 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ Library improvements
Deprecated or removed
---------------------

* indexing with Reals that are not subtypes of Integers (Rationals, FloatingPoint, etc.) has been deprecated ([#10458]).

* `push!(A)` has been deprecated, use `append!` instead of splatting arguments to `push!` ([#10400]).

* `names` for composite datatypes has been deprecated and
Expand Down
23 changes: 23 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,26 @@ function push!(A)
depwarn("push!(A) has been deprecated", :push!)
A
end

# 10458
to_index_nodep(i::Real) = convert(Int,i)::Int

function to_index(i::Real)
depwarn("indexing with non Integer Reals is deprecated", :to_index)
to_index_nodep(i)
end

function to_index{T<:Real}(r::UnitRange{T})
depwarn("indexing with non Integer UnitRanges is deprecated", :to_index)
to_index_nodep(first(r)):to_index_nodep(last(r))
end

function to_index{T<:Real}(r::StepRange{T})
depwarn("indexing with non Integer StepRanges is deprecated", :to_index)
to_index_nodep(first(r)):to_index_nodep(step(r)):to_index_nodep(last(r))
end

function to_index{T<:Real}(A::AbstractArray{T})
depwarn("indexing with non Integer AbstractArrays is deprecated", :to_index)
Int[to_index_nodep(x) for x in A]
end
8 changes: 4 additions & 4 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,16 @@ end

# convert to integer index
to_index(i::Int) = i
to_index(i::Real) = convert(Int,i)::Int
to_index(i::Integer) = convert(Int,i)::Int
to_index(r::UnitRange{Int}) = r
to_index(r::Range{Int}) = r
to_index(I::UnitRange{Bool}) = find(I)
to_index(I::Range{Bool}) = find(I)
to_index{T<:Real}(r::UnitRange{T}) = to_index(first(r)):to_index(last(r))
to_index{T<:Real}(r::StepRange{T}) = to_index(first(r)):to_index(step(r)):to_index(last(r))
to_index{T<:Integer}(r::UnitRange{T}) = to_index(first(r)):to_index(last(r))
to_index{T<:Integer}(r::StepRange{T}) = to_index(first(r)):to_index(step(r)):to_index(last(r))
to_index(I::AbstractArray{Bool}) = find(I)
to_index(A::AbstractArray{Int}) = A
to_index{T<:Real}(A::AbstractArray{T}) = [to_index(x) for x in A]
to_index{T<:Integer}(A::AbstractArray{T}) = [to_index(x) for x in A]
to_index(i1, i2) = to_index(i1), to_index(i2)
to_index(i1, i2, i3) = to_index(i1), to_index(i2), to_index(i3)
to_index(i1, i2, i3, i4) = to_index(i1), to_index(i2), to_index(i3), to_index(i4)
Expand Down
2 changes: 1 addition & 1 deletion base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function match(re::Regex, str::UTF8String, idx::Integer, add_opts::Int32=Int32(0
if !PCRE.exec(re.regex, re.extra, str, idx-1, opts, re.ovec)
return nothing
end
n = length(re.ovec)/3 - 1
n = div(length(re.ovec),3) - 1
mat = SubString(str, re.ovec[1]+1, re.ovec[2])
cap = Union(Void,SubString{UTF8String})[
re.ovec[2i+1] < 0 ? nothing : SubString(str, re.ovec[2i+1]+1, re.ovec[2i+2]) for i=1:n ]
Expand Down
2 changes: 1 addition & 1 deletion doc/manual/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ The general syntax for indexing into an n-dimensional array A is::

where each I\_k may be:

1. A scalar value
1. A scalar integer
2. A ``Range`` of the form ``:``, ``a:b``, or ``a:b:c``
3. An arbitrary integer vector, including the empty vector ``[]``
4. A boolean vector
Expand Down
5 changes: 0 additions & 5 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -893,11 +893,6 @@ function pr8622()
end
@test pr8622() == [0,3,1,0]

# commit b718cbc72e90, getindex(::Number, ::Real)
b718cbc = 5
@test b718cbc[1.0] == 5
@test_throws InexactError b718cbc[1.1]

#6828 - size of specific dimensions
a = Array(Float64, 10)
@test size(a) == (10,)
Expand Down
47 changes: 2 additions & 45 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ for (sz,T) in allsizes
@check_bit_operation getindex(b1) Bool
@check_bit_operation setindex!(b1, true) T
@check_bit_operation setindex!(b1, false) T
@check_bit_operation setindex!(b1, 1.0) T
end

# linear
Expand All @@ -93,7 +92,6 @@ for (sz,T) in allsizes[2:end]
for j = 1:l
@check_bit_operation getindex(b1, j) Bool
end
@check_bit_operation getindex(b1, 100.0) Bool

for j in [0, 1, 63, 64, 65, 127, 128, 129, 191, 192, 193, l-1, l]
@check_bit_operation getindex(b1, 1:j) BitVector
Expand All @@ -103,23 +101,17 @@ for (sz,T) in allsizes[2:end]
m1 = j:(l-j)
@check_bit_operation getindex(b1, m1) BitVector
end
@check_bit_operation getindex(b1, 1.0:100.0) BitVector

t1 = find(bitrand(l))
@check_bit_operation getindex(b1, t1) BitVector
@check_bit_operation getindex(b1, float(t1)) BitVector

for j = 1:l
x = rand(Bool)
@check_bit_operation setindex!(b1, x, j) T
end

x = rand(Bool)
@check_bit_operation setindex!(b1, x, 100.0) T
y = rand(0.0:1.0)
@check_bit_operation setindex!(b1, y, 100) T
y = rand(0.0:1.0)
@check_bit_operation setindex!(b1, y, 100.0) T

for j in [1, 63, 64, 65, 127, 128, 129, 191, 192, 193, l-1]
x = rand(Bool)
Expand All @@ -139,16 +131,12 @@ for (sz,T) in allsizes[2:end]
@check_bit_operation setindex!(b1, b2, m1) T
end
x = rand(Bool)
@check_bit_operation setindex!(b1, x, 1.0:100.0) T
@check_bit_operation setindex!(b1, x, 1:100) T
b2 = bitrand(100)
@check_bit_operation setindex!(b1, b2, 1.0:100.0) T
@check_bit_operation setindex!(b1, b2, 1:100) T

y = rand(0.0:1.0)
@check_bit_operation setindex!(b1, y, 1:100) T
f2 = float(bitrand(100))
@check_bit_operation setindex!(b1, f2, 1:100) T
f2 = float(bitrand(100))
@check_bit_operation setindex!(b1, f2, 1.0:100.0) T

t1 = find(bitrand(l))
x = rand(Bool)
Expand All @@ -158,19 +146,6 @@ for (sz,T) in allsizes[2:end]

y = rand(0.0:1.0)
@check_bit_operation setindex!(b1, y, t1) T
f2 = float(bitrand(length(t1)))
@check_bit_operation setindex!(b1, f2, t1) T

ft1 = float(t1)
x = rand(Bool)
@check_bit_operation setindex!(b1, x, ft1) T
b2 = bitrand(length(t1))
@check_bit_operation setindex!(b1, b2, ft1) T

y = rand(0.0:1.0)
@check_bit_operation setindex!(b1, y, ft1) T
f2 = float(bitrand(length(t1)))
@check_bit_operation setindex!(b1, f2, ft1) T
end

# multidimensional
Expand Down Expand Up @@ -208,14 +183,6 @@ for (k1, k2, T) in Task(gen_getindex_data)
# println(typeof(k1), " ", typeof(k2), " ", T) # uncomment to debug
@check_bit_operation getindex(b1, k1, k2) T
@check_bit_operation getindex(b1, k1, k2, 1) T

#@check_bit_operation getindex(b1, float(k1), k2) T
#@check_bit_operation getindex(b1, k1, float(k2)) T

@check_bit_operation getindex(b1, float(k1), float(k2)) T

@check_bit_operation getindex(b1, k1, k2, 1.0) T
#@check_bit_operation getindex(b1, float(k1), float(k2), 1.0) T
end

function gen_setindex_data()
Expand Down Expand Up @@ -250,16 +217,6 @@ end
for (b2, k1, k2) in Task(gen_setindex_data)
# println(typeof(b2), " ", typeof(k1), " ", typeof(k2)) # uncomment to debug
@check_bit_operation setindex!(b1, b2, k1, k2) BitMatrix

@check_bit_operation setindex!(b1, float(b2), k1, k2) BitMatrix
@check_bit_operation setindex!(b1, b2, float(k1), k2) BitMatrix
#@check_bit_operation setindex!(b1, b2, k1, float(k2)) BitMatrix

#@check_bit_operation setindex!(b1, float(b2), float(k1), k2) BitMatrix
#@check_bit_operation setindex!(b1, float(b2), k1, float(k2)) BitMatrix
#@check_bit_operation setindex!(b1, b2, float(k1), float(k2)) BitMatrix

@check_bit_operation setindex!(b1, float(b2), float(k1), float(k2)) BitMatrix
end

m1, m2 = rand_m1m2()
Expand Down
3 changes: 0 additions & 3 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,6 @@ gstr = Base.GenericString("12");
@test convert(Symbol, gstr)==symbol("12")

@test getindex(gstr, Bool(1))=='1'
@test getindex(gstr, 1.0)=='1'
@test getindex(gstr,Bool(1):Bool(1))=="1"
@test getindex(gstr,AbstractVector([Bool(1):Bool(1);]))=="1"

Expand All @@ -1399,8 +1398,6 @@ gstr = Base.GenericString("12");

@test nextind(AbstractArray([Bool(1):Bool(1);]),1)==2

@test checkbounds(gstr,1.0)==true

@test ind2chr(gstr,2)==2

# issue #10307
Expand Down