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.
25 /// Returns true if the sequence first,last is sorted, false if not.
27 bool sorted(For first, For last)
29 if (first == last) return true;
31 while (++tmp != last) {
32 if (*tmp < *first++) return false;
38 /// Cmp is the same Cmp as you would pass to std::sort.
39 template <class For, class Cmp>
40 bool sorted(For first, For last, Cmp cmp)
42 if (first == last) return true;
44 while (++tmp != last) {
45 if (cmp(*tmp, *first++)) return false;
52 template <class P1, class P2>
53 P1 operator()(std::pair<P1, P2> const & p) {
60 * copy elements in the given range to the output iterator
61 * if the predicate evaluates as true
63 template <class InputIter, class OutputIter, class Func>
64 OutputIter copy_if(InputIter first, InputIter last,
65 OutputIter result, Func func)
67 for (; first != last; ++first) {
76 /// A slot in replacement for std::count for systems where it is broken.
77 template <class Iterator, class T>
78 typename std::iterator_traits<Iterator>::difference_type
79 count (Iterator first, Iterator last, T const & value)
82 return std::count(first, last, value);
84 std::iterator_traits<Iterator>::difference_type n = 0;
86 if (*first++ == value) ++n;
91 /// Remove all duplicate entries in c.
93 void eliminate_duplicates(C & c)
95 // It is a requirement that the container is sorted for
96 // std::unique to work properly.
97 std::sort(c.begin(), c.end());
98 c.erase(std::unique(c.begin(), c.end()), c.end());