Skip to content

Commit 4d661f8

Browse files
authored
Merge pull request #69 from aplavin/master
add intervalsets and staticarrays support
2 parents c703441 + 5d04874 commit 4d661f8

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

Project.toml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
name = "ConstructionBase"
22
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
33
authors = ["Takafumi Arakaki", "Rafael Schouten", "Jan Weidner"]
4-
version = "1.4.1"
4+
version = "1.5.0"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88

9+
[weakdeps]
10+
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
11+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
12+
13+
[extensions]
14+
IntervalSetsExt = "IntervalSets"
15+
StaticArraysExt = "StaticArrays"
16+
917
[compat]
18+
IntervalSets = "0.5, 0.6, 0.7"
19+
StaticArrays = "1"
1020
julia = "1"
1121

1222
[extras]
23+
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
24+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1325
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1426

1527
[targets]
16-
test = ["Test"]
28+
test = ["IntervalSets","StaticArrays","Test"]

ext/IntervalSetsExt.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module IntervalSetsExt
2+
3+
using ConstructionBase
4+
using IntervalSets
5+
6+
ConstructionBase.constructorof(::Type{<:Interval{L, R}}) where {L, R} = Interval{L, R}
7+
8+
end

ext/StaticArraysExt.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module StaticArraysExt
2+
3+
using ConstructionBase
4+
using StaticArrays
5+
6+
# general static arrays need to keep the size parameter
7+
ConstructionBase.constructorof(sa::Type{<:SArray{S}}) where {S} = SArray{S}
8+
ConstructionBase.constructorof(sa::Type{<:MArray{S}}) where {S} = MArray{S}
9+
ConstructionBase.constructorof(sa::Type{<:SizedArray{S}}) where {S} = SizedArray{S}
10+
11+
# static vectors don't even need the explicit size specification
12+
ConstructionBase.constructorof(::Type{<:SVector}) = SVector
13+
ConstructionBase.constructorof(::Type{<:MVector}) = MVector
14+
15+
# set properties by name: x, y, z, w
16+
@generated function ConstructionBase.setproperties(obj::Union{SVector{N}, MVector{N}}, patch::NamedTuple{KS}) where {N, KS}
17+
if KS == (:data,)
18+
:( constructorof(typeof(obj))(only(patch)) )
19+
else
20+
N <= 4 || error("type $obj does not have properties $(join(KS, ", "))")
21+
propnames = (:x, :y, :z, :w)[1:N]
22+
KS propnames || error("type $obj does not have properties $(join(KS, ", "))")
23+
field_exprs = map(enumerate(propnames)) do (i, p)
24+
from = p KS ? :patch : :obj
25+
:( $from.$p )
26+
end
27+
:( constructorof(typeof(obj))($(field_exprs...)) )
28+
end
29+
end
30+
31+
end

test/runtests.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,49 @@ end
487487
@inferred getproperties(funny_numbers(S,20))
488488
@inferred getproperties(funny_numbers(S,40))
489489
end
490+
491+
492+
using StaticArrays, IntervalSets
493+
494+
if isdefined(Base, :get_extension) # some 1.9 version
495+
@testset "staticarrays" begin
496+
sa = @SVector [2, 4, 6, 8]
497+
sa2 = ConstructionBase.constructorof(typeof(sa))((3.0, 5.0, 7.0, 9.0))
498+
@test sa2 === @SVector [3.0, 5.0, 7.0, 9.0]
499+
500+
ma = @MMatrix [2.0 4.0; 6.0 8.0]
501+
ma2 = @inferred ConstructionBase.constructorof(typeof(ma))((1, 2, 3, 4))
502+
@test ma2 isa MArray{Tuple{2,2},Int,2,4}
503+
@test all(ma2 .=== @MMatrix [1 3; 2 4])
504+
505+
sz = SizedArray{Tuple{2,2}}([1 2;3 4])
506+
sz2 = @inferred ConstructionBase.constructorof(typeof(sz))([:a :b; :c :d])
507+
@test sz2 == SizedArray{Tuple{2,2}}([:a :b; :c :d])
508+
@test typeof(sz2) <: SizedArray{Tuple{2,2},Symbol,2,2}
509+
510+
for T in (SVector, MVector)
511+
@test @inferred(ConstructionBase.constructorof(T)((1, 2, 3)))::T == T((1, 2, 3))
512+
@test @inferred(ConstructionBase.constructorof(T{3})((1, 2, 3)))::T == T((1, 2, 3))
513+
@test @inferred(ConstructionBase.constructorof(T{3})((1, 2)))::T == T((1, 2))
514+
@test @inferred(ConstructionBase.constructorof(T{3, Symbol})((1, 2, 3)))::T == T((1, 2, 3))
515+
@test @inferred(ConstructionBase.constructorof(T{3, Symbol})((1, 2)))::T == T((1, 2))
516+
@test @inferred(ConstructionBase.constructorof(T{3, X} where {X})((1, 2, 3)))::T == T((1, 2, 3))
517+
@test @inferred(ConstructionBase.constructorof(T{3, X} where {X})((1, 2)))::T == T((1, 2))
518+
@test @inferred(ConstructionBase.constructorof(T{X, Symbol} where {X})((1, 2, 3)))::T == T((1, 2, 3))
519+
end
520+
521+
sv = SVector(1, 2)
522+
@test SVector(3.0, 2.0) === @inferred setproperties(sv, x = 3.0)
523+
@test SVector(3.0, 5.0) === @inferred setproperties(sv, x = 3.0, y = 5.0)
524+
@test SVector(-1.0, -2.0) === @inferred setproperties(sv, data = (-1.0, -2))
525+
@test_throws "does not have properties z" setproperties(sv, z = 3.0)
526+
@test_throws "does not have properties z" setproperties(SVector(1, 2, 3, 4, 5), z = 3.0)
527+
end
528+
529+
@testset "intervalsets" begin
530+
@test constructorof(typeof(1..2))(0.5, 1.5) === 0.5..1.5
531+
@test constructorof(typeof(OpenInterval(1, 2)))(0.5, 1.5) === OpenInterval(0.5, 1.5)
532+
@test setproperties(1..2, left=0.0) === 0.0..2.0
533+
@test setproperties(OpenInterval(1.0, 2.0), left=1, right=5) === OpenInterval(1, 5)
534+
end
535+
end

0 commit comments

Comments
 (0)