- Notifications
You must be signed in to change notification settings - Fork5
Package-specific AbstractUnitRange types for julia
License
JuliaArrays/CustomUnitRanges.jl
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This Julia package supports the creation of array types with"unconventional" indices, i.e., when the indices may not start at 1.With this package, each custom array type can have a correspondingaxes
range type, consequently providing a means for consistencyin allocation bysimilar
.
Seehttps://docs.julialang.org/en/v1/devdocs/offset-arrays/ formore information about defining and using array types with non-1indices.
Currently this package defines twoAbstractUnitRange
types:
ZeroRange
, whereZeroRange(n)
is the equivalent of0:n-1
, except thatJulia's type system knows that the lower bound is 0. (This isanalogous toBase
'sOneTo
type.) This is useful for definingarrays that are indexed starting with 0.URange
, a parallel toUnitRange
, for defining arbitrary range indices.
This package has a somewhat atypical usage: you shouldinclude
filesfrom this repository at the source level. The reason is that thispackage's range types should beprivate to the module that needsthem; consequently you don't want to define a module in the globalnamespace.
Instead, suppose you're defining an array type that supports arbitraryindices. In broad terms, your module might look like this:
module MyArrayTypeusing CustomUnitRanges: filename_for_urangeinclude(filename_for_urange)struct MyArray{T,N}<:AbstractArray{T,N}...endBase.axes(A::MyArray)= Base.Slice.(map(URange,#=starting indices=#,#=ending indices=#))...end
Here,
using CustomUnitRanges: filename_for_urange
brings a non-exported string,filename_for_urange
, into the scope ofMyArrayType
. The key line is theinclude(filename_for_urange)
statement, which will load (at source-level) the code for theURange
type into yourMyArrayType
module. We chose"URange.jl"
becausehere we want arbitrary indices; had we wanted zero-based indices, wewould have chosen"ZeroRange.jl"
instead. Second, note that theoutput ofaxes
is aSlice
containing aURange
type. More specifically, it'screating a tuple of slices withMyArrayType.URange
---there is no "global"URange
type, so the indices-tuple is thereforespecific to thispackage.
The important result is that two packages, definingMyArray
andOtherArray
, can independently exploitURange
. IfMyArrayType
includes the specialization
function Base.similar(f::Union{Type,Function}, shape::Tuple{URange,Vararg{URange}}MyArray(f(map(length, shape)),#=something for the offset=#)end
and similarly forOtherArrayType
. Then, ifA
is aMyArray
andB
is anOtherArray
,
similar(Array{Int}, axes(A))
will create anotherMyArray
similar(Array{Int}, axes(B))
will create anotherOtherArray
despite the fact that they both useURange
.
About
Package-specific AbstractUnitRange types for julia
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.