1 /*
2 * Inkscape::Util::map_list - apply a function over a list
3 *
4 * Authors:
5 * MenTaLguY <mental@rydia.net>
6 *
7 * Copyright (C) 2004 MenTaLguY
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #ifndef SEEN_INKSCAPE_UTIL_MAP_LIST_H
13 #define SEEN_INKSCAPE_UTIL_MAP_LIST_H
15 #include <algorithm>
16 #include "util/list.h"
18 namespace Inkscape {
20 namespace Util {
22 template <typename T, typename InputIterator, typename UnaryFunction>
23 inline MutableList<T>
24 map_list(UnaryFunction f, InputIterator start, InputIterator end)
25 {
26 if ( start != end ) {
27 MutableList<T> head(f(*start));
28 MutableList<T> tail(head);
29 while ( ++start != end ) {
30 MutableList<T> cell(f(*start));
31 set_rest(tail, cell);
32 tail = cell;
33 }
34 return head;
35 } else {
36 return MutableList<T>();
37 }
38 }
40 template <typename T1, typename T2, typename UnaryFunction>
41 inline MutableList<T1> map_list(UnaryFunction f, List<T2> list) {
42 return map_list(f, list, List<T2>());
43 }
45 template <typename T, typename UnaryFunction>
46 inline List<T>
47 map_list_in_place(UnaryFunction f, List<T> start,
48 List<T> end=List<T>())
49 {
50 std::transform(start, end, start, f);
51 return start;
52 }
54 }
56 }
58 #endif
59 /*
60 Local Variables:
61 mode:c++
62 c-file-style:"stroustrup"
63 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
64 indent-tabs-mode:nil
65 fill-column:99
66 End:
67 */
68 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :