Skip to content

Add append! #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
version:
- '1.0'
- '1.5'
- '1'
- 'nightly'
os:
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CircularArrayBuffers"
uuid = "9de3a189-e0c0-4e15-ba3b-b14b9fb0aec1"
authors = ["Jun Tian <[email protected]> and contributors"]
version = "0.1.2"
version = "0.1.3"

[compat]
julia = "1"
Expand Down
36 changes: 36 additions & 0 deletions src/CircularArrayBuffers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,42 @@ function Base.push!(cb::CircularArrayBuffer{T, N}, data) where {T,N}
cb
end

function Base.append!(cb::CircularArrayBuffer{T, N}, data) where {T,N}
d, r = divrem(length(data) , cb.step_size)
@assert r == 0
if length(data) >= length(cb.buffer)
cb.nframes = capacity(cb)
cb.first = 1
cb.buffer[:] .= @view data[end-length(cb.buffer)+1:end]
else
start_idx = (cb.first-1) * cb.step_size + length(cb) + 1
end_idx = start_idx + length(data) - 1
if start_idx > length(cb.buffer)
start_idx -= length(cb.buffer)
end_idx -= length(cb.buffer)
end
if end_idx > length(cb.buffer)
n_first_part = length(cb.buffer)-start_idx+1
n_second_part = length(data) - n_first_part
cb.buffer[end-n_first_part+1:end] .= @view data[1:n_first_part]
cb.buffer[1:n_second_part] .= @view data[end-n_second_part+1:end]
else
cb.buffer[start_idx:end_idx] .= data
end

if cb.nframes + d > capacity(cb)
cb.first += cb.nframes + d - capacity(cb)
if cb.first > capacity(cb)
cb.first -= capacity(cb)
end
cb.nframes = capacity(cb)
else
cb.nframes += d
end
end
cb
end

function Base.pop!(cb::CircularArrayBuffer{T, N}) where {T,N}
if cb.nframes <= 0
throw(ArgumentError("buffer must be non-empty"))
Expand Down
28 changes: 28 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,32 @@ using Test
@test x == 7 * ones(2,2)
@test b == reshape([c for x in 5:6 for c in x * A], 2, 2, 2)
end

@testset "append!" begin
b = CircularArrayBuffer{Int}(2,3)
append!(b, zeros(2))
append!(b, 1:4)
@test b == [
0 1 3
0 2 4
]


b = CircularArrayBuffer{Int}(2,3)
for i in 1:5
push!(b, fill(i, 2))
end
empty!(b)
append!(b, 1:4)
@test b == [
1 3
2 4
]

append!(b, 5:8)
@test b == [
3 5 7
4 6 8
]
end
end