Documentation for Algorithm
Former-commit-id: a2090cb6f5e7373f4d9adc3c1d5067619b1bd185
This commit is contained in:
parent
0934c21967
commit
d3621c82eb
|
|
@ -27,11 +27,11 @@ namespace Nz
|
|||
template<typename T> std::size_t CountOf(const T& c);
|
||||
template<typename T> void HashCombine(std::size_t& seed, const T& v);
|
||||
|
||||
template<typename T>
|
||||
struct PointedType
|
||||
{
|
||||
using type = void; //< FIXME: I can't make SFINAE work
|
||||
};
|
||||
template<typename T>
|
||||
struct PointedType
|
||||
{
|
||||
using type = void; //< FIXME: I can't make SFINAE work
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct TypeTag {};
|
||||
|
|
|
|||
|
|
@ -30,6 +30,16 @@ namespace Nz
|
|||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Applies the tuple to the function
|
||||
* \return The result of the function
|
||||
*
|
||||
* \param fn Function
|
||||
* \param t Tuple of arguments for the function
|
||||
*
|
||||
* \see Apply
|
||||
*/
|
||||
|
||||
template<typename F, typename Tuple>
|
||||
auto Apply(F&& fn, Tuple&& t)
|
||||
{
|
||||
|
|
@ -38,6 +48,17 @@ namespace Nz
|
|||
return Detail::ApplyImplFunc(std::forward<F>(fn), std::forward<Tuple>(t), std::make_index_sequence<tSize>());
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Applies the tuple to the member function on an object
|
||||
* \return The result of the member function called
|
||||
*
|
||||
* \param object Object of a class
|
||||
* \param fn Member function
|
||||
* \param t Tuple of arguments for the member function
|
||||
*
|
||||
* \see Apply
|
||||
*/
|
||||
|
||||
template<typename O, typename F, typename Tuple>
|
||||
auto Apply(O& object, F&& fn, Tuple&& t)
|
||||
{
|
||||
|
|
@ -46,15 +67,39 @@ namespace Nz
|
|||
return Detail::ApplyImplMethod(object, std::forward<F>(fn), std::forward<Tuple>(t), std::make_index_sequence<tSize>());
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Computes the hash of a hashable object
|
||||
* \return A bytearray which represents the hash
|
||||
*
|
||||
* \param hash Enumeration of type HashType
|
||||
* \param v Object to hash, must be convertible to "Nz::String"
|
||||
*
|
||||
* \see ComputeHash
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
ByteArray ComputeHash(HashType hash, const T& v)
|
||||
{
|
||||
return ComputeHash(AbstractHash::Get(hash).get(), v);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Computes the hash of a hashable object
|
||||
* \return A bytearray which represents the hash
|
||||
*
|
||||
* \param hash Pointer to abstract hash
|
||||
* \param v Object to hash, must be convertible to "Nz::String"
|
||||
*
|
||||
* \remark Produce a NazaraAssert if pointer to Abstracthash is invalid
|
||||
*
|
||||
* \see ComputeHash
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
ByteArray ComputeHash(AbstractHash* hash, const T& v)
|
||||
{
|
||||
NazaraAssert(hash != nullptr, "Invalid abstracthash pointer");
|
||||
|
||||
hash->Begin();
|
||||
|
||||
HashAppend(hash, v);
|
||||
|
|
@ -62,19 +107,44 @@ namespace Nz
|
|||
return hash->End();
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
/*!
|
||||
* \brief Returns the number of elements in a C-array
|
||||
* \return The number of elements
|
||||
*
|
||||
* \param name C-array
|
||||
*
|
||||
* \see CountOf
|
||||
*/
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr std::size_t CountOf(T(&name)[N]) noexcept
|
||||
{
|
||||
return N;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
/*!
|
||||
* \brief Returns the number of elements in a container
|
||||
* \return The number of elements
|
||||
*
|
||||
* \param c Container with the member function "size()"
|
||||
*
|
||||
* \see CountOf
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
std::size_t CountOf(const T& c)
|
||||
{
|
||||
return c.size();
|
||||
}
|
||||
|
||||
// Algorithme venant de CityHash par Google
|
||||
/*!
|
||||
* \brief Combines two hash in one
|
||||
*
|
||||
* \param seed First value that will be modified (expected to be 64bits)
|
||||
* \param v Second value to hash
|
||||
*/
|
||||
|
||||
// Algorithm from CityHash by Google
|
||||
// http://stackoverflow.com/questions/8513911/how-to-create-a-good-hash-combine-with-64-bit-output-inspired-by-boosthash-co
|
||||
template<typename T>
|
||||
void HashCombine(std::size_t& seed, const T& v)
|
||||
|
|
@ -91,10 +161,20 @@ namespace Nz
|
|||
seed = static_cast<std::size_t>(b * kMul);
|
||||
}
|
||||
|
||||
template<typename T> struct PointedType<T*> {typedef T type;};
|
||||
template<typename T> struct PointedType<T* const> {typedef T type;};
|
||||
template<typename T> struct PointedType<T* volatile> {typedef T type;};
|
||||
template<typename T> struct PointedType<T* const volatile> {typedef T type;};
|
||||
template<typename T> struct PointedType<T*> {typedef T type;};
|
||||
template<typename T> struct PointedType<T* const> {typedef T type;};
|
||||
template<typename T> struct PointedType<T* volatile> {typedef T type;};
|
||||
template<typename T> struct PointedType<T* const volatile> {typedef T type;};
|
||||
|
||||
/*!
|
||||
* \brief Serializes a boolean
|
||||
* \return true if serialization succedeed
|
||||
*
|
||||
* \param context Context for the serialization
|
||||
* \param value Boolean to serialize
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
|
||||
inline bool Serialize(SerializationContext& context, bool value)
|
||||
{
|
||||
|
|
@ -113,6 +193,16 @@ namespace Nz
|
|||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Serializes an arithmetic type
|
||||
* \return true if serialization succedeed
|
||||
*
|
||||
* \param context Context for the serialization
|
||||
* \param value Arithmetic type to serialize
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(SerializationContext& context, T value)
|
||||
{
|
||||
|
|
@ -131,6 +221,16 @@ namespace Nz
|
|||
return context.stream->Write(&value, sizeof(T)) == sizeof(T);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unserializes a boolean
|
||||
* \return true if unserialization succedeed
|
||||
*
|
||||
* \param context Context for the unserialization
|
||||
* \param value Pointer to boolean to unserialize
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
|
||||
inline bool Unserialize(SerializationContext& context, bool* value)
|
||||
{
|
||||
if (context.currentBitPos == 8)
|
||||
|
|
@ -149,6 +249,18 @@ namespace Nz
|
|||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unserializes an arithmetic type
|
||||
* \return true if unserialization succedeed
|
||||
*
|
||||
* \param context Context for the unserialization
|
||||
* \param value Pointer to arithmetic type to serialize
|
||||
*
|
||||
* \remark Produce a NazaraAssert if pointer to value is invalid
|
||||
*
|
||||
* \see Serialize, Unserialize
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(SerializationContext& context, T* value)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
#include <Nazara/Math/Vector2.hpp>
|
||||
|
||||
#include <array>
|
||||
|
||||
TEST_CASE("Apply", "[CORE][ALGORITHM]")
|
||||
{
|
||||
SECTION("Apply lambda to two vector2")
|
||||
{
|
||||
Nz::Vector2<int> vector = Nz::Vector2<int>::Unit();
|
||||
auto lambda = [](const Nz::Vector2<int>& vec1, const Nz::Vector2<int>& vec2)
|
||||
{
|
||||
return vec1 + vec2;
|
||||
};
|
||||
|
||||
Nz::Vector2<int> result = Nz::Apply(lambda, std::make_tuple(vector, vector));
|
||||
|
||||
REQUIRE(result == (Nz::Vector2<int>::Unit() * 2));
|
||||
}
|
||||
|
||||
SECTION("Apply member function to vector2")
|
||||
{
|
||||
Nz::Vector2<int> vector = Nz::Vector2<int>::Unit();
|
||||
|
||||
int result = Nz::Apply(vector, &Nz::Vector2<int>::Distance, std::make_tuple(vector));
|
||||
|
||||
REQUIRE(result == 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("ComputeHash", "[CORE][ALGORITHM]")
|
||||
{
|
||||
SECTION("Compute hash of '0'")
|
||||
{
|
||||
auto result = Nz::ComputeHash(Nz::HashType_SHA512, "1234");
|
||||
REQUIRE(result.ToHex().ToUpper() == "D404559F602EAB6FD602AC7680DACBFAADD13630335E951F097AF3900E9DE176B6DB28512F2E000B9D04FBA5133E8B1C6E8DF59DB3A8AB9D60BE4B97CC9E81DB");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue