]> git.lyx.org Git - lyx.git/blob - src/support/lyxalgo.h
6bccfb12966bd75906e70470d093882dd2d9965d
[lyx.git] / src / support / lyxalgo.h
1 // -*- C++ -*-
2 /**
3  * \file lyxalgo.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS
10  *
11  * A variety of useful templates.
12  */
13
14 #ifndef LYX_ALGO_H
15 #define LYX_ALGO_H
16
17 #include <utility>
18 #include <iterator>
19 #include <algorithm>
20 #include <set>
21
22
23 namespace lyx {
24
25
26 /// Returns true if the sequence first,last is sorted, false if not.
27 template <class For>
28 bool sorted(For first, For last)
29 {
30         if (first == last) return true;
31         For tmp = first;
32         while (++tmp != last) {
33                 if (*tmp < *first++) return false;
34         }
35         return true;
36 }
37
38
39 /// Cmp is the same Cmp as you would pass to std::sort.
40 template <class For, class Cmp>
41 bool sorted(For first, For last, Cmp cmp)
42 {
43         if (first == last) return true;
44         For tmp = first;
45         while (++tmp != last) {
46                 if (cmp(*tmp, *first++)) return false;
47         }
48         return true;
49 }
50
51
52 struct firster {
53         template <class P1, class P2>
54         P1 operator()(std::pair<P1, P2> const & p) {
55                 return p.first;
56         }
57 };
58
59
60 /**
61  * copy elements in the given range to the output iterator
62  * if the predicate evaluates as true
63  */
64 template <class InputIter, class OutputIter, class Func>
65 OutputIter copy_if(InputIter first, InputIter last,
66                OutputIter result, Func func)
67 {
68         for (; first != last; ++first) {
69                 if (func(*first)) {
70                         *result++ = *first;
71                 }
72         }
73         return result;
74 }
75
76
77 /// A slot in replacement for std::count for systems where it is broken.
78 template <class Iterator, class T>
79 typename std::iterator_traits<Iterator>::difference_type
80 count (Iterator first, Iterator last, T const & value)
81 {
82 #ifdef HAVE_STD_COUNT
83         return std::count(first, last, value);
84 #else
85         std::iterator_traits<Iterator>::difference_type n = 0;
86         while (first != last)
87                 if (*first++ == value) ++n;
88         return n;
89 #endif
90 }
91
92 /// Remove all duplicate entries in c.
93 template<class C>
94 void eliminate_duplicates(C & c)
95 {
96         C unique_c;
97         std::set<typename C::value_type> s;
98
99         for (typename C::iterator p = c.begin(); p != c.end(); ++p) {
100                 if (s.find(*p) == s.end()) {
101                         unique_c.push_back(*p);
102                         s.insert(*p);
103                 }
104         }
105         swap(c, unique_c);
106 }
107
108 } // namespace lyx
109
110 #endif // LYX_ALGO_H