b67db986adc63578d0579b8e2451e6c9581d9232
[platform/upstream/libzypp.git] / zypp / base / Algorithm.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/Algorithm.h
10  *
11 */
12 #ifndef ZYPP_BASE_ALGORITHM_H
13 #define ZYPP_BASE_ALGORITHM_H
14
15 #include <algorithm>
16
17 ///////////////////////////////////////////////////////////////////
18 namespace zypp
19 { /////////////////////////////////////////////////////////////////
20
21   /** Iterate through <tt>[begin_r,end_r)</tt> and invoke \a fnc_r
22    *  on each item that passes \a filter_r.
23    *
24    * Iteration aborts if \a fnc_r returns \c false.
25    *
26    * \return Number of invokations of \a fnc_r, negative if
27    * loop was aborted by \a fnc_.
28   */
29   template <class _Iterator, class _Filter, class _Function>
30     inline int invokeOnEach( _Iterator begin_r, _Iterator end_r,
31                              _Filter filter_r,
32                              _Function fnc_r )
33     {
34       int cnt = 0;
35       for ( _Iterator it = begin_r; it != end_r; ++it )
36         {
37           if ( filter_r( *it ) )
38             {
39               ++cnt;
40               if ( ! fnc_r( *it ) )
41                   return -cnt;
42             }
43         }
44       return cnt;
45     }
46
47   /** Iterate through <tt>[begin_r,end_r)</tt> and invoke \a fnc_r
48    *  on each item.
49    *
50    * Iteration aborts if \a fnc_r returns \c false.
51    *
52    * \return Number of invokations of \a fnc_r, negative if
53    * loop was aborted by \a fnc_.
54   */
55   template <class _Iterator, class _Function>
56     inline int invokeOnEach( _Iterator begin_r, _Iterator end_r,
57                              _Function fnc_r )
58     {
59       int cnt = 0;
60       for ( _Iterator it = begin_r; it != end_r; ++it )
61         {
62           ++cnt;
63           if ( ! fnc_r( *it ) )
64             return -cnt;
65         }
66       return cnt;
67     }
68
69   /////////////////////////////////////////////////////////////////
70 } // namespace zypp
71 ///////////////////////////////////////////////////////////////////
72 #endif // ZYPP_BASE_ALGORITHM_H