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

Loosen search iterator #221

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
18 changes: 13 additions & 5 deletions src/BioSequences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,34 @@ include("search/pwm.jl")



struct Search{Q,I}
struct Search{F,Q,I}
findfunc::F
query::Q
itr::I
overlap::Bool
end

const DEFAULT_OVERLAP = true

search(query, itr; overlap = DEFAULT_OVERLAP) = Search(query, itr, overlap)
search(query, itr; overlap = DEFAULT_OVERLAP, rev=false) = Search(rev ? findprev : findnext, query, itr, overlap)

function Base.iterate(itr::Search, state=firstindex(itr.itr))
val = findnext(itr.query, itr.itr, state)
function Base.iterate(itr::Search{typeof(findnext)}, state=firstindex(itr.itr))
val = itr.findfunc(itr.query, itr.itr, state)
val === nothing && return nothing
state = itr.overlap ? first(val) + 1 : last(val) + 1
return val, state
end

function Base.iterate(itr::Search{typeof(findprev)}, state=firstindex(itr.itr))
val = itr.findfunc(itr.query, itr.itr, state)
val === nothing && return nothing
state = itr.overlap ? last(val) - 1 : first(val) - 1
return val, state
end

const HasRangeEltype = Union{<:ExactSearchQuery, <:ApproximateSearchQuery, <:Regex}

Base.eltype(::Type{<:Search{Q}}) where {Q<:HasRangeEltype} = UnitRange{Int}
Base.eltype(::Type{<:Search{F,Q}}) where {F,Q<:HasRangeEltype} = UnitRange{Int}
Base.eltype(::Type{<:Search}) = Int
Base.IteratorSize(::Type{<:Search}) = Base.SizeUnknown()

Expand Down