1 // (C) Copyright Jeremy Siek 2001. Permission to copy, use, modify,
2 // sell and distribute this software is granted provided this
3 // copyright notice appears in all copies. This software is provided
4 // "as is" without express or implied warranty, and with no claim as
5 // to its suitability for any purpose.
10 * Hewlett-Packard Company
12 * Permission to use, copy, modify, distribute and sell this software
13 * and its documentation for any purpose is hereby granted without fee,
14 * provided that the above copyright notice appear in all copies and
15 * that both that copyright notice and this permission notice appear
16 * in supporting documentation. Hewlett-Packard Company makes no
17 * representations about the suitability of this software for any
18 * purpose. It is provided "as is" without express or implied warranty.
22 * Silicon Graphics Computer Systems, Inc.
24 * Permission to use, copy, modify, distribute and sell this software
25 * and its documentation for any purpose is hereby granted without fee,
26 * provided that the above copyright notice appear in all copies and
27 * that both that copyright notice and this permission notice appear
28 * in supporting documentation. Silicon Graphics makes no
29 * representations about the suitability of this software for any
30 * purpose. It is provided "as is" without express or implied warranty.
33 #ifndef BOOST_ALGORITHM_HPP
34 # define BOOST_ALGORITHM_HPP
35 # include <boost/detail/iterator.hpp>
36 // Algorithms on sequences
38 // The functions in this file have not yet gone through formal
39 // review, and are subject to change. This is a work in progress.
40 // They have been checked into the detail directory because
41 // there are some graph algorithms that use these functions.
48 template <typename Iter1, typename Iter2>
49 Iter1 begin(const std::pair<Iter1, Iter2>& p) { return p.first; }
51 template <typename Iter1, typename Iter2>
52 Iter2 end(const std::pair<Iter1, Iter2>& p) { return p.second; }
54 template <typename Iter1, typename Iter2>
55 typename boost::detail::iterator_traits<Iter1>::difference_type
56 size(const std::pair<Iter1, Iter2>& p) {
57 return std::distance(p.first, p.second);
61 // These seem to interfere with the std::pair overloads :(
62 template <typename Container>
63 typename Container::iterator
64 begin(Container& c) { return c.begin(); }
66 template <typename Container>
67 typename Container::const_iterator
68 begin(const Container& c) { return c.begin(); }
70 template <typename Container>
71 typename Container::iterator
72 end(Container& c) { return c.end(); }
74 template <typename Container>
75 typename Container::const_iterator
76 end(const Container& c) { return c.end(); }
78 template <typename Container>
79 typename Container::size_type
80 size(const Container& c) { return c.size(); }
83 typename std::vector<T>::iterator
84 begin(std::vector<T>& c) { return c.begin(); }
87 typename std::vector<T>::const_iterator
88 begin(const std::vector<T>& c) { return c.begin(); }
91 typename std::vector<T>::iterator
92 end(std::vector<T>& c) { return c.end(); }
95 typename std::vector<T>::const_iterator
96 end(const std::vector<T>& c) { return c.end(); }
99 typename std::vector<T>::size_type
100 size(const std::vector<T>& c) { return c.size(); }
103 template <class ForwardIterator, class T>
104 void iota(ForwardIterator first, ForwardIterator last, T value)
106 for (; first != last; ++first, ++value)
109 template <typename Container, typename T>
110 void iota(Container& c, const T& value)
112 iota(begin(c), end(c), value);
115 // Also do version with 2nd container?
116 template <typename Container, typename OutIter>
117 OutIter copy(const Container& c, OutIter result) {
118 return std::copy(begin(c), end(c), result);
121 template <typename Container1, typename Container2>
122 bool equal(const Container1& c1, const Container2& c2)
124 if (size(c1) != size(c2))
126 return std::equal(begin(c1), end(c1), begin(c2));
129 template <typename Container>
130 void sort(Container& c) { std::sort(begin(c), end(c)); }
132 template <typename Container, typename Predicate>
133 void sort(Container& c, const Predicate& p) {
134 std::sort(begin(c), end(c), p);
137 template <typename Container>
138 void stable_sort(Container& c) { std::stable_sort(begin(c), end(c)); }
140 template <typename Container, typename Predicate>
141 void stable_sort(Container& c, const Predicate& p) {
142 std::stable_sort(begin(c), end(c), p);
145 template <typename InputIterator, typename Predicate>
146 bool any_if(InputIterator first, InputIterator last, Predicate p)
148 return std::find_if(first, last, p) != last;
150 template <typename Container, typename Predicate>
151 bool any_if(const Container& c, Predicate p)
153 return any_if(begin(c), end(c), p);
156 template <typename InputIterator, typename T>
157 bool contains(InputIterator first, InputIterator last, T value)
159 return std::find(first, last, value) != last;
161 template <typename Container, typename T>
162 bool contains(const Container& c, const T& value)
164 return contains(begin(c), end(c), value);
167 template <typename InputIterator, typename Predicate>
168 bool all(InputIterator first, InputIterator last, Predicate p)
170 for (; first != last; ++first)
175 template <typename Container, typename Predicate>
176 bool all(const Container& c, Predicate p)
178 return all(begin(c), end(c), p);
181 template <typename InputIterator, typename Predicate>
182 bool none(InputIterator first, InputIterator last, Predicate p)
184 return std::find_if(first, last, p) == last;
186 template <typename Container, typename Predicate>
187 bool none(const Container& c, Predicate p)
189 return none(begin(c), end(c), p);
192 template <typename Container, typename T>
193 std::size_t count(const Container& c, const T& value)
195 return std::count(begin(c), end(c), value);
198 template <typename Container, typename Predicate>
199 std::size_t count_if(const Container& c, Predicate p)
201 return std::count_if(begin(c), end(c), p);
204 template <typename ForwardIterator>
205 bool is_sorted(ForwardIterator first, ForwardIterator last)
210 ForwardIterator next = first;
211 for (++next; next != last; first = next, ++next) {
219 template <typename ForwardIterator, typename StrictWeakOrdering>
220 bool is_sorted(ForwardIterator first, ForwardIterator last,
221 StrictWeakOrdering comp)
226 ForwardIterator next = first;
227 for (++next; next != last; first = next, ++next) {
228 if (comp(*next, *first))
235 template <typename Container>
236 bool is_sorted(const Container& c)
238 return is_sorted(begin(c), end(c));
241 template <typename Container, typename StrictWeakOrdering>
242 bool is_sorted(const Container& c, StrictWeakOrdering comp)
244 return is_sorted(begin(c), end(c), comp);
249 #endif // BOOST_ALGORITHM_HPP