Skip to content

Commit

Permalink
Merge pull request #590 from eulerkochy/patch-1
Browse files Browse the repository at this point in the history
Update code as per Julia's community guidelines
  • Loading branch information
eulerkochy authored Mar 30, 2020
2 parents 063094f + 3e26db8 commit a4246e7
Show file tree
Hide file tree
Showing 23 changed files with 133 additions and 159 deletions.
18 changes: 9 additions & 9 deletions src/accumulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
A accumulator is a data structure that maintains an accumulated total for each key.
The particular case where those totals are integers is a counter.
"""
struct Accumulator{T, V<:Number} <: AbstractDict{T,V}
map::Dict{T,V}
struct Accumulator{T, V <: Number} <: AbstractDict{T, V}
map::Dict{T, V}
end

## constructors

Accumulator{T, V}() where {T,V<:Number} = Accumulator{T,V}(Dict{T,V}())
Accumulator{T, V}() where {T, V <: Number} = Accumulator{T, V}(Dict{T, V}())
Accumulator(map::AbstractDict) = Accumulator(Dict(map))
Accumulator(ps::Pair...) = Accumulator(Dict(ps))

counter(T::Type) = Accumulator{T,Int}()
counter(T::Type) = Accumulator{T, Int}()
counter(dct::AbstractDict{T, V}) where {T, V<:Integer} = Accumulator{T, V}(Dict(dct))

"""
Expand All @@ -33,7 +33,7 @@ function counter(seq)
end

eltype_for_accumulator(seq::T) where T = eltype(T)
function eltype_for_accumulator(seq::T) where {T<:Base.Generator}
function eltype_for_accumulator(seq::Base.Generator)
Base.@default_eltype(seq)
end

Expand Down Expand Up @@ -73,7 +73,7 @@ iterate(ct::Accumulator, s...) = iterate(ct.map, s...)
Increments the count for `x` by `v` (defaulting to one)
"""
inc!(ct::Accumulator, x, v::Number) = (ct[x] += v)
inc!(ct::Accumulator{T,V}, x) where {T,V} = inc!(ct, x, one(V))
inc!(ct::Accumulator{T, V}, x) where {T, V} = inc!(ct, x, one(V))

# inc! is preferred over push!, but we need to provide push! for the Bag interpreation
# which is used by classified_collections.jl
Expand Down Expand Up @@ -105,7 +105,7 @@ function merge!(ct::Accumulator, other::Accumulator)
for (x, v) in other
inc!(ct, x, v)
end
ct
return ct
end


Expand Down Expand Up @@ -184,7 +184,7 @@ nsmallest(acc::Accumulator, n) = partialsort!(collect(acc), 1:n, by=last, rev=fa
###########################################################
## Multiset operations

struct MultiplicityException{K,V} <: Exception
struct MultiplicityException{K, V} <: Exception
k::K
v::V
end
Expand Down Expand Up @@ -225,7 +225,7 @@ end
Base.intersect(a::Accumulator, b::Accumulator, c::Accumulator...) = insersect(intersect(a,b), c...)
Base.intersect(a::Accumulator, b::Accumulator) = intersect!(copy(a), b)
function Base.intersect!(a::Accumulator, b::Accumulator)
for k in union(keys(a), keys(b)) # union not interection as we want to check both multiplicties
for k in union(keys(a), keys(b)) # union not interection as we want to check both multiplicities
va = a[k]
vb = b[k]
va >= 0 || throw(MultiplicityException(k, va))
Expand Down
16 changes: 8 additions & 8 deletions src/balanced_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ end
function initializeTree!(tree::Array{TreeNode{K},1}) where K
resize!(tree,1)
tree[1] = TreeNode{K}(K, 1, 2, 0, 0)
nothing
return nothing
end

function initializeData!(data::Array{KDRec{K,D},1}) where {K,D}
resize!(data, 2)
data[1] = KDRec{K,D}(1)
data[2] = KDRec{K,D}(1)
nothing
return nothing
end


Expand Down Expand Up @@ -254,7 +254,7 @@ function empty!(t::BalancedTree23)
t.freedatainds = Vector{Int}()
empty!(t.useddatacells)
push!(t.useddatacells, 1, 2)
nothing
return nothing
end

## Default implementations of eq for Forward, Reverse
Expand Down Expand Up @@ -314,7 +314,7 @@ function findkeyless(t::BalancedTree23, k)
cmp3le_leaf(t.ord, thisnode, k)
curnode = cmp == 1 ? thisnode.child1 :
cmp == 2 ? thisnode.child2 : thisnode.child3
curnode
return curnode
end


Expand All @@ -325,15 +325,15 @@ end

function replaceparent!(data::Array{KDRec{K,D},1}, whichind::Int, newparent::Int) where {K,D}
data[whichind] = KDRec{K,D}(newparent, data[whichind].k, data[whichind].d)
nothing
return nothing
end

function replaceparent!(tree::Array{TreeNode{K},1}, whichind::Int, newparent::Int) where K
tree[whichind] = TreeNode{K}(tree[whichind].child1, tree[whichind].child2,
tree[whichind].child3, newparent,
tree[whichind].splitkey1,
tree[whichind].splitkey2)
nothing
return nothing
end


Expand Down Expand Up @@ -540,7 +540,7 @@ function insert!(t::BalancedTree23{K,D,Ord}, k, d, allowdups::Bool) where {K,D,O
t.rootloc = newrootloc
t.depth += 1
end
true, newind
return true, newind
end


Expand Down Expand Up @@ -1004,5 +1004,5 @@ function delete!(t::BalancedTree23{K,D,Ord}, it::Int) where {K,D,Ord<:Ordering}
end
end
end
nothing
return nothing
end
20 changes: 10 additions & 10 deletions src/circ_deque.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
"""
CircularDeque{T}(n)
Create a double-ended queue of maximum capacity `n`, implemented as a circular buffer. The element type is `T`.
"""
mutable struct CircularDeque{T}
buffer::Vector{T}
capacity::Int
Expand All @@ -11,6 +6,11 @@ mutable struct CircularDeque{T}
last::Int
end

"""
CircularDeque{T}(n)
Create a double-ended queue of maximum capacity `n`, implemented as a circular buffer. The element type is `T`.
"""
CircularDeque{T}(n::Int) where {T} = CircularDeque(Vector{T}(undef, n), n, 0, 1, n)

Base.length(D::CircularDeque) = D.n
Expand All @@ -27,7 +27,7 @@ function Base.empty!(D::CircularDeque)
D.n = 0
D.first = 1
D.last = D.capacity
D
return D
end

Base.isempty(D::CircularDeque) = D.n == 0
Expand All @@ -39,7 +39,7 @@ Get the item at the front of the queue.
"""
@inline function first(D::CircularDeque)
@boundscheck D.n > 0 || throw(BoundsError())
D.buffer[D.first]
return D.buffer[D.first]
end

