Skip to content

Commit

Permalink
Merge pull request #16 from njoy/feature/std-ranges-part7
Browse files Browse the repository at this point in the history
Feature/std ranges part7
  • Loading branch information
whaeck authored Apr 22, 2024
2 parents 066d556 + 2ace627 commit c9eaa0d
Show file tree
Hide file tree
Showing 22 changed files with 1,951 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmake/unit_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_subdirectory( src/tools/std20/algorithm/test )
add_subdirectory( src/tools/std20/concepts/test )
add_subdirectory( src/tools/std20/functional/test )
add_subdirectory( src/tools/std20/iterator/concepts/test )
add_subdirectory( src/tools/std20/iterator/operations/test )
add_subdirectory( src/tools/std20/iterator/test )
add_subdirectory( src/tools/std20/ranges/access/test )
add_subdirectory( src/tools/std20/ranges/concepts/test )
Expand Down
2 changes: 1 addition & 1 deletion src/tools/std20/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
//#include <nanorange/algorithm/max.hpp>
//#include <nanorange/algorithm/max_element.hpp>
//#include <nanorange/algorithm/merge.hpp>
//#include <nanorange/algorithm/min.hpp>
#include "tools/std20/algorithm/min.hpp"
//#include <nanorange/algorithm/min_element.hpp>
//#include <nanorange/algorithm/minmax.hpp>
//#include <nanorange/algorithm/minmax_element.hpp>
Expand Down
78 changes: 78 additions & 0 deletions src/tools/std20/algorithm/min.hpp
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
1 change: 1 addition & 0 deletions src/tools/std20/algorithm/test/CMakeLists.txt
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 )
38 changes: 38 additions & 0 deletions src/tools/std20/algorithm/test/min.test.cpp
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
6 changes: 3 additions & 3 deletions src/tools/std20/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
//#include <nanorange/iterator/back_insert_iterator.hpp>
#include "tools/std20/iterator/common_iterator.hpp"
#include "tools/std20/iterator/concepts.hpp"
//#include <nanorange/iterator/counted_iterator.hpp>
#include "tools/std20/iterator/counted_iterator.hpp"
#include "tools/std20/iterator/default_sentinel.hpp"
//#include <nanorange/iterator/front_insert_iterator.hpp>
//#include <nanorange/iterator/insert_iterator.hpp>
//#include <nanorange/iterator/istream_iterator.hpp>
//#include <nanorange/iterator/istreambuf_iterator.hpp>
//#include <nanorange/iterator/move_iterator.hpp>
//#include <nanorange/iterator/operations.hpp>
#include "tools/std20/iterator/operations.hpp"
//#include <nanorange/iterator/ostream_iterator.hpp>
//#include <nanorange/iterator/ostreambuf_iterator.hpp>
#include "tools/std20/iterator/reverse_iterator.hpp"
//#include <nanorange/iterator/unreachable.hpp>
#include "tools/std20/iterator/unreachable.hpp"

#endif
Loading

0 comments on commit c9eaa0d

Please sign in to comment.