]> git.lyx.org Git - lyx.git/blob - src/support/lyxalgo.h
inherit privately from noncopyable, add algos, use namespace lyx
[lyx.git] / src / support / lyxalgo.h
1 // -*- C++ -*-
2
3 #ifndef LYX_ALGO_H
4 #define LYX_ALGO_H
5
6 #include <utility>
7
8 namespace lyx {
9
10
11 /// Returns true if the sequence first,last is sorted, false if not.
12 template <class For>
13 bool sorted(For first, For last)
14 {
15         if (first == last) return true;
16         For tmp = first;
17         while (++tmp != last) {
18                 if (*tmp < *first++) return false;
19         }
20         return true;
21 }
22
23
24 /// Cmp is the same Cmp as you would pass to std::sort.
25 template <class For, class Cmp>
26 bool sorted(For first, For last, Cmp cmp)
27 {
28         if (first == last) return true;
29         For tmp = first;
30         while (++tmp != last) {
31                 if (cmp(*tmp, *first++)) return false;
32         }
33         return true;
34 }
35
36
37 struct firster {
38         template <class P1, class P2>
39         P1 operator()(std::pair<P1, P2> const & p) {
40                 return p.first;
41         }
42 };
43
44
45 template <class InputIter, class OutputIter, class Func>
46 OutputIter copy_if(InputIter first, InputIter last,
47                OutputIter result, Func func) 
48 {
49         for (; first != last; ++first) {
50                 if (func(*first)) {
51                         *result++ = *first;
52                 }
53         }
54         return result;
55 }
56
57 }  // end of namespace lyx
58 #endif