diff --git a/include/boost/algorithm/wrappers.hpp b/include/boost/algorithm/wrappers.hpp new file mode 100644 index 0000000..ba2ff57 --- /dev/null +++ b/include/boost/algorithm/wrappers.hpp @@ -0,0 +1,52 @@ +/* + Copyright (c) Marshall Clow 2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + Alternate interfaces (aka "wrappers") for algorithms. +*/ + +#ifndef BOOST_ALGORITHM_WRAPPERS_HPP +#define BOOST_ALGORITHM_WRAPPERS_HPP + +namespace boost { namespace algorithm { + +/// \fn find_ptr ( Container &c, Key k ) +/// \return a pointer to the value matching the key in the container, +/// or NULL if the key does not exist in the container. +/// +/// \note: This is a wrapper around Container::find, with a useful interface. +/// Suggested by Olaf van der Spek +/// +/// \param c The container to be searched +/// \param k The key value to search with +template +typename Container::value_type::second_type* +find_ptr ( Container &c, Key k ) +{ + typename Container::iterator iter = c.find ( k ); + return iter == c.end() ? NULL : &iter->second; +} + +/// \fn find_ptr ( const Container &c, Key k ) +/// \return a pointer to the value matching the key in the container, +/// or NULL if the key does not exist in the container. +/// +/// \note: This is a wrapper around Container::find, with a useful interface. +/// Suggested by Olaf van der Spek +/// +/// \param c The container to be searched +/// \param k The key value to search with +template +const typename Container::value_type::second_type* +find_ptr ( const Container &c, Key k ) +{ + typename Container::const_iterator iter = c.find ( k ); + return iter == c.end() ? NULL : &iter->second; +} + + +}} + +#endif diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index b9cfa2a..f8a346a 100755 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -46,6 +46,9 @@ import testing ; [ run hex_test3.cpp : : : : hex_test3 ] [ run hex_test4.cpp : : : : hex_test4 ] [ compile-fail hex_fail1.cpp ] + +# Wrapper tests + [ run wrapper_test1.cpp : : : : wrapper_test1 ] ; } diff --git a/test/wrapper_test1.cpp b/test/wrapper_test1.cpp new file mode 100644 index 0000000..eb17ea7 --- /dev/null +++ b/test/wrapper_test1.cpp @@ -0,0 +1,75 @@ +/* + Copyright (c) Marshall Clow 2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include +#include +#include + +namespace ba = boost::algorithm; + +void test_int () +{ + std::map m; + std::multimap mm; + + int *ptr; + +// try with an empty map + BOOST_CHECK ( ba::find_ptr ( m , 3 ) == NULL ); + BOOST_CHECK ( ba::find_ptr ( mm, 3 ) == NULL ); + + m.insert ( std::make_pair ( 5, 5 )); + mm.insert ( std::make_pair ( 9, 9 )); + BOOST_CHECK ( ba::find_ptr ( m , 3 ) == NULL ); + BOOST_CHECK ( ba::find_ptr ( mm, 3 ) == NULL ); + + ptr = ba::find_ptr ( m, 5 ); + BOOST_CHECK ( ptr != NULL && *ptr == 5 ); + BOOST_CHECK ( ba::find_ptr ( m , 9 ) == NULL ); + + ptr = ba::find_ptr ( mm, 9 ); + BOOST_CHECK ( ptr != NULL && *ptr == 9 ); + BOOST_CHECK ( ba::find_ptr ( mm, 5 ) == NULL ); + +} + +void test_str () +{ + std::map m; + std::multimap mm; + std::string *ptr; + +// try with an empty map + BOOST_CHECK ( ba::find_ptr ( m , 31 ) == NULL ); + BOOST_CHECK ( ba::find_ptr ( mm, 31 ) == NULL ); + + m.insert ( std::make_pair ( 55, "fifty-five" )); + mm.insert ( std::make_pair ( 66, "sixty-six" )); + BOOST_CHECK ( ba::find_ptr ( m , 3 ) == NULL ); + BOOST_CHECK ( ba::find_ptr ( mm, 3 ) == NULL ); + + ptr = ba::find_ptr ( m, 55 ); + BOOST_CHECK ( ptr != NULL && *ptr == "fifty-five" ); + BOOST_CHECK ( ba::find_ptr ( m , 66 ) == NULL ); + + ptr = ba::find_ptr ( mm, 66 ); + BOOST_CHECK ( ptr != NULL && *ptr == "sixty-six" ); + BOOST_CHECK ( ba::find_ptr ( mm, 55 ) == NULL ); +} + +int test_main( int , char* [] ) +{ + test_int (); + test_str (); + return 0; +}