Skip to content

Commit

Permalink
Allow accumulators to take an AbstractDict (#553)
Browse files Browse the repository at this point in the history
* Allow accumulators to take an AbstractDict

* Bump patch version

* Update src/accumulator.jl

Co-Authored-By: Nicole Epp <[email protected]>

* Update test/test_accumulator.jl

Co-Authored-By: Nicole Epp <[email protected]>
  • Loading branch information
2 people authored and oxinabox committed Nov 22, 2019
1 parent 3d68cd3 commit 4e0dfcd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DataStructures"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.17.5"
version = "0.17.6"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
3 changes: 2 additions & 1 deletion src/accumulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ end
## constructors

Accumulator{T, V}() where {T,V<:Number} = Accumulator{T,V}(Dict{T,V}())
Accumulator(map::AbstractDict) = Accumulator(Dict(map))

counter(T::Type) = Accumulator{T,Int}()
counter(dct::Dict{T,V}) where {T,V<:Integer} = Accumulator{T,V}(copy(dct))
counter(dct::AbstractDict{T, V}) where {T, V<:Integer} = Accumulator{T, V}(Dict(dct))

"""
counter(seq)
Expand Down
10 changes: 10 additions & 0 deletions test/test_accumulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
@test ctm["b"] == 0
end

@testset "From AbstractDict" begin
kv = (:a => 1, :b => 2)
for f in (Accumulator, counter)
ct_odict = f(OrderedDict(kv))
ct_dict = f(Dict(kv))
@test ct_odict isa Accumulator{Symbol, Int}
@test ct_odict == ct_dict
end
end

@testset "From Pair" begin
ct4 = counter(Pair{Int,Int})
@test isa(ct4, Accumulator{Pair{Int,Int}})
Expand Down

2 comments on commit 4e0dfcd

@oxinabox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/5741

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.17.6 -m "<description of version>" 4e0dfcddb83875cb887a52e308207e40ba7d4d86
git push origin v0.17.6

Please sign in to comment.