-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove boost::hash and implement our own hash
- Loading branch information
Showing
25 changed files
with
345 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <concepts> | ||
#include <functional> | ||
#include <ranges> | ||
|
||
namespace fintamath { | ||
|
||
namespace stdr = std::ranges; | ||
namespace stdv = std::views; | ||
|
||
template <typename From, typename To> | ||
concept ConvertibleToAndNotSameAs = std::convertible_to<From, To> && !std::same_as<From, To>; | ||
|
||
template <class T> | ||
concept TupleLike = requires { | ||
std::tuple_size<T>::value; | ||
}; | ||
|
||
template <class T> | ||
concept Hashable = requires(const T &v) { | ||
std::hash<T>{}(v); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <tuple> | ||
|
||
#include "fintamath/core/CoreUtils.hpp" | ||
|
||
namespace fintamath::detail { | ||
|
||
template <class T> | ||
struct Hash; | ||
|
||
template <class T> | ||
void hashCombine(size_t &seed, const T &v) noexcept { | ||
Hash<T> hasher; | ||
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); | ||
} | ||
|
||
template <Hashable T> | ||
struct Hash<T> { | ||
size_t operator()(const T &v) const noexcept { | ||
return std::hash<T>{}(v); | ||
} | ||
}; | ||
|
||
template <TupleLike T> | ||
struct Hash<T> { | ||
size_t operator()(const T &v) const noexcept { | ||
std::size_t seed = 0; | ||
hashCombineTuple<0>(seed, v); | ||
return seed; | ||
} | ||
|
||
private: | ||
template <std::size_t i> | ||
static void hashCombineTuple(std::size_t &seed, const T &v) noexcept { | ||
if constexpr (i < std::tuple_size_v<T>) { | ||
Hash<std::tuple_element_t<i, T>> hasher; | ||
hashCombine(seed, hasher(std::get<i>(v))); | ||
hashCombineTuple<i + 1>(seed, v); | ||
} | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,5 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include "fintamath/core/MathObjectUtils.hpp" | ||
#include "fintamath/core/CoreUtils.hpp" | ||
|
||
#include "fintamath/literals/constants/E.hpp" | ||
#include "fintamath/numbers/Integer.hpp" | ||
#include "fintamath/numbers/Rational.hpp" | ||
|
||
using namespace fintamath; | ||
|
||
E c; | ||
Integer i; | ||
|
||
TEST(MathObjectUtilsTests, isTest) { | ||
EXPECT_TRUE(is<IArithmetic>(i)); | ||
EXPECT_FALSE(is<IArithmetic>(c)); | ||
|
||
EXPECT_TRUE(is<IArithmetic>(i.clone().get())); | ||
EXPECT_FALSE(is<IArithmetic>(E().clone().get())); | ||
EXPECT_FALSE(is<IArithmetic>(std::unique_ptr<IArithmetic>().get())); | ||
|
||
EXPECT_TRUE(is<IArithmetic>(i.clone())); | ||
EXPECT_FALSE(is<IArithmetic>(E().clone())); | ||
|
||
EXPECT_TRUE(is<IArithmetic>(std::shared_ptr(i.clone()))); | ||
EXPECT_FALSE(is<IArithmetic>(std::shared_ptr(E().clone()))); | ||
|
||
EXPECT_TRUE(is<IArithmetic>(std::const_pointer_cast<const IMathObject>(std::shared_ptr(i.clone())))); | ||
EXPECT_FALSE(is<IArithmetic>(std::const_pointer_cast<const IMathObject>(std::shared_ptr(E().clone())))); | ||
|
||
EXPECT_TRUE(is<IArithmetic>(std::reference_wrapper<IMathObject>(i))); | ||
EXPECT_FALSE(is<IArithmetic>(std::reference_wrapper<IMathObject>(c))); | ||
|
||
EXPECT_TRUE(is<IArithmetic>(std::reference_wrapper<const IMathObject>(i))); | ||
EXPECT_FALSE(is<IArithmetic>(std::reference_wrapper<const IMathObject>(c))); | ||
} | ||
|
||
TEST(MathObjectUtilsTests, castTest) { | ||
EXPECT_NO_THROW(cast<IArithmetic>(i)); | ||
EXPECT_THROW(cast<IArithmetic>(cast<IMathObject>(c)), std::bad_cast); | ||
|
||
EXPECT_NO_THROW(cast<IArithmetic>(Integer(1))); | ||
EXPECT_THROW(cast<IArithmetic>(cast<IMathObject>(E())), std::bad_cast); | ||
|
||
EXPECT_TRUE(cast<IArithmetic>(i.clone().get())); | ||
EXPECT_FALSE(cast<IArithmetic>(E().clone().get())); | ||
|
||
EXPECT_TRUE(cast<IArithmetic>(const_cast<const IMathObject *>(i.clone().get()))); | ||
EXPECT_FALSE(cast<IArithmetic>(const_cast<const IMathObject *>(E().clone().get()))); | ||
|
||
EXPECT_TRUE(cast<IArithmetic>(i.clone())); | ||
EXPECT_FALSE(cast<IArithmetic>(E().clone())); | ||
|
||
EXPECT_TRUE(cast<IArithmetic>(std::shared_ptr(i.clone()))); | ||
EXPECT_FALSE(cast<IArithmetic>(std::shared_ptr(E().clone()))); | ||
|
||
EXPECT_TRUE(cast<IArithmetic>(std::const_pointer_cast<const IMathObject>(std::shared_ptr(i.clone())))); | ||
EXPECT_FALSE(cast<IArithmetic>(std::const_pointer_cast<const IMathObject>(std::shared_ptr(E().clone())))); | ||
} | ||
// Nothing to test yet |
Oops, something went wrong.