@@ -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
@@ -83,22 +83,22 @@ function Base.push!(cb::CircularArrayBuffer{T, N}, data) where {T,N}
8383 cb
8484end
8585
86- function Base. append! (cb:: CircularArrayBuffer{T, N} , data) where {T,N}
87- d, r = divrem (length (data) , cb. step_size)
86+ function Base. append! (cb:: CircularArrayBuffer{T,N} , data) where {T,N}
87+ d, r = divrem (length (data), cb. step_size)
8888 @assert r == 0
8989 if length (data) >= length (cb. buffer)
9090 cb. nframes = capacity (cb)
9191 cb. first = 1
9292 cb. buffer[:] .= @view data[end - length (cb. buffer)+ 1 : end ]
9393 else
94- start_idx = (cb. first- 1 ) * cb. step_size + length (cb) + 1
94+ start_idx = (cb. first - 1 ) * cb. step_size + length (cb) + 1
9595 end_idx = start_idx + length (data) - 1
9696 if start_idx > length (cb. buffer)
9797 start_idx -= length (cb. buffer)
9898 end_idx -= length (cb. buffer)
9999 end
100100 if end_idx > length (cb. buffer)
101- n_first_part = length (cb. buffer)- start_idx+ 1
101+ n_first_part = length (cb. buffer) - start_idx + 1
102102 n_second_part = length (data) - n_first_part
103103 cb. buffer[end - n_first_part+ 1 : end ] .= @view data[1 : n_first_part]
104104 cb. buffer[1 : n_second_part] .= @view data[end - n_second_part+ 1 : end ]
@@ -119,14 +119,28 @@ function Base.append!(cb::CircularArrayBuffer{T, N}, data) where {T,N}
119119 cb
120120end
121121
122- function Base. pop! (cb:: CircularArrayBuffer{T, N} ) where {T,N}
122+ function Base. pop! (cb:: CircularArrayBuffer{T,N} ) where {T,N}
123123 if cb. nframes <= 0
124124 throw (ArgumentError (" buffer must be non-empty" ))
125125 else
126- res = @views cb[ntuple (_ -> (:), N - 1 )... , cb . nframes]
126+ res = @views cb. buffer [ntuple (_ -> (:), N - 1 )... , _buffer_frame (cb, cb . nframes) ]
127127 cb. nframes -= 1
128128 res
129129 end
130130end
131131
132+ function Base. popfirst! (cb:: CircularArrayBuffer{T,N} ) where {T,N}
133+ if cb. nframes <= 0
134+ throw (ArgumentError (" buffer must be non-empty" ))
135+ else
136+ res = @views cb. buffer[ntuple (_ -> (:), N - 1 )... , _buffer_frame (cb, 1 )]
137+ cb. nframes -= 1
138+ cb. first += 1
139+ if cb. first > capacity (cb)
140+ cb. first = 1
141+ end
142+ res
143+ end
144+ end
145+
132146end
0 commit comments