-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement min_element #100
Open
ecrypa
wants to merge
5
commits into
brunocodutra:master
Choose a base branch
from
ecrypa:feature/min-element
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c314a3c
implement min_element
a0dc056
try to fix min_element example on older compilers
cc03ecc
try to fix min_element example on GCC 4.7
24cc0d6
brute-force attempt to fix min_element example on GCC 4.7
deca2a7
try to fix min_element on GCC 4.7 by indirections
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,122 @@ | ||
#ifndef METAL_LIST_MIN_ELEMENT_HPP | ||
#define METAL_LIST_MIN_ELEMENT_HPP | ||
|
||
#include "../config.hpp" | ||
#include "../detail/sfinae.hpp" | ||
#include "../lambda/lambda.hpp" | ||
#include "../number/less.hpp" | ||
|
||
namespace metal { | ||
/// \cond | ||
namespace detail { | ||
template<class lbd = metal::lambda<metal::less>> | ||
struct _min_element; | ||
} | ||
/// \endcond | ||
|
||
/// \ingroup list | ||
/// | ||
/// ### Description | ||
/// Finds a minimum element in a \list according to an ordering relation. | ||
/// | ||
/// \note{The _first_ minimum element is returned if the ordering relation is [strict].} | ||
/// [strict]: | ||
/// https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings | ||
/// | ||
/// ### Usage | ||
/// For any \list `l` and \lambda `lbd` | ||
/// \code | ||
/// using result = metal::min_element<l, lbd>; | ||
/// \endcode | ||
/// | ||
/// \pre: `l` is not empty, and for any two \values `val_i` and `val_j` | ||
/// contained in `l` `metal::invoke<lbd, val_i, val_j>` returns a \number | ||
/// \returns: \value | ||
/// \semantics: | ||
/// If `l` contains elements `val_0, ..., val_k, ..., val_m-1`, then | ||
/// \code | ||
/// using result = val_k; | ||
/// \endcode | ||
/// where `val_k` is an element in `l` such that | ||
/// * `metal::invoke<lbd, val_k, val_j>{} != false` for all `j` in | ||
/// `[0, k)` and | ||
/// * `metal::invoke<lbd, val_i, val_k>{} == false` for all | ||
/// `i` in `(k, m-1]`. | ||
/// | ||
/// \tip{`lbd` may be omitted, in which case it defaults to `metal::lambda<metal::less>`.} | ||
/// | ||
/// ### Example | ||
/// \snippet list.cpp min_element | ||
/// | ||
/// ### See Also | ||
/// \see min, sort | ||
#if !defined(METAL_WORKAROUND) | ||
template<class seq, class lbd = metal::lambda<metal::less>> | ||
using min_element = | ||
detail::call<detail::_min_element<lbd>::template type, seq>; | ||
#else | ||
// MSVC 14 has shabby SFINAE support in case of default alias template args | ||
template<class seq, class... lbd> | ||
using min_element = | ||
detail::call<detail::_min_element<lbd...>::template type, seq>; | ||
#endif | ||
} | ||
|
||
#include "../list/list.hpp" | ||
#include "../number/if.hpp" | ||
#include "../value/fold_left.hpp" | ||
|
||
namespace metal { | ||
/// \cond | ||
namespace detail { | ||
template<class x, class y> | ||
struct _min_element_min_impl { | ||
template<template<class...> class expr> | ||
using type = if_<expr<y, x>, y, x>; | ||
}; | ||
|
||
template<template<class...> class expr> | ||
struct _min_element_min { | ||
template<class x, class y> | ||
using type = | ||
forward<_min_element_min_impl<x, y>::template type, expr>; | ||
}; | ||
|
||
template<class x, class y> | ||
struct _min_element_gcc_4_7_aliaser { | ||
template<template<class...> class expr> | ||
using type = typename _min_element_min<expr>::template type<x, y>; | ||
}; | ||
|
||
template<template<class...> class expr> | ||
struct _min_element_gcc_4_7_alias { | ||
template<class x, class y> | ||
using type = typename _min_element_gcc_4_7_aliaser< | ||
x, y>::template type<expr>; | ||
|
||
using lbd = lambda<type>; | ||
}; | ||
|
||
template<class seq> | ||
struct _min_element_impl {}; | ||
|
||
template<class... vals> | ||
struct _min_element_impl<list<vals...>> { | ||
template<template<class...> class expr> | ||
using type = fold_left< | ||
typename _min_element_gcc_4_7_alias<expr>::lbd, vals...>; | ||
}; | ||
|
||
template<class lbd> | ||
struct _min_element {}; | ||
|
||
template<template<class...> class expr> | ||
struct _min_element<lambda<expr>> { | ||
template<class seq> | ||
using type = forward<_min_element_impl<seq>::template type, expr>; | ||
}; | ||
} | ||
/// \endcond | ||
} | ||
|
||
#endif |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <metal.hpp> | ||
|
||
#include "test.hpp" | ||
|
||
#define MATRIX(M, N) \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, VALUE(M)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, VALUE(M), VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, VALUE(M), NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, VALUE(M), PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, VALUE(M), LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, VALUE(M), MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, VALUE(M), LAMBDA(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M), VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M), NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M), PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M), LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M), MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M), LAMBDA(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, NUMBER(M), LAMBDA(_)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M), VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M), NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M), PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M), LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M), MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M), LAMBDA(N)>), (BOOL(N == 2))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, PAIR(M), LAMBDA(_)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M)>), (BOOL(M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M), VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M), NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M), PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M), LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M), MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M), LAMBDA(N)>), (BOOL((N == 2 && M > 0) || M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LIST(M), LAMBDA(_)>), (BOOL(M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M)>), (BOOL(M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M), VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M), NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M), PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M), LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M), MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M), LAMBDA(N)>), (BOOL((N == 2 && M > 0) || M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, MAP(M), LAMBDA(_)>), (BOOL(M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>>), (BOOL(M > 0))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, metal::list<NUMBERS(N)>>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, LAMBDA(N)>), (BOOL((N == 2 && M > 0) || M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, metal::list<NUMBERS(M)>, LAMBDA(_)>), (BOOL(M == 1))); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M), VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M), NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M), PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M), LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M), MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M), LAMBDA(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(M), LAMBDA(_)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_), VALUE(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_), NUMBER(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_), PAIR(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_), LIST(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_), MAP(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_), LAMBDA(N)>), (FALSE)); \ | ||
CHECK((metal::is_invocable<metal::lambda<metal::min_element>, LAMBDA(_), LAMBDA(_)>), (FALSE)); \ | ||
CHECK((metal::min_element<metal::list<NUMBERSX20(INC(M))>, metal::lambda<metal::greater>>), (metal::number<M>)); \ | ||
CHECK((metal::min_element<metal::list<NUMBERSX20(INC(M))>, metal::lambda<metal::less>>), (metal::number<0>)); \ | ||
CHECK((metal::min_element<metal::list<NUMBERSX20(INC(M))>>), (metal::number<0>)); \ | ||
CHECK((metal::min_element<metal::list<ENUM(INC(N), NUMBERS FIX(INC(M)))>, metal::lambda<metal::less>>), (metal::number<0>)); \ | ||
CHECK((metal::min_element<metal::list<ENUM(INC(N), NUMBERS FIX(INC(M)))>>), (metal::number<0>)); \ | ||
/**/ | ||
|
||
GEN(MATRIX) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test coverage!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is derived from the test of
metal::sort
.It bothers me that the test is passing while the example fails on GCC 4.7.