Skip to content

Commit

Permalink
0.4 bindings deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Sep 16, 2015
1 parent 1f55a1a commit 5daaacf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 53 deletions.
30 changes: 15 additions & 15 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Usage::

One may also use other element types::

a = DisjointSets{String}(["a", "b", "c", "d"])
a = DisjointSets{AbstractString}(["a", "b", "c", "d"])
union!(a, "a", "b")
in_same_set(a, "c", "d")
push!(a, "f")
Expand Down Expand Up @@ -235,7 +235,7 @@ Note that to create an OrderedSet of a particular type, you must
specify the type in curly-braces::

# create an OrderedSet of Strings
strs = OrderedSet{String}()
strs = OrderedSet{AbstractString}()


----------------------------------
Expand Down Expand Up @@ -265,7 +265,7 @@ Constructors::
Examples using ``DefaultDict``::

dd = DefaultDict(1) # create an (Any=>Any) DefaultDict with a default value of 1
dd = DefaultDict(String, Int, 0) # create a (String=>Int) DefaultDict with a default value of 0
dd = DefaultDict(AbstractString, Int, 0) # create a (AbstractString=>Int) DefaultDict with a default value of 0

d = ['a'=>1, 'b'=>2]
dd = DefaultDict(0, d) # provide a default value to an existing dictionary
Expand All @@ -278,34 +278,34 @@ Examples using ``DefaultDict``::
dd = DefaultDict(()->myfunc()) # call function myfunc to provide the default value

