@@ -6,31 +6,38 @@ struct Space{T}
6
6
s:: T
7
7
end
8
8
9
- Space (s:: Type{T} ) where {T} = Space (typemin (T): typemax (T))
9
+ Space (s:: Type{T} ) where {T<: Number } = Space (typemin (T): typemax (T))
10
10
11
- Space (x, dims:: Int... ) = Space (fill (x, dims))
12
- Space (x:: Type{T} , dims:: Int... ) where {T<: Integer } = Space (fill (typemin (x): typemax (T), dims))
13
- Space (x:: Type{T} , dims:: Int... ) where {T<: AbstractFloat } = Space (fill (typemin (x) .. typemax (T), dims))
11
+ Space (x, dims:: Int... ) = Space (Fill (x, dims))
12
+ Space (x:: Type{T} , dim:: Int , extra_dims:: Int... ) where {T<: Integer } = Space (Fill (typemin (x): typemax (T), dim, extra_dims... ))
13
+ Space (x:: Type{T} , dim:: Int , extra_dims:: Int... ) where {T<: AbstractFloat } = Space (Fill (typemin (x) .. typemax (T), dim, extra_dims... ))
14
+ Space (x:: Type{T} , dim:: Int , extra_dims:: Int... ) where {T} = Space (Fill (T, dim, extra_dims... ))
14
15
15
16
Base. size (s:: Space ) = size (SpaceStyle (s))
17
+ Base. length (s:: Space ) = length (SpaceStyle (s), s)
18
+ Base. getindex (s:: Space , i... ) = getindex (SpaceStyle (s), s, i... )
19
+ Base.:(== )(s1:: Space , s2:: Space ) = s1. s == s2. s
16
20
17
21
# ####
18
22
19
23
abstract type AbstractSpaceStyle{S} end
20
24
21
- Base. size (:: AbstractSpaceStyle{S} ) where {S} = S
22
-
23
25
struct DiscreteSpaceStyle{S} <: AbstractSpaceStyle{S} end
24
26
struct ContinuousSpaceStyle{S} <: AbstractSpaceStyle{S} end
25
27
26
28
SpaceStyle (:: Space{<:Tuple} ) = DiscreteSpaceStyle {()} ()
27
- SpaceStyle (:: Space{<:AbstractRange } ) = DiscreteSpaceStyle {()} ()
29
+ SpaceStyle (:: Space{<:AbstractVector{<:Number} } ) = DiscreteSpaceStyle {()} ()
28
30
SpaceStyle (:: Space{<:AbstractInterval} ) = ContinuousSpaceStyle {()} ()
29
31
30
32
SpaceStyle (s:: Space{<:AbstractArray{<:Tuple}} ) = DiscreteSpaceStyle {size(s.s)} ()
31
33
SpaceStyle (s:: Space{<:AbstractArray{<:AbstractRange}} ) = DiscreteSpaceStyle {size(s.s)} ()
32
34
SpaceStyle (s:: Space{<:AbstractArray{<:AbstractInterval}} ) = ContinuousSpaceStyle {size(s.s)} ()
33
35
36
+ Base. size (:: AbstractSpaceStyle{S} ) where {S} = S
37
+ Base. length (:: DiscreteSpaceStyle{()} , s) = length (s. s)
38
+ Base. getindex (:: DiscreteSpaceStyle{()} , s, i... ) = getindex (s. s, i... )
39
+ Base. length (:: DiscreteSpaceStyle , s) = mapreduce (length, * , s. s)
40
+
34
41
# ####
35
42
36
43
Random. rand (rng:: Random.AbstractRNG , s:: Space ) = rand (rng, s. s)
@@ -45,6 +52,7 @@ Random.rand(
45
52
) = map (x -> rand (rng, x), s. s)
46
53
47
54
Base. in (x, s:: Space ) = x in s. s
55
+ Base. in (x, s:: Space{<:Type} ) = x isa s. s
48
56
49
57
Base. in (
50
58
x,
@@ -69,15 +77,20 @@ function Random.rand(rng::AbstractRNG, s::Interval{:closed,:closed,T}) where {T}
69
77
end
70
78
end
71
79
80
+ Base. iterate (s:: Space , args... ) = iterate (SpaceStyle (s), s, args... )
81
+ Base. iterate (:: DiscreteSpaceStyle{()} , s:: Space , args... ) = iterate (s. s, args... )
82
+
72
83
# ####
73
84
74
- const TupleSpace = Tuple{Vararg{<: Space }}
85
+ const TupleSpace = Tuple{Vararg{Space}}
75
86
const NamedSpace = NamedTuple{<: Any ,<: TupleSpace }
87
+ const VectorSpace = Vector{<: Space }
76
88
const DictSpace = Dict{<: Any ,<: Space }
77
89
78
- Random. rand (rng:: AbstractRNG , s:: Union{TupleSpace,NamedSpace} ) = map (x -> rand (rng, x), s)
90
+ Random. rand (rng:: AbstractRNG , s:: Union{TupleSpace,NamedSpace,VectorSpace } ) = map (x -> rand (rng, x), s)
79
91
Random. rand (rng:: AbstractRNG , s:: DictSpace ) = Dict (k => rand (rng, s[k]) for k in keys (s))
80
92
81
93
Base. in (xs:: Tuple , ts:: TupleSpace ) = length (xs) == length (ts) && all (((x, s),) -> x in s, zip (xs, ts))
94
+ Base. in (xs:: AbstractVector , ts:: VectorSpace ) = length (xs) == length (ts) && all (((x, s),) -> x in s, zip (xs, ts))
82
95
Base. in (xs:: NamedTuple{names} , ns:: NamedTuple{names,<:TupleSpace} ) where {names} = all (((x, s),) -> x in s, zip (xs, ns))
83
96
Base. in (xs:: Dict , ds:: DictSpace ) = length (xs) == length (ds) && all (k -> haskey (ds, k) && xs[k] in ds[k], keys (xs))
0 commit comments