comparator: add comparator for indexed compound types
compares each index of the two parameters in sequence, returning true if the first parameter has the first value that compares less than the second.
This commit is contained in:
parent
046a369f55
commit
6065aa9933
@ -451,6 +451,7 @@ if (TESTS)
|
|||||||
bitwise
|
bitwise
|
||||||
cmdopt
|
cmdopt
|
||||||
colour
|
colour
|
||||||
|
comparator
|
||||||
coord
|
coord
|
||||||
crypto/arc4
|
crypto/arc4
|
||||||
crypto/ice
|
crypto/ice
|
||||||
|
24
test/comparator.cpp
Normal file
24
test/comparator.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "tap.hpp"
|
||||||
|
|
||||||
|
#include "types/comparator.hpp"
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int, char**)
|
||||||
|
{
|
||||||
|
util::TAP::logger tap;
|
||||||
|
|
||||||
|
const auto a = { 1, 2, 3 };
|
||||||
|
const auto b = { 1, 2, 4 };
|
||||||
|
|
||||||
|
tap.expect (
|
||||||
|
util::comparator::indexed<decltype(a)> ()(a, b),
|
||||||
|
"compare initializer_list as a coordinate, success"
|
||||||
|
);
|
||||||
|
|
||||||
|
tap.expect (
|
||||||
|
!util::comparator::indexed<decltype(a)> ()(b, a),
|
||||||
|
"compare initializer_list as a coordinate, failure"
|
||||||
|
);
|
||||||
|
|
||||||
|
return tap.status ();
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2012-2017 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __UTIL_TYPES_COMPARATOR_HPP
|
#ifndef __UTIL_TYPES_COMPARATOR_HPP
|
||||||
@ -19,14 +19,41 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace util {
|
#include "./iterator.hpp"
|
||||||
|
|
||||||
|
namespace util::comparator {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct pointer_comparator {
|
struct pointer_comparator {
|
||||||
bool operator() (const std::unique_ptr<T> &lhs, const std::unique_ptr<T> &rhs);
|
bool operator() (const std::unique_ptr<T> &lhs, const std::unique_ptr<T> &rhs);
|
||||||
bool operator() (const T *lhs, const std::unique_ptr<T> &rhs);
|
bool operator() (const T *lhs, const std::unique_ptr<T> &rhs);
|
||||||
bool operator() (const std::unique_ptr<T> &lhs, const T *rhs);
|
bool operator() (const std::unique_ptr<T> &lhs, const T *rhs);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
/// Provides a comparison interface for types exposing an index operator.
|
||||||
|
///
|
||||||
|
/// Will return true if the first parameter has the first index that
|
||||||
|
/// compares less than the second parameter. Useful for providing a total
|
||||||
|
/// order of coordinate types where this doesn't make sense by default.
|
||||||
|
///
|
||||||
|
/// \tparam T The container data type to compare. Must provide an index
|
||||||
|
/// operator.
|
||||||
|
template <typename T>
|
||||||
|
struct indexed {
|
||||||
|
constexpr
|
||||||
|
bool operator() (const T &a, const T &b)
|
||||||
|
{
|
||||||
|
for (auto x: util::zip (a, b)) {
|
||||||
|
const auto &[i,j] = x; // clang-4.0: workaround for segfault.
|
||||||
|
if (i < j) return true;
|
||||||
|
if (i != j) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "util/types/comparator.ipp"
|
#include "./comparator.ipp"
|
||||||
#endif
|
#endif
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2012-2017 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef __UTIL_TYPES_COMPARATOR_IPP
|
#ifdef __UTIL_TYPES_COMPARATOR_IPP
|
||||||
@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
namespace util {
|
namespace util::comparator {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool
|
bool
|
||||||
pointer_comparator<T>::operator() (const std::unique_ptr<T> &lhs,
|
pointer_comparator<T>::operator() (const std::unique_ptr<T> &lhs,
|
||||||
|
Loading…
Reference in New Issue
Block a user