Skip to content

Commit

Permalink
Give the people what they want.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Feb 22, 2022
1 parent c661046 commit c35918e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/PointEval/PointEvalHandler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ end
struct PointIterator{PH<:PointEvalHandler}
ph::PH
current_idx::ScalarWrapper{Int}
nodes::Vector{Int}
coords::Vector{Vec{2,Float64}}
nodes::FlexibleVector{Int}
coords::FlexibleVector{Vec{2,Float64}}
end
function PointIterator(ph::PointEvalHandler{G}) where {D,C,T,G<:Grid{D,C,T}}
@assert isconcretetype(C) # Check mate MixedDofHandler
N = nnodes_per_cell(ph.grid)
# @assert isconcretetype(C) # Check mate MixedDofHandler
N = nnodes_per_cell(ph.grid, 1)
idx = ScalarWrapper(0)
nodes = zeros(Int, N)
coords = zeros(Vec{D,T}, N)
nodes = FlexibleVector(zeros(Int, N))
coords = FlexibleVector(zeros(Vec{D,T}, N))
return PointIterator(ph, idx, nodes, coords)
end

Expand All @@ -299,7 +299,10 @@ function Base.iterate(p::PointIterator, state = 1)
# Update stuff
p.current_idx[] = state
cid = cellid(p)
N = nnodes_per_cell(p.ph.grid, cid)
resize!(p.nodes, N)
cellnodes!(p.nodes, p.ph.grid, cid)
resize!(p.coords, N)
cellcoords!(p.coords, p.ph.grid, cid)
return (p, state + 1)
end
Expand Down
38 changes: 38 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,41 @@ end
Base.copy(s::ScalarWrapper{T}) where {T} = ScalarWrapper{T}(copy(s.x))

copy!!(x, y) = copyto!(resize!(x, length(y)), y) # Future.copy!


# TODO: Maybe move to Compat.jl, maybe make it possible to wrap the full
# struct to also generate error paths in setproperty!
macro Const(field)
if VERSION >= v"1.8.0-DEV.1148"
Expr(:const, esc(field))
else
return esc(field)
end
end

# Vector with a fast resize!
mutable struct FlexibleVector{T} <: AbstractVector{T}
@Const buf::Vector{T}
len::Int
max_len::Int
FlexibleVector(buf::Vector{T}) where T = new{T}(buf, length(buf), length(buf))
end
Base.IndexStyle(::Type{<:FlexibleVector}) = IndexLinear()
Base.size(v::FlexibleVector) = (v.len, )
Base.@propagate_inbounds function Base.getindex(v::FlexibleVector, i::Int)
@boundscheck checkbounds(v, i)
return @inbounds v.buf[i]
end
Base.@propagate_inbounds function Base.setindex!(v::FlexibleVector, val, i::Int)
@boundscheck checkbounds(v, i)
@inbounds setindex!(v.buf, val, i)
return v
end
function Base.resize!(v::FlexibleVector, l::Int)
if v.max_len < l
resize!(v.buf, l)
v.max_len = l
end
v.len = l
return v
end

0 comments on commit c35918e

Please sign in to comment.