Skip to content

Commit d73446e

Browse files
committed
Use generic numeric overloads for array inputs
1 parent 0fe52d2 commit d73446e

6 files changed

Lines changed: 62 additions & 33 deletions

File tree

spec/unicode_plot_spec.cr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ describe UnicodePlot do
197197
p = UnicodePlot.histogram([1.0, 2.0, 2.0, 3.0], color: {0, 135, 95})
198198
p.to_s.should be_a(String)
199199
end
200+
201+
it "accepts Int arrays via generic numeric overload" do
202+
p = UnicodePlot.histogram([1, 2, 3, 2, 1])
203+
p.to_s.should be_a(String)
204+
end
200205
end
201206

202207
describe "densityplot" do
@@ -242,6 +247,11 @@ describe UnicodePlot do
242247
p = UnicodePlot.densityplot([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], dscale: :log10)
243248
p.should be_a(UnicodePlot::Plot)
244249
end
250+
251+
it "accepts Int arrays via generic numeric overload" do
252+
p = UnicodePlot.densityplot([1, 2, 3], [4, 5, 6])
253+
p.to_s.should be_a(String)
254+
end
245255
end
246256

247257
describe "boxplot" do
@@ -284,6 +294,11 @@ describe UnicodePlot do
284294
p = UnicodePlot.boxplot([1.0, 2.0, 3.0, 7.0], title: "Test")
285295
p.to_s.should contain("Test")
286296
end
297+
298+
it "accepts Int arrays via generic numeric overload" do
299+
p = UnicodePlot.boxplot([1, 2, 3, 7])
300+
p.to_s.should be_a(String)
301+
end
287302
end
288303

289304
describe "heatmap" do
@@ -314,6 +329,11 @@ describe UnicodePlot do
314329
p = UnicodePlot.heatmap(z)
315330
p.to_s.should be_a(String)
316331
end
332+
333+
it "accepts Int matrix via generic numeric overload" do
334+
p = UnicodePlot.heatmap([[1, 2], [3, 4]])
335+
p.to_s.should be_a(String)
336+
end
317337
end
318338

319339
describe "stairs" do
@@ -328,6 +348,11 @@ describe UnicodePlot do
328348
p = UnicodePlot.stairs([Float64::MAX, 0.5], [0.5, 0.5])
329349
p.to_s.should be_a(String)
330350
end
351+
352+
it "accepts Int arrays via generic numeric overload" do
353+
p = UnicodePlot.stairs([1, 2, 3], [4, 5, 6])
354+
p.to_s.should be_a(String)
355+
end
331356
end
332357

333358
describe "BrailleCanvas" do

src/unicode_plot/interface/boxplot.cr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ module UnicodePlot
7878

7979
def boxplot(
8080
names : Array(String),
81-
data : Array(Array(Number)),
81+
data : Array(Array(T)),
8282
**kwargs,
83-
) : Plot
84-
boxplot(names, data.map { |series| series.map(&.to_f) }, **kwargs)
83+
) : Plot forall T
84+
boxplot(names, data.map { |series| to_plot_f64(series) }, **kwargs)
8585
end
8686

8787
def boxplot(
@@ -195,17 +195,17 @@ module UnicodePlot
195195

196196
def boxplot(
197197
name : String,
198-
data : Array(Number),
198+
data : Array(T),
199199
**kwargs,
200-
) : Plot
201-
boxplot(name, data.map(&.to_f), **kwargs)
200+
) : Plot forall T
201+
boxplot(name, to_plot_f64(data), **kwargs)
202202
end
203203

204204
def boxplot(
205-
data : Array(Number),
205+
data : Array(T),
206206
**kwargs,
207-
) : Plot
208-
boxplot(data.map(&.to_f), **kwargs)
207+
) : Plot forall T
208+
boxplot(to_plot_f64(data), **kwargs)
209209
end
210210

