sl@0: // (C) Copyright Jeremy Siek 2001. sl@0: // Distributed under the Boost Software License, Version 1.0. (See accompany- sl@0: // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: /* sl@0: * sl@0: * Copyright (c) 1994 sl@0: * Hewlett-Packard Company sl@0: * sl@0: * Permission to use, copy, modify, distribute and sell this software sl@0: * and its documentation for any purpose is hereby granted without fee, sl@0: * provided that the above copyright notice appear in all copies and sl@0: * that both that copyright notice and this permission notice appear sl@0: * in supporting documentation. Hewlett-Packard Company makes no sl@0: * representations about the suitability of this software for any sl@0: * purpose. It is provided "as is" without express or implied warranty. sl@0: * sl@0: * sl@0: * Copyright (c) 1996 sl@0: * Silicon Graphics Computer Systems, Inc. sl@0: * sl@0: * Permission to use, copy, modify, distribute and sell this software sl@0: * and its documentation for any purpose is hereby granted without fee, sl@0: * provided that the above copyright notice appear in all copies and sl@0: * that both that copyright notice and this permission notice appear sl@0: * in supporting documentation. Silicon Graphics makes no sl@0: * representations about the suitability of this software for any sl@0: * purpose. It is provided "as is" without express or implied warranty. sl@0: */ sl@0: sl@0: #ifndef BOOST_ALGORITHM_HPP sl@0: # define BOOST_ALGORITHM_HPP sl@0: # include sl@0: // Algorithms on sequences sl@0: // sl@0: // The functions in this file have not yet gone through formal sl@0: // review, and are subject to change. This is a work in progress. sl@0: // They have been checked into the detail directory because sl@0: // there are some graph algorithms that use these functions. sl@0: sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: template sl@0: Iter1 begin(const std::pair& p) { return p.first; } sl@0: sl@0: template sl@0: Iter2 end(const std::pair& p) { return p.second; } sl@0: sl@0: template sl@0: typename boost::detail::iterator_traits::difference_type sl@0: size(const std::pair& p) { sl@0: return std::distance(p.first, p.second); sl@0: } sl@0: sl@0: #if 0 sl@0: // These seem to interfere with the std::pair overloads :( sl@0: template sl@0: typename Container::iterator sl@0: begin(Container& c) { return c.begin(); } sl@0: sl@0: template sl@0: typename Container::const_iterator sl@0: begin(const Container& c) { return c.begin(); } sl@0: sl@0: template sl@0: typename Container::iterator sl@0: end(Container& c) { return c.end(); } sl@0: sl@0: template sl@0: typename Container::const_iterator sl@0: end(const Container& c) { return c.end(); } sl@0: sl@0: template sl@0: typename Container::size_type sl@0: size(const Container& c) { return c.size(); } sl@0: #else sl@0: template sl@0: typename std::vector::iterator sl@0: begin(std::vector& c) { return c.begin(); } sl@0: sl@0: template sl@0: typename std::vector::const_iterator sl@0: begin(const std::vector& c) { return c.begin(); } sl@0: sl@0: template sl@0: typename std::vector::iterator sl@0: end(std::vector& c) { return c.end(); } sl@0: sl@0: template sl@0: typename std::vector::const_iterator sl@0: end(const std::vector& c) { return c.end(); } sl@0: sl@0: template sl@0: typename std::vector::size_type sl@0: size(const std::vector& c) { return c.size(); } sl@0: #endif sl@0: sl@0: template sl@0: void iota(ForwardIterator first, ForwardIterator last, T value) sl@0: { sl@0: for (; first != last; ++first, ++value) sl@0: *first = value; sl@0: } sl@0: template sl@0: void iota(Container& c, const T& value) sl@0: { sl@0: iota(begin(c), end(c), value); sl@0: } sl@0: sl@0: // Also do version with 2nd container? sl@0: template sl@0: OutIter copy(const Container& c, OutIter result) { sl@0: return std::copy(begin(c), end(c), result); sl@0: } sl@0: sl@0: template sl@0: bool equal(const Container1& c1, const Container2& c2) sl@0: { sl@0: if (size(c1) != size(c2)) sl@0: return false; sl@0: return std::equal(begin(c1), end(c1), begin(c2)); sl@0: } sl@0: sl@0: template sl@0: void sort(Container& c) { std::sort(begin(c), end(c)); } sl@0: sl@0: template sl@0: void sort(Container& c, const Predicate& p) { sl@0: std::sort(begin(c), end(c), p); sl@0: } sl@0: sl@0: template sl@0: void stable_sort(Container& c) { std::stable_sort(begin(c), end(c)); } sl@0: sl@0: template sl@0: void stable_sort(Container& c, const Predicate& p) { sl@0: std::stable_sort(begin(c), end(c), p); sl@0: } sl@0: sl@0: template sl@0: bool any_if(InputIterator first, InputIterator last, Predicate p) sl@0: { sl@0: return std::find_if(first, last, p) != last; sl@0: } sl@0: template sl@0: bool any_if(const Container& c, Predicate p) sl@0: { sl@0: return any_if(begin(c), end(c), p); sl@0: } sl@0: sl@0: template sl@0: bool contains(InputIterator first, InputIterator last, T value) sl@0: { sl@0: return std::find(first, last, value) != last; sl@0: } sl@0: template sl@0: bool contains(const Container& c, const T& value) sl@0: { sl@0: return contains(begin(c), end(c), value); sl@0: } sl@0: sl@0: template sl@0: bool all(InputIterator first, InputIterator last, Predicate p) sl@0: { sl@0: for (; first != last; ++first) sl@0: if (!p(*first)) sl@0: return false; sl@0: return true; sl@0: } sl@0: template sl@0: bool all(const Container& c, Predicate p) sl@0: { sl@0: return all(begin(c), end(c), p); sl@0: } sl@0: sl@0: template sl@0: std::size_t count(const Container& c, const T& value) sl@0: { sl@0: return std::count(begin(c), end(c), value); sl@0: } sl@0: sl@0: template sl@0: std::size_t count_if(const Container& c, Predicate p) sl@0: { sl@0: return std::count_if(begin(c), end(c), p); sl@0: } sl@0: sl@0: template sl@0: bool is_sorted(ForwardIterator first, ForwardIterator last) sl@0: { sl@0: if (first == last) sl@0: return true; sl@0: sl@0: ForwardIterator next = first; sl@0: for (++next; next != last; first = next, ++next) { sl@0: if (*next < *first) sl@0: return false; sl@0: } sl@0: sl@0: return true; sl@0: } sl@0: sl@0: template sl@0: bool is_sorted(ForwardIterator first, ForwardIterator last, sl@0: StrictWeakOrdering comp) sl@0: { sl@0: if (first == last) sl@0: return true; sl@0: sl@0: ForwardIterator next = first; sl@0: for (++next; next != last; first = next, ++next) { sl@0: if (comp(*next, *first)) sl@0: return false; sl@0: } sl@0: sl@0: return true; sl@0: } sl@0: sl@0: template sl@0: bool is_sorted(const Container& c) sl@0: { sl@0: return is_sorted(begin(c), end(c)); sl@0: } sl@0: sl@0: template sl@0: bool is_sorted(const Container& c, StrictWeakOrdering comp) sl@0: { sl@0: return is_sorted(begin(c), end(c), comp); sl@0: } sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_ALGORITHM_HPP