35
35
#include < initializer_list>
36
36
#include < iterator>
37
37
#include < memory>
38
+ #include < numeric>
38
39
#include < optional>
39
40
#include < regex>
40
41
#include < sstream>
@@ -119,7 +120,7 @@ namespace internal
119
120
namespace
120
121
{
121
122
bool anyPathRegexMatches (
122
- std::map<std::string, std:: regex> regexes ,
123
+ std::regex regex ,
123
124
std::vector<std::string> const &path,
124
125
std::string const &name)
125
126
{
@@ -150,16 +151,10 @@ namespace internal
150
151
pathsToMatch.emplace_back (*path.rbegin () + " /" );
151
152
}
152
153
return std::any_of (
153
- regexes.begin (),
154
- regexes.end (),
155
- [&pathsToMatch](auto const ®ex) {
156
- return std::any_of (
157
- pathsToMatch.begin (),
158
- pathsToMatch.end (),
159
- [®ex](std::string const &candidate_path) {
160
- return std::regex_match (
161
- candidate_path, regex.second );
162
- });
154
+ pathsToMatch.begin (),
155
+ pathsToMatch.end (),
156
+ [®ex](std::string const &candidate_path) {
157
+ return std::regex_match (candidate_path, regex);
163
158
});
164
159
}
165
160
} // namespace
@@ -169,20 +164,29 @@ namespace internal
169
164
std::vector<std::string> const &particles)
170
165
{
171
166
std::regex is_default_path_specification (" [[:alnum:]_]+/" , regex_flags);
172
- for (auto [deque , vec] :
173
- {std::make_tuple (&this ->meshesPath , &meshes),
174
- std::make_tuple (&this ->particlesPath , &particles)})
167
+ for (auto [target_regex , vec] :
168
+ {std::make_tuple (&this ->meshRegex , &meshes),
169
+ std::make_tuple (&this ->particleRegex , &particles)})
175
170
{
176
- std::transform (
177
- vec->begin (),
178
- vec->end (),
179
- std::inserter (*deque, deque->begin ()),
180
- [](auto const &str) {
181
- return std::make_pair (
182
- str,
183
- std::regex (
184
- str, regex_flags | std::regex_constants::optimize));
185
- });
171
+ if (vec->empty ())
172
+ {
173
+ *target_regex = std::regex (
174
+ /* does not match anything */ " a^" ,
175
+ regex_flags | std::regex_constants::optimize);
176
+ continue ;
177
+ }
178
+ auto begin = vec->begin ();
179
+ std::stringstream build_regex;
180
+ build_regex << ' (' << *begin++ << ' )' ;
181
+ for (; begin != vec->end (); ++begin)
182
+ {
183
+ build_regex << " |(" << *begin << ' )' ;
184
+ }
185
+ auto regex_string = build_regex.str ();
186
+ // std::cout << "Using regex string: " << regex_string << std::endl;
187
+ *target_regex = std::regex (
188
+ std::move (regex_string),
189
+ regex_flags | std::regex_constants::optimize);
186
190
}
187
191
setDefaultMeshesParticlesPath (meshes, particles, *this );
188
192
}
@@ -207,12 +211,12 @@ namespace internal
207
211
bool MeshesParticlesPath::isParticle (
208
212
std::vector<std::string> const &path, std::string const &name) const
209
213
{
210
- return anyPathRegexMatches (particlesPath , path, name);
214
+ return anyPathRegexMatches (particleRegex , path, name);
211
215
}
212
216
bool MeshesParticlesPath::isMesh (
213
217
std::vector<std::string> const &path, std::string const &name) const
214
218
{
215
- return anyPathRegexMatches (meshesPath , path, name);
219
+ return anyPathRegexMatches (meshRegex , path, name);
216
220
}
217
221
218
222
CustomHierarchyData::CustomHierarchyData ()
@@ -524,9 +528,7 @@ void CustomHierarchy::flush_internal(
524
528
: concatWithSep (currentPath, " /" ) + " /" ) +
525
529
name;
526
530
}
527
- mpp.meshesPath .emplace (
528
- extend_meshes_path,
529
- std::regex (extend_meshes_path, regex_flags));
531
+ mpp.collectNewMeshesPaths .emplace (std::move (extend_meshes_path));
530
532
}
531
533
mesh.flush (name, flushParams);
532
534
}
@@ -549,9 +551,8 @@ void CustomHierarchy::flush_internal(
549
551
: concatWithSep (currentPath, " /" ) + " /" ) +
550
552
name;
551
553
}
552
- mpp.particlesPath .emplace (
553
- extend_particles_path,
554
- std::regex (extend_particles_path, regex_flags));
554
+ mpp.collectNewParticlesPaths .emplace (
555
+ std::move (extend_particles_path));
555
556
}
556
557
particleSpecies.flush (name, flushParams);
557
558
}
@@ -570,28 +571,25 @@ void CustomHierarchy::flush(
570
571
*/
571
572
572
573
Series s = this ->retrieveSeries ();
573
- internal::MeshesParticlesPath mpp (s. meshesPaths (), s. particlesPaths ());
574
- auto num_meshes_paths = mpp. meshesPath . size ();
575
- auto num_particles_paths = mpp. particlesPath . size ( );
574
+ std::vector<std::string> meshesPaths = s. meshesPaths (),
575
+ particlesPaths = s. particlesPaths ();
576
+ internal::MeshesParticlesPath mpp (meshesPaths, particlesPaths );
576
577
std::vector<std::string> currentPath;
577
578
flush_internal (flushParams, mpp, currentPath);
578
- std::vector<std::string> meshesPaths, particlesPaths;
579
- for (auto [map, vec] :
580
- {std::make_pair (&mpp.meshesPath , &meshesPaths),
581
- std::make_pair (&mpp.particlesPath , &particlesPaths)})
582
- {
583
- std::transform (
584
- map->begin (),
585
- map->end (),
586
- std::back_inserter (*vec),
587
- [](auto const &pair) { return pair.first ; });
588
- }
589
- if (meshesPaths.size () > num_meshes_paths)
579
+ if (!mpp.collectNewMeshesPaths .empty () ||
580
+ !mpp.collectNewParticlesPaths .empty ())
590
581
{
582
+ for (auto [newly_added_paths, vec] :
583
+ {std::make_pair (&mpp.collectNewMeshesPaths , &meshesPaths),
584
+ std::make_pair (&mpp.collectNewParticlesPaths , &particlesPaths)})
585
+ {
586
+ std::transform (
587
+ newly_added_paths->begin (),
588
+ newly_added_paths->end (),
589
+ std::back_inserter (*vec),
590
+ [](auto const &pair) { return pair; });
591
+ }
591
592
s.setMeshesPath (meshesPaths);
592
- }
593
- if (particlesPaths.size () > num_particles_paths)
594
- {
595
593
s.setParticlesPath (particlesPaths);
596
594
}
597
595
}
0 commit comments