211211
def boxplot(
@@ -233,7 +233,7 @@ module UnicodePlot
233233
end
234234

235235
def boxplot(
236-
dict : Hash(String, Array(Number)),
236+
dict : Hash(String, Array(T)),
237237
*,
238238
color : Symbol | UInt32 | {Int32, Int32, Int32} | Array(Symbol) | Array(UInt32) = :green,
239239
title : String = "",
@@ -247,9 +247,9 @@ module UnicodePlot
247247
compact_labels : Bool = false,
248248
compact : Bool = false,
249249
width : Int32? = nil,
250-
) : Plot
250+
) : Plot forall T
251251
boxplot(
252-
dict.transform_values { |v| v.map(&.to_f) },
252+
dict.transform_values { |v| to_plot_f64(v) },
253253
color: color, title: title, xlabel: xlabel,
254254
border: border, margin: margin, padding: padding, labels: labels,
255255
unicode_exponent: unicode_exponent, thousands_separator: thousands_separator,
@@ -286,8 +286,8 @@ module UnicodePlot
286286
plot
287287
end
288288

289-
def boxplot!(plot : Plot, data : Array(Number), **kwargs) : Plot
290-
boxplot!(plot, data.map(&.to_f), **kwargs)
289+
def boxplot!(plot : Plot, data : Array(T), **kwargs) : Plot forall T
290+
boxplot!(plot, to_plot_f64(data), **kwargs)
291291
end
292292

293293
def boxplot!(
@@ -300,7 +300,7 @@ module UnicodePlot
300300
boxplot!(plot, data, name: name, color: color)
301301
end
302302

303-
def boxplot!(plot : Plot, name : String, data : Array(Number), **kwargs) : Plot
304-
boxplot!(plot, name, data.map(&.to_f), **kwargs)
303+
def boxplot!(plot : Plot, name : String, data : Array(T), **kwargs) : Plot forall T
304+
boxplot!(plot, name, to_plot_f64(data), **kwargs)
305305
end
306306
end

src/unicode_plot/interface/densityplot.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ module UnicodePlot
6464
plot
6565
end
6666

67-
def densityplot(x : Array(Number), y : Array(Number), **kwargs) : Plot
68-
densityplot(x.map(&.to_f), y.map(&.to_f), **kwargs)
67+
def densityplot(x : Array(T), y : Array(U), **kwargs) : Plot forall T, U
68+
densityplot(to_plot_f64(x), to_plot_f64(y), **kwargs)
6969
end
7070

7171
def densityplot!(
@@ -80,7 +80,7 @@ module UnicodePlot
8080
scatterplot!(plot, x, y, name: name, color: color)
8181
end
8282

83-
def densityplot!(plot : Plot, x : Array(Number), y : Array(Number), **kwargs) : Plot
84-
densityplot!(plot, x.map(&.to_f), y.map(&.to_f), **kwargs)
83+
def densityplot!(plot : Plot, x : Array(T), y : Array(U), **kwargs) : Plot forall T, U
84+
densityplot!(plot, to_plot_f64(x), to_plot_f64(y), **kwargs)
8585
end
8686
end

src/unicode_plot/interface/heatmap.cr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module UnicodePlot
1111
height_in : Int32?, width_in : Int32?,
1212
margin : Int32, padding : Int32,
1313
fix_ar : Bool,
14+
out_stream : IO? = nil,
1415
) : {Int32, Int32}
1516
return {0, 0} if size_nrows == 0 && size_ncols == 0
1617
size_nrows = size_nrows.clamp(1, Int32::MAX)
@@ -22,9 +23,10 @@ module UnicodePlot
2223
canv_width = size_ncols.to_f
2324
canv_ar = canv_height > 0.0 ? canv_width / canv_height : 1.0
2425

26+
term_height, term_width = out_stream_size(out_stream)
2527
width_diff = margin + padding + size_ncols.to_s.size
26-
max_h = max_height_in > 0 ? max_height_in : 24
27-
max_w = max_width_in > 0 ? max_width_in : (80 - width_diff)
28+
max_h = max_height_in > 0 ? max_height_in : term_height
29+
max_w = max_width_in > 0 ? max_width_in : (term_width - width_diff)
2830

2931
min_canv_h = canv_height.ceil.to_i
3032
min_canv_w = canv_width.ceil.to_i
@@ -129,7 +131,8 @@ z : Array(Array(Float64)),
129131
colorbar_border : Symbol = :solid,
130132
height : Int32? = nil,
131133
width : Int32? = nil,
132-
fix_ar : Bool = false,) : Plot
134+
fix_ar : Bool = false,
135+
out_stream : IO? = nil,) : Plot
133136
raise ArgumentError.new("z must not be empty") if z.empty?
134137

135138
data_nrows = z.size
@@ -222,6 +225,7 @@ z : Array(Array(Float64)),
222225
max_height_in, max_width_in,
223226
height, width,
224227
margin, padding, fix_ar,
228+
out_stream,
225229
)
226230

227231
# zmin/zmax from data or explicit zlim.
@@ -294,7 +298,7 @@ z : Array(Array(Float64)),
294298
plot
295299
end
296300

297-
def heatmap(z : Array(Array(Number)), **kwargs) : Plot
298-
heatmap(z.map { |row| row.map(&.to_f) }, **kwargs)
301+
def heatmap(z : Array(Array(T)), **kwargs) : Plot forall T
302+
heatmap(z.map { |row| to_plot_f64(row) }, **kwargs)
299303
end
300304
end

src/unicode_plot/interface/histogram.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ module UnicodePlot
305305
)
306306
end
307307

308-
def histogram(data : Array(Number), **kwargs) : Plot
309-
histogram(data.map(&.to_f), **kwargs)
308+
def histogram(data : Array(T), **kwargs) : Plot forall T
309+
histogram(to_plot_f64(data), **kwargs)
310310
end
311311
end

src/unicode_plot/interface/stairs.cr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ module UnicodePlot
5151
plot
5252
end
5353

54-
def stairs(x : Array(Number), y : Array(Number), **kwargs) : Plot
55-
stairs(x.map(&.to_f), y.map(&.to_f), **kwargs)
54+
def stairs(x : Array(T), y : Array(U), **kwargs) : Plot forall T, U
55+
stairs(to_plot_f64(x), to_plot_f64(y), **kwargs)
5656
end
5757

5858
def stairs(y : Array(Float64), **kwargs) : Plot
5959
stairs((1..y.size).map(&.to_f), y, **kwargs)
6060
end
6161

62-
def stairs(y : Array(Number), **kwargs) : Plot
63-
stairs(y.map(&.to_f), **kwargs)
62+
def stairs(y : Array(T), **kwargs) : Plot forall T
63+
stairs(to_plot_f64(y), **kwargs)
6464
end
6565

6666
def stairs!(
@@ -102,7 +102,7 @@ module UnicodePlot
102102
plot
103103
end
104104

105-
def stairs!(plot : Plot, x : Array(Number), y : Array(Number), **kwargs) : Plot
106-
stairs!(plot, x.map(&.to_f), y.map(&.to_f), **kwargs)
105+
def stairs!(plot : Plot, x : Array(T), y : Array(U), **kwargs) : Plot forall T, U
106+
stairs!(plot, to_plot_f64(x), to_plot_f64(y), **kwargs)
107107
end
108108
end

0 commit comments

Comments
 (0)