4 * Copyright 1995-2002 the LyX Team
5 * Read the file COPYING
7 * A variety of useful templates.
22 /// Returns true if the sequence first,last is sorted, false if not.
24 bool sorted(For first, For last)
26 if (first == last) return true;
28 while (++tmp != last) {
29 if (*tmp < *first++) return false;
35 /// Cmp is the same Cmp as you would pass to std::sort.
36 template <class For, class Cmp>
37 bool sorted(For first, For last, Cmp cmp)
39 if (first == last) return true;
41 while (++tmp != last) {
42 if (cmp(*tmp, *first++)) return false;
49 template <class P1, class P2>
50 P1 operator()(std::pair<P1, P2> const & p) {
57 * copy elements in the given range to the output iterator
58 * if the predicate evaluates as true
60 template <class InputIter, class OutputIter, class Func>
61 OutputIter copy_if(InputIter first, InputIter last,
62 OutputIter result, Func func)
64 for (; first != last; ++first) {
73 /// A slot in replacement for std::count for systems where it is broken.
74 template <class Iterator, class T>
75 typename std::iterator_traits<Iterator>::difference_type
76 count (Iterator first, Iterator last, T const & value)
79 return std::count(first, last, value);
81 std::iterator_traits<Iterator>::difference_type n = 0;
83 if (*first++ == value) ++n;
88 /// Remove all duplicate entries in c.
90 void eliminate_duplicates(C & c)
92 std::sort(c.begin(), c.end());
93 typename C::iterator p = std::unique(c.begin(), c.end());