paludis  Version 2.6.0
iterator_funcs.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2007, 2008, 2009 Ciaran McCreesh
5  * Copyright (c) 2007 David Leverton
6  *
7  * This file is part of the Paludis package manager. Paludis is free software;
8  * you can redistribute it and/or modify it under the terms of the GNU General
9  * Public License version 2, as published by the Free Software Foundation.
10  *
11  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18  * Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_FUNCS_HH
22 #define PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_FUNCS_HH 1
23 
24 #include <iterator>
25 
26 namespace paludis
27 {
28  using std::next;
29 
30  /**
31  * Return a new iterator pointing to the item before i.
32  *
33  * \ingroup g_iterator
34  */
35  template <typename T_>
36  T_ previous(const T_ & i)
37  {
38  T_ result(i);
39  return --result;
40  }
41 
42  /**
43  * Return the distance from a to b, except if it is greater than n,
44  * in which case return n instead.
45  *
46  * \ingroup g_iterator
47  */
48  template <typename T_>
49  std::size_t capped_distance(T_ a, const T_ & b, unsigned n)
50  {
51  std::size_t x(0);
52  while ((x < n) && (a != b))
53  {
54  ++x;
55  ++a;
56  }
57  return x;
58  }
59 }
60 
61 #endif
Definition: about_metadata-fwd.hh:23
T_ previous(const T_ &i)
Definition: iterator_funcs.hh:36
std::size_t capped_distance(T_ a, const T_ &b, unsigned n)
Definition: iterator_funcs.hh:49