Skip to content

Commit

Permalink
Use sizehint! instead of sizehint
Browse files Browse the repository at this point in the history
  • Loading branch information
kmsquire committed Mar 4, 2015
1 parent a284f97 commit 177145e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/DataStructures.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module DataStructures

using Compat
@compat import Base.sizehint!

import Base: length, isempty, start, next, done,
show, dump, empty!, getindex, setindex!, get, get!,
in, haskey, keys, merge, copy, cat,
push!, pop!, shift!, unshift!, insert!,
union!, delete!, similar, sizehint,
union!, delete!, similar,
isequal, hash,
map, reverse,
endof, first, last, eltype, getkey, values,
Expand All @@ -16,7 +17,7 @@ module DataStructures

export Deque, Stack, Queue
export deque, enqueue!, dequeue!, update!
export capacity, num_blocks, front, back, top, sizehint
@compat export capacity, num_blocks, front, back, top, sizehint!

export Accumulator, counter
export ClassifiedCollections
Expand Down
17 changes: 9 additions & 8 deletions src/defaultdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ DefaultDictBase{F,D<:Associative}(default::F, d::D) = ((K,V)=eltype(d); DefaultD
# Functions

# most functions are simply delegated to the wrapped dictionary
@delegate DefaultDictBase.d [ sizehint, empty!, setindex!, get, haskey,
getkey, pop!, delete!, start, done, next,
isempty, length ]
@compat @delegate DefaultDictBase.d [ sizehint!, empty!, setindex!, get, haskey,
getkey, pop!, delete!, start, done, next,
isempty, length ]

similar{K,V,F}(d::DefaultDictBase{K,V,F}) = DefaultDictBase{K,V,F}(d.default)
in{T<:DefaultDictBase}(key, v::Base.KeyIterator{T}) = key in keys(v.dict.d)
Expand Down Expand Up @@ -129,10 +129,11 @@ for (DefaultDict,O) in [(:DefaultDict, :Unordered), (:DefaultOrderedDict, :Order
## Functions

# Most functions are simply delegated to the wrapped DefaultDictBase object
@delegate $DefaultDict.d [ sizehint, empty!, setindex!,
getindex, get, get!, haskey,
getkey, pop!, delete!, start, next,
done, next, isempty, length]
# @compat is used for sizehint!
@compat @delegate $DefaultDict.d [ sizehint!, empty!, setindex!,
getindex, get, get!, haskey,
getkey, pop!, delete!, start, next,
done, next, isempty, length ]

similar{K,V,F}(d::$DefaultDict{K,V,F}) = $DefaultDict{K,V,F}(d.d.default)
in{T<:$DefaultDict}(key, v::Base.KeyIterator{T}) = key in keys(v.dict.d.d)
Expand Down Expand Up @@ -172,7 +173,7 @@ end

## Most functions are simply delegated to the wrapped DefaultDictBase object

# @delegate DefaultSortedDict.d [ sizehint, empty!, setindex!,
# @compat @delegate DefaultSortedDict.d [ sizehint, empty!, setindex!,
# getindex, get, get!, haskey,
# getkey, pop!, delete!, start, next,
# done, next, isempty, length]
Expand Down
2 changes: 1 addition & 1 deletion src/disjoint_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type DisjointSets{T}
function DisjointSets(xs) # xs must be iterable
imap = Dict{T,Int}()
n = length(xs)
sizehint!(imap, n)
@compat sizehint!(imap, n)
id = 0
for x in xs
imap[x] = (id += 1)
Expand Down
16 changes: 8 additions & 8 deletions src/hashdict.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HashDict

import Base: KeyIterator, ValueIterator, haskey, get, getkey, delete!,
pop!, empty!, filter!, setindex!, getindex, similar,
sizehint, length, filter, isempty, start, next, done,
keys, values, _tablesz, skip_deleted, serialize, deserialize, serialize_type
@compat import Base: KeyIterator, ValueIterator, haskey, get, getkey, delete!,
pop!, empty!, filter!, setindex!, getindex, similar,
sizehint!, length, filter, isempty, start, next, done,
keys, values, _tablesz, skip_deleted, serialize, deserialize, serialize_type

typealias Unordered Nothing
typealias Ordered Int
Expand All @@ -26,7 +26,7 @@ type HashDict{K,V,O<:Union(Ordered,Unordered)} <: Associative{K,V}
HashDict(p::Pair) = setindex!(HashDict{K,V,O}(), p.second, p.first)
function HashDict(ps::Pair{K,V}...)
h = HashDict{K,V,O}()
sizehint(h, length(ps))
@compat sizehint!(h, length(ps))
for p in ps
h[p.first] = p.second
end
Expand All @@ -47,7 +47,7 @@ type HashDict{K,V,O<:Union(Ordered,Unordered)} <: Associative{K,V}
end
function HashDict(kv)
h = HashDict{K,V,O}()
sizehint(h, length(kv))
@compat sizehint!(h, length(kv))
for (k,v) in kv
h[k] = v
end
Expand Down Expand Up @@ -92,7 +92,7 @@ end

function deserialize{K,V,O}(s, T::Type{HashDict{K,V,O}})
n = read(s, Int32)
t = T(); sizehint(t, n)
t = T(); @compat sizehint!(t, n)
for i = 1:n
k = deserialize(s)
v = deserialize(s)
Expand Down Expand Up @@ -252,7 +252,7 @@ function _compact_order{K,V}(h::HashDict{K,V,Ordered})
nothing
end

function sizehint(d::HashDict, newsz)
@compat function sizehint!(d::HashDict, newsz)
oldsz = length(d.slots)
if newsz <= oldsz
# todo: shrink
Expand Down
2 changes: 1 addition & 1 deletion src/heaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#
# - push!(h, v) add a value to the heap
#
# - sizehint(h) set size hint to a heap
# - sizehint!(h) set size hint to a heap
#
# - top(h) return the top value of a heap
#
Expand Down
12 changes: 6 additions & 6 deletions src/ordereddict.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ordered dict

import Base: haskey, get, get!, getkey, delete!, push!, pop!, empty!,
setindex!, getindex, sizehint, length, isempty, start,
@compat import Base: haskey, get, get!, getkey, delete!, push!, pop!, empty!,
setindex!, getindex, sizehint!, length, isempty, start,
next, done, keys, values, setdiff, setdiff!,
union, union!, intersect, isequal, filter, filter!,
hash, eltype
Expand Down Expand Up @@ -51,10 +51,10 @@ copy(d::OrderedDict) = OrderedDict(d)

## Most functions are simply delegated to the wrapped HashDict

@delegate OrderedDict.d [ haskey, get, get!, getkey, delete!, pop!,
empty!, setindex!, getindex, sizehint,
length, isempty, start, next, done, keys,
values ]
@compat @delegate OrderedDict.d [ haskey, get, get!, getkey, delete!, pop!,
empty!, setindex!, getindex, sizehint,
length, isempty, start, next, done, keys,
values ]

similar{K,V}(d::OrderedDict{K,V}) = OrderedDict{K,V}()
in{T<:OrderedDict}(key, v::Base.KeyIterator{T}) = key in keys(v.dict.d.d)
6 changes: 3 additions & 3 deletions src/orderedset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ OrderedSet() = OrderedSet{Any}()
OrderedSet(xs) = OrderedSet{eltype(xs)}(xs)


show(io::IO, s::OrderedSet) = (show(io, typeof(s)); Base.show_comma_array(io, s,'(',')'))
show(io::IO, s::OrderedSet) = (show(io, typeof(s)); print(io, "("); !isempty(s) && Base.show_vector(io, s,'[',']'); print(io, ")"))

@delegate OrderedSet.dict [isempty, length, sizehint]
@delegate OrderedSet.dict [isempty, length, @compat sizehint!]

eltype{T}(s::OrderedSet{T}) = T

Expand All @@ -43,7 +43,7 @@ done(s::OrderedSet, state) = done(s.dict, state)
next(s::OrderedSet, i) = (s.dict.keys[s.dict.order[i]], skip_deleted(s.dict,i+1))

# TODO: simplify me?
pop!(s::OrderedSet) = (val = s.dict.keys[start(s.dict)]; delete!(s.dict, val); val)
pop!(s::OrderedSet) = (val = s.dict.keys[s.dict.order[start(s.dict)]]; delete!(s.dict, val); val)

union(s::OrderedSet) = copy(s)
function union(s::OrderedSet, sets...)
Expand Down

0 comments on commit 177145e

Please sign in to comment.