|
14 | 14 | # handle case of 3 or more |
15 | 15 | product(s1, s2, s3, args...) = foldl(product, (s1, s2, s3, args...)) # not totally sure if this should be foldl or foldr |
16 | 16 |
|
17 | | -struct TupleSpaceProduct{T<:Tuple} |
| 17 | +struct TupleProduct{T<:Tuple} |
18 | 18 | ss::T |
19 | 19 | end |
20 | 20 |
|
21 | | -TupleSpaceProduct(s1, s2, others...) = TupleSpaceProduct((s1, s2, others...)) |
| 21 | +TupleProduct(s1, s2, others...) = TupleProduct((s1, s2, others...)) |
22 | 22 |
|
23 | | -subspaces(s::TupleSpaceProduct) = s.ss |
| 23 | +subspaces(s::TupleProduct) = s.ss |
24 | 24 |
|
25 | | -product(s1::TupleSpaceProduct, s2::TupleSpaceProduct) = TupleSpaceProduct(subspaces(s1)..., subspaces(s2)...) |
| 25 | +product(s1::TupleProduct, s2::TupleProduct) = TupleProduct(subspaces(s1)..., subspaces(s2)...) |
26 | 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 |
| 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 |
30 | 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)...) |
| 31 | + if s1 isa TupleProduct |
| 32 | + return TupleProduct(subspaces(s1)..., s2) |
| 33 | + elseif s2 isa TupleProduct |
| 34 | + return TupleProduct(s1, subspaces(s2)...) |
35 | 35 | else |
36 | | - return TupleSpaceProduct(s1, s2) |
| 36 | + return TupleProduct(s1, s2) |
37 | 37 | end |
38 | 38 | end |
39 | 39 |
|
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)) |
0 commit comments