@@ -76,24 +76,42 @@ capacity(cb::CircularArrayBuffer{T,N}) where {T,N} = size(cb.buffer, N)
76
76
isfull (cb:: CircularArrayBuffer ) = cb. nframes == capacity (cb)
77
77
Base. isempty (cb:: CircularArrayBuffer ) = cb. nframes == 0
78
78
79
+ """
80
+ _buffer_index(cb::CircularArrayBuffer, i::Int)
81
+
82
+ Return the index of the `i`-th element in the buffer.
83
+ """
79
84
@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
83
100
else
84
- ind
101
+ @info " oops! idx $(idx) > 2n $(2 n) "
102
+ return idx - n
85
103
end
86
104
end
87
- @inline _buffer_index (cb:: CircularArrayBuffer , I:: AbstractVector{<:Integer} ) = map (Base. Fix1 (_buffer_index, cb), I)
88
105
106
+ """
107
+ _buffer_frame(cb::CircularArrayBuffer, i::Int)
108
+
109
+ Return the index of the `i`-th frame in the buffer.
110
+ """
89
111
@inline function _buffer_frame (cb:: CircularArrayBuffer , i:: Int )
90
112
n = capacity (cb)
91
113
idx = cb. first + i - 1
92
- if idx > n
93
- idx - n
94
- else
95
- idx
96
- end
114
+ return wrap_index (idx, n)
97
115
end
98
116
99
117
_buffer_frame (cb:: CircularArrayBuffer , I:: CartesianIndex ) = CartesianIndex (map (i-> _buffer_frame (cb, i), Tuple (I)))
0 commit comments