@@ -92,10 +92,12 @@ class FieldLayout {
92
92
// The rank is the number of tags associated to this field.
93
93
int rank () const { return m_rank; }
94
94
95
- int dim_idx (const FieldTag t) const ;
95
+ // If throw_if_multiple_matches=false, simply return the idx of the
96
+ // first match, otherwise throws an exception if 2+ matches are found
97
+ int dim_idx (const FieldTag t, bool throw_if_multiple_matches = true ) const ;
96
98
97
- int dim (const std::string& name) const ;
98
- int dim (const FieldTag tag) const ;
99
+ int dim (const std::string& name, bool throw_if_multiple_matches = true ) const ;
100
+ int dim (const FieldTag tag, bool throw_if_multiple_matches = true ) const ;
99
101
int dim (const int idim) const ;
100
102
const std::vector<int >& dims () const { return m_dims; }
101
103
const extents_type& extents () const { return m_extents; }
@@ -157,8 +159,7 @@ class FieldLayout {
157
159
void compute_type ();
158
160
void set_extents ();
159
161
void add_dim (const FieldTag t, const int extent, const std::string& name,
160
- bool prepend_instead_of_append = false );
161
-
162
+ bool prepend_instead_of_append = false );
162
163
163
164
int m_rank;
164
165
std::vector<FieldTag> m_tags;
@@ -174,22 +175,27 @@ bool operator== (const FieldLayout& fl1, const FieldLayout& fl2);
174
175
175
176
// ========================== IMPLEMENTATION ======================= //
176
177
177
- inline int FieldLayout::dim_idx (const FieldTag t) const {
178
- // Check exactly one tag (no ambiguity)
179
- EKAT_REQUIRE_MSG (ekat::count (m_tags,t)==1 ,
180
- " Error! FieldTag::dim_idx requires that the tag appears exactly once.\n "
178
+ inline int FieldLayout::dim_idx (const FieldTag t, bool throw_if_multiple_matches) const {
179
+ EKAT_REQUIRE_MSG ( (not throw_if_multiple_matches) or ekat::count (m_tags,t)<=1 ,
180
+ " [FieldTag::dim_idx] Error! Multiple matches found for the requested tag.\n "
181
+ " - field tag: " + e2str (t) + " \n "
182
+ " - field layout: " + this ->to_string () + " \n " );
183
+
184
+ auto it = ekat::find (m_tags,t);
185
+ EKAT_REQUIRE_MSG (it!=m_tags.end (),
186
+ " [FieldTag::dim_idx] Error! Requested tag not found.\n "
181
187
" - field tag: " + e2str (t) + " \n "
182
- " - tag count : " + std:: to_string (ekat::count (m_tags,t) ) + " \n " );
188
+ " - field layout : " + this -> to_string () + " \n " );
183
189
184
- return std::distance (m_tags.begin (),ekat::find (m_tags,t) );
190
+ return std::distance (m_tags.begin (),it );
185
191
}
186
192
187
193
// returns extent
188
- inline int FieldLayout::dim (const FieldTag t) const {
189
- return m_dims[dim_idx (t)];
194
+ inline int FieldLayout::dim (const FieldTag t, bool throw_if_multiple_matches ) const {
195
+ return m_dims[dim_idx (t,throw_if_multiple_matches )];
190
196
}
191
197
192
- inline int FieldLayout::dim (const std::string& name) const {
198
+ inline int FieldLayout::dim (const std::string& name, bool throw_if_multiple_matches ) const {
193
199
auto it = ekat::find (m_names,name);
194
200
195
201
// Check if found
@@ -198,7 +204,7 @@ inline int FieldLayout::dim (const std::string& name) const {
198
204
" - layout dims: " + ekat::join (m_names," ," ) + " \n " );
199
205
200
206
// Check only one tag (no ambiguity)
201
- EKAT_REQUIRE_MSG (ekat::count (m_names,name)==1 ,
207
+ EKAT_REQUIRE_MSG ( ( not throw_if_multiple_matches) or ekat::count (m_names,name)==1 ,
202
208
" Error! Dimension name '" + name + " ' appears multiple times.\n "
203
209
" - layout dims: " + ekat::join (m_names," ," ) + " \n " );
204
210
0 commit comments