Skip to content

Commit e83a732

Browse files
committed
added tuple fallback
1 parent 02605b5 commit e83a732

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ The interface for hybrid continuous-discrete spaces is currently planned, but no
5151

5252
The Cartesian product of two spaces `a` and `b` can be constructed with `c = product(a, b)`.
5353

54-
The exact form of the resulting space is unspecified and should be considered an implementation detail. The only guarantees are (1) that there will be one unique element of `c` for every combination of one object from `a` and one object from `b` and (2) that the resulting collection conforms to the interface above.
54+
The exact form of the resulting space is unspecified and should be considered an implementation detail. The only guarantees are (1) that there will be one unique element of `c` for every combination of one object from `a` and one object from `b` and (2) that the resulting space conforms to the interface above.
5555

56-
The `TupleProductSpace` constructor provides a specialized Cartesian product where each element is a tuple, i.e. `TupleProductSpace(a, b)` has elements of type `Tuple{eltype(a), eltype(b)}`.
56+
The `TupleSpaceProduct` constructor provides a specialized Cartesian product where each element is a tuple, i.e. `TupleSpaceProduct(a, b)` has elements of type `Tuple{eltype(a), eltype(b)}`.
5757

5858
---
5959

src/basic.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ promote_spacestyle(::FiniteSpaceStyle, ::FiniteSpaceStyle) = FiniteSpaceStyle()
2727
promote_spacestyle(::ContinuousSpaceStyle, ::ContinuousSpaceStyle) = ContinuousSpaceStyle()
2828
promote_spacestyle(_, _) = UnknownSpaceStyle()
2929

30+
# handle case of 3 or more
31+
promote_spacestyle(s1, s2, s3, others...) = foldl(promote_spacestyle, (s1, s2, s3, args...))
32+
3033
function elsize end # note: different than Base.elsize
3134

3235
function bounds end

src/product.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,34 @@ function product(b1::Box, b2::Box)
77
if size(b1.lower, 2) == size(b2.lower, 2) # same number of columns
88
return Box(vcat(b1.lower, b2.lower), vcat(b1.upper, b2.upper))
99
else
10-
return GenericrSpaceProduct((b1, b2))
10+
return TupleSpaceProduct((b1, b2))
1111
end
1212
end
1313

1414
# handle case of 3 or more
15-
product(s1, s2, s3, args...) = foldl(product, (s1, s2, s3, args...))
15+
product(s1, s2, s3, args...) = foldl(product, (s1, s2, s3, args...)) # not totally sure if this should be foldl or foldr
1616

17-
struct TupleProductSpace{T<:Tuple}
18-
members::T
17+
struct TupleSpaceProduct{T<:Tuple}
18+
ss::T
1919
end
2020

21-
# handle any case not covered above
22-
product(s1, s2) = TupleProductSpace((s1, s2))
21+
TupleSpaceProduct(s1, s2, others...) = TupleSpaceProduct((s1, s2, others...))
22+
23+
subspaces(s::TupleSpaceProduct) = s.ss
24+
25+
product(s1::TupleSpaceProduct, s2::TupleSpaceProduct) = TupleSpaceProduct(subspaces(s1)..., subspaces(s2)...)
26+
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
30+
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)...)
35+
else
36+
return TupleSpaceProduct(s1, s2)
37+
end
38+
end
39+
40+
SpaceStyle(s::TupleSpaceProduct) = promote_spacestyle(subspaces(s)...)

0 commit comments

Comments
 (0)