-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from njoy/feature/std-ranges-part7
Feature/std ranges part7
- Loading branch information
Showing
22 changed files
with
1,951 additions
and
7 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
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,78 @@ | ||
// nanorange/algorithm/min.hpp | ||
// | ||
// Copyright (c) 2018 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef NANORANGE_ALGORITHM_MIN_HPP_INCLUDED | ||
#define NANORANGE_ALGORITHM_MIN_HPP_INCLUDED | ||
|
||
#include "tools/std20/ranges.hpp" | ||
|
||
NANO_BEGIN_NAMESPACE | ||
|
||
namespace detail { | ||
|
||
struct min_fn { | ||
private: | ||
template <typename Rng, typename Comp, typename Proj> | ||
static constexpr iter_value_t<iterator_t<Rng>> | ||
impl(Rng&& rng, Comp& comp, Proj& proj) | ||
{ | ||
auto first = nano::begin(rng); | ||
const auto last = nano::end(rng); | ||
|
||
// Empty ranges not allowed | ||
auto result = *first; | ||
|
||
while(++first != last) { | ||
auto&& val = *first; | ||
if (nano::invoke(comp, nano::invoke(proj, val), | ||
nano::invoke(proj, result))) { | ||
result = std::forward<decltype(val)>(val); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public: | ||
template <typename T, typename Comp = ranges::less, typename Proj = identity> | ||
constexpr std::enable_if_t< | ||
indirect_strict_weak_order<Comp, projected<const T*, Proj>>, | ||
const T&> | ||
operator()(const T& a, const T& b, Comp comp = Comp{}, Proj proj = Proj{}) const | ||
{ | ||
return nano::invoke(comp, nano::invoke(proj, b), | ||
nano::invoke(proj, a)) ? b : a; | ||
} | ||
|
||
template <typename T, typename Comp = ranges::less, typename Proj = identity> | ||
constexpr std::enable_if_t< | ||
copyable<T> && | ||
indirect_strict_weak_order<Comp, projected<const T*, Proj>>, | ||
T> | ||
operator()(std::initializer_list<T> rng, Comp comp = Comp{}, | ||
Proj proj = Proj{}) const | ||
{ | ||
return min_fn::impl(rng, comp, proj); | ||
} | ||
|
||
template <typename Rng, typename Comp = ranges::less, typename Proj = identity> | ||
constexpr std::enable_if_t< | ||
input_range<Rng> && copyable<iter_value_t<iterator_t<Rng>>> && | ||
indirect_strict_weak_order<Comp, projected<iterator_t<Rng>, Proj>>, | ||
range_value_t<Rng>> | ||
operator()(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{}) const | ||
{ | ||
return min_fn::impl(std::forward<Rng>(rng), comp, proj); | ||
} | ||
}; | ||
|
||
} | ||
|
||
NANO_INLINE_VAR(detail::min_fn, min) | ||
|
||
NANO_END_NAMESPACE | ||
|
||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_cpp_test( std20.algorithm.equal equal.test.cpp ) | ||
add_cpp_test( std20.algorithm.min min.test.cpp ) |
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,38 @@ | ||
// include Catch2 | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
// what we are testing | ||
#include "tools/std20/algorithm.hpp" | ||
|
||
// other includes | ||
|
||
// convenience typedefs | ||
//using namespace njoy::tools; | ||
namespace std20 = nano; | ||
|
||
// test code | ||
struct Foo { | ||
|
||
int i; | ||
}; | ||
|
||
SCENARIO( "equal" ) { | ||
|
||
int a[] = { 1, 2, 3 }; | ||
std::vector< int > v = { 1, 2, 3 }; | ||
Foo list[] = { Foo{1}, Foo{2}, Foo{3} }; | ||
|
||
CHECK( 1 == std20::ranges::min( a ) ); | ||
CHECK( 3 == std20::ranges::min( a, std::greater{} ) ); | ||
|
||
CHECK( 1 == std20::ranges::min( v ) ); | ||
CHECK( 3 == std20::ranges::min( v, std::greater{} ) ); | ||
|
||
CHECK( 1 == std20::ranges::min( { 1, 2, 3 } ) ); | ||
CHECK( 3 == std20::ranges::min( { 1, 2, 3 }, std::greater{} ) ); | ||
|
||
Foo result = std20::ranges::min( list, std::less< int >{}, &Foo::i ); | ||
CHECK( 1 == result.i ); | ||
result = std20::ranges::min( list, std::greater< int >{}, &Foo::i ); | ||
CHECK( 3 == result.i ); | ||
} // SCENARIO |
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
Oops, something went wrong.