Skip to content

Commit

Permalink
Use tuples instead of vectors for array dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Aug 21, 2024
1 parent 36640fa commit 4744e94
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 69 deletions.
22 changes: 11 additions & 11 deletions src/mdarray/attribute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function getdimensionssize(attribute::AbstractAttribute)::NTuple{<:Any,Int}
@assert !isnull(attribute)
count = Ref{Csize_t}()
sizeptr = GDAL.gdalattributegetdimensionssize(attribute, count)
size = ntuple(n -> unsafe_load(sizeptr, n), count[])
size = reverse(ntuple(d -> Int(unsafe_load(sizeptr, d), count[])))
GDAL.vsifree(sizeptr)
return Int.(size)
return size
end

function readasraw(attribute::AbstractAttribute)::AbstractVector{UInt8}
Expand Down Expand Up @@ -158,7 +158,7 @@ function gettotalelementscount(attribute::AbstractAttribute)::Int64
return Int64(GDAL.gdalattributegettotalelementscount(attribute))
end

function Base.length(attribute::AbstractAttribute)
function Base.length(attribute::AbstractAttribute)::Int
@assert !isnull(attribute)
return Int(gettotalelementscount(attribute))
end
Expand All @@ -178,8 +178,8 @@ function getdimensions(
dimensionshptr =
GDAL.gdalattributegetdimensions(attribute, dimensionscountref)
dimensions = AbstractDimension[
IDimension(unsafe_load(dimensionshptr, n), attribute.dataset) for
n in 1:dimensionscountref[]
IDimension(unsafe_load(dimensionshptr, d), attribute.dataset) for
d in dimensionscountref[]:-1:1
]
GDAL.vsifree(dimensionshptr)
return dimensions
Expand All @@ -193,8 +193,8 @@ function unsafe_getdimensions(
dimensionshptr =
GDAL.gdalattributegetdimensions(attribute, dimensionscountref)
dimensions = AbstractDimension[
Dimension(unsafe_load(dimensionshptr, n), attribute.dataset) for
n in 1:dimensionscountref[]
Dimension(unsafe_load(dimensionshptr, d), attribute.dataset) for
d in dimensionscountref[]:-1:1
]
GDAL.vsifree(dimensionshptr)
return dimensions
Expand All @@ -215,31 +215,31 @@ end
function getblocksize(
attribute::AbstractAttribute,
options::OptionList = nothing,
)::AbstractVector{Int64}
)::NTuple{<:Any,Int}
@assert !isnull(attribute)
count = Ref{Csize_t}()
blocksizeptr = GDAL.gdalattributegetblocksize(
attribute,
count,
CSLConstListWrapper(options),
)
blocksize = Int64[unsafe_load(blocksizeptr, n) for n in 1:count[]]
blocksize = reverse(ntuple(d -> Int(unsafe_load(blocksizeptr, d)), count[]))
GDAL.vsifree(blocksizeptr)
return blocksize
end

function getprocessingchunksize(
attribute::AbstractAttribute,
maxchunkmemory::Integer,
)::AbstractVector{Int64}
)::NTuple{<:AnyInt}
@assert !isnull(attribute)
count = Ref{Csize_t}()
chunksizeptr = GDAL.gdalattributegetprocessingchunksize(
attribute,
count,
maxchunkmemory,
)
chunksize = Int64[unsafe_load(chunksizeptr, n) for n in 1:count[]]
chunksize = reverse(ntuple(d -> Int(unsafe_load(chunksizeptr, d)), count[]))
GDAL.vsifree(chunksizeptr)
return chunksize
end
Expand Down
13 changes: 6 additions & 7 deletions src/mdarray/group.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ function deletegroup(
options::OptionList = nothing,
)::Bool
@assert !isnull(group)
# TODO: Do we need to set group.ptr = C_NULL?
return GDAL.gdalgroupdeletegroup(group, name, CSLConstListWrapper(options))
end

Expand Down Expand Up @@ -224,7 +223,7 @@ end
function unsafe_createmdarray(
group::AbstractGroup,
name::AbstractString,
dimensions::AbstractVector{<:AbstractDimension},
dimensions::VectorLike{<:AbstractDimension},
datatype::AbstractExtendedDataType,
options::OptionList = nothing,
)::AbstractMDArray
Expand All @@ -246,7 +245,7 @@ end
function createmdarray(
group::AbstractGroup,
name::AbstractString,
dimensions::AbstractVector{<:AbstractDimension},
dimensions::VectorLike{<:AbstractDimension},
datatype::AbstractExtendedDataType,
options::OptionList = nothing,
)::AbstractMDArray
Expand Down Expand Up @@ -498,7 +497,7 @@ end
function unsafe_createattribute(
group::AbstractGroup,
name::AbstractString,
dimensions::AbstractVector{<:Integer},
dimensions::VectorLike{<:Integer},
datatype::AbstractExtendedDataType,
options::OptionList = nothing,
)::AbstractAttribute
Expand All @@ -508,7 +507,7 @@ function unsafe_createattribute(
group,
name,
length(dimensions),
dimensions,
reverse(dimensions),
datatype,
CSLConstListWrapper(options),
)
Expand All @@ -519,7 +518,7 @@ end
function createattribute(
group::AbstractGroup,
name::AbstractString,
dimensions::AbstractVector{<:Integer},
dimensions::VectorLike{<:Integer},
datatype::AbstractExtendedDataType,
options::OptionList = nothing,
)::AbstractAttribute
Expand All @@ -529,7 +528,7 @@ function createattribute(
group,
name,
length(dimensions),
dimensions,
reverse(dimensions),
datatype,
CSLConstListWrapper(options),
)
Expand Down
85 changes: 46 additions & 39 deletions src/mdarray/mdarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ end

