| OpenMS 2.5.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-2020.
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
37 #include <OpenMS/DATASTRUCTURES/String.h>
38 #include <OpenMS/CONCEPT/Exception.h>
39 #include <OpenMS/OpenMSConfig.h>
40 #include <OpenMS/config.h>
41
42 #include <algorithm>
43 #include <cmath>
44 #include <iterator>
45 #include <vector>
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:
83 struct DoubleTolerancePredicate_
84 {
85 DoubleTolerancePredicate_(const double& target, const double& tolerance) :
86 tolerance_(tolerance),
87 target_(target)
88 {}
89
96 inline bool operator()(const double& value)
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
145 template <typename T, typename E>
146 static bool contains(const std::vector<T>& container, const E& elem)
147 {
148 return find(container.begin(), container.end(), elem) != container.end();
149 }
150
160 static bool contains(const std::vector<double>& container, const double& elem, double tolerance = 0.00001)
161 {
162 return find_if(container.begin(), container.end(), DoubleTolerancePredicate_(elem, tolerance)) != container.end();
163 }
164
165
166 enum class CASE { SENSITIVE, INSENSITIVE};
176 static bool contains(const std::vector<String>& container, String elem, const CASE cs)
177 {
178 if (cs == CASE::SENSITIVE) return contains(container, elem);
179 // case INsensitive ...
180 elem.toLower();
181 return find_if(container.begin(), container.end(), [&elem](String ce) {
182 return elem == ce.toLower();
183 }) != container.end();
184 }
185
192 template <typename T>
193 static String concatenate(const std::vector<T>& container, const String& glue = "")
194 {
195 return concatenate< std::vector<T> >(container, glue);
196 }
197
204 template <typename T>
205 static String concatenate(const T& container, const String& glue = "")
206 {
207 // handle empty containers
208 if (container.empty()) return "";
209
210 typename T::const_iterator it = container.begin();
211 String ret = String(*it);
212 // we have handled the first element
213 ++it;
214 // add the rest
215 for (; it != container.end(); ++it)
216 {
217 ret += (glue + String(*it));
218 }
219
220 return ret;
221 }
222
226 template <typename T, typename E>
227 static Int getIndex(const std::vector<T>& container, const E& elem)
228 {
229 typename std::vector<T>::const_iterator pos =
230 std::find(container.begin(), container.end(), elem);
231 if (pos == container.end()) return -1;
232
233 return static_cast<Int>(std::distance(container.begin(), pos));
234 }
235
236 };
237
238 namespace detail
239 {
240 template <typename T>
241 T convert(const String& s);
242
243 template<>
244 inline Int convert(const String& s)
245 {
246 return s.toInt();
247 }
248 template<>
249 inline double convert(const String& s)
250 {
251 return s.toDouble();
252 }
253 template<>
254 inline float convert(const String& s)
255 {
256 return s.toFloat();
257 }
258 }
259
260 template <typename T>
261 inline std::vector<T> ListUtils::create(const std::vector<String>& s)
262 {
263 std::vector<T> c;
264 c.reserve(s.size());
265 for (std::vector<String>::const_iterator it = s.begin(); it != s.end(); ++it)
266 {
267 try
268 {
269 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)
270 }
271 catch (...)
272 {
273 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + *it + "'");
274 }
275 }
276
277 return c;
278 }
279
281 template <>
282 inline std::vector<String> ListUtils::create(const std::vector<String>& s)
283 {
284 return s;
285 }
286
287 } // namespace OpenMS
288
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::ListUtils::DoubleTolerancePredicate_::operator()
bool operator()(const double &value)
Returns true if | value - target | < tolerance.
Definition: ListUtils.h:96
OpenMS::IntList
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:55
OpenMS::String::toLower
String & toLower()
Converts the string to lowercase.
OpenMS::ListUtils::DoubleTolerancePredicate_::DoubleTolerancePredicate_
DoubleTolerancePredicate_(const double &target, const double &tolerance)
Definition: ListUtils.h:85
OpenMS::ListUtils::getIndex
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:227
Exception.h
OpenMS::ListUtils::CASE
CASE
Definition: ListUtils.h:166
OpenMS::ListUtils::contains
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:146
OpenMS::ListUtils::contains
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:160
OpenMS::ListUtils::create
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
OpenMS::ListUtils::DoubleTolerancePredicate_
Predicate to check double equality with a given tolerance.
Definition: ListUtils.h:83
OpenMS::String::toInt
Int toInt() const
Conversion to int.
OpenMS::String::toDouble
double toDouble() const
Conversion to double.
OpenMS::ListUtils::DoubleTolerancePredicate_::target_
double target_
The target value that should be found.
Definition: ListUtils.h:105
String.h
OpenMS::DoubleList
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:62
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::ListUtils
Collection of utility functions for management of vectors.
Definition: ListUtils.h:77
OpenMS::ListUtils::contains
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:176
OpenMS::detail::convert
T convert(const String &s)
Definition: ListUtils.h:244
seqan::find
bool find(TFinder &finder, const Pattern< TNeedle, FuzzyAC > &me, PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:884
OpenMS::Constants::c
const double c
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
int
OpenMS::Exception::ConversionError
Invalid conversion exception.
Definition: Exception.h:362
OpenMS::String::split
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
OpenMS::ListUtils::DoubleTolerancePredicate_::tolerance_
double tolerance_
The allowed tolerance.
Definition: ListUtils.h:103
OpenMS::String::toFloat
float toFloat() const
Conversion to float.
OpenMS::ListUtils::concatenate
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:193
OpenMS::Constants::E
const double E
Euler&#39;s number - base of the natural logarithm.
OpenMS::ListUtils::concatenate
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:205
OpenMS::String::trim
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...