6161# So `condition` also has to be pure, which shouldn't be hard because it should basically
6262# just be comparing symbols
6363
64+ macro def_naxis (name, name_dim)
65+ nname = Symbol (:n , name)
66+ nname_doc = """
67+ $nname (x) -> Int
68+
69+ Returns the size along the dimension corresponding to the $name .
70+ """
71+
72+ esc (quote
73+ @doc $ nname_doc
74+ @inline $ nname (x) = Base. size (x, $ name_dim (x))
75+ end )
76+ end
77+
78+
79+ macro def_axis_keys (name, name_dims)
80+ name_keys = Symbol (name, :_keys )
81+ name_keys_doc = """
82+ $name_keys (x)
83+
84+ Returns the keys corresponding to the $name axis
85+ """
86+ esc (quote
87+ @doc $ name_keys_doc
88+ @inline $ name_keys (x) = keys (axes (x, $ name_dims (x)))
89+ end )
90+ end
91+
92+
93+
94+ macro def_axis_indices (name, name_dim)
95+ name_indices = Symbol (name, :_indices )
96+ name_indices_doc = """
97+ $name_indices (x)
98+
99+ Returns the indices corresponding to the $name axis
100+ """
101+ esc (quote
102+ @doc $ name_indices_doc
103+ @inline $ name_indices (x) = indices (axes (x, $ name_dim (x)))
104+ end )
105+ end
106+
107+
108+ # TODO I'm not sure this is the best name for this one
109+ macro def_axis_type (name, name_dim)
110+ name_type = Symbol (name, :_axis_type )
111+ name_type_doc = """
112+ $name_type (x)
113+
114+ Returns the key type corresponding to the $name axis.
115+ """
116+
117+ esc (quote
118+ @doc $ name_type_doc
119+ @inline $ name_type (x) = keytype (axes (x, $ name_dim (x)))
120+ end )
121+ end
122+
123+ macro def_selectdim (name, name_dim)
124+ name_selectdim = Symbol (:select_ , name, :dim )
125+ name_selectdim_doc = """
126+ $name_selectdim (x, i)
127+
128+ Return a view of all the data of `x` where the index for the $name dimension equals `i`.
129+ """
130+
131+ esc (quote
132+ @doc $ name_selectdim_doc
133+ @inline $ name_selectdim (x, i) = selectdim (x, $ name_dim (x), i)
134+
135+ end )
136+ end
137+
138+ macro def_eachslice (name, name_dim)
139+ each_name = Symbol (:each_ , name)
140+ each_name_doc = """
141+ $each_name (x)
142+
143+ Create a generator that iterates over the $name dimensions `A`, returning views that select
144+ all the data from the other dimensions in `A`.
145+ """
146+ esc (quote
147+ @doc $ each_name_doc
148+ @inline $ each_name (x) = eachslice (x, dims= $ name_dim (x))
149+ end )
150+ end
151+
64152"""
65153 @defdim name condition
66154
@@ -92,7 +180,16 @@ julia> @defdim time is_time
92180 `@defdim` should be considered experimental and subject to change
93181
94182"""
95- macro defdim (name, condition)
183+ macro defdim (
184+ name,
185+ condition,
186+ def_naxis:: Bool = true ,
187+ def_axis_keys:: Bool = true ,
188+ def_axis_indices:: Bool = true ,
189+ def_axis_type:: Bool = true ,
190+ def_selectdim:: Bool = true ,
191+ def_eachslice:: Bool = true ,
192+ )
96193
97194 dim_noerror_name = Symbol (:dim_noerror_ , name)
98195
@@ -103,13 +200,6 @@ macro defdim(name, condition)
103200 Returns the dimension corresponding to $name .
104201 """
105202
106- nname = Symbol (:n , name)
107- nname_doc = """
108- $nname (x) -> Int
109-
110- Returns the size along the dimension corresponding to the $name .
111- """
112-
113203 has_name_dim = Symbol (:has_ , name, :dim )
114204 has_name_dim_doc = """
115205 $has_name_dim (x) -> Bool
@@ -130,42 +220,6 @@ macro defdim(name, condition)
130220 Returns an `AxisIterator` along the $name axis.
131221 """
132222
133- name_indices = Symbol (name, :_indices )
134- name_indices_doc = """
135- $name_indices (x)
136-
137- Returns the indices corresponding to the $name axis
138- """
139-
140- name_keys = Symbol (name, :_keys )
141- name_keys_doc = """
142- $name_keys (x)
143-
144- Returns the keys corresponding to the $name axis
145- """
146-
147- name_type = Symbol (name, :_axis_type )
148- name_type_doc = """
149- $name_type (x)
150-
151- Returns the key type corresponding to the $name axis.
152- """
153-
154- name_selectdim = Symbol (:select_ , name, :dim )
155- name_selectdim_doc = """
156- $name_selectdim (x, i)
157-
158- Return a view of all the data of `x` where the index for the $name dimension equals `i`.
159- """
160-
161- each_name = Symbol (:each_ , name)
162- each_name_doc = """
163- $each_name (x)
164-
165- Create a generator that iterates over the $name dimensions `A`, returning views that select
166- all the data from the other dimensions in `A`.
167- """
168-
169223 err_msg = " Method $(Symbol (condition)) is not true for any dimensions of "
170224
171225 esc (quote
@@ -186,9 +240,6 @@ macro defdim(name, condition)
186240 end
187241 end
188242
189- @doc $ nname_doc
190- @inline $ nname (x) = Base. size (x, $ name_dim (x))
191-
192243 @doc $ has_name_dim_doc
193244 @inline $ has_name_dim (x) = ! ($ dim_noerror_name (dimnames (x)) === 0 )
194245
@@ -198,22 +249,30 @@ macro defdim(name, condition)
198249 @doc $ name_axis_itr
199250 @inline $ name_axis (x, sz; kwargs... ) = AxisIterator (axes (x, $ name_dim (x)), sz; kwargs... )
200251
201- @doc $ name_keys_doc
202- @inline $ name_keys (x) = keys ($ name_axis (x))
252+ if $ def_naxis
253+ Interface. @def_naxis ($ name, $ name_dim)
254+ end
203255
204- @doc $ name_indices_doc
205- @inline $ name_indices (x) = values ($ name_axis (x))
256+ if $ def_axis_keys
257+ Interface. @def_axis_keys ($ name, $ name_dim)
258+ end
206259
207- @doc $ name_type_doc
208- @inline $ name_type (x) = keytype ($ name_axis (x))
260+ if $ def_axis_indices
261+ Interface. @def_axis_indices ($ name, $ name_dim)
262+ end
209263
210- @doc $ name_selectdim_doc
211- @inline $ name_selectdim (x, i) = selectdim (x, $ name_dim (x), i)
264+ if $ def_axis_type
265+ Interface. @def_axis_type ($ name, $ name_dim)
266+ end
212267
213- @doc $ each_name_doc
214- @inline $ each_name (x) = eachslice (x, dims= $ name_dim (x))
268+ if $ def_selectdim
269+ Interface. @def_selectdim ($ name, $ name_dim)
270+ end
271+
272+ if $ def_eachslice
273+ Interface. @def_eachslice ($ name, $ name_dim)
274+ end
215275
216276 nothing
217277 end )
218278end
219-
0 commit comments