Skip to content

Commit

Permalink
Merge pull request #237 from cstjean/pull-request/483460f4
Browse files Browse the repository at this point in the history
Add warning on unordered to ordered conversion. Fixes #216
  • Loading branch information
kmsquire committed Jan 26, 2017
2 parents 6846f65 + eae3098 commit 52b74de
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/default_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ for _Dict in [:Dict, :OrderedDict]
end
end

isordered{T<:DefaultOrderedDict}(::Type{T}) = true

## This should be uncommented to provide a DefaultSortedDict

Expand Down
14 changes: 13 additions & 1 deletion src/ordered_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,20 @@ similar{K,V}(d::OrderedDict{K,V}) = OrderedDict{K,V}()
length(d::OrderedDict) = length(d.keys) - d.ndel
isempty(d::OrderedDict) = (length(d)==0)

"""
isordered(::Type)
Property of associative containers, that is `true` if the container type has a
defined order (such as `OrderedDict` and `SortedDict`), and `false` otherwise.
"""
isordered{T<:Associative}(::Type{T}) = false
isordered{T<:OrderedDict}(::Type{T}) = true

# conversion between OrderedDict types
function convert{K,V}(::Type{OrderedDict{K,V}},d::Associative)
function convert{K,V}(::Type{OrderedDict{K,V}}, d::Associative)
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
h = OrderedDict{K,V}()
for (k,v) in d
ck = convert(K,k)
Expand Down
2 changes: 2 additions & 0 deletions src/sorted_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,5 @@ end

similar{K,D,Ord<:Ordering}(m::SortedDict{K,D,Ord}) =
SortedDict{K,D,Ord}(orderobject(m))

isordered{T<:SortedDict}(::Type{T}) = true
2 changes: 2 additions & 0 deletions src/sorted_multi_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,5 @@ end

similar{K,D,Ord<:Ordering}(m::SortedMultiDict{K,D,Ord}) =
SortedMultiDict(K[], D[], orderobject(m))

isordered{T<:SortedMultiDict}(::Type{T}) = true
3 changes: 3 additions & 0 deletions test/test_default_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@ end
# Alternate constructor
@test isa(DefaultOrderedDict(0.0), DefaultOrderedDict{Any, Any, Float64})
@test isa(DefaultOrderedDict(0.0, [(1, 1.0)]), DefaultOrderedDict{Int, Float64, Float64})

# issue #216
@test DataStructures.isordered(DefaultOrderedDict{Int, String})
end
4 changes: 4 additions & 0 deletions test/test_ordered_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ for k5886 in keys(d5886)
d5886[k5886] += 1
end

# issue #216
@test DataStructures.isordered(OrderedDict{Int, String})
@test !DataStructures.isordered(Dict{Int, String})

# Test merging
let
a = OrderedDict("foo" => 0.0, "bar" => 42.0)
Expand Down
5 changes: 5 additions & 0 deletions test/test_sorted_containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ end
merge!(c3,c3)
@test isequal(c3,c1)
checkcorrectness(c3.bt, false)

# issue #216
@test DataStructures.isordered(SortedDict{Int, String})
end


Expand Down Expand Up @@ -1444,6 +1447,8 @@ end
end
@test count == 2
end
# issue #216
@test DataStructures.isordered(SortedMultiDict{Int, String})
end


Expand Down

0 comments on commit 52b74de

Please sign in to comment.