Skip to content

Commit

Permalink
[RNG] add range( first, last ) to return iterator between [first..last)
Browse files Browse the repository at this point in the history
  • Loading branch information
gastank committed Sep 25, 2024
1 parent cd0137b commit 653676b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions engine/util/rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,21 @@ struct basic_rng_t
return range<T>( T{}, max );
}

// Uniform distribution in the range [first element..last element]
template <typename T, typename U = std::remove_reference_t<decltype( *std::begin( std::declval<T&>() ) )>>
U& range( T& container )
// Uniform distribution in the range [first iterator..last iterator)
template <
typename T,
typename std::enable_if_t<
std::is_base_of_v<std::forward_iterator_tag, typename std::iterator_traits<T>::iterator_category>, int> = 0>
T range( T first, T last )
{
return container[ range( container.size() ) ];
return first + range( std::distance( first, last ) );
}

// Uniform distribution across [container.front()..container.back()]
template <typename T, typename U = std::remove_reference_t<decltype( *std::begin( std::declval<T&>() ) )>>
const U& range( const T& container )
U& range( T& container )
{
return container[ range( container.size() ) ];
return *range( container.begin(), container.end() );
}

/// Gaussian Distribution, Non-truncated
Expand Down

0 comments on commit 653676b

Please sign in to comment.