Skip to content

Commit 6fd05e6

Browse files
Refactor push!
1 parent bfe180c commit 6fd05e6

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/CircularArrayBuffers.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,25 @@ function Base.empty!(cb::CircularArrayBuffer)
122122
cb
123123
end
124124

125-
function Base.push!(cb::CircularArrayBuffer{T,N}, data::D) where {T,N,D}
125+
function _update_first_and_nframes!(cb)
126126
if isfull(cb)
127127
cb.first = (cb.first == capacity(cb) ? 1 : cb.first + 1)
128128
else
129129
cb.nframes += 1
130130
end
131-
if N == 1
132-
i = _buffer_frame(cb, cb.nframes)
133-
cb.buffer[i:i] .= Ref(data)
134-
else
135-
cb.buffer[ntuple(_ -> (:), N - 1)..., _buffer_frame(cb, cb.nframes)] .= data
136-
end
137-
cb
131+
return cb
132+
end
133+
134+
function Base.push!(cb::CircularArrayBuffer{T,N}, data) where {T,N}
135+
_update_first_and_nframes!(cb)
136+
cb.buffer[ntuple(_ -> (:), N - 1)..., _buffer_frame(cb, cb.nframes)] .= data
137+
return cb
138+
end
139+
140+
function Base.push!(cb::CircularArrayBuffer{T,1}, data) where {T}
141+
_update_first_and_nframes!(cb)
142+
i = _buffer_frame(cb, cb.nframes)
143+
cb.buffer[i:i] .= Ref(data)
138144
end
139145

140146
function Base.append!(cb::CircularArrayBuffer{T,N}, data::D) where {T,N,D}

0 commit comments

Comments
 (0)