1 // (C) Copyright Jeremy Siek 2001.
\r
2 // Distributed under the Boost Software License, Version 1.0. (See accompany-
\r
3 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
8 * Hewlett-Packard Company
\r
10 * Permission to use, copy, modify, distribute and sell this software
\r
11 * and its documentation for any purpose is hereby granted without fee,
\r
12 * provided that the above copyright notice appear in all copies and
\r
13 * that both that copyright notice and this permission notice appear
\r
14 * in supporting documentation. Hewlett-Packard Company makes no
\r
15 * representations about the suitability of this software for any
\r
16 * purpose. It is provided "as is" without express or implied warranty.
\r
19 * Copyright (c) 1996
\r
20 * Silicon Graphics Computer Systems, Inc.
\r
22 * Permission to use, copy, modify, distribute and sell this software
\r
23 * and its documentation for any purpose is hereby granted without fee,
\r
24 * provided that the above copyright notice appear in all copies and
\r
25 * that both that copyright notice and this permission notice appear
\r
26 * in supporting documentation. Silicon Graphics makes no
\r
27 * representations about the suitability of this software for any
\r
28 * purpose. It is provided "as is" without express or implied warranty.
\r
31 #ifndef BOOST_ALGORITHM_HPP
\r
32 # define BOOST_ALGORITHM_HPP
\r
33 # include <boost/detail/iterator.hpp>
\r
34 // Algorithms on sequences
\r
36 // The functions in this file have not yet gone through formal
\r
37 // review, and are subject to change. This is a work in progress.
\r
38 // They have been checked into the detail directory because
\r
39 // there are some graph algorithms that use these functions.
\r
41 #include <algorithm>
\r
46 template <typename Iter1, typename Iter2>
\r
47 Iter1 begin(const std::pair<Iter1, Iter2>& p) { return p.first; }
\r
49 template <typename Iter1, typename Iter2>
\r
50 Iter2 end(const std::pair<Iter1, Iter2>& p) { return p.second; }
\r
52 template <typename Iter1, typename Iter2>
\r
53 typename boost::detail::iterator_traits<Iter1>::difference_type
\r
54 size(const std::pair<Iter1, Iter2>& p) {
\r
55 return std::distance(p.first, p.second);
\r
59 // These seem to interfere with the std::pair overloads :(
\r
60 template <typename Container>
\r
61 typename Container::iterator
\r
62 begin(Container& c) { return c.begin(); }
\r
64 template <typename Container>
\r
65 typename Container::const_iterator
\r
66 begin(const Container& c) { return c.begin(); }
\r
68 template <typename Container>
\r
69 typename Container::iterator
\r
70 end(Container& c) { return c.end(); }
\r
72 template <typename Container>
\r
73 typename Container::const_iterator
\r
74 end(const Container& c) { return c.end(); }
\r
76 template <typename Container>
\r
77 typename Container::size_type
\r
78 size(const Container& c) { return c.size(); }
\r
80 template <typename T>
\r
81 typename std::vector<T>::iterator
\r
82 begin(std::vector<T>& c) { return c.begin(); }
\r
84 template <typename T>
\r
85 typename std::vector<T>::const_iterator
\r
86 begin(const std::vector<T>& c) { return c.begin(); }
\r
88 template <typename T>
\r
89 typename std::vector<T>::iterator
\r
90 end(std::vector<T>& c) { return c.end(); }
\r
92 template <typename T>
\r
93 typename std::vector<T>::const_iterator
\r
94 end(const std::vector<T>& c) { return c.end(); }
\r
96 template <typename T>
\r
97 typename std::vector<T>::size_type
\r
98 size(const std::vector<T>& c) { return c.size(); }
\r
101 template <class ForwardIterator, class T>
\r
102 void iota(ForwardIterator first, ForwardIterator last, T value)
\r
104 for (; first != last; ++first, ++value)
\r
107 template <typename Container, typename T>
\r
108 void iota(Container& c, const T& value)
\r
110 iota(begin(c), end(c), value);
\r
113 // Also do version with 2nd container?
\r
114 template <typename Container, typename OutIter>
\r
115 OutIter copy(const Container& c, OutIter result) {
\r
116 return std::copy(begin(c), end(c), result);
\r
119 template <typename Container1, typename Container2>
\r
120 bool equal(const Container1& c1, const Container2& c2)
\r
122 if (size(c1) != size(c2))
\r
124 return std::equal(begin(c1), end(c1), begin(c2));
\r
127 template <typename Container>
\r
128 void sort(Container& c) { std::sort(begin(c), end(c)); }
\r
130 template <typename Container, typename Predicate>
\r
131 void sort(Container& c, const Predicate& p) {
\r
132 std::sort(begin(c), end(c), p);
\r
135 template <typename Container>
\r
136 void stable_sort(Container& c) { std::stable_sort(begin(c), end(c)); }
\r
138 template <typename Container, typename Predicate>
\r
139 void stable_sort(Container& c, const Predicate& p) {
\r
140 std::stable_sort(begin(c), end(c), p);
\r
143 template <typename InputIterator, typename Predicate>
\r
144 bool any_if(InputIterator first, InputIterator last, Predicate p)
\r
146 return std::find_if(first, last, p) != last;
\r
148 template <typename Container, typename Predicate>
\r
149 bool any_if(const Container& c, Predicate p)
\r
151 return any_if(begin(c), end(c), p);
\r
154 template <typename InputIterator, typename T>
\r
155 bool container_contains(InputIterator first, InputIterator last, T value)
\r
157 return std::find(first, last, value) != last;
\r
159 template <typename Container, typename T>
\r
160 bool container_contains(const Container& c, const T& value)
\r
162 return container_contains(begin(c), end(c), value);
\r
165 template <typename Container, typename T>
\r
166 std::size_t count(const Container& c, const T& value)
\r
168 return std::count(begin(c), end(c), value);
\r
171 template <typename Container, typename Predicate>
\r
172 std::size_t count_if(const Container& c, Predicate p)
\r
174 return std::count_if(begin(c), end(c), p);
\r
177 template <typename ForwardIterator>
\r
178 bool is_sorted(ForwardIterator first, ForwardIterator last)
\r
183 ForwardIterator next = first;
\r
184 for (++next; next != last; first = next, ++next) {
\r
185 if (*next < *first)
\r
192 template <typename ForwardIterator, typename StrictWeakOrdering>
\r
193 bool is_sorted(ForwardIterator first, ForwardIterator last,
\r
194 StrictWeakOrdering comp)
\r
199 ForwardIterator next = first;
\r
200 for (++next; next != last; first = next, ++next) {
\r
201 if (comp(*next, *first))
\r
208 template <typename Container>
\r
209 bool is_sorted(const Container& c)
\r
211 return is_sorted(begin(c), end(c));
\r
214 template <typename Container, typename StrictWeakOrdering>
\r
215 bool is_sorted(const Container& c, StrictWeakOrdering comp)
\r
217 return is_sorted(begin(c), end(c), comp);
\r
220 } // namespace boost
\r
222 #endif // BOOST_ALGORITHM_HPP
\r