Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Package-specific AbstractUnitRange types for julia

License

NotificationsYou must be signed in to change notification settings

JuliaArrays/CustomUnitRanges.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

codecov.io

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.

What's in this package

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.

Usage

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 theURangetype 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. IfMyArrayTypeincludes 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

Stars

Watchers

Forks

Packages

No packages published

Contributors10

Languages


[8]ページ先頭

©2009-2025 Movatter.jp