williamr@4
|
1 |
// (C) Copyright Jeremy Siek 2001.
|
williamr@4
|
2 |
// Distributed under the Boost Software License, Version 1.0. (See accompany-
|
williamr@4
|
3 |
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
4 |
|
williamr@4
|
5 |
/*
|
williamr@4
|
6 |
*
|
williamr@4
|
7 |
* Copyright (c) 1994
|
williamr@4
|
8 |
* Hewlett-Packard Company
|
williamr@4
|
9 |
*
|
williamr@4
|
10 |
* Permission to use, copy, modify, distribute and sell this software
|
williamr@4
|
11 |
* and its documentation for any purpose is hereby granted without fee,
|
williamr@4
|
12 |
* provided that the above copyright notice appear in all copies and
|
williamr@4
|
13 |
* that both that copyright notice and this permission notice appear
|
williamr@4
|
14 |
* in supporting documentation. Hewlett-Packard Company makes no
|
williamr@4
|
15 |
* representations about the suitability of this software for any
|
williamr@4
|
16 |
* purpose. It is provided "as is" without express or implied warranty.
|
williamr@4
|
17 |
*
|
williamr@4
|
18 |
*
|
williamr@4
|
19 |
* Copyright (c) 1996
|
williamr@4
|
20 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@4
|
21 |
*
|
williamr@4
|
22 |
* Permission to use, copy, modify, distribute and sell this software
|
williamr@4
|
23 |
* and its documentation for any purpose is hereby granted without fee,
|
williamr@4
|
24 |
* provided that the above copyright notice appear in all copies and
|
williamr@4
|
25 |
* that both that copyright notice and this permission notice appear
|
williamr@4
|
26 |
* in supporting documentation. Silicon Graphics makes no
|
williamr@4
|
27 |
* representations about the suitability of this software for any
|
williamr@4
|
28 |
* purpose. It is provided "as is" without express or implied warranty.
|
williamr@4
|
29 |
*/
|
williamr@4
|
30 |
|
williamr@4
|
31 |
#ifndef BOOST_ALGORITHM_HPP
|
williamr@4
|
32 |
# define BOOST_ALGORITHM_HPP
|
williamr@4
|
33 |
# include <boost/detail/iterator.hpp>
|
williamr@4
|
34 |
// Algorithms on sequences
|
williamr@2
|
35 |
//
|
williamr@4
|
36 |
// The functions in this file have not yet gone through formal
|
williamr@4
|
37 |
// review, and are subject to change. This is a work in progress.
|
williamr@4
|
38 |
// They have been checked into the detail directory because
|
williamr@4
|
39 |
// there are some graph algorithms that use these functions.
|
williamr@2
|
40 |
|
williamr@4
|
41 |
#include <algorithm>
|
williamr@4
|
42 |
#include <vector>
|
williamr@2
|
43 |
|
williamr@2
|
44 |
namespace boost {
|
williamr@2
|
45 |
|
williamr@4
|
46 |
template <typename Iter1, typename Iter2>
|
williamr@4
|
47 |
Iter1 begin(const std::pair<Iter1, Iter2>& p) { return p.first; }
|
williamr@4
|
48 |
|
williamr@4
|
49 |
template <typename Iter1, typename Iter2>
|
williamr@4
|
50 |
Iter2 end(const std::pair<Iter1, Iter2>& p) { return p.second; }
|
williamr@4
|
51 |
|
williamr@4
|
52 |
template <typename Iter1, typename Iter2>
|
williamr@4
|
53 |
typename boost::detail::iterator_traits<Iter1>::difference_type
|
williamr@4
|
54 |
size(const std::pair<Iter1, Iter2>& p) {
|
williamr@4
|
55 |
return std::distance(p.first, p.second);
|
williamr@2
|
56 |
}
|
williamr@2
|
57 |
|
williamr@4
|
58 |
#if 0
|
williamr@4
|
59 |
// These seem to interfere with the std::pair overloads :(
|
williamr@4
|
60 |
template <typename Container>
|
williamr@4
|
61 |
typename Container::iterator
|
williamr@4
|
62 |
begin(Container& c) { return c.begin(); }
|
williamr@4
|
63 |
|
williamr@4
|
64 |
template <typename Container>
|
williamr@4
|
65 |
typename Container::const_iterator
|
williamr@4
|
66 |
begin(const Container& c) { return c.begin(); }
|
williamr@4
|
67 |
|
williamr@4
|
68 |
template <typename Container>
|
williamr@4
|
69 |
typename Container::iterator
|
williamr@4
|
70 |
end(Container& c) { return c.end(); }
|
williamr@4
|
71 |
|
williamr@4
|
72 |
template <typename Container>
|
williamr@4
|
73 |
typename Container::const_iterator
|
williamr@4
|
74 |
end(const Container& c) { return c.end(); }
|
williamr@4
|
75 |
|
williamr@4
|
76 |
template <typename Container>
|
williamr@4
|
77 |
typename Container::size_type
|
williamr@4
|
78 |
size(const Container& c) { return c.size(); }
|
williamr@4
|
79 |
#else
|
williamr@4
|
80 |
template <typename T>
|
williamr@4
|
81 |
typename std::vector<T>::iterator
|
williamr@4
|
82 |
begin(std::vector<T>& c) { return c.begin(); }
|
williamr@4
|
83 |
|
williamr@4
|
84 |
template <typename T>
|
williamr@4
|
85 |
typename std::vector<T>::const_iterator
|
williamr@4
|
86 |
begin(const std::vector<T>& c) { return c.begin(); }
|
williamr@4
|
87 |
|
williamr@4
|
88 |
template <typename T>
|
williamr@4
|
89 |
typename std::vector<T>::iterator
|
williamr@4
|
90 |
end(std::vector<T>& c) { return c.end(); }
|
williamr@4
|
91 |
|
williamr@4
|
92 |
template <typename T>
|
williamr@4
|
93 |
typename std::vector<T>::const_iterator
|
williamr@4
|
94 |
end(const std::vector<T>& c) { return c.end(); }
|
williamr@4
|
95 |
|
williamr@4
|
96 |
template <typename T>
|
williamr@4
|
97 |
typename std::vector<T>::size_type
|
williamr@4
|
98 |
size(const std::vector<T>& c) { return c.size(); }
|
williamr@4
|
99 |
#endif
|
williamr@4
|
100 |
|
williamr@4
|
101 |
template <class ForwardIterator, class T>
|
williamr@4
|
102 |
void iota(ForwardIterator first, ForwardIterator last, T value)
|
williamr@4
|
103 |
{
|
williamr@4
|
104 |
for (; first != last; ++first, ++value)
|
williamr@4
|
105 |
*first = value;
|
williamr@2
|
106 |
}
|
williamr@4
|
107 |
template <typename Container, typename T>
|
williamr@4
|
108 |
void iota(Container& c, const T& value)
|
williamr@4
|
109 |
{
|
williamr@4
|
110 |
iota(begin(c), end(c), value);
|
williamr@4
|
111 |
}
|
williamr@4
|
112 |
|
williamr@4
|
113 |
// Also do version with 2nd container?
|
williamr@4
|
114 |
template <typename Container, typename OutIter>
|
williamr@4
|
115 |
OutIter copy(const Container& c, OutIter result) {
|
williamr@4
|
116 |
return std::copy(begin(c), end(c), result);
|
williamr@4
|
117 |
}
|
williamr@2
|
118 |
|
williamr@4
|
119 |
template <typename Container1, typename Container2>
|
williamr@4
|
120 |
bool equal(const Container1& c1, const Container2& c2)
|
williamr@4
|
121 |
{
|
williamr@4
|
122 |
if (size(c1) != size(c2))
|
williamr@4
|
123 |
return false;
|
williamr@4
|
124 |
return std::equal(begin(c1), end(c1), begin(c2));
|
williamr@4
|
125 |
}
|
williamr@2
|
126 |
|
williamr@4
|
127 |
template <typename Container>
|
williamr@4
|
128 |
void sort(Container& c) { std::sort(begin(c), end(c)); }
|
williamr@2
|
129 |
|
williamr@4
|
130 |
template <typename Container, typename Predicate>
|
williamr@4
|
131 |
void sort(Container& c, const Predicate& p) {
|
williamr@4
|
132 |
std::sort(begin(c), end(c), p);
|
williamr@4
|
133 |
}
|
williamr@2
|
134 |
|
williamr@4
|
135 |
template <typename Container>
|
williamr@4
|
136 |
void stable_sort(Container& c) { std::stable_sort(begin(c), end(c)); }
|
williamr@4
|
137 |
|
williamr@4
|
138 |
template <typename Container, typename Predicate>
|
williamr@4
|
139 |
void stable_sort(Container& c, const Predicate& p) {
|
williamr@4
|
140 |
std::stable_sort(begin(c), end(c), p);
|
williamr@4
|
141 |
}
|
williamr@4
|
142 |
|
williamr@4
|
143 |
template <typename InputIterator, typename Predicate>
|
williamr@4
|
144 |
bool any_if(InputIterator first, InputIterator last, Predicate p)
|
williamr@4
|
145 |
{
|
williamr@4
|
146 |
return std::find_if(first, last, p) != last;
|
williamr@4
|
147 |
}
|
williamr@4
|
148 |
template <typename Container, typename Predicate>
|
williamr@4
|
149 |
bool any_if(const Container& c, Predicate p)
|
williamr@4
|
150 |
{
|
williamr@4
|
151 |
return any_if(begin(c), end(c), p);
|
williamr@4
|
152 |
}
|
williamr@4
|
153 |
|
williamr@4
|
154 |
template <typename InputIterator, typename T>
|
williamr@4
|
155 |
bool contains(InputIterator first, InputIterator last, T value)
|
williamr@4
|
156 |
{
|
williamr@4
|
157 |
return std::find(first, last, value) != last;
|
williamr@4
|
158 |
}
|
williamr@4
|
159 |
template <typename Container, typename T>
|
williamr@4
|
160 |
bool contains(const Container& c, const T& value)
|
williamr@4
|
161 |
{
|
williamr@4
|
162 |
return contains(begin(c), end(c), value);
|
williamr@4
|
163 |
}
|
williamr@4
|
164 |
|
williamr@4
|
165 |
template <typename InputIterator, typename Predicate>
|
williamr@4
|
166 |
bool all(InputIterator first, InputIterator last, Predicate p)
|
williamr@4
|
167 |
{
|
williamr@4
|
168 |
for (; first != last; ++first)
|
williamr@4
|
169 |
if (!p(*first))
|
williamr@4
|
170 |
return false;
|
williamr@4
|
171 |
return true;
|
williamr@4
|
172 |
}
|
williamr@4
|
173 |
template <typename Container, typename Predicate>
|
williamr@4
|
174 |
bool all(const Container& c, Predicate p)
|
williamr@4
|
175 |
{
|
williamr@4
|
176 |
return all(begin(c), end(c), p);
|
williamr@4
|
177 |
}
|
williamr@4
|
178 |
|
williamr@4
|
179 |
template <typename Container, typename T>
|
williamr@4
|
180 |
std::size_t count(const Container& c, const T& value)
|
williamr@4
|
181 |
{
|
williamr@4
|
182 |
return std::count(begin(c), end(c), value);
|
williamr@4
|
183 |
}
|
williamr@4
|
184 |
|
williamr@4
|
185 |
template <typename Container, typename Predicate>
|
williamr@4
|
186 |
std::size_t count_if(const Container& c, Predicate p)
|
williamr@4
|
187 |
{
|
williamr@4
|
188 |
return std::count_if(begin(c), end(c), p);
|
williamr@4
|
189 |
}
|
williamr@4
|
190 |
|
williamr@4
|
191 |
template <typename ForwardIterator>
|
williamr@4
|
192 |
bool is_sorted(ForwardIterator first, ForwardIterator last)
|
williamr@4
|
193 |
{
|
williamr@4
|
194 |
if (first == last)
|
williamr@4
|
195 |
return true;
|
williamr@4
|
196 |
|
williamr@4
|
197 |
ForwardIterator next = first;
|
williamr@4
|
198 |
for (++next; next != last; first = next, ++next) {
|
williamr@4
|
199 |
if (*next < *first)
|
williamr@4
|
200 |
return false;
|
williamr@4
|
201 |
}
|
williamr@4
|
202 |
|
williamr@4
|
203 |
return true;
|
williamr@4
|
204 |
}
|
williamr@4
|
205 |
|
williamr@4
|
206 |
template <typename ForwardIterator, typename StrictWeakOrdering>
|
williamr@4
|
207 |
bool is_sorted(ForwardIterator first, ForwardIterator last,
|
williamr@4
|
208 |
StrictWeakOrdering comp)
|
williamr@4
|
209 |
{
|
williamr@4
|
210 |
if (first == last)
|
williamr@4
|
211 |
return true;
|
williamr@4
|
212 |
|
williamr@4
|
213 |
ForwardIterator next = first;
|
williamr@4
|
214 |
for (++next; next != last; first = next, ++next) {
|
williamr@4
|
215 |
if (comp(*next, *first))
|
williamr@4
|
216 |
return false;
|
williamr@4
|
217 |
}
|
williamr@4
|
218 |
|
williamr@4
|
219 |
return true;
|
williamr@4
|
220 |
}
|
williamr@4
|
221 |
|
williamr@4
|
222 |
template <typename Container>
|
williamr@4
|
223 |
bool is_sorted(const Container& c)
|
williamr@4
|
224 |
{
|
williamr@4
|
225 |
return is_sorted(begin(c), end(c));
|
williamr@4
|
226 |
}
|
williamr@4
|
227 |
|
williamr@4
|
228 |
template <typename Container, typename StrictWeakOrdering>
|
williamr@4
|
229 |
bool is_sorted(const Container& c, StrictWeakOrdering comp)
|
williamr@4
|
230 |
{
|
williamr@4
|
231 |
return is_sorted(begin(c), end(c), comp);
|
williamr@4
|
232 |
}
|
williamr@4
|
233 |
|
williamr@2
|
234 |
} // namespace boost
|
williamr@2
|
235 |
|
williamr@4
|
236 |
#endif // BOOST_ALGORITHM_HPP
|