Skip to content

Commit

Permalink
Don't assume that one(T) and zero(T) return type T. See #655
Browse files Browse the repository at this point in the history
  • Loading branch information
dcjones committed Aug 11, 2015
1 parent 10b1dab commit 1cb02d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
10 changes: 6 additions & 4 deletions src/bincount.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,20 @@ end
function choose_bin_count_1d_discrete(xs::AbstractArray, xs_set::AbstractArray,
d_min=1, d_max=150)
n = length(xs_set)
T = eltype(xs)
if n == 0
return 1, Int[0], 0
elseif n == 1
return 1, Int[length(xs)], xs[1] + one(eltype(xs))
return 1, Int[length(xs)], xs[1] + convert(T, one(T))
end

# minimum distance between two values
mingap = zero(eltype(xs))
T = eltype(xs)
mingap = convert(T, zero(T))
for (i, j) in zip(1:length(xs_set)-1, 2:length(xs_set))
gap = xs_set[j] - xs_set[i]
if isconcrete(gap) && gap > zero(eltype(xs))
if mingap == zero(eltype(xs))
if isconcrete(gap) && gap > zero(T)
if mingap == convert(T, zero(T))
mingap = gap
else
mingap = min(gap, mingap)
Expand Down
22 changes: 13 additions & 9 deletions src/geom/bar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function render_colorless_bar(geom::BarGeometry,
orientation::Symbol)
if orientation == :horizontal
XT = eltype(aes.x)
xz = zero(XT)
xz = convert(XT, zero(XT))
ctx = compose!(
context(),
rectangle([min(xz, x) for x in aes.x],
Expand All @@ -65,7 +65,7 @@ function render_colorless_bar(geom::BarGeometry,
svgclass("geometry"))
else
YT = eltype(aes.y)
yz = zero(YT)
yz = convert(YT, zero(YT))
ctx = compose!(
context(),
rectangle([Measure(cx=xmin) + theme.bar_spacing/2 for xmin in aes.xmin],
Expand Down Expand Up @@ -103,8 +103,10 @@ function render_colorful_stacked_bar(geom::BarGeometry,
ctx = context()
if orientation == :horizontal
stack_height_dict = Dict()
T = eltype(aes.x)
z = convert(T, zero(T))
for y in aes.ymin
stack_height_dict[y] = zero(eltype(aes.x))
stack_height_dict[y] = z
end
stack_height = zeros(eltype(aes.x), length(idxs))

Expand All @@ -122,8 +124,10 @@ function render_colorful_stacked_bar(geom::BarGeometry,
[(aes.ymax[i] - aes.ymin[i])*cy - theme.bar_spacing for i in idxs]))
elseif orientation == :vertical
stack_height_dict = Dict()
T = eltype(aes.y)
z = convert(T, zero(T))
for x in aes.xmin
stack_height_dict[x] = zero(eltype(aes.y))
stack_height_dict[x] = z
end
stack_height = zeros(eltype(aes.y), length(idxs))

Expand Down Expand Up @@ -190,7 +194,7 @@ function render_colorful_dodged_bar(geom::BarGeometry,
end

XT = eltype(aes.x)
xz = zero(XT)
xz = convert(XT, zero(XT))

aes_x = aes.x[idxs]
compose!(
Expand Down Expand Up @@ -222,7 +226,7 @@ function render_colorful_dodged_bar(geom::BarGeometry,
end

YT = eltype(aes.y)
yz = zero(YT)
yz = convert(YT, zero(YT))

aes_y = aes.y[idxs]
compose!(
Expand Down Expand Up @@ -292,14 +296,14 @@ function render(geom::BarGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
minvalue, maxvalue = minimum(values), maximum(values)
T = typeof((maxvalue - minvalue) / 1.0)

span = zero(T)
span = convert(T, zero(T))
unique_count = length(Set(values))
if unique_count > 1
span = (maximum(values) - minimum(values)) / convert(Float64, (unique_count - 1))
end

if span == zero(T)
span = one(T)
if span == convert(T, zero(T))
span = convert(T, one(T))
end

T = promote_type(eltype(values), typeof(span/2.0))
Expand Down
3 changes: 2 additions & 1 deletion src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,8 @@ function minimum_span(vars::Vector{Symbol}, aes::Gadfly.Aesthetics)
continue
end
dataspan = data[2] - data[1]
z = zero(eltype(data))
T = eltype(data)
z = convert(T, zero(T))
sorteddata = sort(data)
for (u, v) in partition(sorteddata, 2, 1)
δ = v - u
Expand Down

0 comments on commit 1cb02d2

Please sign in to comment.