OpenMS
Loading...
Searching...
No Matches
ListUtils.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- 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
21namespace 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
52 class OPENMS_DLLAPI ListUtils
53 {
54private:
59 {
60 DoubleTolerancePredicate_(const double& target, const double& tolerance) :
61 tolerance_(tolerance),
62 target_(target)
63 {}
64
71 inline bool operator()(const double& value) const
72 {
73 return std::fabs(value - target_) < tolerance_;
74 }
75
76private:
78 double tolerance_;
80 double target_;
81 };
82
83public:
93 template <typename T>
94 static std::vector<T> create(const String& str, const char splitter = ',')
95 {
96 // temporary storage for the individual elements of the string
97 std::vector<String> temp_string_vec;
98 str.split(splitter, temp_string_vec);
99 return create<T>(temp_string_vec);
100 }
101
110 template <typename T>
111 static std::vector<T> create(const std::vector<String>& s);
112
113
120 template <typename T>
121 static std::vector<String> toStringList(const std::vector<T>& s)
122 {
123 StringList out;
124 out.reserve(s.size());
125 for (const auto& elem : s) out.push_back(elem);
126 return out;
127 }
128
137 template <typename T, typename E>
138 static bool contains(const std::vector<T>& container, const E& elem)
139 {
140 return find(container.begin(), container.end(), elem) != container.end();
141 }
142
152 static bool contains(const std::vector<double>& container, const double& elem, double tolerance = 0.00001)
153 {
154 return find_if(container.begin(), container.end(), DoubleTolerancePredicate_(elem, tolerance)) != container.end();
155 }
156
157
158 enum class CASE { SENSITIVE, INSENSITIVE};
168 static bool contains(const std::vector<String>& container, String elem, const CASE case_sensitive)
169 {
170 if (case_sensitive == CASE::SENSITIVE) return contains(container, elem);
171 // case insensitive ...
172 elem.toLower();
173 return find_if(container.begin(), container.end(), [&elem](String ce) {
174 return elem == ce.toLower();
175 }) != container.end();
176 }
177
184 template <typename T>
185 static String concatenate(const std::vector<T>& container, const String& glue = "")
186 {
187 return concatenate< std::vector<T> >(container, glue);
188 }
189
196 template <typename T>
197 static String concatenate(const T& container, const String& glue = "")
198 {
199 // handle empty containers
200 if (container.empty()) return "";
201
202 typename T::const_iterator it = container.begin();
203 String ret = String(*it);
204 // we have handled the first element
205 ++it;
206 // add the rest
207 for (; it != container.end(); ++it)
208 {
209 ret += (glue + String(*it));
210 }
211
212 return ret;
213 }
214
218 template <typename T, typename E>
219 static Int getIndex(const std::vector<T>& container, const E& elem)
220 {
221 typename std::vector<T>::const_iterator pos =
222 std::find(container.begin(), container.end(), elem);
223 if (pos == container.end()) return -1;
224
225 return static_cast<Int>(std::distance(container.begin(), pos));
226 }
227
228 };
229
230 namespace detail
231 {
232 template <typename T>
233 T convert(const String& s);
234
235 template<>
236 inline Int32 convert(const String& s)
237 {
238 return s.toInt32();
239 }
240 template<>
241 inline double convert(const String& s)
242 {
243 return s.toDouble();
244 }
245 template<>
246 inline float convert(const String& s)
247 {
248 return s.toFloat();
249 }
250 template<>
251 inline std::string convert(const String& s)
252 {
253 return static_cast<std::string>(s);
254 }
255 }
256
257 template <typename T>
258 inline std::vector<T> ListUtils::create(const std::vector<String>& s)
259 {
260 std::vector<T> c;
261 c.reserve(s.size());
262 for (std::vector<String>::const_iterator it = s.begin(); it != s.end(); ++it)
263 {
264 try
265 {
266 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)
267 }
268 catch (...)
269 {
270 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + *it + "'");
271 }
272 }
273
274 return c;
275 }
276
278 template <>
279 inline std::vector<String> ListUtils::create(const std::vector<String>& s)
280 {
281 return s;
282 }
283
284} // namespace OpenMS
Invalid conversion exception.
Definition Exception.h:331
Collection of utility functions for management of vectors.
Definition ListUtils.h:53
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:152
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:138
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:168
CASE
Definition ListUtils.h:158
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:185
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:219
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:197
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:94
static std::vector< String > toStringList(const std::vector< T > &s)
Converts a vector of T's to a vector of Strings.
Definition ListUtils.h:121
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.
float toFloat() const
Conversion to float.
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
bool contains(T value, T min, T max)
Is a value contained in [min, max] ?
Definition MathFunctions.h:70
T convert(const String &s)
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Predicate to check double equality with a given tolerance.
Definition ListUtils.h:59
bool operator()(const double &value) const
Returns true if | value - target | < tolerance.
Definition ListUtils.h:71
double target_
The target value that should be found.
Definition ListUtils.h:80
double tolerance_
The allowed tolerance.
Definition ListUtils.h:78
DoubleTolerancePredicate_(const double &target, const double &tolerance)
Definition ListUtils.h:60