OpenMS  2.8.0
ListUtils.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Stephan Aiche, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 #include <OpenMS/OpenMSConfig.h>
40 #include <OpenMS/config.h>
41 
42 #include <cmath>
43 #include <iterator>
44 #include <vector>
45 #include <algorithm>
46 
47 namespace OpenMS
48 {
49 
55  typedef std::vector<Int> IntList;
56 
62  typedef std::vector<double> DoubleList;
63 
64 
70  typedef std::vector<String> StringList;
71 
77  class OPENMS_DLLAPI ListUtils
78  {
79 private:
84  {
85  DoubleTolerancePredicate_(const double& target, const double& tolerance) :
86  tolerance_(tolerance),
87  target_(target)
88  {}
89 
96  inline bool operator()(const double& value) const
97  {
98  return std::fabs(value - target_) < tolerance_;
99  }
100 
101 private:
103  double tolerance_;
105  double target_;
106  };
107 
108 public:
117  template <typename T>
118  static std::vector<T> create(const String& str, const char splitter = ',')
119  {
120  // temporary storage for the individual elements of the string
121  std::vector<String> temp_string_vec;
122  str.split(splitter, temp_string_vec);
123  return create<T>(temp_string_vec);
124  }
125 
134  template <typename T>
135  static std::vector<T> create(const std::vector<String>& s);
136 
137 
144  template <typename T>
145  static std::vector<String> toStringList(const std::vector<T>& s)
146  {
147  StringList out;
148  out.reserve(s.size());
149  for (const auto& elem : s) out.push_back(elem);
150  return out;
151  }
152 
161  template <typename T, typename E>
162  static bool contains(const std::vector<T>& container, const E& elem)
163  {
164  return find(container.begin(), container.end(), elem) != container.end();
165  }
166 
176  static bool contains(const std::vector<double>& container, const double& elem, double tolerance = 0.00001)
177  {
178  return find_if(container.begin(), container.end(), DoubleTolerancePredicate_(elem, tolerance)) != container.end();
179  }
180 
181 
182  enum class CASE { SENSITIVE, INSENSITIVE};
192  static bool contains(const std::vector<String>& container, String elem, const CASE cs)
193  {
194  if (cs == CASE::SENSITIVE) return contains(container, elem);
195  // case insensitive ...
196  elem.toLower();
197  return find_if(container.begin(), container.end(), [&elem](String ce) {
198  return elem == ce.toLower();
199  }) != container.end();
200  }
201 
208  template <typename T>
209  static String concatenate(const std::vector<T>& container, const String& glue = "")
210  {
211  return concatenate< std::vector<T> >(container, glue);
212  }
213 
220  template <typename T>
221  static String concatenate(const T& container, const String& glue = "")
222  {
223  // handle empty containers
224  if (container.empty()) return "";
225 
226  typename T::const_iterator it = container.begin();
227  String ret = String(*it);
228  // we have handled the first element
229  ++it;
230  // add the rest
231  for (; it != container.end(); ++it)
232  {
233  ret += (glue + String(*it));
234  }
235 
236  return ret;
237  }
238 
242  template <typename T, typename E>
243  static Int getIndex(const std::vector<T>& container, const E& elem)
244  {
245  typename std::vector<T>::const_iterator pos =
246  std::find(container.begin(), container.end(), elem);
247  if (pos == container.end()) return -1;
248 
249  return static_cast<Int>(std::distance(container.begin(), pos));
250  }
251 
252  };
253 
254  namespace detail
255  {
256  template <typename T>
257  T convert(const String& s);
258 
259  template<>
260  inline Int convert(const String& s)
261  {
262  return s.toInt();
263  }
264  template<>
265  inline double convert(const String& s)
266  {
267  return s.toDouble();
268  }
269  template<>
270  inline float convert(const String& s)
271  {
272  return s.toFloat();
273  }
274  template<>
275  inline std::string convert(const String& s)
276  {
277  return static_cast<std::string>(s);
278  }
279  }
280 
281  template <typename T>
282  inline std::vector<T> ListUtils::create(const std::vector<String>& s)
283  {
284  std::vector<T> c;
285  c.reserve(s.size());
286  for (std::vector<String>::const_iterator it = s.begin(); it != s.end(); ++it)
287  {
288  try
289  {
290  c.push_back(detail::convert<T>(String(*it).trim())); // succeeds only if the whole output can be explained, i.e. "1.3 3" will fail (which is good)
291  }
292  catch (...)
293  {
294  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + *it + "'");
295  }
296  }
297 
298  return c;
299  }
300 
302  template <>
303  inline std::vector<String> ListUtils::create(const std::vector<String>& s)
304  {
305  return s;
306  }
307 
308 } // namespace OpenMS
309 
Invalid conversion exception.
Definition: Exception.h:356
Collection of utility functions for management of vectors.
Definition: ListUtils.h:78
static bool contains(const std::vector< double > &container, const double &elem, double tolerance=0.00001)
Checks whether the element elem is contained in the given container of floating point numbers.
Definition: ListUtils.h:176
static bool contains(const std::vector< T > &container, const E &elem)
Checks whether the element elem is contained in the given container.
Definition: ListUtils.h:162
static bool contains(const std::vector< String > &container, String elem, const CASE cs)
Checks whether the String elem is contained in the given container (potentially case insensitive)
Definition: ListUtils.h:192
static std::vector< String > toStringList(const std::vector< T > &s)
Converts a vector of T's to a vector of Strings.
Definition: ListUtils.h:145
CASE
Definition: ListUtils.h:182
static String concatenate(const std::vector< T > &container, const String &glue="")
Concatenates all elements of the container and puts the glue string between elements.
Definition: ListUtils.h:209
static Int getIndex(const std::vector< T > &container, const E &elem)
Get the index of the first occurrence of an element in the vector (or -1 if not found)
Definition: ListUtils.h:243
static String concatenate(const T &container, const String &glue="")
Concatenates all elements of the container and puts the glue string between elements.
Definition: ListUtils.h:221
static std::vector< T > create(const String &str, const char splitter=',')
Returns a list that is created by splitting the given comma-separated string.
Definition: ListUtils.h:118
A more convenient string class.
Definition: String.h:60
Int toInt() const
Conversion to int.
double toDouble() const
Conversion to double.
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
String & toLower()
Converts the string to lowercase.
int Int
Signed integer type.
Definition: Types.h:102
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:55
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:62
const double c
Definition: Constants.h:209
const double E
Euler's number - base of the natural logarithm.
Definition: Constants.h:78
T convert(const String &s)
Definition: ListUtils.h:260
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Predicate to check double equality with a given tolerance.
Definition: ListUtils.h:84
bool operator()(const double &value) const
Returns true if | value - target | < tolerance.
Definition: ListUtils.h:96
double target_
The target value that should be found.
Definition: ListUtils.h:105
double tolerance_
The allowed tolerance.
Definition: ListUtils.h:103
DoubleTolerancePredicate_(const double &target, const double &tolerance)
Definition: ListUtils.h:85