Skip to content

Commit

Permalink
Merge pull request #353 from fredrikekre/fe/void-abstractdict
Browse files Browse the repository at this point in the history
Void -> Nothing and Associative -> AbstractDict
  • Loading branch information
oxinabox committed Jan 5, 2018
2 parents f1d976a + 3bb9c57 commit 91ccb8d
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
@@ -1,2 +1,2 @@
julia 0.6
Compat 0.39
Compat 0.41
2 changes: 1 addition & 1 deletion src/DataStructures.jl
Expand Up @@ -16,7 +16,7 @@ module DataStructures
union, intersect, symdiff, setdiff, issubset,
find, searchsortedfirst, searchsortedlast, endof, in

import Compat.uninitialized
using Compat: uninitialized, Nothing, Cvoid, AbstractDict

export Deque, Stack, Queue, CircularDeque
export deque, enqueue!, dequeue!, dequeue_pair!, update!, reverse_iter
Expand Down
6 changes: 3 additions & 3 deletions src/accumulator.jl
@@ -1,6 +1,6 @@
# A counter type

struct Accumulator{T, V<:Number} <: Associative{T,V}
struct Accumulator{T, V<:Number} <: AbstractDict{T,V}
map::Dict{T,V}
end

Expand Down Expand Up @@ -43,7 +43,7 @@ length(a::Accumulator) = length(a.map)

get(ct::Accumulator, x, default) = get(ct.map, x, default)
# need to allow user specified default in order to
# correctly implement "informal" Associative interface
# correctly implement "informal" AbstractDict interface

getindex(ct::Accumulator{T,V}, x) where {T,V} = get(ct.map, x, zero(V))

Expand Down Expand Up @@ -80,7 +80,7 @@ inc!(ct::Accumulator{T,V}, x) where {T,V} = inc!(ct, x, one(V))
push!(ct::Accumulator, x) = inc!(ct, x)
push!(ct::Accumulator, x, a::Number) = inc!(ct, x, a)

# To remove ambiguities related to Accumulator now being a subtype of Associative
# To remove ambiguities related to Accumulator now being a subtype of AbstractDict
push!(ct::Accumulator, x::Pair) = inc!(ct, x)


Expand Down
4 changes: 2 additions & 2 deletions src/container_loops.jl
Expand Up @@ -184,7 +184,7 @@ end



# Next definition needed to break ambiguity with keys(Associative) from Dict.jl
# Next definition needed to break ambiguity with keys(AbstractDict) from Dict.jl

@inline keys(ba::SortedDict{K,D,Ord}) where {K, D, Ord <: Ordering} = SDMKeyIteration(ba)
@inline keys(ba::T) where {T <: SDMIterableTypesBase} = SDMKeyIteration(ba)
Expand All @@ -198,7 +198,7 @@ in(k, keyit::SDMKeyIteration{SortedMultiDict{K,D,Ord}}) where {K,D,Ord <: Orderi



# Next definition needed to break ambiguity with values(Associative) from Dict.jl
# Next definition needed to break ambiguity with values(AbstractDict) from Dict.jl
@inline values(ba::SortedDict{K,D,Ord}) where {K, D, Ord <: Ordering} = SDMValIteration(ba)
@inline values(ba::T) where {T <: SDMIterableTypesBase} = SDMValIteration(ba)
@inline semitokens(ba::T) where {T <: SDMIterableTypesBase} = SDMSemiTokenIteration(ba)
Expand Down
16 changes: 8 additions & 8 deletions src/default_dict.jl
Expand Up @@ -14,12 +14,12 @@
# subclassed.
#

struct DefaultDictBase{K,V,F,D} <: Associative{K,V}
struct DefaultDictBase{K,V,F,D} <: AbstractDict{K,V}
default::F
d::D

check_D(D,K,V) = (D <: Associative{K,V}) ||
throw(ArgumentError("Default dict must be <: Associative{$K,$V}"))
check_D(D,K,V) = (D <: AbstractDict{K,V}) ||
throw(ArgumentError("Default dict must be <: AbstractDict{$K,$V}"))

DefaultDictBase{K,V,F,D}(x::F, kv::AbstractArray{Tuple{K,V}}) where {K,V,F,D} =
(check_D(D,K,V); new{K,V,F,D}(x, D(kv)))
Expand All @@ -41,7 +41,7 @@ DefaultDictBase(k,v) = throw(ArgumentError("no default specified"))
DefaultDictBase(default::F) where {F} = DefaultDictBase{Any,Any,F,Dict{Any,Any}}(default)
DefaultDictBase(default::F, kv::AbstractArray{Tuple{K,V}}) where {K,V,F} = DefaultDictBase{K,V,F,Dict{K,V}}(default, kv)
DefaultDictBase(default::F, ps::Pair{K,V}...) where {K,V,F} = DefaultDictBase{K,V,F,Dict{K,V}}(default, ps...)
DefaultDictBase(default::F, d::D) where {F,D<:Associative} = (K=keytype(d); V=valtype(d); DefaultDictBase{K,V,F,D}(default, d))
DefaultDictBase(default::F, d::D) where {F,D<:AbstractDict} = (K=keytype(d); V=valtype(d); DefaultDictBase{K,V,F,D}(default, d))

# Constructor for DefaultDictBase{Int,Float64}(0.0)
DefaultDictBase{K,V}(default::F) where {K,V,F} = DefaultDictBase{K,V,F,Dict{K,V}}(default)
Expand Down Expand Up @@ -87,7 +87,7 @@ end
for _Dict in [:Dict, :OrderedDict]
DefaultDict = Symbol("Default"*string(_Dict))
@eval begin
struct $DefaultDict{K,V,F} <: Associative{K,V}
struct $DefaultDict{K,V,F} <: AbstractDict{K,V}
d::DefaultDictBase{K,V,F,$_Dict{K,V}}

$DefaultDict{K,V,F}(x, ps::Pair{K,V}...) where {K,V,F} =
Expand All @@ -111,7 +111,7 @@ for _Dict in [:Dict, :OrderedDict]
$DefaultDict(default::F, kv::AbstractArray{Tuple{K,V}}) where {K,V,F} = $DefaultDict{K,V,F}(default, kv)
$DefaultDict(default::F, ps::Pair{K,V}...) where {K,V,F} = $DefaultDict{K,V,F}(default, ps...)

$DefaultDict(default::F, d::Associative) where {F} = ((K,V)= (Base.keytype(d), Base.valtype(d)); $DefaultDict{K,V,F}(default, $_Dict(d)))
$DefaultDict(default::F, d::AbstractDict) where {F} = ((K,V)= (Base.keytype(d), Base.valtype(d)); $DefaultDict{K,V,F}(default, $_Dict(d)))

# Constructor syntax: DefaultDictBase{Int,Float64}(default)
$DefaultDict{K,V}() where {K,V} = throw(ArgumentError("$DefaultDict: no default specified"))
Expand Down Expand Up @@ -155,7 +155,7 @@ isordered(::Type{T}) where {T<:DefaultOrderedDict} = true

## This should be uncommented to provide a DefaultSortedDict

# struct DefaultSortedDict{K,V,F} <: Associative{K,V}
# struct DefaultSortedDict{K,V,F} <: AbstractDict{K,V}
# d::DefaultDictBase{K,V,F,SortedDict{K,V}}

# DefaultSortedDict(x, kv::AbstractArray{(K,V)}) = new(DefaultDictBase{K,V,F,SortedDict{K,V}}(x, kv))
Expand All @@ -179,7 +179,7 @@ isordered(::Type{T}) where {T<:DefaultOrderedDict} = true
# DefaultSortedDict{K,V,F}(::Type{K}, ::Type{V}, default::F) = DefaultSortedDict{K,V,F}(default)
# DefaultSortedDict{K,V,F}(default::F, kv::AbstractArray{(K,V)}) = DefaultSortedDict{K,V,F}(default, kv)

# DefaultSortedDict{F}(default::F, d::Associative) = ((K,V)=eltype(d); DefaultSortedDict{K,V,F}(default, SortedDict(d)))
# DefaultSortedDict{F}(default::F, d::AbstractDict) = ((K,V)=eltype(d); DefaultSortedDict{K,V,F}(default, SortedDict(d)))

## Functions

Expand Down
18 changes: 9 additions & 9 deletions src/ordered_dict.jl
Expand Up @@ -13,7 +13,7 @@ import Base: haskey, get, get!, getkey, delete!, push!, pop!, empty!,
`OrderedDict`s are simply dictionaries whose entries have a particular order. The order
refers to insertion order, which allows deterministic iteration over the dictionary or set.
"""
mutable struct OrderedDict{K,V} <: Associative{K,V}
mutable struct OrderedDict{K,V} <: AbstractDict{K,V}
slots::Array{Int32,1}
keys::Array{K,1}
vals::Array{V,1}
Expand Down Expand Up @@ -63,7 +63,7 @@ OrderedDict(kv::Tuple{Vararg{Pair}}) = OrderedDict{Any,An

OrderedDict(kv::AbstractArray{Tuple{K,V}}) where {K,V} = OrderedDict{K,V}(kv)
OrderedDict(kv::AbstractArray{Pair{K,V}}) where {K,V} = OrderedDict{K,V}(kv)
OrderedDict(kv::Associative{K,V}) where {K,V} = OrderedDict{K,V}(kv)
OrderedDict(kv::AbstractDict{K,V}) where {K,V} = OrderedDict{K,V}(kv)

OrderedDict(ps::Pair{K,V}...) where {K,V} = OrderedDict{K,V}(ps)
OrderedDict(ps::Pair{K}...,) where {K} = OrderedDict{K,Any}(ps)
Expand Down Expand Up @@ -98,11 +98,11 @@ isempty(d::OrderedDict) = (length(d)==0)
Property of associative containers, that is `true` if the container type has a
defined order (such as `OrderedDict` and `SortedDict`), and `false` otherwise.
"""
isordered(::Type{T}) where {T<:Associative} = false
isordered(::Type{T}) where {T<:AbstractDict} = false
isordered(::Type{T}) where {T<:OrderedDict} = true

# conversion between OrderedDict types
function convert(::Type{OrderedDict{K,V}}, d::Associative) where {K,V}
function convert(::Type{OrderedDict{K,V}}, d::AbstractDict) where {K,V}
if !isordered(typeof(d))
Base.depwarn("Conversion to OrderedDict is deprecated for unordered associative containers (in this case, $(typeof(d))). Use an ordered or sorted associative type, such as SortedDict and OrderedDict.", :convert)
end
Expand Down Expand Up @@ -280,11 +280,11 @@ end
function _setindex!(h::OrderedDict, v, key, index)
hk, hv = h.keys, h.vals
#push!(h.keys, key)
ccall(:jl_array_grow_end, Void, (Any, UInt), hk, 1)
ccall(:jl_array_grow_end, Cvoid, (Any, UInt), hk, 1)
nk = length(hk)
@inbounds hk[nk] = key
#push!(h.vals, v)
ccall(:jl_array_grow_end, Void, (Any, UInt), hv, 1)
ccall(:jl_array_grow_end, Cvoid, (Any, UInt), hv, 1)
@inbounds hv[nk] = v
@inbounds h.slots[index] = nk
h.dirty = true
Expand Down Expand Up @@ -409,8 +409,8 @@ end
function _delete!(h::OrderedDict, index)
@inbounds ki = h.slots[index]
@inbounds h.slots[index] = -ki
ccall(:jl_arrayunset, Void, (Any, UInt), h.keys, ki-1)
ccall(:jl_arrayunset, Void, (Any, UInt), h.vals, ki-1)
ccall(:jl_arrayunset, Cvoid, (Any, UInt), h.keys, ki-1)
ccall(:jl_arrayunset, Cvoid, (Any, UInt), h.vals, ki-1)
h.ndel += 1
h.dirty = true
h
Expand All @@ -436,7 +436,7 @@ else
end
next(v::ValueIterator{T}, i) where {T<:OrderedDict} = (v.dict.vals[i], i+1)

function merge(d::OrderedDict, others::Associative...)
function merge(d::OrderedDict, others::AbstractDict...)
K, V = keytype(d), valtype(d)
for other in others
K = promote_type(K, keytype(other))
Expand Down
8 changes: 4 additions & 4 deletions src/ordered_set.jl
Expand Up @@ -6,10 +6,10 @@
# (see https://github.com/JuliaLang/julia/issues/5533)

struct OrderedSet{T}
dict::OrderedDict{T,Void}
dict::OrderedDict{T,Nothing}

OrderedSet{T}() where {T} = new{T}(OrderedDict{T,Void}())
OrderedSet{T}(xs) where {T} = union!(new{T}(OrderedDict{T,Void}()), xs)
OrderedSet{T}() where {T} = new{T}(OrderedDict{T,Nothing}())
OrderedSet{T}(xs) where {T} = union!(new{T}(OrderedDict{T,Nothing}()), xs)
end
OrderedSet() = OrderedSet{Any}()
OrderedSet(xs) = OrderedSet{eltype(xs)}(xs)
Expand Down Expand Up @@ -52,7 +52,7 @@ pop!(s::OrderedSet) = pop!(s.dict)[1]

union(s::OrderedSet) = copy(s)
function union(s::OrderedSet, sets...)
u = OrderedSet{Base.join_eltype(s, sets...)}()
u = OrderedSet{Base.promote_eltype(s, sets...)}()
union!(u,s)
for t in sets
union!(u,t)
Expand Down
2 changes: 1 addition & 1 deletion src/priorityqueue.jl
Expand Up @@ -23,7 +23,7 @@ PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
"a" => 2
```
"""
mutable struct PriorityQueue{K,V,O<:Ordering} <: Associative{K,V}
mutable struct PriorityQueue{K,V,O<:Ordering} <: AbstractDict{K,V}
# Binary heap of (element, priority) pairs.
xs::Array{Pair{K,V}, 1}
o::O
Expand Down
12 changes: 6 additions & 6 deletions src/sorted_dict.jl
@@ -1,7 +1,7 @@
## A SortedDict is a wrapper around balancedTree with
## methods similiar to those of Julia container Dict.

mutable struct SortedDict{K, D, Ord <: Ordering} <: Associative{K,D}
mutable struct SortedDict{K, D, Ord <: Ordering} <: AbstractDict{K,D}
bt::BalancedTree23{K,D,Ord}

## Base constructors
Expand Down Expand Up @@ -99,9 +99,9 @@ information about ordering.
"""
SortedDict{K,D}(o::Ord, ps::Pair...) where {K,D,Ord<:Ordering} = SortedDict{K,D,Ord}(o, ps)

# Construction from Associatives

SortedDict(o::Ord, d::Associative{K,D}) where {K,D,Ord<:Ordering} = SortedDict{K,D,Ord}(o, d)
# Construction from AbstractDicts
SortedDict(o::Ord, d::AbstractDict{K,D}) where {K,D,Ord<:Ordering} = SortedDict{K,D,Ord}(o, d)

## Construction from iteratables of Pairs/Tuples

Expand Down Expand Up @@ -522,7 +522,7 @@ end


function mergetwo!(m::SortedDict{K,D,Ord},
m2::Associative{K,D}) where {K,D,Ord <: Ordering}
m2::AbstractDict{K,D}) where {K,D,Ord <: Ordering}
for (k,v) in m2
m[convert(K,k)] = convert(D,v)
end
Expand Down Expand Up @@ -573,7 +573,7 @@ provides equivalent functionality. Time: O(*cN* log *N*), where *N*
is the total size of all the arguments.
"""
function merge!(m::SortedDict{K,D,Ord},
others::Associative{K,D}...) where {K,D,Ord <: Ordering}
others::AbstractDict{K,D}...) where {K,D,Ord <: Ordering}
for o in others
mergetwo!(m,o)
end
Expand All @@ -594,7 +594,7 @@ function is not available for SortedSet, but the `union` function
*N*), where *N* is the total size of all the arguments.
"""
function merge(m::SortedDict{K,D,Ord},
others::Associative{K,D}...) where {K,D,Ord <: Ordering}
others::AbstractDict{K,D}...) where {K,D,Ord <: Ordering}
result = packcopy(m)
merge!(result, others...)
result
Expand Down
12 changes: 6 additions & 6 deletions src/sorted_multi_dict.jl
Expand Up @@ -81,8 +81,8 @@ SortedMultiDict(o::Ordering, ps::Pair...) = SortedMultiDict(o, ps)
SortedMultiDict{K,D}(ps::Pair...) where {K,D} = SortedMultiDict{K,D,ForwardOrdering}(Forward, ps)
SortedMultiDict{K,D}(o::Ord, ps::Pair...) where {K,D,Ord<:Ordering} = SortedMultiDict{K,D,Ord}(o, ps)

# Construction from Associatives
SortedMultiDict(o::Ord, d::Associative{K,D}) where {K,D,Ord<:Ordering} = SortedMultiDict{K,D,Ord}(o, d)
# Construction from AbstractDicts
SortedMultiDict(o::Ord, d::AbstractDict{K,D}) where {K,D,Ord<:Ordering} = SortedMultiDict{K,D,Ord}(o, d)

## Construction from iteratables of Pairs/Tuples

Expand Down Expand Up @@ -376,10 +376,10 @@ function isequal(m1::SortedMultiDict, m2::SortedMultiDict)
end
end

const SDorAssociative = Union{Associative,SortedMultiDict}
const SDorAbstractDict = Union{AbstractDict,SortedMultiDict}

function mergetwo!(m::SortedMultiDict{K,D,Ord},
m2::SDorAssociative) where {K,D,Ord <: Ordering}
m2::SDorAbstractDict) where {K,D,Ord <: Ordering}
for (k,v) in m2
insert!(m.bt, convert(K,k), convert(D,v), true)
end
Expand Down Expand Up @@ -428,7 +428,7 @@ provides equivalent functionality. Time: O(*cN* log *N*), where *N*
is the total size of all the arguments.
"""
function merge!(m::SortedMultiDict{K,D,Ord},
others::SDorAssociative...) where {K,D,Ord <: Ordering}
others::SDorAbstractDict...) where {K,D,Ord <: Ordering}
for o in others
mergetwo!(m,o)
end
Expand All @@ -449,7 +449,7 @@ function is not available for SortedSet, but the `union` function
*N*), where *N* is the total size of all the arguments.
"""
function merge(m::SortedMultiDict{K,D,Ord},
others::SDorAssociative...) where {K,D,Ord <: Ordering}
others::SDorAbstractDict...) where {K,D,Ord <: Ordering}
result = packcopy(m)
merge!(result, others...)
result
Expand Down
4 changes: 2 additions & 2 deletions src/sorted_set.jl
Expand Up @@ -16,10 +16,10 @@ array) and ordering object `o`. The ordering object defaults to
`Forward` if not specified.
"""
mutable struct SortedSet{K, Ord <: Ordering}
bt::BalancedTree23{K,Void,Ord}
bt::BalancedTree23{K,Nothing,Ord}

function SortedSet{K,Ord}(o::Ord=Forward, iter=[]) where {K,Ord<:Ordering}
sorted_set = new{K,Ord}(BalancedTree23{K,Void,Ord}(o))
sorted_set = new{K,Ord}(BalancedTree23{K,Nothing,Ord}(o))

for item in iter
push!(sorted_set, item)
Expand Down
4 changes: 2 additions & 2 deletions src/trie.jl
Expand Up @@ -30,8 +30,8 @@ end
Trie() = Trie{Any}()
Trie(ks::AbstractVector{K}, vs::AbstractVector{V}) where {K<:AbstractString,V} = Trie{V}(ks, vs)
Trie(kv::AbstractVector{Tuple{K,V}}) where {K<:AbstractString,V} = Trie{V}(kv)
Trie(kv::Associative{K,V}) where {K<:AbstractString,V} = Trie{V}(kv)
Trie(ks::AbstractVector{K}) where {K<:AbstractString} = Trie{Void}(ks, similar(ks, Void))
Trie(kv::AbstractDict{K,V}) where {K<:AbstractString,V} = Trie{V}(kv)
Trie(ks::AbstractVector{K}) where {K<:AbstractString} = Trie{Nothing}(ks, similar(ks, Nothing))

function setindex!(t::Trie{T}, val::T, key::AbstractString) where T
node = t
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Expand Up @@ -2,6 +2,7 @@ using Compat.Test
using DataStructures
const IntSet = DataStructures.IntSet
using Primes, Compat
using Compat.Unicode: lowercase

@test isempty(detect_ambiguities(Base, Core, DataStructures))

Expand Down
2 changes: 1 addition & 1 deletion test/test_ordered_dict.jl
Expand Up @@ -212,7 +212,7 @@
let
local bar
bestkey(d, key) = key
bestkey(d::Associative{K,V}, key) where {K<:AbstractString,V} = string(key)
bestkey(d::AbstractDict{K,V}, key) where {K<:AbstractString,V} = string(key)
bar(x) = bestkey(x, :y)
@test bar(OrderedDict([(:x, [1,2,5])])) == :y
@test bar(OrderedDict([("x", [1,2,5])])) == "y"
Expand Down
8 changes: 4 additions & 4 deletions test/test_sorted_containers.jl
Expand Up @@ -578,17 +578,17 @@ end
@test x1 === dfc[1]
@test x1 === get!(dfc, 1, [1000])
@test x1 === get(dfc, 1, [1000])

x2 = get!(()->[2], dfc, 2)
@test x2 == [2]
@test x2 === dfc[2]
@test x2 === get!(()->[1000], dfc, 2)
@test x2 === get(()->[1000], dfc, 2)

@test [42] == get(()->[42], dfc, 3)
@test !haskey(dfc, 3)
@test [43] == get(dfc, 4, [43])
@test !haskey(dfc, 4)
@test !haskey(dfc, 4)
end


Expand Down Expand Up @@ -1816,7 +1816,7 @@ function sorted_dict_timing2(numtrial::Int, expectedk::String, expectedd::String
end
end

function SDConstruct(a::Associative; lt::Function=isless, by::Function=identity)
function SDConstruct(a::AbstractDict; lt::Function=isless, by::Function=identity)
if by == identity
return SortedDict(a, Lt(lt))
elseif lt == isless
Expand Down
2 changes: 1 addition & 1 deletion test/test_trie.jl
Expand Up @@ -24,7 +24,7 @@
@test isa(Trie(ks, vs), Trie{Int})
@test isa(Trie(kvs), Trie{Int})
@test isa(Trie(Dict(kvs)), Trie{Int})
@test isa(Trie(ks), Trie{Void})
@test isa(Trie(ks), Trie{Nothing})


# path iterator
Expand Down

0 comments on commit 91ccb8d

Please sign in to comment.