mirror of
https://github.com/boostorg/functional.git
synced 2025-08-02 22:14:28 +02:00
Add support for complex numbers to Boost.Hash
[SVN r39983]
This commit is contained in:
@@ -708,6 +708,14 @@ for(; first != last; ++first)
|
|||||||
<parameter name="val"><paramtype>std::multimap<K, T, C, A> const&</paramtype></parameter>
|
<parameter name="val"><paramtype>std::multimap<K, T, C, A> const&</paramtype></parameter>
|
||||||
</signature>
|
</signature>
|
||||||
|
|
||||||
|
<signature>
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
</template>
|
||||||
|
<type>std::size_t</type>
|
||||||
|
<parameter name="val"><paramtype>std::complex<T> const&</paramtype></parameter>
|
||||||
|
</signature>
|
||||||
|
|
||||||
<description><para>
|
<description><para>
|
||||||
Generally shouldn't be called directly by users, instead they should use
|
Generally shouldn't be called directly by users, instead they should use
|
||||||
<classname>boost::hash</classname>, <functionname>boost::hash_range</functionname>
|
<classname>boost::hash</classname>, <functionname>boost::hash_range</functionname>
|
||||||
@@ -784,6 +792,12 @@ for(; first != last; ++first)
|
|||||||
<functionname>hash_combine</functionname>(seed, val.second);
|
<functionname>hash_combine</functionname>(seed, val.second);
|
||||||
return seed;</programlisting></entry>
|
return seed;</programlisting></entry>
|
||||||
</row>
|
</row>
|
||||||
|
<row>
|
||||||
|
<entry>
|
||||||
|
<code>std::complex<T></code>
|
||||||
|
</entry>
|
||||||
|
<entry>When <code>T</code> is a built in type and <code>val.imag() == 0</code>, the result is equal to <code>hash_value(val.real())</code>. Otherwise an unspecified value, except that equal arguments shall yield the same result.</entry>
|
||||||
|
</row>
|
||||||
</tbody>
|
</tbody>
|
||||||
</tgroup>
|
</tgroup>
|
||||||
</informaltable>
|
</informaltable>
|
||||||
|
@@ -33,6 +33,7 @@ test-suite functional/hash
|
|||||||
[ run hash_deque_test.cpp ]
|
[ run hash_deque_test.cpp ]
|
||||||
[ run hash_set_test.cpp ]
|
[ run hash_set_test.cpp ]
|
||||||
[ run hash_map_test.cpp ]
|
[ run hash_map_test.cpp ]
|
||||||
|
[ run hash_complex_test.cpp ]
|
||||||
[ run link_test.cpp link_test_2.cpp ]
|
[ run link_test.cpp link_test_2.cpp ]
|
||||||
[ run link_ext_test.cpp link_no_ext_test.cpp ]
|
[ run link_ext_test.cpp link_no_ext_test.cpp ]
|
||||||
[ run container_fwd_test.cpp ]
|
[ run container_fwd_test.cpp ]
|
||||||
|
108
hash/test/hash_complex_test.cpp
Normal file
108
hash/test/hash_complex_test.cpp
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
|
||||||
|
// Copyright 2005-2007 Daniel James.
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
#include "./config.hpp"
|
||||||
|
|
||||||
|
#ifdef TEST_EXTENSIONS
|
||||||
|
# ifdef TEST_STD_INCLUDES
|
||||||
|
# include <functional>
|
||||||
|
# else
|
||||||
|
# include <boost/functional/hash.hpp>
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/detail/lightweight_test.hpp>
|
||||||
|
|
||||||
|
#ifdef TEST_EXTENSIONS
|
||||||
|
|
||||||
|
#include <complex>
|
||||||
|
#include <sstream>
|
||||||
|
#include <boost/limits.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_MSVC)
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable:4244) // conversion from 'unsigned long' to 'unsigned short', possible loss of data
|
||||||
|
#pragma warning(disable:4512) // assignment operator could not be generated
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/random/mersenne_twister.hpp>
|
||||||
|
#include <boost/random/uniform_int.hpp>
|
||||||
|
#include <boost/random/uniform_real.hpp>
|
||||||
|
#include <boost/random/variate_generator.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_MSVC)
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void generic_complex_tests(std::complex<T> v)
|
||||||
|
{
|
||||||
|
HASH_NAMESPACE::hash<std::complex<T> > complex_hasher;
|
||||||
|
|
||||||
|
BOOST_TEST(complex_hasher(v) == complex_hasher(v));
|
||||||
|
|
||||||
|
HASH_NAMESPACE::hash<T> real_hasher;
|
||||||
|
T real = v.real();
|
||||||
|
T imag = v.imag();
|
||||||
|
|
||||||
|
BOOST_TEST(real_hasher(real) == complex_hasher(std::complex<T>(real)));
|
||||||
|
|
||||||
|
if(imag != 0 && real_hasher(real) == complex_hasher(v)) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os<<"real_hasher("<<real<<") == complex_hasher("
|
||||||
|
<<v.real()<<" + "<<v.imag()<<"i) == "
|
||||||
|
<<real_hasher(real)<<" (This might not be a bug).";
|
||||||
|
BOOST_ERROR(os.str().c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Float>
|
||||||
|
void complex_float_tests(Float*)
|
||||||
|
{
|
||||||
|
boost::mt19937 rng;
|
||||||
|
boost::uniform_real<Float> uniform;
|
||||||
|
boost::variate_generator<boost::mt19937&, boost::uniform_real<Float> >
|
||||||
|
uniform_generator(rng, uniform);
|
||||||
|
|
||||||
|
for(int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
std::complex<Float> v(uniform_generator(), uniform_generator());
|
||||||
|
generic_complex_tests(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Integer>
|
||||||
|
void complex_integral_tests(Integer*)
|
||||||
|
{
|
||||||
|
boost::mt19937 rng;
|
||||||
|
boost::uniform_int<Integer> uniform(
|
||||||
|
(std::numeric_limits<Integer>::min)(),
|
||||||
|
(std::numeric_limits<Integer>::max)());
|
||||||
|
boost::variate_generator<boost::mt19937&, boost::uniform_int<Integer> >
|
||||||
|
uniform_generator(rng, uniform);
|
||||||
|
|
||||||
|
for(int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
std::complex<Integer>v(uniform_generator(), uniform_generator());
|
||||||
|
generic_complex_tests(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
complex_float_tests((float*) 0);
|
||||||
|
complex_float_tests((double*) 0);
|
||||||
|
complex_float_tests((long double*) 0);
|
||||||
|
complex_integral_tests((short*) 0);
|
||||||
|
complex_integral_tests((int*) 0);
|
||||||
|
complex_integral_tests((long*) 0);
|
||||||
|
complex_integral_tests((unsigned short*) 0);
|
||||||
|
complex_integral_tests((unsigned int*) 0);
|
||||||
|
complex_integral_tests((unsigned long*) 0);
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@@ -88,6 +88,9 @@ namespace boost
|
|||||||
template <class K, class T, class C, class A>
|
template <class K, class T, class C, class A>
|
||||||
std::size_t hash_value(std::multimap<K, T, C, A> const& v);
|
std::size_t hash_value(std::multimap<K, T, C, A> const& v);
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
std::size_t hash_value(std::complex<T> const&);
|
||||||
|
|
||||||
// Implementation
|
// Implementation
|
||||||
|
|
||||||
namespace hash_detail
|
namespace hash_detail
|
||||||
@@ -138,6 +141,7 @@ namespace boost
|
|||||||
{
|
{
|
||||||
return static_cast<std::size_t>(v);
|
return static_cast<std::size_t>(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::size_t hash_value(char v)
|
inline std::size_t hash_value(char v)
|
||||||
{
|
{
|
||||||
return static_cast<std::size_t>(v);
|
return static_cast<std::size_t>(v);
|
||||||
|
Reference in New Issue
Block a user