-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotatingBuffer.jl
35 lines (35 loc) · 1.36 KB
/
RotatingBuffer.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using SampledSignals
import Base.getindex
import Base.setindex!
import Base.size
import Base.length
import Base.lastindex
mutable struct RotatingBuffer{T<:AbstractArray}
data::Vector{T}
current_order::Vector{Int64}
end
function RotatingBuffer(samp::T, num_bufs::Int64) where {T<:Any}
init_data = [deepcopy(samp) for i in 1:num_bufs]
init_order = [i for i in 1:num_bufs]
return RotatingBuffer(init_data, init_order)
end
getindex(v::RotatingBuffer{T}, i::Int) where T<:AbstractArray = getindex(v.data, getindex(v.current_order, i));
setindex!(v::RotatingBuffer{T}, val::Any, i::Int) where T<:AbstractArray = setindex!(v.data, val, getindex(v.current_order, i));
size(v::RotatingBuffer{T}) where T<:AbstractArray = size(v.data);
length(v::RotatingBuffer{T}) where T<:AbstractArray = size(v.data);
lastindex(v::RotatingBuffer{T}) where T<:AbstractArray = lastindex(lastindex(current_order));
function rotate_clockwise!(v::RotatingBuffer{T}) where T<:AbstractArray
if length(v.current_order) > 1
tmp1 = v.current_order[1]
tmp2 = v.current_order[2]
for i in eachindex(v.current_order)
if i != length(v.current_order)
tmp2 = v.current_order[i+1]
setindex!(v.current_order, tmp1, i+1)
tmp1 = tmp2
else
v.current_order[1] = tmp1
end
end
end
end