function resize!(
mdarray::AbstractMDArray,
newdimsizes::AbstractVector{<:Integer},
newdimsizes::VectorLike{<:Integer},
options::OptionList = nothing,
)::Bool
@assert !isnull(mdarray)
Expand Down Expand Up @@ -339,7 +339,7 @@ end
# TODO: Wrap GDAL.GDALRIOResampleAlg
function unsafe_getresampled(
mdarray::AbstractMDArray,
newdims::Union{Nothing,AbstractVector{<:AbstractDimension}},
newdims::Union{Nothing,VectorLike{<:AbstractDimension}},
resamplealg::GDAL.GDALRIOResampleAlg,
targetsrs::Union{Nothing,AbstractSpatialRef},
options::OptionList = nothing,
Expand All @@ -359,7 +359,7 @@ end

function getresampled(
mdarray::AbstractMDArray,
newdims::Union{Nothing,AbstractVector{<:AbstractDimension}},
newdims::Union{Nothing,VectorLike{<:AbstractDimension}},
resamplealg::GDAL.GDALRIOResampleAlg,
targetsrs::Union{Nothing,AbstractSpatialRef},
options::OptionList = nothing,
Expand Down Expand Up @@ -540,27 +540,34 @@ function clearstatistics(mdarray::AbstractMDArray)::Nothing
end

function getcoordinatevariables(
mdarray::AbstractMDArray,
)::AbstractVector{<:AbstractMDArray}
mdarray::AbstractMDArray{<:Any,D},
)::NTuple{D,T where T<:AbstractMDArray} where {D}
@assert !isnull(mdarray)
count = Ref{Csize_t}()
coordinatevariablesptr =
GDAL.gdalmdarraygetcoordinatevariables(mdarray, count)
coordinatevariables = AbstractMDArray[
IMDArray(unsafe_load(coordinatevariablesptr, n), mdarray.dataset)
for n in 1:count[]
]
coordinatevariables = reverse(
ntuple(
d -> IMDArray(
unsafe_load(coordinatevariablesptr, d),
mdarray.dataset,
),
count[],
),
)
GDAL.vsifree(coordinatevariablesptr)
return coordinatevariables
end

function adviseread(
mdarray::AbstractMDArray,
arraystartidx::Union{Nothing,AbstractVector{<:Integer}},
count::Union{Nothing,AbstractVector{<:Integer}},
mdarray::AbstractMDArray{<:Any,D},
arraystartidx::Union{Nothing,IndexLike{D}},
count::Union{Nothing,IndexLike{D}},
options::OptionList = nothing,
)::Bool
)::Bool where {D}
@assert !isnull(mdarray)
@assert isnothing(arraystartix) ? true : length(arraystartidx) == D
@assert isnothing(count) ? true : length(count == D)
return GDAL.gdalmdarrayadviseread(
mdarray,
isnothing(arraystartidx) ? C_NULL : reverse(arraystartidx),
Expand Down Expand Up @@ -640,36 +647,40 @@ getdimensioncount(mdarray::AbstractMDArray{<:Any,D}) where {D} = D
Base.ndims(mdarray::AbstractMDArray)::Int = getdimensioncount(mdarray)

function getdimensions(
mdarray::AbstractMDArray,
)::AbstractVector{<:AbstractDimension}
mdarray::AbstractMDArray{<:Any,D},
)::NTuple{D,T where T<:AbstractDimension} where {D}
@assert !isnull(mdarray)
dimensionscountref = Ref{Csize_t}()
dimensionshptr = GDAL.gdalmdarraygetdimensions(mdarray, dimensionscountref)
dimensions = AbstractDimension[
IDimension(unsafe_load(dimensionshptr, n), mdarray.dataset) for
n in dimensionscountref[]:-1:1
]
dimensions = reverse(
ntuple(
d ->
IDimension(unsafe_load(dimensionshptr, d), mdarray.dataset),
dimensionscountref[],
),
)
GDAL.vsifree(dimensionshptr)
return dimensions
end

