1 /*
2 * Inkscape::Util::filter_list - select a subset of the items in 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_FILTER_LIST_H
13 #define SEEN_INKSCAPE_UTIL_FILTER_LIST_H
15 #include "util/list.h"
16 #include "util/list-copy.h"
18 namespace Inkscape {
20 namespace Util {
22 template <typename InputIterator, typename UnaryPredicate>
23 inline typename Traits::ListCopy<InputIterator>::ResultList
24 filter_list(UnaryPredicate p, InputIterator start, InputIterator end) {
25 typedef typename Traits::ListCopy<InputIterator>::ResultList ResultList;
26 ResultList head;
27 ResultList tail;
28 while ( start != end && !p(*start) ) {
29 ++start;
30 }
31 if ( start != end ) {
32 head = tail = ResultList(*start);
33 ++start;
34 }
35 while ( start != end ) {
36 if (p(*start)) {
37 set_rest(tail, ResultList(*start));
38 ++tail;
39 }
40 ++start;
41 }
42 return head;
43 }
45 template <typename T, typename UnaryPredicate>
46 inline typename Traits::ListCopy<List<T> >::ResultList
47 filter_list(UnaryPredicate p, List<T> const &list) {
48 return filter_list(p, list, List<T>());
49 }
51 }
53 }
55 #endif
56 /*
57 Local Variables:
58 mode:c++
59 c-file-style:"stroustrup"
60 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
61 indent-tabs-mode:nil
62 fill-column:99
63 End:
64 */
65 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :