Skip to content

Commit 01cf9e3

Browse files
committed
support popfirst
1 parent 0885223 commit 01cf9e3

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/CircularArrayBuffers.jl

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ mutable struct CircularArrayBuffer{T,N} <: AbstractArray{T,N}
1515
step_size::Int
1616
end
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

2222
function CircularArrayBuffer{T}(d::Integer...) where {T}
2323
N = length(d)
@@ -65,7 +65,7 @@ function Base.empty!(cb::CircularArrayBuffer)
6565
cb
6666
end
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
8080
end
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
116116
end
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
126126
end
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+
128142
end

test/runtests.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,17 @@ using Test
7272
@test length(b) == 2
7373
@test b[[1, 2]] == [7, 8]
7474

75-
x = pop!(b)
76-
@test x == 8
75+
x = popfirst!(b)
76+
@test x == 7
7777
@test length(b) == 1
78-
@test b[1] == 7
78+
@test b[1] == 8
7979

8080
x = pop!(b)
81-
@test x == 7
81+
@test x == 8
8282
@test length(b) == 0
8383

8484
@test_throws ArgumentError pop!(b)
85+
@test_throws ArgumentError popfirst!(b)
8586
end
8687

8788
@testset "2D Float64" begin
@@ -123,21 +124,21 @@ using Test
123124
@test b == reshape([c for x in 5:7 for c in x * A], 2, 2, 3)
124125

125126
x = pop!(b)
126-
@test x == 7 * ones(2,2)
127+
@test x == 7 * ones(2, 2)
127128
@test b == reshape([c for x in 5:6 for c in x * A], 2, 2, 2)
128129
end
129130

130131
@testset "append!" begin
131-
b = CircularArrayBuffer{Int}(2,3)
132+
b = CircularArrayBuffer{Int}(2, 3)
132133
append!(b, zeros(2))
133134
append!(b, 1:4)
134135
@test b == [
135136
0 1 3
136137
0 2 4
137138
]
138139

139-
140-
b = CircularArrayBuffer{Int}(2,3)
140+
141+
b = CircularArrayBuffer{Int}(2, 3)
141142
for i in 1:5
142143
push!(b, fill(i, 2))
143144
end

0 commit comments

Comments
 (0)