-
Notifications
You must be signed in to change notification settings - Fork 152
WIP Return SUnitRange
from indices(::StaticArray)
#150
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,31 @@ show(io::IO, ::Type{SUnitRange}) = print(io, "SUnitRange") | |
function show(io::IO, ::MIME"text/plain", ::SUnitRange{Start, L}) where {Start, L} | ||
print(io, "SUnitRange($Start,$(Start + L - 1))") | ||
end | ||
|
||
# For this type to be usable as `indices`, they need to support some more stuff | ||
Base.unsafe_length(r::SUnitRange) = length(r) | ||
@inline first(r::SUnitRange{Start}) where {Start} = Start # matches Base.UnitRange when L == 0... | ||
@inline endof(r::SUnitRange{Start, L}) where {Start, L} = L | ||
|
||
(==)(::SUnitRange{Start, L}, ::SUnitRange{Start, L}) where {Start, L} = true | ||
(==)(::SUnitRange{Start1, L1}, ::SUnitRange{Start2, L2}) where {Start1, Start2, L1, L2} = false | ||
|
||
(==)(::SUnitRange{1, L}, r::Base.OneTo) where {L} = L == r.stop | ||
(==)(::SUnitRange, ::Base.OneTo) = false | ||
(==)(r::Base.OneTo, ::SUnitRange{1, L}) where {L} = L == r.stop | ||
(==)(::Base.OneTo, ::SUnitRange) = false | ||
|
||
start(r::SUnitRange) = 1 | ||
next(r::SUnitRange, i) = (r[i], i+1) | ||
done(r::SUnitRange{Start, L}, i) where {Start, L} = i > L | ||
|
||
@pure Base.UnitRange{Int}(::SUnitRange{Start, L}) where {Start, L} = Start : (Start + L - 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason this needs to be |
||
@pure Base.Slice(::SUnitRange{Start, L}) where {Start, L} = Base.Slice(Start : (Start + L - 1)) # TODO not optimal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not wrong but is perhaps pushing the limits. convert(::Type{Base.Slice}, r::SUnitRange{1,L}) = Base.Slice(r)
convert(::Type{Base.Slice}, r::SUnitRange) = error("$r cannot be converted to Base.Slice, consider `Base.Slice(r)` instead") ( |
||
|
||
function Base.checkindex(::Type{Bool}, ::SUnitRange{Start, L}, r::UnitRange{Int}) where {Start, L} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason not to define one method for |
||
return first(r) < Start | last(r) >= Start + L | ||
end | ||
|
||
function Base.checkindex(::Type{Bool}, ::SUnitRange{Start, L}, r::Base.Slice{UnitRange{Int}}) where {Start, L} | ||
return first(r) < Start | last(r) >= Start + L | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may also want a variant for generic
AbstractUnitRange
.