@@ -76,24 +76,42 @@ capacity(cb::CircularArrayBuffer{T,N}) where {T,N} = size(cb.buffer, N)
7676isfull (cb:: CircularArrayBuffer ) = cb. nframes == capacity (cb)
7777Base. isempty (cb:: CircularArrayBuffer ) = cb. nframes == 0
7878
79+ """
80+ _buffer_index(cb::CircularArrayBuffer, i::Int)
81+
82+ Return the index of the `i`-th element in the buffer.
83+ """
7984@inline function _buffer_index (cb:: CircularArrayBuffer , i:: Int )
80- ind = (cb. first - 1 ) * cb. step_size + i
81- if ind > length (cb. buffer)
82- ind - length (cb. buffer)
85+ idx = (cb. first - 1 ) * cb. step_size + i
86+ return wrap_index (idx, length (cb. buffer))
87+ end
88+ @inline _buffer_index (cb:: CircularArrayBuffer , I:: AbstractVector{<:Integer} ) = map (Base. Fix1 (_buffer_index, cb), I)
89+
90+ """
91+ wrap_index(idx, n)
92+
93+ Return the index of the `idx`-th element in the buffer, if index is one past the size, return 1, else error.
94+ """
95+ function wrap_index (idx, n)
96+ if idx <= n
97+ return idx
98+ elseif idx <= 2 n
99+ return idx - n
83100 else
84- ind
101+ @info " oops! idx $(idx) > 2n $(2 n) "
102+ return idx - n
85103 end
86104end
87- @inline _buffer_index (cb:: CircularArrayBuffer , I:: AbstractVector{<:Integer} ) = map (Base. Fix1 (_buffer_index, cb), I)
88105
106+ """
107+ _buffer_frame(cb::CircularArrayBuffer, i::Int)
108+
109+ Return the index of the `i`-th frame in the buffer.
110+ """
89111@inline function _buffer_frame (cb:: CircularArrayBuffer , i:: Int )
90112 n = capacity (cb)
91113 idx = cb. first + i - 1
92- if idx > n
93- idx - n
94- else
95- idx
96- end
114+ return wrap_index (idx, n)
97115end
98116
99117_buffer_frame (cb:: CircularArrayBuffer , I:: CartesianIndex ) = CartesianIndex (map (i-> _buffer_frame (cb, i), Tuple (I)))
0 commit comments