Skip to content

Commit

Permalink
Merge pull request #54 from open-space-collective/dev@lucas
Browse files Browse the repository at this point in the history
Dev@lucas
  • Loading branch information
lucas-bremond authored Oct 1, 2018
2 parents bb22945 + adc4d0a commit 2e9df36
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
32 changes: 32 additions & 0 deletions include/Library/Core/Containers/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,38 @@ class Array : public std::vector<T>

bool contains ( const T& anElement ) const ;

/// @brief Check if array is near another array
///
/// @code
/// Array<Real> firstArray = {1.0, 2.0, 3.0} ;
/// Array<Real> 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<T>& anArray,
const T& aTolerance ) const ;

/// @brief Check if array is near another array
///
/// @code
/// Array<Real> firstArray = {1.0, 2.0, 3.0} ;
/// Array<Real> 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<T>& anArray,
const std::function<bool (const T&, const T&)>& aComparator ) const ;

/// @brief Access first element
///
/// @code
Expand Down
48 changes: 48 additions & 0 deletions src/Library/Core/Containers/Array.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,54 @@ bool Array<T>::isEmpty ( )
bool Array<T>::contains ( const T& anElement ) const
{
return std::find(this->begin(), this->end(), anElement) != this->end() ;
}

template <class T>
bool Array<T>::isNear ( const Array<T>& 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 <class T>
bool Array<T>::isNear ( const Array<T>& anArray,
const std::function<bool (const T&, const T&)>& 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 <class T>
Expand Down
63 changes: 63 additions & 0 deletions test/Library/Core/Containers/Array.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <Library/Core/Containers/Array.hpp>
#include <Library/Core/Types/String.hpp>
#include <Library/Core/Types/Real.hpp>
#include <Library/Core/Types/Integer.hpp>
#include <Library/Core/Types/Size.hpp>
#include <Library/Core/Types/Index.hpp>
Expand Down Expand Up @@ -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<Real> array = { 1.0, 2.0, 3.0 } ;

EXPECT_TRUE(array.isNear(array, 0.0)) ;

}

{

const Array<Real> firstArray = { 1.0, 2.0, 3.0 } ;
const Array<Real> secondArray = { 1.0, 2.0, 3.0 } ;

EXPECT_TRUE(firstArray.isNear(secondArray, 0.0)) ;

}

{

const Array<Real> firstArray = { 1.0, 2.0, 3.0 } ;
const Array<Real> secondArray = { 1.0, 2.0, 4.0 } ;

EXPECT_TRUE(firstArray.isNear(secondArray, 1.0)) ;

}

{

const Array<Real> firstArray = { 1.0, 2.0, 3.0 } ;
const Array<Real> secondArray = { 1.0, 2.0, 3.0, 4.0 } ;

EXPECT_FALSE(firstArray.isNear(secondArray, 1.0)) ;

}

// Comparator

{

const Array<Real> firstArray = { 1.0, 2.0, 3.0 } ;
const Array<Real> 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)
{

Expand Down
12 changes: 10 additions & 2 deletions tools/development/helpers/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}

################################################################################################################################################################

0 comments on commit 2e9df36

Please sign in to comment.