@@ -15,9 +15,9 @@ mutable struct CircularArrayBuffer{T,N} <: AbstractArray{T,N}
1515 step_size:: Int
1616end
1717
18- const CircularVectorBuffer{T} = CircularArrayBuffer{T, 1 }
18+ const CircularVectorBuffer{T} = CircularArrayBuffer{T,1 }
1919
20- CircularVectorBuffer {T} (n:: Integer ) where T = CircularArrayBuffer {T} (n)
20+ CircularVectorBuffer {T} (n:: Integer ) where {T} = CircularArrayBuffer {T} (n)
2121
2222function CircularArrayBuffer {T} (d:: Integer... ) where {T}
2323 N = length (d)
@@ -65,7 +65,7 @@ function Base.empty!(cb::CircularArrayBuffer)
6565 cb
6666end
6767
68- function Base. push! (cb:: CircularArrayBuffer{T, N} , data) where {T,N}
68+ function Base. push! (cb:: CircularArrayBuffer{T,N} , data) where {T,N}
6969 if cb. nframes == capacity (cb)
7070 cb. first = (cb. first == capacity (cb) ? 1 : cb. first + 1 )
7171 else
@@ -79,22 +79,22 @@ function Base.push!(cb::CircularArrayBuffer{T, N}, data) where {T,N}
7979 cb
8080end
8181
82- function Base. append! (cb:: CircularArrayBuffer{T, N} , data) where {T,N}
83- d, r = divrem (length (data) , cb. step_size)
82+ function Base. append! (cb:: CircularArrayBuffer{T,N} , data) where {T,N}
83+ d, r = divrem (length (data), cb. step_size)
8484 @assert r == 0
8585 if length (data) >= length (cb. buffer)
8686 cb. nframes = capacity (cb)
8787 cb. first = 1
8888 cb. buffer[:] .= @view data[end - length (cb. buffer)+ 1 : end ]
8989 else
90- start_idx = (cb. first- 1 ) * cb. step_size + length (cb) + 1
90+ start_idx = (cb. first - 1 ) * cb. step_size + length (cb) + 1
9191 end_idx = start_idx + length (data) - 1
9292 if start_idx > length (cb. buffer)
9393 start_idx -= length (cb. buffer)
9494 end_idx -= length (cb. buffer)
9595 end
9696 if end_idx > length (cb. buffer)
97- n_first_part = length (cb. buffer)- start_idx+ 1
97+ n_first_part = length (cb. buffer) - start_idx + 1
9898 n_second_part = length (data) - n_first_part
9999 cb. buffer[end - n_first_part+ 1 : end ] .= @view data[1 : n_first_part]
100100 cb. buffer[1 : n_second_part] .= @view data[end - n_second_part+ 1 : end ]
@@ -115,14 +115,28 @@ function Base.append!(cb::CircularArrayBuffer{T, N}, data) where {T,N}
115115 cb
116116end
117117
118- function Base. pop! (cb:: CircularArrayBuffer{T, N} ) where {T,N}
118+ function Base. pop! (cb:: CircularArrayBuffer{T,N} ) where {T,N}
119119 if cb. nframes <= 0
120120 throw (ArgumentError (" buffer must be non-empty" ))
121121 else
122- res = @views cb[ntuple (_ -> (:), N - 1 )... , cb . nframes]
122+ res = @views cb. buffer [ntuple (_ -> (:), N - 1 )... , _buffer_frame (cb, cb . nframes) ]
123123 cb. nframes -= 1
124124 res
125125 end
126126end
127127
128+ function Base. popfirst! (cb:: CircularArrayBuffer{T,N} ) where {T,N}
129+ if cb. nframes <= 0
130+ throw (ArgumentError (" buffer must be non-empty" ))
131+ else
132+ res = @views cb. buffer[ntuple (_ -> (:), N - 1 )... , _buffer_frame (cb, 1 )]
133+ cb. nframes -= 1
134+ cb. first += 1
135+ if cb. first > capacity (cb)
136+ cb. first = 1
137+ end
138+ res
139+ end
140+ end
141+
128142end
0 commit comments