Skip to content

Commit 02eb43a

Browse files
kaandocalpiever
andauthored
Added Base.sizehint! (#221)
* Added Base.sizehint! implementation * Added sizehint! test * Apply suggestions from code review Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> Co-authored-by: kaandocal <> Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com>
1 parent eec1738 commit 02eb43a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/structarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ function Base.empty!(s::StructArray)
411411
foreachfield(empty!, s)
412412
end
413413

414+
function Base.sizehint!(s::StructArray, i::Integer)
415+
for a in components(s)
416+
sizehint!(a, i)
417+
end
418+
return s
419+
end
420+
414421
for op in [:cat, :hcat, :vcat]
415422
@eval begin
416423
function Base.$op(args::StructArray...; kwargs...)

test/runtests.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,15 @@ end
397397
t[5] = p
398398
@test t[5] == p
399399
end
400-
400+
@testset "sizehint!" begin
401+
t = StructArray{Pair}(([3, 5], [:a, :b]))
402+
sizehint!(t, 5)
403+
@test @allocated(resize!(t, 5)) == 0
404+
@test length(t) == 5
405+
p = 1 => :c
406+
t[5] = p
407+
@test t[5] == p
408+
end
401409
@testset "concat" begin
402410
t = StructArray{Pair}(([3, 5], ["a", "b"]))
403411
push!(t, (2 => "d"))
@@ -1019,4 +1027,4 @@ end
10191027
A = @inferred StructArrays.map_params(zero, namedtypes)
10201028
C = map(zero, NamedTuple{(:a, :b, :c)}(map(zero, fieldtypes(types))))
10211029
@test A === C
1022-
end
1030+
end

0 commit comments

Comments
 (0)