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
14#include <OpenMS/OpenMSConfig.h>
15#include <OpenMS/config.h>
16
17#include <cmath>
18#include <iterator>
19#include <vector>
20#include <algorithm>
21
22namespace OpenMS
23{
24
31 class OPENMS_DLLAPI ListUtils
32 {
33private:
38 {
39 DoubleTolerancePredicate_(const double& target, const double& tolerance) :
40 tolerance_(tolerance),
41 target_(target)
42 {}
43
50 inline bool operator()(const double& value) const
51 {
52 return std::fabs(value - target_) < tolerance_;
53 }
54
55private:
57 double tolerance_;
59 double target_;
60 };
61
62public:
72 template <typename T>
73 static std::vector<T> create(const std::string& str, const char splitter = ',')
74 {
75 // temporary storage for the individual elements of the string
76 std::vector<std::string> temp_string_vec;
77 StringUtils::split(str, splitter, temp_string_vec);
78 return create<T>(temp_string_vec);
79 }
80
89 template <typename T>
90 static std::vector<T> create(const std::vector<std::string>& s);
91
92
99 template <typename T>
100 static std::vector<std::string> toStringList(const std::vector<T>& s)
101 {
102 StringList out;
103 out.reserve(s.size());
104 for (const auto& elem : s) out.push_back(StringUtils::toStr(elem));
105 return out;
106 }
107
116 template <typename T, typename E>
117 static bool contains(const std::vector<T>& container, const E& elem)
118 {
119 return find(container.begin(), container.end(), elem) != container.end();
120 }
121
131 static bool contains(const std::vector<double>& container, const double& elem, double tolerance = 0.00001)
132 {
133 return find_if(container.begin(), container.end(), DoubleTolerancePredicate_(elem, tolerance)) != container.end();
134 }
135
136
137 enum class CASE { SENSITIVE, INSENSITIVE};
147 static bool contains(const std::vector<std::string>& container, std::string elem, const CASE case_sensitive)
148 {
149 if (case_sensitive == CASE::SENSITIVE) return contains(container, elem);
150 // case insensitive ...
151 StringUtils::toLower(elem);
152 return find_if(container.begin(), container.end(), [&elem](std::string ce) {
153 return elem == StringUtils::toLower(ce);
154 }) != container.end();
155 }
156
163 template <typename T>
164 static std::string concatenate(const std::vector<T>& container, const std::string& glue = "")
165 {
166 return concatenate< std::vector<T> >(container, glue);
167 }
168
175 template <typename T>
176 static std::string concatenate(const T& container, const std::string& glue = "")
177 {
178 // handle empty containers
179 if (container.empty()) return "";
180
181 typename T::const_iterator it = container.begin();
182 std::string ret =StringUtils::toStr(*it);
183 // we have handled the first element
184 ++it;
185 // add the rest
186 for (; it != container.end(); ++it)
187 {
188 ret += (glue + StringUtils::toStr(*it));
189 }
190
191 return ret;
192 }
193
197 template <typename T, typename E>
198 static Int getIndex(const std::vector<T>& container, const E& elem)
199 {
200 typename std::vector<T>::const_iterator pos =
201 std::find(container.begin(), container.end(), elem);
202 if (pos == container.end()) return -1;
203
204 return static_cast<Int>(std::distance(container.begin(), pos));
205 }
206
207 };
208
209 namespace detail
210 {
211 template <typename T>
212 T convert(const std::string& s);
213
214 template<>
215 inline Int32 convert(const std::string& s)
216 {
217 return StringUtils::toInt32(s);
218 }
219 template<>
220 inline double convert(const std::string& s)
221 {
222 return StringUtils::toDouble(s);
223 }
224 template<>
225 inline float convert(const std::string& s)
226 {
227 return StringUtils::toFloat(s);
228 }
229 template<>
230 inline std::string convert(const std::string& s)
231 {
232 return static_cast<std::string>(s);
233 }
234 }
235
236 template <typename T>
237 inline std::vector<T> ListUtils::create(const std::vector<std::string>& s)
238 {
239 std::vector<T> c;
240 c.reserve(s.size());
241 for (std::vector<std::string>::const_iterator it = s.begin(); it != s.end(); ++it)
242 {
243 try
244 {
245 c.push_back(detail::convert<T>(StringUtils::trimmed(*it))); // succeeds only if the whole output can be explained, i.e. "1.3 3" will fail (which is good)
246 }
247 catch (...)
248 {
249 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,std::string("Could not convert string '") + *it + "'");
250 }
251 }
252
253 return c;
254 }
255
257 template <>
258 inline std::vector<std::string> ListUtils::create(const std::vector<std::string>& s)
259 {
260 return s;
261 }
262
263} // namespace OpenMS
Invalid conversion exception.
Definition Exception.h:331
Collection of utility functions for management of vectors.
Definition ListUtils.h:32
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:131
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:117
static std::vector< T > create(const std::string &str, const char splitter=',')
Returns a list that is created by splitting the given comma-separated string.
Definition ListUtils.h:73
static bool contains(const std::vector< std::string > &container, std::string elem, const CASE case_sensitive)
Checks whether the String elem is contained in the given container (potentially case insensitive)
Definition ListUtils.h:147
static std::string concatenate(const T &container, const std::string &glue="")
Concatenates all elements of the container and puts the glue string between elements.
Definition ListUtils.h:176
static std::vector< std::string > toStringList(const std::vector< T > &s)
Converts a vector of T's to a vector of Strings.
Definition ListUtils.h:100
CASE
Definition ListUtils.h:137
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:198
static std::string concatenate(const std::vector< T > &container, const std::string &glue="")
Concatenates all elements of the container and puts the glue string between elements.
Definition ListUtils.h:164
int32_t Int32
Signed integer type (32bit)
Definition Types.h:26
int Int
Signed integer type.
Definition Types.h:72
std::vector< std::string > StringList
Vector of String.
Definition TypeAliases.h:39
bool contains(T value, T min, T max)
Is a value contained in [min, max] ?
Definition MathFunctions.h:71
float toFloat(const std::string &s)
Definition StringUtils.h:170
std::string trimmed(std::string s)
Returns a trimmed copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:399
double toDouble(const std::string &s)
Definition StringUtils.h:171
Int32 toInt32(const std::string &s)
Definition StringUtils.h:168
T convert(const std::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:38
bool operator()(const double &value) const
Returns true if | value - target | < tolerance.
Definition ListUtils.h:50
double target_
The target value that should be found.
Definition ListUtils.h:59
double tolerance_
The allowed tolerance.
Definition ListUtils.h:57
DoubleTolerancePredicate_(const double &target, const double &tolerance)
Definition ListUtils.h:39