4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS
11 * A variety of useful templates.
26 /// Returns true if the sequence first,last is sorted, false if not.
28 bool sorted(For first, For last)
30 if (first == last) return true;
32 while (++tmp != last) {
33 if (*tmp < *first++) return false;
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)
43 if (first == last) return true;
45 while (++tmp != last) {
46 if (cmp(*tmp, *first++)) return false;
53 template <class P1, class P2>
54 P1 operator()(std::pair<P1, P2> const & p) {
61 * copy elements in the given range to the output iterator
62 * if the predicate evaluates as true
64 template <class InputIter, class OutputIter, class Func>
65 OutputIter copy_if(InputIter first, InputIter last,
66 OutputIter result, Func func)
68 for (; first != last; ++first) {
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)
83 return std::count(first, last, value);
85 std::iterator_traits<Iterator>::difference_type n = 0;
87 if (*first++ == value) ++n;
92 /// Remove all duplicate entries in c.
94 void eliminate_duplicates(C & c)
97 std::set<typename C::value_type> s;
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);