-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwrite_buffer.jl
48 lines (37 loc) · 1.22 KB
/
write_buffer.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
mutable struct BSONWriteBuffer <: DenseVector{UInt8}
data::Vector{UInt8}
len::Int
BSONWriteBuffer() = new(UInt8[], 0)
end
function Base.sizehint!(buffer::BSONWriteBuffer, capacity::Integer)
resize!(buffer.data, max(capacity, buffer.len))
buffer
end
@inline function Base.empty!(buffer::BSONWriteBuffer)
buffer.len = 0
buffer
end
@inline Base.length(buffer::BSONWriteBuffer) = buffer.len
@inline Base.size(buffer::BSONWriteBuffer) = (buffer.len,)
@inline Base.sizeof(buffer::BSONWriteBuffer) = buffer.len
@inline Base.elsize(BSONWriteBuffer) = 1
@inline function Base.getindex(buffer::BSONWriteBuffer, i::Integer)
@boundscheck (i > 0 && i <= buffer.len) || throw(BoundsError(buffer, i))
@inbounds buffer.data[i]
end
@inline function Base.resize!(buffer::BSONWriteBuffer, len::Int)
if length(buffer.data) < len
resize!(buffer.data, len * 2)
end
buffer.len = len
buffer
end
@inline Base.pointer(buffer::BSONWriteBuffer) = pointer(buffer.data)
@inline function Base.push!(buffer::BSONWriteBuffer, x::UInt8)
if length(buffer.data) == buffer.len
resize!(buffer.data, max(buffer.len * 2, 10))
end
buffer.len += 1
@inbounds buffer.data[buffer.len] = x
buffer
end