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.
24 /// Returns true if the sequence first,last is sorted, false if not.
26 bool sorted(For first, For last)
28 if (first == last) return true;
30 while (++tmp != last) {
31 if (*tmp < *first++) return false;
37 /// Cmp is the same Cmp as you would pass to std::sort.
38 template <class For, class Cmp>
39 bool sorted(For first, For last, Cmp cmp)
41 if (first == last) return true;
43 while (++tmp != last) {
44 if (cmp(*tmp, *first++)) return false;
51 template <class P1, class P2>
52 P1 operator()(std::pair<P1, P2> const & p) {
59 * copy elements in the given range to the output iterator
60 * if the predicate evaluates as true
62 template <class InputIter, class OutputIter, class Func>
63 OutputIter copy_if(InputIter first, InputIter last,
64 OutputIter result, Func func)
66 for (; first != last; ++first) {
75 /// A slot in replacement for std::count for systems where it is broken.
76 template <class Iterator, class T>
77 typename std::iterator_traits<Iterator>::difference_type
78 count (Iterator first, Iterator last, T const & value)
81 return std::count(first, last, value);
83 std::iterator_traits<Iterator>::difference_type n = 0;
85 if (*first++ == value) ++n;
90 /// Remove all duplicate entries in c.
92 void eliminate_duplicates(C & c)
94 std::sort(c.begin(), c.end());
95 typename C::iterator p = std::unique(c.begin(), c.end());