OpenMS
ListUtils.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Stephan Aiche, Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
13 #include <OpenMS/OpenMSConfig.h>
14 #include <OpenMS/config.h>
15 
16 #include <cmath>
17 #include <iterator>
18 #include <vector>
19 #include <algorithm>
20 
21 namespace OpenMS
22 {
23 
29  typedef std::vector<Int> IntList;
30 
36  typedef std::vector<double> DoubleList;
37 
38 
44  typedef std::vector<String> StringList;
45 
51  class OPENMS_DLLAPI ListUtils
52  {
53 private:
58  {
59  DoubleTolerancePredicate_(const double& target, const double& tolerance) :
60  tolerance_(tolerance),
61  target_(target)
62  {}
63 
70  inline bool operator()(const double& value) const
71  {
72  return std::fabs(value - target_) < tolerance_;
73  }
74 
75 private:
77  double tolerance_;
79  double target_;
80  };
81 
82 public:
92  template <typename T>
93  static std::vector<T> create(const String& str, const char splitter = ',')
94  {
95  // temporary storage for the individual elements of the string
96  std::vector<String> temp_string_vec;
97  str.split(splitter, temp_string_vec);
98  return create<T>(temp_string_vec);
99  }
100 
109  template <typename T>
110  static std::vector<T> create(const std::vector<String>& s);
111 
112 
119  template <typename T>
120  static std::vector<String> toStringList(const std::vector<T>& s)
121  {
122  StringList out;
123  out.reserve(s.size());
124  for (const auto& elem : s) out.push_back(elem);
125  return out;
126  }
127 
136  template <typename T, typename E>
137  static bool contains(const std::vector<T>& container, const E& elem)
138  {
139  return find(container.begin(), container.end(), elem) != container.end();
140  }
141 
151  static bool contains(const std::vector<double>& container, const double& elem, double tolerance = 0.00001)
152  {
153  return find_if(container.begin(), container.end(), DoubleTolerancePredicate_(elem, tolerance)) != container.end();
154  }
155 
156 
157  enum class CASE { SENSITIVE, INSENSITIVE};
167  static bool contains(const std::vector<String>& container, String elem, const CASE case_sensitive)
168  {
169  if (case_sensitive == CASE::SENSITIVE) return contains(container, elem);
170  // case insensitive ...
171  elem.toLower();
172  return find_if(container.begin(), container.end(), [&elem](String ce) {
173  return elem == ce.toLower();
174  }) != container.end();
175  }
176 
183  template <typename T>
184  static String concatenate(const std::vector<T>& container, const String& glue = "")
185  {
186  return concatenate< std::vector<T> >(container, glue);
187  }
188 
195  template <typename T>
196  static String concatenate(const T& container, const String& glue = "")
197  {
198  // handle empty containers
199  if (container.empty()) return "";
200 
201  typename T::const_iterator it = container.begin();
202  String ret = String(*it);
203  // we have handled the first element
204  ++it;
205  // add the rest
206  for (; it != container.end(); ++it)
207  {
208  ret += (glue + String(*it));
209  }
210 
211  return ret;
212  }
213 
217  template <typename T, typename E>
218  static Int getIndex(const std::vector<T>& container, const E& elem)
219  {
220  typename std::vector<T>::const_iterator pos =
221  std::find(container.begin(), container.end(), elem);
222  if (pos == container.end()) return -1;
223 
224  return static_cast<Int>(std::distance(container.begin(), pos));
225  }
226 
227  };
228 
229  namespace detail
230  {
231  template <typename T>
232  T convert(const String& s);
233 
234  template<>
235  inline Int32 convert(const String& s)
236  {
237  return s.toInt32();
238  }
239  template<>
240  inline double convert(const String& s)
241  {
242  return s.toDouble();
243  }
244  template<>
245  inline float convert(const String& s)
246  {
247  return s.toFloat();
248  }
249  template<>
250  inline std::string convert(const String& s)
251  {
252  return static_cast<std::string>(s);
253  }
254  }
255 
256  template <typename T>
257  inline std::vector<T> ListUtils::create(const std::vector<String>& s)
258  {
259  std::vector<T> c;
260  c.reserve(s.size());
261  for (std::vector<String>::const_iterator it = s.begin(); it != s.end(); ++it)
262  {
263  try
264  {
265  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)
266  }
267  catch (...)
268  {
269  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + *it + "'");
270  }
271  }
272 
273  return c;
274  }
275 
277  template <>
278  inline std::vector<String> ListUtils::create(const std::vector<String>& s)
279  {
280  return s;
281  }
282 
283 } // namespace OpenMS
284 
Invalid conversion exception.
Definition: Exception.h:330
Collection of utility functions for management of vectors.
Definition: ListUtils.h:52
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:151
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:137
static bool contains(const std::vector< String > &container, String elem, const CASE case_sensitive)
Checks whether the String elem is contained in the given container (potentially case insensitive)
Definition: ListUtils.h:167
static std::vector< String > toStringList(const std::vector< T > &s)
Converts a vector of T's to a vector of Strings.
Definition: ListUtils.h:120
CASE
Definition: ListUtils.h:157
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:184
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:218
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:196
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:93
A more convenient string class.
Definition: String.h:34
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.
Int32 toInt32() const
Conversion to Int32.
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.
int32_t Int32
Signed integer type (32bit)
Definition: Types.h:26
int Int
Signed integer type.
Definition: Types.h:72
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:29
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:36
const double c
Definition: Constants.h:188
const double E
Euler's number - base of the natural logarithm.
Definition: Constants.h:52
bool contains(T value, T min, T max)
Is a value contained in [min, max] ?
Definition: MathFunctions.h:64
T convert(const String &s)
Definition: ListUtils.h:235
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Predicate to check double equality with a given tolerance.
Definition: ListUtils.h:58
bool operator()(const double &value) const
Returns true if | value - target | < tolerance.
Definition: ListUtils.h:70
double target_
The target value that should be found.
Definition: ListUtils.h:79
double tolerance_
The allowed tolerance.
Definition: ListUtils.h:77
DoubleTolerancePredicate_(const double &target, const double &tolerance)
Definition: ListUtils.h:59