"""
Expand All @@ -49,16 +49,16 @@ Get the item from the back of the queue.
"""
@inline function last(D::CircularDeque)
@boundscheck D.n > 0 || throw(BoundsError())
D.buffer[D.last]
return D.buffer[D.last]
end

@inline function Base.push!(D::CircularDeque, v)
@boundscheck D.n < D.capacity || throw(BoundsError()) # prevent overflow
D.n += 1
tmp = D.last+1
tmp = D.last + 1
D.last = ifelse(tmp > D.capacity, 1, tmp) # wraparound
@inbounds D.buffer[D.last] = v
D
return D
end

@inline function Base.pop!(D::CircularDeque)
Expand Down
38 changes: 14 additions & 24 deletions src/circular_buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Reset the buffer.
"""
function Base.empty!(cb::CircularBuffer)
cb.length = 0
cb
return cb
end

Base.@propagate_inbounds function _buffer_index_checked(cb::CircularBuffer, i::Int)
Expand All @@ -38,11 +38,7 @@ end
@inline function _buffer_index(cb::CircularBuffer, i::Int)
n = cb.capacity
idx = cb.first + i - 1
if idx > n
idx - n
else
idx
end
return ifelse(idx > n, idx - n, idx)
end

"""
Expand All @@ -64,7 +60,7 @@ Store data to the `i`-th element of `CircularBuffer`.
"""
@inline Base.@propagate_inbounds function Base.setindex!(cb::CircularBuffer, data, i::Int)
cb.buffer[_buffer_index_checked(cb, i)] = data
cb
return cb
end

"""
Expand All @@ -73,12 +69,10 @@ end
Remove the element at the back.
"""
@inline function Base.pop!(cb::CircularBuffer)
if cb.length == 0
throw(ArgumentError("array must be non-empty"))
end
(cb.length == 0) && throw(ArgumentError("array must be non-empty"))
i = _buffer_index(cb, cb.length)
cb.length -= 1
cb.buffer[i]
return cb.buffer[i]
end

"""
Expand All @@ -94,7 +88,7 @@ Add an element to the back and overwrite front if full.
cb.length += 1
end
cb.buffer[_buffer_index(cb, cb.length)] = data
cb
return cb
end

"""
Expand All @@ -109,7 +103,7 @@ function popfirst!(cb::CircularBuffer)
i = cb.first
cb.first = (cb.first + 1 > cb.capacity ? 1 : cb.first + 1)
cb.length -= 1
cb.buffer[i]
return cb.buffer[i]
end

"""
Expand All @@ -125,7 +119,7 @@ function pushfirst!(cb::CircularBuffer, data)
cb.length += 1
end
cb.buffer[cb.first] = data
cb
return cb
end

"""
Expand All @@ -139,7 +133,7 @@ function Base.append!(cb::CircularBuffer, datavec::AbstractVector)
for i in max(1, n-capacity(cb)+1):n
push!(cb, datavec[i])
end
cb
return cb
end

"""
Expand All @@ -152,7 +146,7 @@ function Base.fill!(cb::CircularBuffer, data)
for i in 1:capacity(cb)-length(cb)
push!(cb, data)
end
cb
return cb
end

"""
Expand Down Expand Up @@ -200,19 +194,15 @@ isfull(cb::CircularBuffer) = length(cb) == cb.capacity
Get the first element of CircularBuffer.
"""
Base.@propagate_inbounds function Base.first(cb::CircularBuffer)
@boundscheck if cb.length == 0
throw(BoundsError(cb, 1))
end
cb.buffer[cb.first]
@boundscheck (cb.length == 0) && throw(BoundsError(cb, 1))
return cb.buffer[cb.first]
end
"""
last(cb::CircularBuffer)
Get the last element of CircularBuffer.
"""
Base.@propagate_inbounds function Base.last(cb::CircularBuffer)
@boundscheck if cb.length == 0
throw(BoundsError(cb, 1))
end
cb.buffer[_buffer_index(cb, cb.length)]
@boundscheck (cb.length == 0) && throw(BoundsError(cb, 1))
return cb.buffer[_buffer_index(cb, cb.length)]
end
Loading

0 comments on commit a4246e7

Please sign in to comment.