Skip to content

Commit

Permalink
import DataStructures
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Broda committed Jun 21, 2018
1 parent 8ff256c commit 049b068
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/ARCH.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ using Distributions
using Roots
using Compat #for circular_buffer
import StatsBase: stderror
import DataStructures: CircularBuffer, _buffer_index_checked, _buffer_index,
capacity, isfull
include("circular_buffer.jl")# no bounds checks
#using DataStructures: CircularBuffer
import Base: show, showerror, Random.rand, eltype, mean
import StatsBase: StatisticalModel, loglikelihood, nobs, fit, fit!, adjr2, aic,
bic, aicc, dof, coef, coefnames, coeftable, CoefTable
Expand Down
63 changes: 0 additions & 63 deletions src/circular_buffer.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
"""
New items are pushed to the back of the list, overwriting values in a circular fashion.
"""
mutable struct CircularBuffer{T} <: AbstractVector{T}
capacity::Int
first::Int
length::Int
buffer::Vector{T}

CircularBuffer{T}(capacity::Int) where {T} = new{T}(capacity, 1, 0, Vector{T}(undef, capacity))
end

function Base.empty!(cb::CircularBuffer)
cb.length = 0
cb
end

Base.@propagate_inbounds function _buffer_index_checked(cb::CircularBuffer, i::Int)
@boundscheck if i < 1 || i > cb.length
throw(BoundsError(cb, i))
Expand All @@ -41,15 +24,6 @@ end
cb
end

function Base.pop!(cb::CircularBuffer)
if cb.length == 0
throw(ArgumentError("array must be non-empty"))
end
i = _buffer_index(cb, cb.length)
cb.length -= 1
cb.buffer[i]
end

@inline function Base.push!(cb::CircularBuffer, data)
# if full, increment and overwrite, otherwise push
if cb.length == cb.capacity
Expand All @@ -60,40 +34,3 @@ end
cb.buffer[_buffer_index(cb, cb.length)] = data
cb
end

function Compat.popfirst!(cb::CircularBuffer)
if cb.length == 0
throw(ArgumentError("array must be non-empty"))
end
i = cb.first
cb.first = (cb.first + 1 > cb.capacity ? 1 : cb.first + 1)
cb.length -= 1
cb.buffer[i]
end

function Compat.pushfirst!(cb::CircularBuffer, data)
# if full, decrement and overwrite, otherwise pushfirst
if cb.length < cb.capacity
cb.length += 1
end
cb.first = (cb.first == 1 ? cb.length : cb.first - 1)
cb.buffer[cb.first] = data
cb
end

function Base.append!(cb::CircularBuffer, datavec::AbstractVector)
# push at most last `capacity` items
n = length(datavec)
for i in max(1, n-capacity(cb)+1):n
push!(cb, datavec[i])
end
cb
end

Base.length(cb::CircularBuffer) = cb.length
Base.size(cb::CircularBuffer) = (length(cb),)
Base.convert(::Type{Array}, cb::CircularBuffer{T}) where {T} = T[x for x in cb]
Base.isempty(cb::CircularBuffer) = cb.length == 0

capacity(cb::CircularBuffer) = cb.capacity
isfull(cb::CircularBuffer) = length(cb) == cb.capacity

0 comments on commit 049b068

Please sign in to comment.