OpenMS  2.4.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-2018.
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 $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
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 #include <boost/lexical_cast.hpp>
48 #include <boost/algorithm/string/trim.hpp>
49 
50 namespace OpenMS
51 {
52 
58  typedef std::vector<Int> IntList;
59 
65  typedef std::vector<double> DoubleList;
66 
67 
73  typedef std::vector<String> StringList;
74 
80  class OPENMS_DLLAPI ListUtils
81  {
82 private:
87  {
88  DoubleTolerancePredicate_(const double& target, const double& tolerance) :
89  tolerance_(tolerance),
90  target_(target)
91  {}
92 
99  inline bool operator()(const double& value)
100  {
101  return std::fabs(value - target_) < tolerance_;
102  }
103 
104 private:
106  double tolerance_;
108  double target_;
109  };
110 
111 public:
120  template <typename T>
121  static std::vector<T> create(const String& str, const char splitter = ',')
122  {
123  // temporary storage for the individual elements of the string
124  std::vector<String> temp_string_vec;
125  str.split(splitter, temp_string_vec);
126  return create<T>(temp_string_vec);
127  }
128 
137  template <typename T>
138  static std::vector<T> create(const std::vector<String>& s);
139 
148  template <typename T, typename E>
149  static bool contains(const std::vector<T>& container, const E& elem)
150  {
151  return find(container.begin(), container.end(), elem) != container.end();
152  }
153 
163  static bool contains(const std::vector<double>& container, const double& elem, double tolerance = 0.00001)
164  {
165  return find_if(container.begin(), container.end(), DoubleTolerancePredicate_(elem, tolerance)) != container.end();
166  }
167 
174  template <typename T>
175  static String concatenate(const std::vector<T>& container, const String& glue = "")
176  {
177  return concatenate< std::vector<T> >(container, glue);
178  }
179 
186  template <typename T>
187  static String concatenate(const T& container, const String& glue = "")
188  {
189  // handle empty containers
190  if (container.empty()) return "";
191 
192  typename T::const_iterator it = container.begin();
193  String ret = String(*it);
194  // we have handled the first element
195  ++it;
196  // add the rest
197  for (; it != container.end(); ++it)
198  {
199  ret += (glue + String(*it));
200  }
201 
202  return ret;
203  }
204 
208  template <typename T, typename E>
209  static Int getIndex(const std::vector<T>& container, const E& elem)
210  {
211  typename std::vector<T>::const_iterator pos =
212  std::find(container.begin(), container.end(), elem);
213  if (pos == container.end()) return -1;
214 
215  return static_cast<Int>(std::distance(container.begin(), pos));
216  }
217 
218  };
219 
220  template <typename T>
221  inline std::vector<T> ListUtils::create(const std::vector<String>& s)
222  {
223  std::vector<T> c;
224  c.reserve(s.size());
225  for (std::vector<String>::const_iterator it = s.begin(); it != s.end(); ++it)
226  {
227  try
228  {
229  c.push_back(boost::lexical_cast<T>(boost::trim_copy(*it))); // succeeds only if the whole output can be explained, i.e. "1.3 3" will fail (which is good)
230  }
231  catch (boost::bad_lexical_cast&)
232  {
233  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + *it + "'");
234  }
235  }
236 
237  return c;
238  }
239 
241  template <>
242  inline std::vector<String> ListUtils::create(const std::vector<String>& s)
243  {
244  return s;
245  }
246 
247 } // namespace OpenMS
248 
const double E
Euler&#39;s number - base of the natural logarithm.
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:163
A more convenient string class.
Definition: String.h:57
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:121
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:65
Predicate to check double equality with a given tolerance.
Definition: ListUtils.h:86
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:58
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
bool find(TFinder &finder, const Pattern< TNeedle, FuzzyAC > &me, PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:884
DoubleTolerancePredicate_(const double &target, const double &tolerance)
Definition: ListUtils.h:88
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:175
Invalid conversion exception.
Definition: Exception.h:362
Collection of utility functions for management of vectors.
Definition: ListUtils.h:80
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
bool operator()(const double &value)
Returns true if | value - target | < tolerance.
Definition: ListUtils.h:99
double target_
The target value that should be found.
Definition: ListUtils.h:108
double tolerance_
The allowed tolerance.
Definition: ListUtils.h:106
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:209
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:187
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:149
int Int
Signed integer type.
Definition: Types.h:102
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.