Skip to content

Commit 361d999

Browse files
committed
finished implementing TupleProduct
1 parent e83a732 commit 361d999

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

src/CommonRLSpaces.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export
2525
include("array.jl")
2626

2727
export
28-
product
28+
product,
29+
TupleProduct
2930

3031
include("product.jl")
3132

src/product.jl

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,43 @@ end
1414
# handle case of 3 or more
1515
product(s1, s2, s3, args...) = foldl(product, (s1, s2, s3, args...)) # not totally sure if this should be foldl or foldr
1616

17-
struct TupleSpaceProduct{T<:Tuple}
17+
struct TupleProduct{T<:Tuple}
1818
ss::T
1919
end
2020

21-
TupleSpaceProduct(s1, s2, others...) = TupleSpaceProduct((s1, s2, others...))
21+
TupleProduct(s1, s2, others...) = TupleProduct((s1, s2, others...))
2222

23-
subspaces(s::TupleSpaceProduct) = s.ss
23+
subspaces(s::TupleProduct) = s.ss
2424

25-
product(s1::TupleSpaceProduct, s2::TupleSpaceProduct) = TupleSpaceProduct(subspaces(s1)..., subspaces(s2)...)
25+
product(s1::TupleProduct, s2::TupleProduct) = TupleProduct(subspaces(s1)..., subspaces(s2)...)
2626

27-
# handle any case not covered elsewhere by making a TupleSpaceProduct
28-
# if one of the members is already a TupleSpaceProduct, we add put them together in a new "flat" TupleSpaceProduct
29-
# note: if we had defined product(s1::TupleSpaceProduct, s2) it might be annoying because product(s1, s2::AnotherSpace) would be ambiguous with it
27+
# handle any case not covered elsewhere by making a TupleProduct
28+
# if one of the members is already a TupleProduct, we add put them together in a new "flat" TupleProduct
29+
# note: if we had defined product(s1::TupleProduct, s2) it might be annoying because product(s1, s2::AnotherProduct) would be ambiguous with it
3030
function product(s1, s2)
31-
if s1 isa TupleSpaceProduct
32-
return TupleSpaceProduct(subspaces(s1)..., s2)
33-
elseif s2 isa TupleSpaceProduct
34-
return TupleSpaceProduct(s1, subspaces(s2)...)
31+
if s1 isa TupleProduct
32+
return TupleProduct(subspaces(s1)..., s2)
33+
elseif s2 isa TupleProduct
34+
return TupleProduct(s1, subspaces(s2)...)
3535
else
36-
return TupleSpaceProduct(s1, s2)
36+
return TupleProduct(s1, s2)
3737
end
3838
end
3939

40-
SpaceStyle(s::TupleSpaceProduct) = promote_spacestyle(subspaces(s)...)
40+
SpaceStyle(s::TupleProduct) = promote_spacestyle(map(SpaceStyle, subspaces(s))...)
41+
42+
Base.rand(rng::AbstractRNG, sp::Random.SamplerTrivial{<:TupleProduct}) = map(s->rand(rng, s), subspaces(sp[]))
43+
function Base.in(element, space::TupleProduct)
44+
@assert length(element) == length(subspaces(space))
45+
return all(element[i] in s for (i, s) in enumerate(subspaces(space)))
46+
end
47+
Base.eltype(space::TupleProduct) = Tuple{map(eltype, subspaces(space))...}
48+
49+
Base.length(space::TupleProduct) = mapreduce(length, *, subspaces(space))
50+
Base.iterate(space, args...) = iterate(Iterators.product(subspaces(space)...), args...)
51+
52+
function bounds(s::TupleProduct)
53+
bds = map(bounds, subspaces(s))
54+
return (first.(bds), last.(bds))
55+
end
56+
Base.clamp(x, s::TupleProduct) = map(clamp, x, subspaces(s))

test/product.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,29 @@ end
1313
@test @inferred product(Box([1,3], [2,4]), 5..6) == Box([1,3,5], [2,4,6])
1414
@test @inferred product(5..6, Box([1,3], [2,4])) == Box([5,1,3], [6,2,4])
1515
end
16+
17+
@testset "TupleProduct discrete" begin
18+
tp = TupleProduct([1,2], [3,4])
19+
@test @inferred rand(tp) in tp
20+
@test (1,3) in tp
21+
@test !((1,2) in tp)
22+
@test @inferred eltype(tp) == Tuple{Int, Int}
23+
@test @inferred SpaceStyle(tp) == FiniteSpaceStyle()
24+
elems = @inferred collect(tp)
25+
@test @inferred all(e in tp for e in elems)
26+
@test @inferred all(e in elems for e in tp)
27+
end
28+
29+
@testset "TupleProduct continuous" begin
30+
tp = TupleProduct(1..2, 3..4)
31+
@test @inferred rand(tp) in tp
32+
@test (1,3) in tp
33+
@test !((1,2) in tp)
34+
@test_broken eltype(tp) == Tuple{Float64, Float64}
35+
@test @inferred SpaceStyle(tp) == ContinuousSpaceStyle()
36+
@test @inferred bounds(tp) == ((1,3), (2,4))
37+
@test @inferred bounds(TupleProduct(1..2, 3..4, 5..6)) == ((1,3,5), (2,4,6))
38+
@test @inferred clamp((0,0), tp) == (1, 3)
39+
end
40+
41+

0 commit comments

Comments
 (0)