diff --git a/include/Library/Core/Containers/Array.hpp b/include/Library/Core/Containers/Array.hpp index 0303e67b..b1dffe94 100755 --- a/include/Library/Core/Containers/Array.hpp +++ b/include/Library/Core/Containers/Array.hpp @@ -176,6 +176,38 @@ class Array : public std::vector bool contains ( const T& anElement ) const ; + /// @brief Check if array is near another array + /// + /// @code + /// Array firstArray = {1.0, 2.0, 3.0} ; + /// Array secondArray = {1.0, 2.0, 3.0 + 1e-15} ; + /// firstArray.isNear(secondArray, 1e-15) ; // True + /// @endcode + /// + /// @param [in] anArray An array + /// @param [in] aTolerance A tolerance + /// @return True if array is near another array + + bool isNear ( const Array& anArray, + const T& aTolerance ) const ; + + /// @brief Check if array is near another array + /// + /// @code + /// Array firstArray = {1.0, 2.0, 3.0} ; + /// Array secondArray = {1.0, 2.0, 3.0 + 1e-15} ; + /// firstArray.isNear(secondArray, + /// [] (const Real& aFirstValue, const Real& aSecondValue) -> bool + /// { return aFirstValue.isNear(aSecondValue, 1e-15) ; }) ; // True + /// @endcode + /// + /// @param [in] anArray An array + /// @param [in] aComparator A comparator + /// @return True if array is near another array + + bool isNear ( const Array& anArray, + const std::function& aComparator ) const ; + /// @brief Access first element /// /// @code diff --git a/src/Library/Core/Containers/Array.tpp b/src/Library/Core/Containers/Array.tpp index 002d5257..4389df18 100644 --- a/src/Library/Core/Containers/Array.tpp +++ b/src/Library/Core/Containers/Array.tpp @@ -128,6 +128,54 @@ bool Array::isEmpty ( ) bool Array::contains ( const T& anElement ) const { return std::find(this->begin(), this->end(), anElement) != this->end() ; +} + + template +bool Array::isNear ( const Array& anArray, + const T& aTolerance ) const +{ + + if (this->getSize() != anArray.getSize()) + { + return false ; + } + + for (const auto elementTuple : library::core::ctnr::iterators::Zip(*this, anArray)) + { + + if (!std::get<0>(elementTuple).isNear(std::get<1>(elementTuple), aTolerance)) + { + return false ; + } + + } + + return true ; + +} + + template +bool Array::isNear ( const Array& anArray, + const std::function& aComparator ) const +{ + + if (this->getSize() != anArray.getSize()) + { + return false ; + } + + for (const auto elementTuple : library::core::ctnr::iterators::Zip(*this, anArray)) + { + + if (!aComparator(std::get<0>(elementTuple), std::get<1>(elementTuple))) + { + return false ; + } + + } + + return true ; + } template diff --git a/test/Library/Core/Containers/Array.test.cpp b/test/Library/Core/Containers/Array.test.cpp index 25f86515..962997c5 100755 --- a/test/Library/Core/Containers/Array.test.cpp +++ b/test/Library/Core/Containers/Array.test.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -216,6 +217,68 @@ TEST (Library_Core_Containers_Array, Contains) } +TEST (Library_Core_Containers_Array, IsNear) +{ + + using library::core::types::Real ; + using library::core::ctnr::Array ; + + // Tolerance + + { + + const Array array = { 1.0, 2.0, 3.0 } ; + + EXPECT_TRUE(array.isNear(array, 0.0)) ; + + } + + { + + const Array firstArray = { 1.0, 2.0, 3.0 } ; + const Array secondArray = { 1.0, 2.0, 3.0 } ; + + EXPECT_TRUE(firstArray.isNear(secondArray, 0.0)) ; + + } + + { + + const Array firstArray = { 1.0, 2.0, 3.0 } ; + const Array secondArray = { 1.0, 2.0, 4.0 } ; + + EXPECT_TRUE(firstArray.isNear(secondArray, 1.0)) ; + + } + + { + + const Array firstArray = { 1.0, 2.0, 3.0 } ; + const Array secondArray = { 1.0, 2.0, 3.0, 4.0 } ; + + EXPECT_FALSE(firstArray.isNear(secondArray, 1.0)) ; + + } + + // Comparator + + { + + const Array firstArray = { 1.0, 2.0, 3.0 } ; + const Array secondArray = { 1.0, 2.0, 4.0 } ; + + EXPECT_TRUE + ( + firstArray.isNear(secondArray, + [] (const Real& aFirstValue, const Real& aSecondValue) -> bool + { return aFirstValue.isNear(aSecondValue, 1.0) ; } + ) + ) ; + + } + +} + TEST (Library_Core_Containers_Array, AccessFirst) { diff --git a/tools/development/helpers/build.sh b/tools/development/helpers/build.sh index 7115fe0f..08d559f5 100755 --- a/tools/development/helpers/build.sh +++ b/tools/development/helpers/build.sh @@ -9,8 +9,16 @@ ################################################################################################################################################################ -cmake .. +if [[ ! -z $1 ]] && [[ $1 == "--debug" ]]; then -make -j ${cpu_count} + cmake -DCMAKE_BUILD_TYPE=Debug .. + +else + + cmake .. + +fi + +make -j ${cpu_count:-1} ################################################################################################################################################################ \ No newline at end of file