/usr/include/OpenMS/KERNEL/ComparatorUtils.h is in libopenms-dev 1.11.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | // --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Clemens Groepl $
// $Authors: $
// --------------------------------------------------------------------------
#ifndef OPENMS_KERNEL_COMPARATORUTILS_H
#define OPENMS_KERNEL_COMPARATORUTILS_H
#include <functional>
/**
@defgroup ComparatorUtils ComparatorUtils
@brief A collection of utilities for comparators.
@ingroup Kernel
@code
#include <OpenMS/KERNEL/ComparatorUtils.h>
@endcode
This file contains some lightweight class templates
which simplify the (re-)usage and composition of <b>comparator classes</b>:
- ReverseComparator (reverse the direction of comparison)
- LexicographicComparator (combine comparators lexicographically)
- PointerComparator (compare pointers like the type they point to)
.
We provide corresponding "make-functions" so that you will not need to write
out the type names in the template instantiation.
We explain this with a simple example. First a few prerequisites.
@dontinclude Tutorial_ComparatorUtils.C
@until String;
The class IntRealString has three data members. The class
IntRealStringVector is a vector of IntRealString.
We add some print() methods for convenience.
@until };
@until };
Now we will exercise various ways of sorting such a vector.
Of course, we could use the std::sort algorithm with a comparison function
like the following.
@until }
This is straightforward but does not generalize well. Instead we introduce
three <b>comparator classes</b>:
@until };
@until };
@until };
Note how the std::binary_function template provides the necessary type
information (sometimes this is called "reflection").
Now we show various uses of the reverseComparator and lexicographicComparator
function templates.
@until vec.print();
@until vec.print();
@until vec.print();
@until vec.print();
@until vec.print();
@until vec.print();
And here is an application of the pointerComparator function template:
@until main
The output of the example program is:
@code
After initialization:
(1, 4.5, paul)
(2, 4.5, josie)
(1, 4.5, john)
(2, 3.9, kim)
Sorted using lessByInt function:
(1, 4.5, paul)
(1, 4.5, john)
(2, 4.5, josie)
(2, 3.9, kim)
Sorted using LessByInt comparator class:
(1, 4.5, paul)
(1, 4.5, john)
(2, 4.5, josie)
(2, 3.9, kim)
Sorted using reversed LessByInt comparator class:
(2, 4.5, josie)
(2, 3.9, kim)
(1, 4.5, paul)
(1, 4.5, john)
Sorted using lexicographic order: 1. LessByInt, 2. LessByReal
(1, 4.5, paul)
(1, 4.5, john)
(2, 3.9, kim)
(2, 4.5, josie)
Sorted using lexicographic order: 1. reversed LessByInt, 2. LessByReal, 3. LessByString
(2, 3.9, kim)
(2, 4.5, josie)
(1, 4.5, john)
(1, 4.5, paul)
ptr_vec before sorting
(2, 3.9, kim)
(2, 4.5, josie)
(1, 4.5, john)
(1, 4.5, paul)
ptr_vec after sorting with pointerComparator(LessByString())
(1, 4.5, john)
(2, 4.5, josie)
(2, 3.9, kim)
(1, 4.5, paul)
@endcode
Note that pointerComparator can also be used with full-blown iterator classes.
(It should work with everything that provides an operator*(), but the typedefs will always be pointers.)
Note that these templates can also be used with different types for "left" and "right".
*/
namespace OpenMS
{
/**
@brief Wrapper that takes a comparator for `something' and makes a
comparator for <i>pointers</i> to `something' out of it. Normally you should use
the make-function pointerComparator() because then you do not need to
specify the template arguments.
This works by dereferencing the arguments (unary operator*) before
comparing them.
<br> E.g. you can use
<code>PointerComparator<Peak1D::IntensityLess></code> to compare
<code>Peak1D*</code> in the same way as
<code>Peak1D::IntensityLess</code> works for <code>Peak1D</code> .
*/
template <class Cmp>
struct PointerComparator :
public std::binary_function<typename Cmp::first_argument_type *, typename Cmp::second_argument_type *, typename Cmp::result_type>
{
PointerComparator(PointerComparator const & pCmp) :
cmp_(pCmp.cmp_)
{}
PointerComparator(Cmp const & cmp = Cmp()) :
cmp_(cmp)
{}
template <typename T1, typename T2>
typename Cmp::result_type
operator()(T1 left, T2 right) const
{
return cmp_(*left, *right); // T must have operator* defined
}
protected:
Cmp const & cmp_;
};
/**
@brief Make-function to create a PointerComparator from another comparator without the need to
specify the template arguments.
@relatesalso PointerComparator
For example,
<pre>
int i = 88, j = 99;
if ( pointerComparator(std::less<int>())(&i,&j) )
{
// // yes, 88 < 99.
}
</pre>
*/
template <class Cmp>
PointerComparator<Cmp> pointerComparator(Cmp const & cmp)
{
return PointerComparator<Cmp>(cmp);
}
/**
@brief Wrapper that reverses (exchanges) the two arguments of a comparator.
Normally you should use the make-function reverseComparator()
because then you do not need to specify the template arguments.
For example, <code>ReverseComparator<less<T> ></code> works like <code>greater<T></code> .
*/
template <class Cmp>
struct ReverseComparator :
std::binary_function<typename Cmp::second_argument_type, typename Cmp::first_argument_type, typename Cmp::result_type>
// (Note that here we must reverse the order of template args!)
{
ReverseComparator(ReverseComparator const & cmp) :
cmp_(cmp.cmp_) {}
ReverseComparator(Cmp const & cmp = Cmp()) :
cmp_(cmp) {}
template <typename T1, typename T2>
typename Cmp::result_type
operator()(T1 left, T2 right) const
{
return cmp_(right, left); // the other way round
}
protected:
Cmp const & cmp_;
};
/**
@brief Make-function to create a ReverseComparator from another comparator without the need to
specify the template arguments.
@relatesalso ReverseComparator
For example,
<pre>
int i = 88, j = 99;
if ( reverseComparator(std::less<int>())(j,i) )
{
// // yes, 99 > 88.
}
</pre>
*/
template <class Cmp>
ReverseComparator<Cmp> reverseComparator(Cmp const & cmp)
{
return ReverseComparator<Cmp>(cmp);
}
/**
@brief A wrapper class that combines two comparators lexicographically.
Normally you should use the make-function lexicographicComparator()
because then you do not need to specify the template arguments.
Both comparators should of course have the same argument types. The
result_type is bool, that is, we perform a two-way comparison like
<code>less<></code> and its relatives.
*/
template <typename Cmp1, typename Cmp2>
struct LexicographicComparator :
std::binary_function<typename Cmp1::first_argument_type, typename Cmp1::second_argument_type, bool>
{
LexicographicComparator(Cmp1 const & cmp1 = Cmp1(), Cmp2 const & cmp2 = Cmp2()) :
cmp1_(cmp1), cmp2_(cmp2) {}
template <typename T1, typename T2>
bool
operator()(T1 left, T2 right) const
{
if (cmp1_(left, right))
{
return true;
}
else
{
if (cmp1_(right, left))
{
return false;
}
else
{
return cmp2_(left, right);
}
}
}
protected:
Cmp1 const & cmp1_;
Cmp2 const & cmp2_;
};
/**
@brief Make-function to create a LexicographicComparator from two other comparators without
the need to specify the template arguments.
@relatesalso LexicographicComparator
The usage is similar to pointerComparator() or reverseComparator(), which see.
*/
template <typename Cmp1, typename Cmp2>
LexicographicComparator<Cmp1, Cmp2> lexicographicComparator(Cmp1 const & cmp1, Cmp2 const & cmp2)
{
return LexicographicComparator<Cmp1, Cmp2>(cmp1, cmp2);
}
/**
@brief Class for comparison of std::pair using first ONLY e.g. for use with std::sort
*/
template <typename PairType>
struct PairComparatorFirstElement :
std::binary_function<PairType, PairType, bool>
{
bool operator()(const PairType & left, const PairType & right) const
{
return left.first < right.first;
}
};
/**
@brief Class for comparison of std::pair using second ONLY e.g. for use with std::sort
*/
template <typename PairType>
struct PairComparatorSecondElement :
std::binary_function<PairType, PairType, bool>
{
bool operator()(const PairType & left, const PairType & right) const
{
return left.second < right.second;
}
};
/**
@brief Class for comparison of std::pair using first ONLY e.g. for use with std::sort
*/
template <typename PairType>
struct PairComparatorFirstElementMore :
std::binary_function<PairType, PairType, bool>
{
bool operator()(const PairType & left, const PairType & right) const
{
return left.first > right.first;
}
};
/**
@brief Class for comparison of std::pair using second ONLY e.g. for use with std::sort
*/
template <typename PairType>
struct PairComparatorSecondElementMore :
std::binary_function<PairType, PairType, bool>
{
bool operator()(const PairType & left, const PairType & right) const
{
return left.second > right.second;
}
};
/**
@brief Class for comparison of std::pair using first ONLY e.g. for use with std::sort
*/
template <typename PairType>
struct PairMatcherFirstElement :
std::binary_function<PairType, PairType, bool>
{
bool operator()(const PairType & left, const PairType & right) const
{
return left.first == right.first;
}
};
/**
@brief Struct for comparison of std::pair using second ONLY e.g. for use with std::sort
*/
template <typename PairType>
struct PairMatcherSecondElement :
std::binary_function<PairType, PairType, bool>
{
bool operator()(const PairType & left, const PairType & right) const
{
return left.second == right.second;
}
};
/**
@brief Struct for binary predicate to consider equality with a certain tolerance
@param i first value
@param i second value
@return bool if the two parameters are in tolerance close to each other
*/
template <typename CompareType>
struct EqualInTolerance :
public std::binary_function<CompareType, CompareType, bool>
{
CompareType & tolerance;
explicit EqualInTolerance(CompareType & c) :
tolerance(c)
{}
bool operator()(CompareType i, CompareType j)
{
CompareType diff = fabs(i - j);
return diff <= tolerance;
}
};
}
#endif // OPENMS_KERNEL_COMPARATORUTILS_H
|