mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-08-21 05:43:42 -07:00
Add manual seed input for rando generation (#2057)
* add manual seed input for rando generation * add tooltip for seed input * switch to calloc * add seed testing count generator * add console command for rando gen * add boost and custom hash_32 functions * use hash_32 funcs for rando generation * limit seed input field to uint32 * rename custom boost header imports to boost_custom
This commit is contained in:
parent
170a9103f9
commit
589e25948e
20 changed files with 540 additions and 47 deletions
|
@ -0,0 +1,47 @@
|
|||
// 32 bit implementation based off of Boost hash
|
||||
|
||||
#ifndef BOOST_HASH_DETAIL_HASH_MIX_32_HPP
|
||||
#define BOOST_HASH_DETAIL_HASH_MIX_32_HPP
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <cstddef>
|
||||
#include <climits>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace hash_detail
|
||||
{
|
||||
|
||||
template<uint32_t Bits> struct hash_mix_impl_32;
|
||||
|
||||
// hash_mix for 32 bit
|
||||
//
|
||||
// We use the "best xmxmx" implementation from
|
||||
// https://github.com/skeeto/hash-prospector/issues/19
|
||||
|
||||
template<> struct hash_mix_impl_32<32>
|
||||
{
|
||||
inline static boost::uint32_t fn( boost::uint32_t x )
|
||||
{
|
||||
boost::uint32_t const m1 = 0x21f0aaad;
|
||||
boost::uint32_t const m2 = 0x735a2d97;
|
||||
|
||||
x ^= x >> 16;
|
||||
x *= m1;
|
||||
x ^= x >> 15;
|
||||
x *= m2;
|
||||
x ^= x >> 15;
|
||||
|
||||
return x;
|
||||
}
|
||||
};
|
||||
|
||||
inline uint32_t hash_mix_32( uint32_t v )
|
||||
{
|
||||
return hash_mix_impl_32<32>::fn( v );
|
||||
}
|
||||
|
||||
} // namespace hash_detail
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_HASH_DETAIL_HASH_MIX_32_HPP
|
117
soh/include/boost_custom/container_hash/detail/hash_range_32.hpp
Normal file
117
soh/include/boost_custom/container_hash/detail/hash_range_32.hpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
// 32 bit implementation based off of Boost hash
|
||||
// Only implementing 32 bit version of char based ranges
|
||||
|
||||
#ifndef BOOST_HASH_DETAIL_HASH_RANGE_32_HPP
|
||||
#define BOOST_HASH_DETAIL_HASH_RANGE_32_HPP
|
||||
|
||||
#include <boost_custom/container_hash/hash_fwd_32.hpp>
|
||||
#include <boost_custom/container_hash/version.hpp>
|
||||
|
||||
#if BOOST_VERSION_HAS_HASH_RANGE
|
||||
#include <boost/container_hash/detail/hash_range.hpp>
|
||||
#else
|
||||
#include <boost/type_traits/integral_constant.hpp>
|
||||
#include <boost/type_traits/enable_if.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <cstddef>
|
||||
#include <climits>
|
||||
#include <iterator>
|
||||
#endif // #if BOOST_VERSION_HAS_HASH_RANGE
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace hash_detail
|
||||
{
|
||||
|
||||
#if !BOOST_VERSION_HAS_HASH_RANGE
|
||||
|
||||
template<class T> struct is_char_type: public boost::false_type {};
|
||||
|
||||
#if CHAR_BIT == 8
|
||||
|
||||
template<> struct is_char_type<char>: public boost::true_type {};
|
||||
template<> struct is_char_type<signed char>: public boost::true_type {};
|
||||
template<> struct is_char_type<unsigned char>: public boost::true_type {};
|
||||
|
||||
#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L
|
||||
template<> struct is_char_type<char8_t>: public boost::true_type {};
|
||||
#endif
|
||||
|
||||
#if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
|
||||
template<> struct is_char_type<std::byte>: public boost::true_type {};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif // #if !BOOST_VERSION_HAS_HASH_RANGE
|
||||
|
||||
template<class It>
|
||||
inline typename boost::enable_if_<
|
||||
is_char_type<typename std::iterator_traits<It>::value_type>::value &&
|
||||
is_same<typename std::iterator_traits<It>::iterator_category, std::random_access_iterator_tag>::value,
|
||||
std::size_t>::type
|
||||
hash_range_32( uint32_t seed, It first, It last )
|
||||
{
|
||||
std::size_t n = static_cast<std::size_t>( last - first );
|
||||
|
||||
for( ; n >= 4; first += 4, n -= 4 )
|
||||
{
|
||||
// clang 5+, gcc 5+ figure out this pattern and use a single mov on x86
|
||||
// gcc on s390x and power BE even knows how to use load-reverse
|
||||
|
||||
boost::uint32_t w =
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[0] ) ) |
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[1] ) ) << 8 |
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[2] ) ) << 16 |
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[3] ) ) << 24;
|
||||
|
||||
hash_combine_32( seed, w );
|
||||
}
|
||||
|
||||
{
|
||||
// add a trailing suffix byte of 0x01 because otherwise sequences of
|
||||
// trailing zeroes are indistinguishable from end of string
|
||||
|
||||
boost::uint32_t w = 0x01u;
|
||||
|
||||
switch( n )
|
||||
{
|
||||
case 1:
|
||||
|
||||
w =
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[0] ) ) |
|
||||
0x0100u;
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
|
||||
w =
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[0] ) ) |
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[1] ) ) << 8 |
|
||||
0x010000u;
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
|
||||
w =
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[0] ) ) |
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[1] ) ) << 8 |
|
||||
static_cast<boost::uint32_t>( static_cast<unsigned char>( first[2] ) ) << 16 |
|
||||
0x01000000u;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
hash_combine_32( seed, w );
|
||||
}
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
||||
} // namespace hash_detail
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_HASH_DETAIL_HASH_RANGE_32_HPP
|
172
soh/include/boost_custom/container_hash/hash_32.hpp
Normal file
172
soh/include/boost_custom/container_hash/hash_32.hpp
Normal file
|
@ -0,0 +1,172 @@
|
|||
// 32 bit implementation based off of Boost hash
|
||||
// Only implementing 32 bit versions integral and string based hashes
|
||||
|
||||
#ifndef BOOST_FUNCTIONAL_HASH_HASH_32_HPP
|
||||
#define BOOST_FUNCTIONAL_HASH_HASH_32_HPP
|
||||
|
||||
#include <boost/container_hash/hash.hpp>
|
||||
#include <boost_custom/container_hash/hash_fwd_32.hpp>
|
||||
#include <boost_custom/container_hash/detail/hash_mix_32.hpp>
|
||||
#include <boost_custom/container_hash/detail/hash_range_32.hpp>
|
||||
#include <boost_custom/container_hash/version.hpp>
|
||||
|
||||
#if !BOOST_VERSION_HAS_HASH_RANGE
|
||||
#include <boost/type_traits/is_unsigned.hpp>
|
||||
#include <boost/type_traits/make_unsigned.hpp>
|
||||
|
||||
#if BOOST_WORKAROUND(__GNUC__, < 3) \
|
||||
&& !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
|
||||
#define BOOST_HASH_CHAR_TRAITS string_char_traits
|
||||
#else
|
||||
#define BOOST_HASH_CHAR_TRAITS char_traits
|
||||
#endif
|
||||
|
||||
#endif // #if !BOOST_VERSION_HAS_HASH_RANGE
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//
|
||||
// boost::hash_value
|
||||
//
|
||||
|
||||
// integral types
|
||||
|
||||
namespace hash_detail
|
||||
{
|
||||
template<class T,
|
||||
bool bigger_than_size_t = (sizeof(T) > sizeof(uint32_t)),
|
||||
bool is_unsigned = boost::is_unsigned<T>::value,
|
||||
std::size_t size_t_bits = sizeof(uint32_t) * CHAR_BIT,
|
||||
std::size_t type_bits = sizeof(T) * CHAR_BIT>
|
||||
struct hash_integral_impl_32;
|
||||
|
||||
template<class T, bool is_unsigned, std::size_t size_t_bits, std::size_t type_bits> struct hash_integral_impl_32<T, false, is_unsigned, size_t_bits, type_bits>
|
||||
{
|
||||
static uint32_t fn( T v )
|
||||
{
|
||||
return static_cast<uint32_t>( v );
|
||||
}
|
||||
};
|
||||
|
||||
template<class T, std::size_t size_t_bits, std::size_t type_bits> struct hash_integral_impl_32<T, true, false, size_t_bits, type_bits>
|
||||
{
|
||||
static uint32_t fn( T v )
|
||||
{
|
||||
typedef typename boost::make_unsigned<T>::type U;
|
||||
|
||||
if( v >= 0 )
|
||||
{
|
||||
return hash_integral_impl_32<U>::fn( static_cast<U>( v ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
return ~hash_integral_impl_32<U>::fn( static_cast<U>( ~static_cast<U>( v ) ) );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<class T> struct hash_integral_impl_32<T, true, true, 32, 64>
|
||||
{
|
||||
static uint32_t fn( T v )
|
||||
{
|
||||
uint32_t seed = 0;
|
||||
|
||||
seed = static_cast<uint32_t>( v >> 32 ) + hash_detail::hash_mix_32( seed );
|
||||
seed = static_cast<uint32_t>( v ) + hash_detail::hash_mix_32( seed );
|
||||
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
template<class T> struct hash_integral_impl_32<T, true, true, 32, 128>
|
||||
{
|
||||
static uint32_t fn( T v )
|
||||
{
|
||||
uint32_t seed = 0;
|
||||
|
||||
seed = static_cast<uint32_t>( v >> 96 ) + hash_detail::hash_mix_32( seed );
|
||||
seed = static_cast<uint32_t>( v >> 64 ) + hash_detail::hash_mix_32( seed );
|
||||
seed = static_cast<uint32_t>( v >> 32 ) + hash_detail::hash_mix_32( seed );
|
||||
seed = static_cast<uint32_t>( v ) + hash_detail::hash_mix_32( seed );
|
||||
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace hash_detail
|
||||
|
||||
template <typename T>
|
||||
typename boost::enable_if_<boost::is_integral<T>::value, uint32_t>::type
|
||||
hash_value_32( T v )
|
||||
{
|
||||
return hash_detail::hash_integral_impl_32<T>::fn( v );
|
||||
}
|
||||
|
||||
// contiguous ranges (string, vector, array)
|
||||
#if BOOST_VERSION_HAS_HASH_RANGE
|
||||
template <typename T>
|
||||
typename boost::enable_if_<container_hash::is_contiguous_range<T>::value, uint32_t>::type
|
||||
hash_value_32( T const& v )
|
||||
{
|
||||
return boost::hash_range_32( v.data(), v.data() + v.size() );
|
||||
}
|
||||
#else
|
||||
template <class Ch, class A>
|
||||
inline uint32_t hash_value_32(
|
||||
std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const& v)
|
||||
{
|
||||
return boost::hash_range_32( v.data(), v.data() + v.size() );
|
||||
}
|
||||
#endif
|
||||
|
||||
//
|
||||
// boost::hash_combine
|
||||
//
|
||||
|
||||
template <class T>
|
||||
inline void hash_combine_32( uint32_t& seed, T const& v )
|
||||
{
|
||||
seed = boost::hash_detail::hash_mix_32( seed + 0x9e3779b9 + boost::hash_32<T>()( v ) );
|
||||
}
|
||||
|
||||
//
|
||||
// boost::hash_range
|
||||
//
|
||||
|
||||
template <class It>
|
||||
inline void hash_range_32( uint32_t& seed, It first, It last )
|
||||
{
|
||||
seed = hash_detail::hash_range_32( seed, first, last );
|
||||
}
|
||||
|
||||
template <class It>
|
||||
inline uint32_t hash_range_32( It first, It last )
|
||||
{
|
||||
uint32_t seed = 0;
|
||||
|
||||
hash_range_32( seed, first, last );
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
||||
//
|
||||
// boost::hash
|
||||
//
|
||||
|
||||
template <class T> struct hash_32
|
||||
{
|
||||
typedef T argument_type;
|
||||
typedef uint32_t result_type;
|
||||
|
||||
uint32_t operator()( T const& val ) const
|
||||
{
|
||||
return hash_value_32( val );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#undef BOOST_HASH_CHAR_TRAITS
|
||||
|
||||
#endif // #ifndef BOOST_FUNCTIONAL_HASH_HASH_32_HPP
|
25
soh/include/boost_custom/container_hash/hash_fwd_32.hpp
Normal file
25
soh/include/boost_custom/container_hash/hash_fwd_32.hpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 32 bit implementation based off of Boost hash
|
||||
|
||||
#ifndef BOOST_FUNCTIONAL_HASH_FWD_32_HPP
|
||||
#define BOOST_FUNCTIONAL_HASH_FWD_32_HPP
|
||||
|
||||
#include <boost/container_hash/hash_fwd.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
namespace container_hash
|
||||
{
|
||||
|
||||
} // namespace container_hash
|
||||
|
||||
template<class T> struct hash_32;
|
||||
|
||||
template<class T> void hash_combine_32( uint32_t& seed, T const& v );
|
||||
|
||||
template<class It> void hash_range_32( uint32_t&, It, It );
|
||||
template<class It> uint32_t hash_range_32( It, It );
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_FUNCTIONAL_HASH_FWD_32_HPP
|
9
soh/include/boost_custom/container_hash/version.hpp
Normal file
9
soh/include/boost_custom/container_hash/version.hpp
Normal file
|
@ -0,0 +1,9 @@
|
|||
|
||||
#ifndef BOOST_CONTAINER_HASH_VERSION_HPP
|
||||
#define BOOST_CONTAINER_HASH_VERSION_HPP
|
||||
|
||||
#include <boost/version.hpp>
|
||||
|
||||
#define BOOST_VERSION_HAS_HASH_RANGE ((BOOST_VERSION / 100 % 1000) >= 81)
|
||||
|
||||
#endif // #ifndef BOOST_CONTAINER_HASH_VERSION_HPP
|
Loading…
Add table
Add a link
Reference in a new issue