Skip to content

Commit

Permalink
Merge pull request #3 from andyferris/ajf/splitapplycombine
Browse files Browse the repository at this point in the history
Reverse dependency with SplitApplyCombine.jl
  • Loading branch information
andyferris authored Dec 18, 2019
2 parents c19b637 + 72b7dd7 commit 207cbda
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 184 deletions.
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
authors = ["Andy Ferris <[email protected]>"]
name = "Dictionaries"
uuid = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
version = "0.1.0"
version = "0.2.0"

[deps]
Indexing = "313cdc1a-70c2-5d6a-ae34-0150d3930a38"
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[extras]
Expand All @@ -16,5 +15,4 @@ test = ["Test"]

[compat]
julia = "1"
SplitApplyCombine = "0.4.1"
Indexing = "1.1"
4 changes: 1 addition & 3 deletions src/Dictionaries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module Dictionaries
using Random

using Indexing
using SplitApplyCombine

using Base: @propagate_inbounds
using Base: @propagate_inbounds, Callable

export getindices, setindices!, mapview

Expand All @@ -27,7 +26,6 @@ include("foreach.jl")
include("map.jl")
include("broadcast.jl")
include("find.jl")
include("group.jl")
include("show.jl")

include("PairDictionary.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/HashDictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function deletetoken!(d::HashDictionary{I, T}, token) where {I, T}
end

function Base.sizehint!(d::HashDictionary, sz::Int)
_sizehint!(d.indices, d.values, sz)
d.values = _sizehint!(d.indices, d.values, sz)
return d
end

Expand Down
2 changes: 1 addition & 1 deletion src/HashIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function _sizehint!(h::HashIndices{T}, values::Union{Nothing, Vector}, newsz::In
# grow at least 25%
newsz = min(max(newsz, (oldsz*5)>>2),
Base.max_values(T))
_rehash!(h, values, newsz)
return _rehash!(h, values, newsz)
end


Expand Down
16 changes: 0 additions & 16 deletions src/MappedDictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@ struct MappedDictionary{I, T, F, Maps <: Tuple{AbstractDictionary{<:I}, Vararg{A
dicts::Maps
end

function SplitApplyCombine.mapview(f, d::AbstractDictionary)
I = keytype(d)
T = Core.Compiler.return_type(f, Tuple{eltype(d)})

return MappedDictionary{I, T, typeof(f), Tuple{typeof(d)}}(f, (d,))
end

function SplitApplyCombine.mapview(f, d::AbstractDictionary, ds::AbstractDictionary...)
I = typejoin(keytype(d), keytype.(ds)...)
T = Core.Compiler.return_type(f, Tuple{eltype(d), eltype.(ds)...})

# Check the things have the same keys...

return MappedDictionary{I, T, typeof(f), typeof((d, ds...))}(f, (d, ds...))
end

Base.keys(d::MappedDictionary{I}) where {I} = keys(d.dicts[1])::AbstractIndices{I}

function Base.isassigned(d::MappedDictionary{I}, i::I) where {I}
Expand Down
150 changes: 0 additions & 150 deletions src/group.jl

This file was deleted.

33 changes: 29 additions & 4 deletions src/insertion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,32 @@ function Base.get!(d::AbstractDictionary{I, T}, i::I, default::T) where {I, T}
end
end

# TODO: the `get!(f, dict, i)` form. Similarly for `set!` - see
"""
get!(f::Union{Function, Type}, dict::AbstractDictionary, i)
Return the value `dict[i]` if index `i` exists. Otherwise, a new index `i` is inserted and
set to the value `f()`, which is returned.
"""
function Base.get!(f::Callable, d::AbstractDictionary{I}, i) where {I}
i2 = convert(I, i)
if !isequal(i, i2)
throw(ArgumentError("$i is not a valid key for type $I"))
end
get!(f, d, i2)
end


function Base.get!(f::Callable, d::AbstractDictionary{I}, i::I) where {I}
(hadindex, token) = gettoken!(d, i)
if hadindex
return gettokenvalue(d, token)
else
default = f()
settokenvalue!(d, token, default)
return default
end
end


"""
delete!(indices::AbstractIndices, i)
Expand Down Expand Up @@ -301,7 +326,7 @@ end

Base.merge!(d::AbstractDictionary, ds::AbstractDictionary...) = merge!(last, d, ds...)

function Base.merge!(combiner::Function, d::AbstractDictionary, d2::AbstractDictionary)
function Base.merge!(combiner::Callable, d::AbstractDictionary, d2::AbstractDictionary)
for (i, v) in pairs(d2)
(hasindex, token) = gettoken!(d, i)
if hasindex
Expand Down Expand Up @@ -329,11 +354,11 @@ function Base.merge!(::typeof(first), d::AbstractDictionary, d2::AbstractDiction
return d
end

function Base.merge!(combiner::Function, d::AbstractDictionary, d2::AbstractDictionary, ds::AbstractDictionary...)
function Base.merge!(combiner::Callable, d::AbstractDictionary, d2::AbstractDictionary, ds::AbstractDictionary...)
merge!(combiner, merge!(combiner, d, d2), ds...)
end

function Base.merge!(combiner::Function, d::AbstractIndices, d2::AbstractIndices)
function Base.merge!(combiner::Callable, d::AbstractIndices, d2::AbstractIndices)
# Hopefully no-one provides a bad combiner
union!(d, d2)
end
Expand Down
15 changes: 11 additions & 4 deletions test/map.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
@testset "map" begin
function _mapview(f, d::AbstractDictionary)
I = keytype(d)
T = Core.Compiler.return_type(f, Tuple{eltype(d)})

return MappedDictionary{I, T, typeof(f), Tuple{typeof(d)}}(f, (d,))
end

i = HashIndices([1,2,3,4,5])

@test issetequal(pairs(map(iseven, i)::HashDictionary), [1=>false, 2=>true, 3=>false, 4=>true, 5=>false])
@test issetequal(pairs(map(isodd, i)::HashDictionary), [1=>true, 2=>false, 3=>true, 4=>false, 5=>true])

@test issetequal(pairs(mapview(iseven, i)::Dictionaries.MappedDictionary), [1=>false, 2=>true, 3=>false, 4=>true, 5=>false])
@test issetequal(pairs(mapview(isodd, i)::Dictionaries.MappedDictionary), [1=>true, 2=>false, 3=>true, 4=>false, 5=>true])
@test issetequal(pairs(_mapview(iseven, i)::Dictionaries.MappedDictionary), [1=>false, 2=>true, 3=>false, 4=>true, 5=>false])
@test issetequal(pairs(_mapview(isodd, i)::Dictionaries.MappedDictionary), [1=>true, 2=>false, 3=>true, 4=>false, 5=>true])

d = HashDictionary([1,2,3,4,5], [1,3,2,4,5])

@test issetequal(pairs(map(iseven, d)::HashDictionary), [1=>false, 2=>false, 3=>true, 4=>true, 5=>false])
@test issetequal(pairs(map(isodd, d)::HashDictionary), [1=>true, 2=>true, 3=>false, 4=>false, 5=>true])

@test issetequal(pairs(mapview(iseven, d)::Dictionaries.MappedDictionary), [1=>false, 2=>false, 3=>true, 4=>true, 5=>false])
@test issetequal(pairs(mapview(isodd, d)::Dictionaries.MappedDictionary), [1=>true, 2=>true, 3=>false, 4=>false, 5=>true])
@test issetequal(pairs(_mapview(iseven, d)::Dictionaries.MappedDictionary), [1=>false, 2=>false, 3=>true, 4=>true, 5=>false])
@test issetequal(pairs(_mapview(isodd, d)::Dictionaries.MappedDictionary), [1=>true, 2=>true, 3=>false, 4=>false, 5=>true])
end
2 changes: 0 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Test
using Dictionaries
using SplitApplyCombine
using Indexing

include("Indices.jl")
Expand All @@ -14,4 +13,3 @@ include("map.jl")
include("broadcast.jl")
include("filter.jl")
include("find.jl")
include("group.jl")

0 comments on commit 207cbda

Please sign in to comment.