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 
39 #include <sstream>
40 #include <vector>
42 
43 namespace Nektar
44 {
45 
47 {
48 public:
50  const std::string &str, std::vector<unsigned int> &out);
51 
52  template <typename T>
53  static bool GenerateVector(const std::string &str, std::vector<T> &out);
54 
55  /**
56  * @brief Generate a compressed comma-separated string representation of a
57  * vector of unsigned integers.
58  *
59  * This utility routine takes entries of @p v and returns a string
60  * sequence. For example,
61  *
62  * std::vector<unsigned int> vec = {1,2,3,4,6,7,8,5,2,3};
63  * std::string output = ParseUtils::GenerateVector(vec);
64  *
65  * will produce an `output` string containing `1-4,6-8,5,2,3`.
66  *
67  * @param v Vector of unsigned integers.
68  * @return Compressed comma separated string.
69  */
70  template <typename T>
71  static std::string GenerateSeqString(const std::vector<T> &v)
72  {
73  static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
74  "Unsigned integer type required.");
75 
76  if (v.size() == 0)
77  {
78  return "";
79  }
80 
81  std::ostringstream ss;
82  auto first = v[0], last = v[0];
83 
84  ss << v[0];
85 
86  for (auto &i : v)
87  {
88  if (i != last + 1 && i != last)
89  {
90  if (last != first)
91  {
92  ss << '-' << last;
93  }
94  ss << ',' << i;
95  first = i;
96  }
97  last = i;
98  }
99 
100  if (last != first)
101  {
102  ss << '-' << last;
103  }
104 
105  return ss.str();
106  }
107 };
108 
109 }
110 
111 #endif //NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
#define LIB_UTILITIES_EXPORT
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:135
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:71
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:108