-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from tshort/static_type
Add utilities to convert object models
- Loading branch information
Showing
6 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
abstract type StaticContext end | ||
abstract type DefaultStaticContext <: StaticContext end | ||
|
||
struct DefaultCtx <: DefaultStaticContext end | ||
|
||
""" | ||
```julia | ||
static_type(ctx::StaticContext, x) | ||
static_type(x) | ||
``` | ||
Returns an object similar to `x` with contents converted based on rules | ||
specified by `ctx`. `static_type` can be used for types or for objects. | ||
For the default case, this converts `Array`s to `MallocArray`s and | ||
`String`s to `MallocString`s. | ||
To define your own rules, create a new `StaticContext` and then define | ||
two versions of `static_type` for each type you would like to convert. | ||
One converts the value, and one converts the type. Here is the builtin | ||
example for converting Arrays: | ||
``` | ||
struct MyCtx <: StaticContext end | ||
static_type(ctx::MyCtx, x::Array) = MallocArray(x) | ||
static_type(ctx::MyCtx, ::Type{Array{T,N}}) where {T,N} = MallocArray{T,N} | ||
``` | ||
For this context struct, inherit from `StaticTools.DefaultStaticContext` | ||
to build on the defaults, or inherit from `StaticTools.StaticContext` | ||
to define rules from scratch. | ||
`static_type` is mainly useful for converting objects that are heavily | ||
paramaterized. The SciML infrastructure has a lot of this. The main | ||
objects like a `DiffEq.Integrator` has many type parameters, and by | ||
default, some are not amenable to static compilation. `static_type` | ||
can be used to convert them to forms that can help numerical code to | ||
be statically compiled. | ||
`static_type` cannot convert all objects automatically. It transforms | ||
all type parameters and the contents of each field in an object | ||
(recursively). But, some objects do not define a "fully specified" | ||
constructor. In some cases, another method, `static_type_contents` | ||
can help by returning the components to help for a manual invocation | ||
of the constructor. | ||
Note that any `Malloc`-objects created through this function must still be | ||
`free`d manually if you do not wish to leak memory. | ||
""" | ||
static_type(x) = static_type(DefaultCtx(), x) | ||
static_type(ctx::DefaultStaticContext, x::Array) = MallocArray(x) | ||
static_type(ctx::DefaultStaticContext, ::Type{Array{T,N}}) where {T,N} = MallocArray{T,N} | ||
static_type(ctx::DefaultStaticContext, x::Vector{Vector{T}}) where {T} = MallocArray(MallocArray.(x)) | ||
static_type(ctx::DefaultStaticContext, ::Type{Vector{Vector{T}}}) where {T} = MallocVector{MallocVector{T}} | ||
static_type(ctx::DefaultStaticContext, x::Tuple) = tuple((static_type(ctx, y) for y in x)...) | ||
static_type(ctx::DefaultStaticContext, x::String) = MallocString(x) | ||
static_type(ctx::DefaultStaticContext, ::Type{String}) = MallocString | ||
|
||
# version for types including parameters | ||
function static_type(ctx::StaticContext, ::Type{T}) where {T} | ||
(!isconcretetype(T) || length(T.parameters) == 0) && return T | ||
return T.name.wrapper{(static_type(ctx, p) for p in T.parameters)...} | ||
end | ||
|
||
function static_type(ctx::StaticContext, x::T) where T | ||
length(fieldnames(T)) == 0 && return x | ||
newtypes, newfields = static_type_contents(ctx, x) | ||
if length(newtypes) > 0 | ||
return T.name.wrapper{newtypes...}(newfields...) | ||
else | ||
return T.name.wrapper(newfields...) | ||
end | ||
end | ||
|
||
""" | ||
```julia | ||
static_type_contents(ctx::StaticContext, x) | ||
static_type_contents(x) | ||
``` | ||
Returns a tuple with: | ||
* a vector of type parameters for `x` transformed by `static_type` | ||
* a vector of the contents of the fields in `x` transformed by | ||
`static_type` | ||
Results can be useful for defining objects that do not define a | ||
fully specified constructor. | ||
""" | ||
static_type_contents(x) = static_type_contents(DefaultCtx(), x) | ||
function static_type_contents(ctx::StaticContext, x::T) where T | ||
newtypes = [static_type(ctx, p) for p in T.parameters] | ||
newfields = [static_type(ctx, getfield(x, i)) for i in 1:fieldcount(T)] | ||
return newtypes, newfields | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
struct X{A,B,C} | ||
a::A | ||
b::B | ||
c::C | ||
end | ||
|
||
x = X([1,2,3], 3, "hello") | ||
xt = static_type(x) | ||
xtt = static_type(typeof(x)) | ||
|
||
@test xt.a isa MallocArray | ||
@test xt.b isa Int | ||
@test xt.c isa MallocString | ||
@test xtt.parameters[1] == MallocVector{Int} | ||
|
||
types, fields = static_type_contents(x) | ||
|
||
@test types[1] == MallocVector{Int} | ||
@test fields[1] isa MallocVector | ||
@test fields[1][1] == 1 | ||
|
||
x = X(1, X([1,2], 1, "hello"), "hello") | ||
xt = static_type(x) | ||
|
||
@test xt.b.a isa MallocArray | ||
@test xt.b.c isa MallocString | ||
|
||
|
04026f6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
04026f6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/83562
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: