comparison src/Grids/equidistant_grid.jl @ 1255:1989d432731a refactor/grids

Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 22 Feb 2023 22:38:25 +0100
parents ff8f335c32d1
children 198ccda331a6
comparison
equal deleted inserted replaced
1254:f98d8ede0e90 1255:1989d432731a
4 #TODO: Document recomendations for type of range. (LinRange is faster?) 4 #TODO: Document recomendations for type of range. (LinRange is faster?)
5 struct EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1,1} 5 struct EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1,1}
6 points::R 6 points::R
7 end 7 end
8 8
9 Base.eltype(g::EquidistantGrid{T}) where T = T
10 Base.getindex(g::EquidistantGrid, i) = g.points[i]
11 Base.size(g::EquidistantGrid) = size(g.points)
12 Base.length(g::EquidistantGrid) = length(g.points)
13 Base.eachindex(g::EquidistantGrid) = eachindex(g.points) 9 Base.eachindex(g::EquidistantGrid) = eachindex(g.points)
14 10
11 # Indexing interface
12 Base.getindex(g::EquidistantGrid, i) = g.points[i]
15 Base.firstindex(g::EquidistantGrid) = firstindex(g.points) 13 Base.firstindex(g::EquidistantGrid) = firstindex(g.points)
16 Base.lastindex(g::EquidistantGrid) = lastindex(g.points) 14 Base.lastindex(g::EquidistantGrid) = lastindex(g.points)
17 15
18 # TODO: Make sure collect works! 16 # Iteration interface
17 Base.iterate(g::EquidistantGrid) = iterate(g.points)
18 Base.iterate(g::EquidistantGrid, state) = iterate(g.points, state)
19
20 Base.IteratorSize(::Type{EquidistantGrid}) = Base.HasShape{1}()
21 Base.eltype(::Type{EquidistantGrid{T}}) where T = T
22 Base.length(g::EquidistantGrid) = length(g.points)
23 Base.size(g::EquidistantGrid) = size(g.points)
19 24
20 25
21 """ 26 """
22 spacing(grid::EquidistantGrid) 27 spacing(grid::EquidistantGrid)
23 28