From 8ccd860fd15c32af11257383ca908689b451dcba Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Fri, 10 Mar 2017 15:47:39 +0800 Subject: [PATCH] EqualityForAccumulators --- src/accumulator.jl | 4 ++++ test/test_accumulator.jl | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/accumulator.jl b/src/accumulator.jl index 03b836bd8..a93fe6cf4 100644 --- a/src/accumulator.jl +++ b/src/accumulator.jl @@ -30,6 +30,10 @@ length(a::Accumulator) = length(a.map) ## retrieval +get{T,V}(ct::Accumulator{T,V}, x::T, default) = get(ct.map, x, default) +# need to allow user specified default in order to +# correctly implement "informal" Associative interface + getindex{T,V}(ct::Accumulator{T,V}, x::T) = get(ct.map, x, zero(V)) haskey{T,V}(ct::Accumulator{T,V}, x::T) = haskey(ct.map, x) diff --git a/test/test_accumulator.jl b/test/test_accumulator.jl index abc957ae1..ad2d10c3a 100644 --- a/test/test_accumulator.jl +++ b/test/test_accumulator.jl @@ -66,3 +66,7 @@ ct4 = counter(Pair{Int,Int}) @test isa(ct4, Accumulator{Pair{Int,Int}}) @test push!(ct4, 1=>2) == 1 @test push!(ct4, 1=>2) == 2 + + +@test counter([2,3,4,4]) == counter([4,2,3,4]) +@test counter([2,3,4,4]) != counter([4,2,3,4,4])