Skip to content

Commit

Permalink
#126 update <ureact/detail/algorithm.hpp> to get rid of <iterator> us…
Browse files Browse the repository at this point in the history
…age if ureact's algorithm replacement is used
  • Loading branch information
YarikTH committed Aug 21, 2023
1 parent 0ac1521 commit acf55cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
25 changes: 21 additions & 4 deletions include/ureact/detail/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
#ifndef UREACT_DETAIL_ALGORITHM_HPP
#define UREACT_DETAIL_ALGORITHM_HPP

#include <iterator>

#include <ureact/detail/defines.hpp>

#ifdef UREACT_USE_STD_ALGORITHM
# include <algorithm>
#else
# include <cstddef>
# include <utility>
#endif

UREACT_BEGIN_NAMESPACE

// Partial alternative to <algorithm> is provided and used by default because library requires
// only a few algorithms while standard <algorithm> is quite bloated
// also `next` and `distance` from <iterator> are added, because standard <iterator> is even more bloated
namespace detail
{

Expand Down Expand Up @@ -72,6 +74,21 @@ void iter_swap( LhsForwardIt a, RhsForwardIt b )
swap( *a, *b );
}

// random_access_iterator_tag version of std::next()
template <typename LhsForwardIt>
auto next( LhsForwardIt iter, size_t n = 1 )
{
iter += n;
return iter;
}

// random_access_iterator_tag version of std::distance()
template <typename InputIter>
auto distance( InputIter first, InputIter last )
{
return last - first;
}

// Code based on possible implementation at
// https://en.cppreference.com/w/cpp/algorithm/partition
template <typename ForwardIt, typename Pred>
Expand All @@ -83,7 +100,7 @@ ForwardIt partition( ForwardIt first, ForwardIt last, Pred pred )
return first;
}

for( ForwardIt i = std::next( first ); i != last; ++i )
for( ForwardIt i = detail::next( first ); i != last; ++i )
{
if( pred( *i ) )
{
Expand All @@ -101,7 +118,7 @@ void sort( ForwardIt first, ForwardIt last )
{
if( first == last )
return;
const auto pivot = *std::next( first, std::distance( first, last ) / 2 );
const auto pivot = *detail::next( first, detail::distance( first, last ) / 2 );
const auto middle1
= detail::partition( first, last, [pivot]( const auto& em ) { return em < pivot; } );
const auto middle2
Expand Down
1 change: 1 addition & 0 deletions tests/src/adaptor/observe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//
#include "ureact/adaptor/observe.hpp"

#include <iterator>
#include <sstream>

#include "catch2_extra.hpp"
Expand Down

0 comments on commit acf55cd

Please sign in to comment.