Skip to content

Commit 97adceb

Browse files
authored
rename pool back to compact_mem and implement here (#235)
* rename pool back to compact_mem and implement here * add tests
1 parent 349b400 commit 97adceb

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

src/IndexedTables.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using OnlineStatsBase: OnlineStat, fit!
66

77
using StructArrays: StructVector, StructArray, foreachfield, fieldarrays,
88
collect_structarray, staticschema, ArrayInitializer, refine_perm!, collect_structarray,
9-
collect_empty_structarray, grow_to_structarray!, collect_to_structarray!, pool,
9+
collect_empty_structarray, grow_to_structarray!, collect_to_structarray!, replace_storage,
1010
GroupPerm, GroupJoinPerm, roweq, rowcmp, fastpermute!
1111

1212
import Tables, TableTraits, IteratorInterfaceExtensions, TableTraitsUtils

src/columns.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,6 @@ function init_inputs(f::Tup, input, isvec)
696696
# functions and input
697697
NT((fs...,)), rows(NT((xs...,)))
698698
end
699+
700+
# utils
701+
compact_mem(v::Columns) = replace_storage(compact_mem, v)

src/ndsparse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function aggregate!(f, x::NDSparse)
398398
idxs, data = x.index, x.data
399399
n = length(idxs)
400400
newlen = 0
401-
for ii in GroupPerm(pool(idxs), Base.OneTo(n))
401+
for ii in GroupPerm(compact_mem(idxs), Base.OneTo(n))
402402
newlen += 1
403403
if newlen != last(ii)
404404
copyrow!(idxs, newlen, last(ii))

src/sortperm.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ function sortpermby(t, by; cache=false, return_keys=false)
3535

3636
if matched_cols == length(by)
3737
# first n index columns
38-
return return_keys ? (partial_perm, pool(rows(t, by))) : partial_perm
38+
return return_keys ? (partial_perm, compact_mem(rows(t, by))) : partial_perm
3939
end
4040

41-
byrows = pool(rows(t, by))
41+
byrows = compact_mem(rows(t, by))
4242
bycols = columns(byrows)
4343
perm = if matched_cols > 0
4444
nxtcol = bycols[matched_cols+1]

src/utils.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ astuple(n::NamedTuple) = Tuple(n)
7676

7777
# optimized sortperm: pool non isbits types before calling sortperm_fast or sortperm_by
7878

79-
sortperm_fast(x) = sortperm(pool(x))
79+
sortperm_fast(x) = sortperm(compact_mem(x))
8080

8181
function append_n!(X, val, n)
8282
l = length(X)
@@ -266,3 +266,10 @@ getsubfields(t::Tuple, fields) = t[fields]
266266

267267
product(itr) = itr
268268
product(itrs...) = Base.Generator(reverse, Iterators.product(reverse(itrs)...))
269+
270+
# pool non isbits types
271+
272+
compact_mem(v::PooledArray) = v
273+
compact_mem(v::AbstractArray{T}) where {T} = isbitstype(T) ? v : PooledArray(v)
274+
compact_mem(v::StringArray{String}) =
275+
map(String, PooledArray(convert(StringArray{WeakRefString{UInt8}}, v)))

test/test_utils.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,22 @@ let x = rand(3), y = rand(3), v = rand(3), w = rand(3)
3333
@test vcat((Columns((x,y))), Columns((v,w))) == Columns((vcat(x,v), vcat(y,w)))
3434
@test vcat(Columns(x=x,y=y), Columns(x=v,y=w)) == Columns(x=vcat(x,v), y=vcat(y,w))
3535
end
36+
37+
@testset "compact_mem" begin
38+
v = Columns(a=rand(10), b = fill("string", 10))
39+
v_pooled = IndexedTables.replace_storage(v) do c
40+
isbitstype(eltype(c)) ? c : convert(PooledArrays.PooledArray, c)
41+
end
42+
@test eltype(v) == eltype(v_pooled)
43+
@test all(v.a .== v_pooled.a)
44+
@test all(v.b .== v_pooled.b)
45+
@test !isa(v_pooled.a, PooledArrays.PooledArray)
46+
@test isa(v_pooled.b, PooledArrays.PooledArray)
47+
@test v_pooled == IndexedTables.compact_mem(v)
48+
s = WeakRefStrings.StringArray(["a", "b", "c"])
49+
@test IndexedTables.compact_mem(s) isa PooledArrays.PooledArray{String}
50+
@test IndexedTables.compact_mem(s)[1] == "a"
51+
@test IndexedTables.compact_mem(s)[2] == "b"
52+
@test IndexedTables.compact_mem(s)[3] == "c"
53+
@test IndexedTables.compact_mem(IndexedTables.compact_mem(s)) == IndexedTables.compact_mem(s)
54+
end

0 commit comments

Comments
 (0)