Nektar++
ParseUtils.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ParseUtils.h
4//
5// For more information, please see: http://www.nektar.info/
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: This file contains various parsing utilities, primarily used
32// by SpatialDomains to process input files.
33//
34////////////////////////////////////////////////////////////////////////////////
35
36#ifndef NEKTAR_LIBUTILITIES_PARSEUTILS_H
37#define NEKTAR_LIBUTILITIES_PARSEUTILS_H
38
40#include <set>
41#include <sstream>
42#include <vector>
43
44namespace Nektar
45{
46
48{
49public:
51 const std::string &str, std::vector<unsigned int> &out);
52
53 template <typename T>
54 static bool GenerateVector(const std::string &str, std::vector<T> &out);
55
56 /**
57 * @brief Generate a compressed comma-separated string representation of a
58 * vector of unsigned integers.
59 *
60 * This utility routine takes entries of @p v and returns a string
61 * sequence. For example,
62 *
63 * std::vector<unsigned int> vec = {1,2,3,4,6,7,8,5,2,3};
64 * std::string output = ParseUtils::GenerateVector(vec);
65 *
66 * will produce an `output` string containing `1-4,6-8,5,2,3`.
67 *
68 * @param v Vector of unsigned integers.
69 * @return Compressed comma separated string.
70 */
71 template <typename T>
72 static std::string GenerateSeqString(const std::vector<T> &v)
73 {
74 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
75 "Unsigned integer type required.");
76
77 if (v.size() == 0)
78 {
79 return "";
80 }
81
82 std::ostringstream ss;
83 auto first = v[0], last = v[0];
84
85 ss << v[0];
86
87 for (auto &i : v)
88 {
89 if (i != last + 1 && i != last)
90 {
91 if (last != first)
92 {
93 ss << '-' << last;
94 }
95 ss << ',' << i;
96 first = i;
97 }
98 last = i;
99 }
100
101 if (last != first)
102 {
103 ss << '-' << last;
104 }
105
106 return ss.str();
107 }
108
109 /**
110 * @brief Generate a set of variable locations.
111 *
112 * This utility routine takes entries of @p str and returns a integer
113 * set that contains locations of parsed variables in @p variables.
114 * For example,
115 *
116 * str = "0,1,w,p";
117 * variables = {"u", "v", "w", "p"};
118 *
119 * will produce an set `out = {0,1,2,3}`.
120 *
121 * @param str A string of variables separated by comma.
122 * @param variables A vector of variables
123 * @return out A set of locations of parsed variables in @p variables.
124 */
126 const std::string &str, const std::vector<std::string> &variables,
127 std::set<int> &out);
128
130 const std::string &str, const std::vector<std::string> &variables,
131 std::vector<int> &out);
132};
133
134} // namespace Nektar
135
136#endif // NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
#define LIB_UTILITIES_EXPORT
static bool GenerateVariableSet(const std::string &str, const std::vector< std::string > &variables, std::set< int > &out)
Generate a set of variable locations.
Definition: ParseUtils.cpp:168
static std::string GenerateSeqString(const std::vector< T > &v)
Generate a compressed comma-separated string representation of a vector of unsigned integers.
Definition: ParseUtils.h:72
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:130
static bool GenerateSeqVector(const std::string &str, std::vector< unsigned int > &out)
Takes a comma-separated compressed string and converts it to entries in a vector.
Definition: ParseUtils.cpp:104
static bool GenerateVariableVector(const std::string &str, const std::vector< std::string > &variables, std::vector< int > &out)
Definition: ParseUtils.cpp:185