Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2017.
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 #ifndef OPENMS_DATASTRUCTURES_LISTUTILS_H
36 #define OPENMS_DATASTRUCTURES_LISTUTILS_H
37 
40 #include <OpenMS/OpenMSConfig.h>
41 #include <OpenMS/config.h>
42 
43 #include <algorithm>
44 #include <cmath>
45 #include <iterator>
46 #include <vector>
47 
48 #include <boost/lexical_cast.hpp>
49 #include <boost/algorithm/string/trim.hpp>
50 
51 namespace OpenMS
52 {
53 
59  typedef std::vector<Int> IntList;
60 
66  typedef std::vector<double> DoubleList;
67 
68 
74  typedef std::vector<String> StringList;
75 
81  class OPENMS_DLLAPI ListUtils
82  {
83 private:
88  {
89  DoubleTolerancePredicate_(const double& target, const double& tolerance) :
90  tolerance_(tolerance),
91  target_(target)
92  {}
93 
100  inline bool operator()(const double& value)
101  {
102  return std::fabs(value - target_) < tolerance_;
103  }
104 
105 private:
107  double tolerance_;
109  double target_;
110  };
111 
112 public:
121  template <typename T>
122  static std::vector<T> create(const String& str, const char splitter = ',')
123  {
124  // temporary storage for the individual elements of the string
125  std::vector<String> temp_string_vec;
126  str.split(splitter, temp_string_vec);
127  return create<T>(temp_string_vec);
128  }
129 
138  template <typename T>
139  static std::vector<T> create(const std::vector<String>& s);
140 
149  template <typename T, typename E>
150  static bool contains(const std::vector<T>& container, const E& elem)
151  {
152  return find(container.begin(), container.end(), elem) != container.end();
153  }
154 
164  static bool contains(const std::vector<double>& container, const double& elem, double tolerance = 0.00001)
165  {
166  return find_if(container.begin(), container.end(), DoubleTolerancePredicate_(elem, tolerance)) != container.end();
167  }
168 
175  template <typename T>
176  static String concatenate(const std::vector<T>& container, const String& glue = "")
177  {
178  return concatenate< std::vector<T> >(container, glue);
179  }
180 
187  template <typename T>
188  static String concatenate(const T& container, const String& glue = "")
189  {
190  // handle empty containers
191  if (container.empty()) return "";
192 
193  typename T::const_iterator it = container.begin();
194  String ret = String(*it);
195  // we have handled the first element
196  ++it;
197  // add the rest
198  for (; it != container.end(); ++it)
199  {
200  ret += (glue + String(*it));
201  }
202 
203  return ret;
204  }
205 
209  template <typename T, typename E>
210  static Int getIndex(const std::vector<T>& container, const E& elem)
211  {
212  typename std::vector<T>::const_iterator pos =
213  std::find(container.begin(), container.end(), elem);
214  if (pos == container.end()) return -1;
215 
216  return static_cast<Int>(std::distance(container.begin(), pos));
217  }
218 
219  };
220 
221  template <typename T>
222  inline std::vector<T> ListUtils::create(const std::vector<String>& s)
223  {
224  std::vector<T> c;
225  c.reserve(s.size());
226  for (std::vector<String>::const_iterator it = s.begin(); it != s.end(); ++it)
227  {
228  try
229  {
230  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)
231  }
232  catch (boost::bad_lexical_cast&)
233  {
234  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + *it + "'");
235  }
236  }
237 
238  return c;
239  }
240 
242  template <>
243  inline std::vector<String> ListUtils::create(const std::vector<String>& s)
244  {
245  return s;
246  }
247 
248 } // namespace OpenMS
249 
250 #endif // OPENMS_DATASTRUCTURES_LISTUTILS_H
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:164
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:122
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:66
Predicate to check double equality with a given tolerance.
Definition: ListUtils.h:87
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:59
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
DoubleTolerancePredicate_(const double &target, const double &tolerance)
Definition: ListUtils.h:89
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:176
Invalid conversion exception.
Definition: Exception.h:363
Collection of utility functions for management of vectors.
Definition: ListUtils.h:81
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:74
bool operator()(const double &value)
Returns true if | value - target | < tolerance.
Definition: ListUtils.h:100
double target_
The target value that should be found.
Definition: ListUtils.h:109
double tolerance_
The allowed tolerance.
Definition: ListUtils.h:107
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:210
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:188
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:150
int Int
Signed integer type.
Definition: Types.h:103
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:01 using doxygen 1.8.13