function unsafe_getdimensions(
mdarray::AbstractMDArray,
)::AbstractVector{<:AbstractDimension}
mdarray::AbstractMDArray{<:Any,D},
)::NTuple{D,T where T<:AbstractDimension} where {D}
@assert !isnull(mdarray)
dimensionscountref = Ref{Csize_t}()
dimensionshptr = GDAL.gdalmdarraygetdimensions(mdarray, dimensionscountref)
dimensions = AbstractDimension[
Dimension(unsafe_load(dimensionshptr, n), mdarray.dataset) for
n in dimensionscountref[]:-1:1
]
dimensions = reverse(
ntuple(
d -> Dimension(unsafe_load(dimensionshptr, d), mdarray.dataset),
dimensionscountref[],
),
)
GDAL.vsifree(dimensionshptr)
return dimensions
end

function Base.size(mdarray::AbstractMDArray)
function Base.size(mdarray::AbstractMDArray{<:Any,D})::NTuple{D,Int} where {D}
getdimensions(mdarray) do dimensions
D = length(dimensions)
return ntuple(d -> getsize(dimensions[d]), D)
end
end
Expand All @@ -686,45 +697,41 @@ end

Base.eltype(mdarray::AbstractMDArray{T}) where {T} = T

function getblocksize(mdarray::AbstractMDArray)::AbstractVector{Int64}
function getblocksize(
mdarray::AbstractMDArray{<:Any,D},
)::NTuple{D,Int} where {D}
@assert !isnull(mdarray)
count = Ref{Csize_t}()
blocksizeptr = GDAL.gdalmdarraygetblocksize(mdarray, count)
blocksize = Int64[unsafe_load(blocksizeptr, n) for n in count[]:-1:1]
blocksize = reverse(ntuple(d -> Int(unsafe_load(blocksizeptr, d)), count[]))
GDAL.vsifree(blocksizeptr)
return blocksize
end

DiskArrays.haschunks(::AbstractMDArray) = DiskArrays.Chunked()

function DiskArrays.eachchunk(
mdarray::AbstractMDArray{<:Any,D},
)::NTuple{D,Int} where {D}
blocksize = getblocksize(mdarray)
return DiskArrays.GridChunks(mdarray, Int.(blocksize))
return DiskArrays.GridChunks(mdarray, blocksize)
end

function getprocessingchunksize(
mdarray::AbstractMDArray,
maxchunkmemory::Integer,
)::AbstractVector{Int64}
)::AbstractVector{Int}
@assert !isnull(mdarray)
count = Ref{Csize_t}()
chunksizeptr =
GDAL.gdalmdarraygetprocessingchunksize(mdarray, count, maxchunkmemory)
chunksize = Int64[unsafe_load(chunksizeptr, n) for n in count[]:-1:1]
chunksize = Int[unsafe_load(chunksizeptr, n) for n in count[]:-1:1]
GDAL.vsifree(chunksizeptr)
return chunksize
end

# processperchunk

const IndexLike{D} =
Union{AbstractVector{<:Integer},CartesianIndex{D},NTuple{D,<:Integer}}
const RangeLike{D} = Union{
AbstractVector{<:AbstractRange{<:Integer}},
NTuple{D,<:AbstractRange{<:Integer}},
}

function read!(
mdarray::AbstractMDArray,
arraystartidx::IndexLike{D},
Expand Down
23 changes: 20 additions & 3 deletions src/mdarray/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ isnull(x::StyleTool) = x.ptr == C_NULL

################################################################################

const VectorLike{T} = Union{AbstractVector{<:T},NTuple{<:Any,X where X<:T}}

const IndexLike{D} = Union{
AbstractVector{<:Integer},
CartesianIndex{D},
NTuple{D,I where I<:Integer},
}
const RangeLike{D} = Union{
AbstractVector{<:AbstractRange{<:Integer}},
NTuple{D,R where R<:AbstractRange{<:Integer}},
}

################################################################################

Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::AbstractExtendedDataType) = x.ptr
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::AbstractEDTComponent) = x.ptr
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::AbstractGroup) = x.ptr
Expand Down Expand Up @@ -227,11 +241,11 @@ function destroy(dimension::AbstractDimension)::Nothing
return nothing
end

function destroy(edtcomponents::AbstractVector{<:AbstractEDTComponent})
function destroy(edtcomponents::VectorLike{<:AbstractEDTComponent})
return destroy.(edtcomponents)
end
destroy(attributes::AbstractVector{<:AbstractAttribute}) = destroy.(attributes)
destroy(dimensions::AbstractVector{<:AbstractDimension}) = destroy.(dimensions)
destroy(attributes::VectorLike{<:AbstractAttribute}) = destroy.(attributes)
destroy(dimensions::VectorLike{<:AbstractDimension}) = destroy.(dimensions)

################################################################################

Expand Down Expand Up @@ -280,6 +294,9 @@ struct DimensionHList
return new(dimensionhs, dimensions)
end
end
function DimensionHList(dimensions::NTuple{<:Any,T where T<:AbstractDimension})
return DimensionHList([dimensions...])
end

function Base.cconvert(
::Type{Ptr{GDAL.GDALDimensionH}},
Expand Down
Loading

0 comments on commit 4744e94

Please sign in to comment.