Skip to content

Commit cefb989

Browse files
authored
Add optimised fill!(::MutableMemoryView{UInt8}, ::UInt8) (#30)
1 parent 3ece65c commit cefb989

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "MemoryViews"
22
uuid = "a791c907-b98b-4e44-8f4d-e4c2362c6b2f"
3-
version = "0.3.5"
3+
version = "0.3.6"
44
authors = ["Jakob Nybo Nissen <jakobnybonissen@gmail.com>"]
55

66
[weakdeps]

src/basic.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ function Base.copyto!(dst::MutableMemoryView{T}, src::MemoryView{T}) where {T}
179179
return unsafe_copyto!(dst, src)
180180
end
181181

182+
function Base.fill!(v::MutableMemoryView{UInt8}, x::Integer)
183+
xT = convert(UInt8, x)::UInt8
184+
isempty(v) && return v
185+
GC.@preserve v @ccall memset(
186+
pointer(v)::Ptr{Nothing},
187+
Int32(xT)::Cint,
188+
(length(v) % UInt)::Csize_t
189+
)::Cvoid
190+
return v
191+
end
192+
182193
# Optimised methods that don't boundscheck
183194
function Base.findnext(p::Function, mem::MemoryView, start::Integer)
184195
i = Int(start)::Int

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,18 @@ end
580580
end
581581
end
582582

583+
@testset "memset" begin
584+
@test_throws Exception fill!(ImmutableMemoryView(UInt8[1, 2, 3]), 0x02)
585+
586+
v = MemoryView(UInt8[1, 2, 3, 4, 5])
587+
fill!(v, 0x03)
588+
@test v == fill(0x03, length(v))
589+
590+
v = MemoryView(UInt8[])
591+
fill!(v, 0x03)
592+
@test isempty(v)
593+
end
594+
583595
@testset "Iterators.reverse" begin
584596
for v in Any[AbstractString["abc", "def", ""], Char['a', 'b'], UInt32[], Int16[9, 2, 1]]
585597
mem = MemoryView(v)

0 commit comments

Comments
 (0)