2011-09-01 02:48:32 +00:00
|
|
|
#ifndef SPROUT_DETAIL_ALGORITHM_HPP
|
|
|
|
#define SPROUT_DETAIL_ALGORITHM_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <sprout/config.hpp>
|
2011-10-01 15:19:13 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
2011-09-01 02:48:32 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
//
|
|
|
|
// min
|
|
|
|
//
|
|
|
|
template<typename T>
|
|
|
|
SPROUT_CONSTEXPR T const& min(T const& a, T const& b) {
|
|
|
|
return b < a ? b : a;
|
|
|
|
}
|
|
|
|
template<typename T, typename Compare>
|
|
|
|
SPROUT_CONSTEXPR T const& min(T const& a, T const& b, Compare comp) {
|
|
|
|
return comp(b, a) ? b : a;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// max
|
|
|
|
//
|
|
|
|
template<typename T>
|
|
|
|
SPROUT_CONSTEXPR T const& max(T const& a, T const& b) {
|
|
|
|
return a < b ? b : a;
|
|
|
|
}
|
|
|
|
template<typename T, typename Compare>
|
|
|
|
SPROUT_CONSTEXPR T const& max(T const& a, T const& b, Compare comp) {
|
|
|
|
return comp(a, b) ? b : a;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// count
|
|
|
|
//
|
2011-10-27 02:24:47 +00:00
|
|
|
template<typename InputIterator, typename T>
|
|
|
|
SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type count(
|
|
|
|
InputIterator first,
|
|
|
|
InputIterator last,
|
|
|
|
T const& value
|
2011-09-01 02:48:32 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return first == last
|
|
|
|
? 0
|
2011-10-01 15:19:13 +00:00
|
|
|
: (*first == value ? 1 : 0) + sprout::detail::count(sprout::next(first), last, value)
|
2011-09-01 02:48:32 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// count_if
|
|
|
|
//
|
2011-10-27 02:24:47 +00:00
|
|
|
template<typename InputIterator, typename Predicate>
|
|
|
|
SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type count_if(
|
|
|
|
InputIterator first,
|
|
|
|
InputIterator last,
|
2011-09-01 02:48:32 +00:00
|
|
|
Predicate pred
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first == last
|
|
|
|
? 0
|
2011-10-27 02:24:47 +00:00
|
|
|
: (pred(*first) ? 1 : 0) + sprout::detail::count_if(sprout::next(first), last, pred)
|
|
|
|
;
|
2011-09-01 02:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// equal
|
|
|
|
//
|
2011-10-27 02:24:47 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2>
|
2011-09-01 02:48:32 +00:00
|
|
|
SPROUT_CONSTEXPR bool equal(
|
2011-10-27 02:24:47 +00:00
|
|
|
InputIterator1 first1,
|
|
|
|
InputIterator1 last1,
|
|
|
|
InputIterator2 first2
|
2011-09-01 02:48:32 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return first1 == last1
|
|
|
|
? true
|
2011-10-01 15:19:13 +00:00
|
|
|
: *first1 == *first2 && sprout::detail::equal(sprout::next(first1), last1, sprout::next(first2))
|
2011-09-01 02:48:32 +00:00
|
|
|
;
|
|
|
|
}
|
2011-10-27 02:24:47 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2, typename Predicate>
|
2011-09-01 02:48:32 +00:00
|
|
|
SPROUT_CONSTEXPR bool equal(
|
2011-10-27 02:24:47 +00:00
|
|
|
InputIterator1 first1,
|
|
|
|
InputIterator1 last1,
|
|
|
|
InputIterator2 first2,
|
2011-09-01 02:48:32 +00:00
|
|
|
Predicate pred
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first1 == last1
|
|
|
|
? true
|
2011-10-27 02:24:47 +00:00
|
|
|
: pred(*first1, *first2) && sprout::detail::equal(sprout::next(first1), last1, sprout::next(first2), pred)
|
|
|
|
;
|
2011-09-01 02:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// lexicographical_compare
|
|
|
|
//
|
2011-10-27 02:24:47 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2>
|
2011-09-01 02:48:32 +00:00
|
|
|
SPROUT_CONSTEXPR bool lexicographical_compare(
|
2011-10-27 02:24:47 +00:00
|
|
|
InputIterator1 first1,
|
|
|
|
InputIterator1 last1,
|
|
|
|
InputIterator2 first2,
|
|
|
|
InputIterator2 last2
|
2011-09-01 02:48:32 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return first2 == last2
|
|
|
|
? false
|
|
|
|
: first1 == last1 || *first1 < *first2
|
|
|
|
? true
|
|
|
|
: *first2 < *first1
|
|
|
|
? false
|
2011-10-27 02:24:47 +00:00
|
|
|
: sprout::detail::lexicographical_compare(sprout::next(first1), last1, sprout::next(first2), last2)
|
|
|
|
;
|
2011-09-01 02:48:32 +00:00
|
|
|
}
|
2011-10-27 02:24:47 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
2011-09-01 02:48:32 +00:00
|
|
|
SPROUT_CONSTEXPR bool lexicographical_compare(
|
2011-10-27 02:24:47 +00:00
|
|
|
InputIterator1 first1,
|
|
|
|
InputIterator1 last1,
|
|
|
|
InputIterator2 first2,
|
|
|
|
InputIterator2 last2,
|
2011-09-01 02:48:32 +00:00
|
|
|
Compare comp
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first2 == last2
|
|
|
|
? false
|
|
|
|
: first1 == last1 || comp(*first1, *first2)
|
|
|
|
? true
|
|
|
|
: comp(*first2, *first1)
|
|
|
|
? false
|
2011-10-27 02:24:47 +00:00
|
|
|
: sprout::detail::lexicographical_compare(sprout::next(first1), last1, sprout::next(first2), last2, comp)
|
|
|
|
;
|
2011-09-01 02:48:32 +00:00
|
|
|
}
|
2011-10-25 09:16:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// find
|
|
|
|
//
|
2011-10-27 02:24:47 +00:00
|
|
|
template<typename InputIterator, typename T>
|
|
|
|
SPROUT_CONSTEXPR InputIterator find(InputIterator first, InputIterator last, T const& value) {
|
2011-10-25 09:16:27 +00:00
|
|
|
return first == last || *first == value
|
|
|
|
? first
|
|
|
|
: sprout::detail::find(sprout::next(first), last, value)
|
|
|
|
;
|
|
|
|
}
|
2011-09-01 02:48:32 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_HPP
|