Skip to content
Snippets Groups Projects
Verified Commit 185b857a authored by Michiel Cottaar's avatar Michiel Cottaar
Browse files

Adjust iteration to go across multiple repeats

parent 2b07d523
No related branches found
No related tags found
No related merge requests found
module BaseSequences module BaseSequences
import ...ContainerBlocks: ContainerBlock import ...ContainerBlocks: ContainerBlock
import ...AllBuildingBlocks: BaseBuildingBlock import ...AllBuildingBlocks: BaseBuildingBlock
import ...Variables: TR, duration import ...Variables: TR, duration, start_time
""" """
Super-type of any sequence of non-overlapping building blocks that should be played after each other. Super-type of any sequence of non-overlapping building blocks that should be played after each other.
...@@ -11,27 +11,58 @@ It contains `N` [`ContainerBlock`](@ref) objects (e.g., building blocks or other ...@@ -11,27 +11,58 @@ It contains `N` [`ContainerBlock`](@ref) objects (e.g., building blocks or other
Main interface: Main interface:
- Acts as an iterable containing the blocks and sequences. - Acts as an iterable containing the blocks and sequences.
- Indiviual blocks/sequences can be obtained using indexing. - Indiviual blocks/sequences can be obtained using indexing.
- If there is a finite number of repeats, the iteration will continue over all repeats.
Sub-types need to implement: Sub-types need to implement:
- `Base.getindex`: returns the actual component for each integer index in 1:N. - `get_index_single_TR`: return the index assuming it is between 1 and N
- [`nrepeat`](@ref): how often the sequence should repeat (if not implemented, this will be 1).
- [`TR`](@ref): time scale on which to repeat (if not implemented, this will be the sum of the individual block durations).
""" """
abstract type BaseSequence{N} <: ContainerBlock end abstract type BaseSequence{N} <: ContainerBlock end
function Base.getindex(bs::BaseSequence{N}, index::Integer) where {N}
if index < 1 || index > length(bs)
throw(BoundsError(bs, index))
end
base_index = ((index - 1) % N) + 1
return get_index_single_TR(bs, base_index)
end
Base.iterate(bs::BaseSequence) = Base.iterate(bs, 1) Base.iterate(bs::BaseSequence) = Base.iterate(bs, 1)
Base.iterate(bs::BaseSequence{N}, index::Integer) where {N} = index > N ? nothing : (bs[index], index + 1) Base.iterate(bs::BaseSequence{N}, index::Integer) where {N} = index > length(bs) ? nothing : (bs[index], index + 1)
Base.length(::BaseSequence{N}) where {N} = N Base.length(bs::BaseSequence{N}) where {N} = nrepeat(bs) * N
Base.eltype(::Type{<:BaseSequence}) = ContainerBlock Base.eltype(::Type{<:BaseSequence}) = ContainerBlock
function start_time(bs::BaseSequence{N}, index::Integer) where {N}
nTR = div(index-1, N, RoundDown)
if 0 < nrepeat(bs) <= nTR
throw(BoundsError(bs, index))
end
base_index = ((index - 1) % N) + 1
base_time = sum(i -> duration(bs[i]), 1:base_index)
if iszero(nTR)
return base_time
else
return nTR * TR(bs) + base_time
end
end
"""
get_index_single_TR(sequence, index)
Used internally by any [`BaseSequence`](@ref) to get a specific block.
The `index` should be between 1 and N.
It should be implemented for any sub-classes of [`BaseSequence`](@ref).
"""
function get_index_single_TR end
""" """
nrepeat(sequence) nrepeat(sequence)
How often sequence should be repeated. How often sequence should be repeated.
Set to 0 to repeat indefinetely (default value if not overriden for a particular sequence type).
""" """
nrepeat(bs::BaseSequence) = 0 nrepeat(bs::BaseSequence) = 1
""" """
TR(sequence) TR(sequence)
......
...@@ -5,7 +5,7 @@ import ...Variables: get_free_variable, TR, VariableType ...@@ -5,7 +5,7 @@ import ...Variables: get_free_variable, TR, VariableType
import ...AllBuildingBlocks: Wait, BuildingBlock import ...AllBuildingBlocks: Wait, BuildingBlock
import ...BuildSequences: global_model import ...BuildSequences: global_model
import ...Components: EventComponent import ...Components: EventComponent
import ..BaseSequences: ContainerBlock, BaseSequence, nrepeat import ..BaseSequences: ContainerBlock, BaseSequence, nrepeat, get_index_single_TR
""" """
Sequence(blocks; TR=:min, nrepeat=0) Sequence(blocks; TR=:min, nrepeat=0)
...@@ -35,7 +35,9 @@ end ...@@ -35,7 +35,9 @@ end
Sequence(blocks...; kwargs...) = Sequence([blocks...]; kwargs...) Sequence(blocks...; kwargs...) = Sequence([blocks...]; kwargs...)
Base.getindex(s::Sequence, i::Integer) = s.blocks[i] get_index_single_TR(s::Sequence, i::Integer) = s.blocks[i]
Base.IteratorSize(::Type{<:Sequence}) = Base.IsInfinite()
nrepeat(::Sequence) = Inf
""" """
to_block(block_like) to_block(block_like)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment