Skip to content

Commit

Permalink
Merge pull request #893 from KronosTheLate/allow_other_int_types_for_…
Browse files Browse the repository at this point in the history
…type_container_size

Allow other int types for type container size
  • Loading branch information
oxinabox authored Jan 30, 2024
2 parents f51727d + e4d7d54 commit 45386fb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ docs/site/
docs/Manifest.toml
Manifest.toml
benchmark/*.json
.vscode
1 change: 1 addition & 0 deletions src/circ_deque.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ end
Create a double-ended queue of maximum capacity `n`, implemented as a circular buffer. The element type is `T`.
"""
CircularDeque{T}(n::Int) where {T} = CircularDeque(Vector{T}(undef, n), n, 0, 1, n)
CircularDeque{T}(n::Integer) where {T} = CircularDeque(Vector{T}(undef, Int(n)), Int(n), 0, 1, Int(n))

Base.length(D::CircularDeque) = D.n
Base.eltype(::Type{CircularDeque{T}}) where {T} = T
Expand Down
11 changes: 7 additions & 4 deletions src/circular_buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ mutable struct CircularBuffer{T} <: AbstractVector{T}
len <= length(buf) || throw(ArgumentError("Value of 'length' must be <= length of buffer"))
return new{T}(length(buf), f, len, buf)
end

# Convert any `Integer` to whatever `Int` is on the relevant machine
CircularBuffer{T}(f::Integer, len::Integer, buf::Integer) where {T} = CircularBuffer{T}(Int(f), Int(len), Int(buf))
end

function CircularBuffer{T}(iter, capacity::Int) where {T}
function CircularBuffer{T}(iter, capacity::Integer) where {T}
vec = copyto!(Vector{T}(undef,capacity), iter)
CircularBuffer{T}(1, length(iter),vec)
end

CircularBuffer(capacity::Int) = CircularBuffer{Any}(capacity)
CircularBuffer(capacity::Integer) = CircularBuffer{Any}(capacity)

CircularBuffer{T}(capacity::Int) where {T} = CircularBuffer{T}(T[],capacity)
CircularBuffer{T}(capacity::Integer) where {T} = CircularBuffer{T}(T[],capacity)

CircularBuffer(iter,capacity::Int) = CircularBuffer{eltype(iter)}(iter,capacity)
CircularBuffer(iter,capacity::Integer) = CircularBuffer{eltype(iter)}(iter,capacity)

function CircularBuffer{T}(iter) where {T}
vec = reshape(collect(T,iter),:)
Expand Down
11 changes: 7 additions & 4 deletions src/deque.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ mutable struct DequeBlock{T}
blk.next = blk
return blk
end

# Convert any `Integer` to whatever `Int` is on the relevant machine
DequeBlock{T}(capa::Integer, front::Integer) where T = DequeBlock{T}(Int(capa), Int(front))
end

# block at the rear of the chain, elements towards the front
rear_deque_block(ty::Type{T}, n::Int) where {T} = DequeBlock{T}(n, 1)
rear_deque_block(ty::Type{T}, n::Integer) where {T} = DequeBlock{T}(n, 1)

# block at the head of the train, elements towards the back
head_deque_block(ty::Type{T}, n::Int) where {T} = DequeBlock{T}(n, n+1)
head_deque_block(ty::Type{T}, n::Integer) where {T} = DequeBlock{T}(n, n+1)

capacity(blk::DequeBlock) = blk.capa
Base.length(blk::DequeBlock) = blk.back - blk.front + 1
Expand All @@ -37,7 +40,7 @@ isrear(blk::DequeBlock) = blk.next === blk

# reset the block to empty, and position

function reset!(blk::DequeBlock{T}, front::Int) where T
function reset!(blk::DequeBlock{T}, front::Integer) where T
empty!(blk.data)
resize!(blk.data, blk.capa)
blk.front = front
Expand Down Expand Up @@ -80,7 +83,7 @@ mutable struct Deque{T}
head::DequeBlock{T}
rear::DequeBlock{T}

function Deque{T}(blksize::Int) where T
function Deque{T}(blksize::Integer) where T
head = rear = rear_deque_block(T, blksize)
new{T}(1, blksize, 0, head, rear)
end
Expand Down

0 comments on commit 45386fb

Please sign in to comment.