Skip to content

Commit c404f93

Browse files
authored
Merge pull request #7 from findmyway/master
Support `popfirst!`
2 parents d9675ad + 8b8bbd8 commit c404f93

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
@@ -83,22 +83,22 @@ function Base.push!(cb::CircularArrayBuffer{T, N}, data) where {T,N}
8383
cb
8484
end
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
120120
end
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
130130
end
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+
132146
end

test/runtests.jl

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

84-
x = pop!(b)
85-
@test x == 8
84+
x = popfirst!(b)
85+
@test x == 7
8686
@test length(b) == 1
87-
@test b[1] == 7
87+
@test b[1] == 8
8888

8989
x = pop!(b)
90-
@test x == 7
90+
@test x == 8
9191
@test length(b) == 0
9292

9393
@test_throws ArgumentError pop!(b)
94+
@test_throws ArgumentError popfirst!(b)
9495
end
9596

9697
@testset "2D Float64" begin
@@ -132,21 +133,21 @@ using Test
132133
@test b == reshape([c for x in 5:7 for c in x * A], 2, 2, 3)
133134

134135
x = pop!(b)
135-
@test x == 7 * ones(2,2)
136+
@test x == 7 * ones(2, 2)
136137
@test b == reshape([c for x in 5:6 for c in x * A], 2, 2, 2)
137138
end
138139

139140
@testset "append!" begin
140-
b = CircularArrayBuffer{Int}(2,3)
141+
b = CircularArrayBuffer{Int}(2, 3)
141142
append!(b, zeros(2))
142143
append!(b, 1:4)
143144
@test b == [
144145
0 1 3
145146
0 2 4
146147
]
147148

148-
149-
b = CircularArrayBuffer{Int}(2,3)
149+
150+
b = CircularArrayBuffer{Int}(2, 3)
150151
for i in 1:5
151152
push!(b, fill(i, 2))
152153
end

0 commit comments

Comments
 (0)