Skip to content
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

Allow other int types for type container size #893

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
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 @@
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))

Check warning on line 15 in src/circ_deque.jl

View check run for this annotation

Codecov / codecov/patch

src/circ_deque.jl#L15

Added line #L15 was not covered by tests
Comment on lines 14 to +15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just do

Suggested change
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))
CircularDeque{T}(n::Integer) where {T} = CircularDeque(Vector{T}(undef, n), n, 0, 1, n)

since constructors automatically call convert on all arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though about that option, but it somehow seemed more clear to me to explicitly do what will happen automatically. It somehow makes the code more readable to me, because it shows it's purpose more clearly. But I do not have strong opinions, so if you prefer this, it is fine by me ^_^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point (getting rid of this behavour has actually been talked about for julia 2.0).

On the other hand, code that doesn't exist can't have bugs.


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 @@
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))
Comment on lines +22 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per above i don't think this is needed since constructors automatically convert.
Even if you have another inner constructor.

Suggested change
# 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

Check warning on line 25 in src/circular_buffer.jl

View check run for this annotation

Codecov / codecov/patch

src/circular_buffer.jl#L24-L25

Added lines #L24 - L25 were not covered by tests

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))
Comment on lines +23 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per above

Suggested change
# 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
Loading