iterator_funcs.hh

00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2007, 2008, 2009 Ciaran McCreesh
00005  * Copyright (c) 2007 David Leverton
00006  *
00007  * This file is part of the Paludis package manager. Paludis is free software;
00008  * you can redistribute it and/or modify it under the terms of the GNU General
00009  * Public License version 2, as published by the Free Software Foundation.
00010  *
00011  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00014  * details.
00015  *
00016  * You should have received a copy of the GNU General Public License along with
00017  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018  * Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_FUNCS_HH
00022 #define PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_FUNCS_HH 1
00023 
00024 #include <iterator>
00025 
00026 namespace paludis
00027 {
00028 #ifdef PALUDIS_HAVE_STD_NEXT
00029     using std::next;
00030 #else
00031     /**
00032      * Return a new iterator pointing to the item after i.
00033      *
00034      * \ingroup g_iterator
00035      */
00036     template <typename T_>
00037     T_ next(const T_ & i)
00038     {
00039         T_ result(i);
00040         return ++result;
00041     }
00042 
00043      /**
00044      * Return a new iterator advanced by a distance of n.
00045      *
00046      * \ingroup g_iterator
00047      */
00048     template <typename T_>
00049     T_ next(const T_ & i, const int & n)
00050     {
00051         T_ result(i);
00052         std::advance(result, n);
00053         return result;
00054     }
00055 #endif
00056 
00057     /**
00058      * Return a new iterator pointing to the item before i.
00059      *
00060      * \ingroup g_iterator
00061      */
00062     template <typename T_>
00063     T_ previous(const T_ & i)
00064     {
00065         T_ result(i);
00066         return --result;
00067     }
00068 
00069     /**
00070      * Return the distance from a to b, except if it is greater than n,
00071      * in which case return n instead.
00072      *
00073      * \ingroup g_iterator
00074      */
00075     template <typename T_>
00076     std::size_t capped_distance(T_ a, const T_ & b, unsigned n)
00077     {
00078         std::size_t x(0);
00079         while ((x < n) && (a != b))
00080         {
00081             ++x;
00082             ++a;
00083         }
00084         return x;
00085     }
00086 }
00087 
00088 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4