# These all create the same default dict
dd = @compat DefaultDict(String, Vector{Int}, # Vector{Int}() is Julia v0.4 notation
dd = @compat DefaultDict(AbstractString, Vector{Int}, # Vector{Int}() is Julia v0.4 notation
() -> Vector{Int}()) # @compat allows it to be used on v0.3
dd = DefaultDict(String, Vector{Int}, () -> Int[])
dd = DefaultDict(AbstractString, Vector{Int}, () -> Int[])

# dd = DefaultDict(String, Vector{Int}, # **Note! Julia v0.4 and later only!
# dd = DefaultDict(AbstractString, Vector{Int}, # **Note! Julia v0.4 and later only!
# Vector{Int}) # the second Vector{Int} is called as a function

push!(dd["A"], 1)
push!(dd["B"], 2)

julia> dd
DefaultDict{String,Array{Int64,1},Function} with 2 entries:
DefaultDict{AbstractString,Array{Int64,1},Function} with 2 entries:
"B" => [2]
"A" => [1]

# create a Dictionary of type String=>DefaultDict{String, Int}, where the default of the
# create a Dictionary of type AbstractString=>DefaultDict{AbstractString, Int}, where the default of the
# inner set of DefaultDicts is zero
dd = DefaultDict(String, DefaultDict, () -> DefaultDict(String,Int,0))
dd = DefaultDict(AbstractString, DefaultDict, () -> DefaultDict(AbstractString,Int,0))

```

Note that in the last example, we need to use a function to create each new ``DefaultDict``.
If we forget, we will end up using the same ``DefaultDict`` for all default values::

julia> dd = DefaultDict(String, DefaultDict, DefaultDict(String,Int,0));
julia> dd = DefaultDict(AbstractString, DefaultDict, DefaultDict(AbstractString,Int,0));

julia> dd["a"]
DefaultDict{String,Int64,Int64,Dict{K,V}}()
DefaultDict{AbstractString,Int64,Int64,Dict{K,V}}()

julia> dd["b"]["a"] = 1
1
Expand All @@ -318,7 +318,7 @@ If we forget, we will end up using the same ``DefaultDict`` for all default valu
Trie
----

An implementation of the `Trie` data structure. This is an associative structure, with `String` keys::
An implementation of the `Trie` data structure. This is an associative structure, with `AbstractString` keys::

t=Trie{Int}()
t["Rob"]=42
Expand All @@ -330,7 +330,7 @@ An implementation of the `Trie` data structure. This is an associative structure
Constructors::

Trie(keys, values) # construct a Trie with the given keys and values
Trie(keys) # construct a Trie{Nothing} with the given keys and with values = nothing
Trie(keys) # construct a Trie{Void} with the given keys and with values = nothing
Trie(kvs::AbstractVector{(K, V)}) # construct a Trie from the given vector of (key, value) pairs
Trie(kvs::Associative{K, V}) # construct a Trie from the given associative structure

Expand Down Expand Up @@ -385,12 +385,12 @@ SortedSet has
only keys; it is an alternative to the built-in
``Set`` container. Internally,
SortedSet is implemented as a SortedDict in which the value type
is ``Nothing``.
is ``Void``.
Finally, SortedMultiDict is similar to SortedDict except that each key
can be associated with multiple values. The (key,value) pairs in
a SortedMultiDict are stored according to the sorted order for keys,
and (key,value) pairs with the same
key are stored in order of insertion.
key are stored in order of insertion.

The containers internally use a 2-3 tree, which is a
kind of balanced tree and is described in many elementary data
Expand Down
18 changes: 9 additions & 9 deletions src/hashdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ else
const SerState = Base.Serializer.SerializationState
end

typealias Unordered Nothing
@compat typealias Unordered Void
typealias Ordered Int

type HashDict{K,V,O<:Union(Ordered,Unordered)} <: Associative{K,V}
slots::Array{Uint8,1}
slots::Array{UInt8,1}
keys::Array{K,1}
vals::Array{V,1}
idxs::Array{O,1}
Expand All @@ -28,7 +28,7 @@ type HashDict{K,V,O<:Union(Ordered,Unordered)} <: Associative{K,V}

function HashDict()
n = 16
new(zeros(Uint8,n), Array(K,n), Array(V,n), Array(O,n), Array(O,0), 0, 0, identity)
new(zeros(UInt8,n), Array(K,n), Array(V,n), Array(O,n), Array(O,0), 0, 0, identity)
end
if VERSION >= v"0.4.0-dev+980"
HashDict(p::Pair) = setindex!(HashDict{K,V,O}(), p.second, p.first)
Expand Down Expand Up @@ -132,7 +132,7 @@ function rehash{K,V}(h::HashDict{K,V,Unordered}, newsz)
oldv = h.vals
sz = length(olds)

slots = zeros(Uint8,newsz)
slots = zeros(UInt8,newsz)
keys = Array(K, newsz)
vals = Array(V, newsz)
count0 = h.count
Expand Down Expand Up @@ -190,7 +190,7 @@ function rehash{K,V}(h::HashDict{K,V,Ordered}, newsz)
oldo = h.order
sz = length(olds)

slots = zeros(Uint8,newsz)
slots = zeros(UInt8,newsz)
keys = Array(K, newsz)
vals = Array(V, newsz)
idxs = Array(Int, newsz)
Expand Down Expand Up @@ -484,17 +484,17 @@ end

function _delete!(h::HashDict, index)
h.slots[index] = 0x2
ccall(:jl_arrayunset, Void, (Any, Uint), h.keys, index-1)
ccall(:jl_arrayunset, Void, (Any, Uint), h.vals, index-1)
@compat ccall(:jl_arrayunset, Void, (Any, UInt), h.keys, index-1)
@compat ccall(:jl_arrayunset, Void, (Any, UInt), h.vals, index-1)
h.ndel += 1
h.count -= 1
return h
end

function _delete!{K,V}(h::HashDict{K,V,Ordered}, index)
h.slots[index] = 0x2
ccall(:jl_arrayunset, Void, (Any, Uint), h.keys, index-1)
ccall(:jl_arrayunset, Void, (Any, Uint), h.vals, index-1)
@compat ccall(:jl_arrayunset, Void, (Any, UInt), h.keys, index-1)
@compat ccall(:jl_arrayunset, Void, (Any, UInt), h.vals, index-1)
h.order[h.idxs[index]] = 0
h.ndel += 1
h.count -= 1
Expand Down
8 changes: 4 additions & 4 deletions src/orderedset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# TODO: Most of these functions should be removed once AbstractSet is introduced there
# (see https://github.com/JuliaLang/julia/issues/5533)

immutable OrderedSet{T}
dict::HashDict{T,Nothing,Ordered}
@compat immutable OrderedSet{T}
dict::HashDict{T,Void,Ordered}

OrderedSet() = new(HashDict{T,Nothing,Ordered}())
OrderedSet(xs) = union!(new(HashDict{T,Nothing,Ordered}()), xs)
OrderedSet() = new(HashDict{T,Void,Ordered}())
OrderedSet(xs) = union!(new(HashDict{T,Void,Ordered}()), xs)
end
OrderedSet() = OrderedSet{Any}()
OrderedSet(xs) = OrderedSet{eltype(xs)}(xs)
Expand Down
16 changes: 7 additions & 9 deletions src/sortedSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
## methods similiar to those of the julia Set.


type SortedSet{K, Ord <: Ordering}
bt::BalancedTree23{K,Nothing,Ord}
@compat type SortedSet{K, Ord <: Ordering}
bt::BalancedTree23{K,Void,Ord}

## Zero-argument constructor, or possibly one argument to specify order.
## Zero-argument constructor, or possibly one argument to specify order.

function SortedSet(o::Ord=Forward)
bt1 = BalancedTree23{K,Nothing,Ord}(o)
bt1 = BalancedTree23{K,Void,Ord}(o)
new(bt1)
end


end


Expand All @@ -26,8 +24,8 @@ typealias SetSemiToken IntSemiToken

## This one takes an iterable; ordering type is optional.

SortedSet{Ord <: Ordering}(iter, o::Ord=Forward) =
sortedset_with_eltype(iter, eltype(iter), o)
SortedSet{Ord <: Ordering}(iter, o::Ord=Forward) =
sortedset_with_eltype(iter, eltype(iter), o)

function sortedset_with_eltype{K,Ord}(iter, ::Type{K}, o::Ord)
h = SortedSet{K,Ord}(o)
Expand All @@ -42,7 +40,7 @@ end
## This function looks up a key in the tree;
## if not found, then it returns a marker for the
## end of the tree.

@inline function find(m::SortedSet, k_)
ll, exactfound = findkey(m.bt, convert(keytype(m),k_))
IntSemiToken(exactfound? ll : 2)
Expand Down
26 changes: 13 additions & 13 deletions src/trie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ type Trie{T}
end

Trie() = Trie{Any}()
Trie{K<:String,V}(ks::AbstractVector{K}, vs::AbstractVector{V}) = Trie{V}(ks, vs)
Trie{K<:String,V}(kv::AbstractVector{@compat Tuple{K,V}}) = Trie{V}(kv)
Trie{K<:String,V}(kv::Associative{K,V}) = Trie{V}(kv)
Trie{K<:String}(ks::AbstractVector{K}) = Trie{Nothing}(ks, similar(ks, Nothing))
Trie{K<:AbstractString,V}(ks::AbstractVector{K}, vs::AbstractVector{V}) = Trie{V}(ks, vs)
Trie{K<:AbstractString,V}(kv::AbstractVector{@compat Tuple{K,V}}) = Trie{V}(kv)
Trie{K<:AbstractString,V}(kv::Associative{K,V}) = Trie{V}(kv)
@compat Trie{K<:AbstractString}(ks::AbstractVector{K}) = Trie{Void}(ks, similar(ks, Void))

function setindex!{T}(t::Trie{T}, val::T, key::String)
function setindex!{T}(t::Trie{T}, val::T, key::AbstractString)
node = t
for char in key
if !haskey(node.children, char)
Expand All @@ -45,15 +45,15 @@ function setindex!{T}(t::Trie{T}, val::T, key::String)
node.value = val
end

function getindex(t::Trie, key::String)
function getindex(t::Trie, key::AbstractString)
node = subtrie(t, key)
if node != nothing && node.is_key
return node.value
end
throw(KeyError("key not found: $key"))
end

function subtrie(t::Trie, prefix::String)
function subtrie(t::Trie, prefix::AbstractString)
node = t
for char in prefix
if !haskey(node.children, char)
Expand All @@ -65,20 +65,20 @@ function subtrie(t::Trie, prefix::String)
node
end

function haskey(t::Trie, key::String)
function haskey(t::Trie, key::AbstractString)
node = subtrie(t, key)
node != nothing && node.is_key
end

function get(t::Trie, key::String, notfound)
function get(t::Trie, key::AbstractString, notfound)
node = subtrie(t, key)
if node != nothing && node.is_key
return node.value
end
notfound
end

function keys(t::Trie, prefix::String="", found=String[])
function keys(t::Trie, prefix::AbstractString="", found=AbstractString[])
if t.is_key
push!(found, prefix)
end
Expand All @@ -88,7 +88,7 @@ function keys(t::Trie, prefix::String="", found=String[])
found
end

function keys_with_prefix(t::Trie, prefix::String)
function keys_with_prefix(t::Trie, prefix::AbstractString)
st = subtrie(t, prefix)
st != nothing ? keys(st,prefix) : []
end
Expand All @@ -100,7 +100,7 @@ end
# see the comments and implementation below for details.
immutable TrieIterator
t::Trie
str::String
str::AbstractString
end

# At the start, there is no previous iteration,
Expand All @@ -125,4 +125,4 @@ function done(it::TrieIterator, state)
return !(it.str[i] in keys(t.children))
end

path(t::Trie, str::String) = TrieIterator(t, str)
path(t::Trie, str::AbstractString) = TrieIterator(t, str)
4 changes: 2 additions & 2 deletions test/test_defaultdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Base.Test

# construction
@test_throws ErrorException DefaultDict()
@test_throws ErrorException DefaultDict(String, Int)
@test_throws ErrorException DefaultDict(AbstractString, Int)

if VERSION >= v"0.4.0-dev+980"
@test typeof(DefaultDict(0.0, 1 => 1.0)) == DefaultDict{Int,Float64,Float64}
Expand Down Expand Up @@ -70,7 +70,7 @@ s = similar(d)

# construction
@test_throws ErrorException DefaultOrderedDict()
@test_throws ErrorException DefaultOrderedDict(String, Int)
@test_throws ErrorException DefaultOrderedDict(AbstractString, Int)

# empty dictionary
d = DefaultOrderedDict(Char, Int, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/test_trie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kvs = collect(zip(ks, vs))
@test typeof(Trie(ks, vs)) == Trie{Int}
@test typeof(Trie(kvs)) == Trie{Int}
@test typeof(Trie(Dict(kvs))) == Trie{Int}
@test typeof(Trie(ks)) == Trie{Nothing}
@test typeof(Trie(ks)) == @compat Trie{Void}


# path iterator
Expand Down

0 comments on commit 5daaacf

Please sign in to comment.