43 template <
typename T >
44 constexpr
int sign( T
const& val )
55 template <
typename T >
56 [[nodiscard]] constexpr T
ceil_to( T val, T base )
61 return val + base - m;
65 template < additive_operators T, arithmetic_operators U >
66 [[nodiscard]] constexpr U
map_range( T input, T from_min, T from_max, U to_min, U to_max )
68 return to_min +
static_cast< U
>(
69 ( to_max - to_min ) *
static_cast< U
>( input - from_min ) /
70 static_cast< U
>( from_max - from_min ) );
74 template < container Container >
75 [[nodiscard]] constexpr std::size_t
cont_size( Container
const& cont ) noexcept
77 if constexpr ( static_sized< Container > )
78 return std::tuple_size_v< Container >;
80 return std::size( cont );
85 template <
typename T >
86 [[nodiscard]] constexpr
bool
89 return float(
std::abs( lh - rh ) ) < eps;
93 template < referenceable_container Container,
typename Iterator = iterator_of_t< Container > >
100 template < referenceable_container Container,
typename Iterator = iterator_of_t< Container > >
111 container_invocable< Container > PredicateCallable = std::identity >
112 [[nodiscard]] constexpr
auto find_if( Container&& cont, PredicateCallable&&
f = std::identity() )
114 auto beg = std::begin( cont );
115 auto end = std::end( cont );
116 for ( ; beg != end; ++beg )
126 container_invocable< Container > PredicateCallable = std::identity >
128 [[nodiscard]] constexpr std::size_t
129 find_if( Container&& t, PredicateCallable&&
f = std::identity() )
132 std::forward< Container >( t ),
133 std::forward< PredicateCallable >(
f ),
134 std::make_index_sequence< std::tuple_size_v< std::decay_t< Container > > >{} );
139 template < container Container,
typename T >
140 [[nodiscard]] constexpr
auto find( Container&& cont, T
const& item )
142 return find_if( std::forward< Container >( cont ), [&](
auto const& sub_item ) {
143 return sub_item == item;
148 template < container Container,
typename T >
149 [[nodiscard]] constexpr
bool contains( Container
const& cont, T
const& item )
151 if constexpr ( range_container< Container > )
152 return find( cont, item ) != cont.end();
158 template < gettable_container Container, container_invocable< Container > UnaryCallable >
163 [&
f]<
typename... Items >( Items&&... items ) {
164 (
f( std::forward< Items >( items ) ), ... );
166 std::forward< Container >( cont ) );
170 template < range_container Container, container_invocable< Container > UnaryCallable >
173 for (
auto&& item : std::forward< Container >( cont ) )
174 f( std::forward< decltype( item ) >( item ) );
182 container_invocable< Container >
UnaryCallable = std::identity,
183 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
184 [[nodiscard]] constexpr min_max< T >
192 auto val =
f( item );
204 container_invocable< Container >
UnaryCallable = std::identity,
205 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
220 container_invocable< Container >
UnaryCallable = std::identity,
221 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
233 template < container Container, container_invocable< Container > UnaryCallable = std::
identity >
248 container_invocable< Container >
UnaryCallable = std::identity,
249 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
260 template < container Container,
typename T,
typename BinaryCallable >
273 container_invocable< Container >
UnaryCallable = std::identity,
274 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
281 if constexpr ( std::is_arithmetic_v< T > )
291 container_invocable< Container >
UnaryCallable = std::identity,
292 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
295 T u =
avg( cont,
f );
297 T
res =
sum( cont, [&
f, &u](
auto& val ) {
298 auto v =
f( val ) - u;
301 if constexpr ( std::is_arithmetic_v< T > )
309 template < container LhContainer, container RhContainer,
typename BinaryCallable >
310 constexpr
void for_cross_joint( LhContainer&& lh_cont, RhContainer&& rh_cont, BinaryCallable&&
f )
312 for_each( lh_cont, [&](
auto& lh_item ) {
313 for_each( rh_cont, [&](
auto& rh_item ) {
314 f( lh_item, rh_item );
321 template < container Container, container_invocable< Container > PredicateCallable = std::
identity >
322 [[nodiscard]] constexpr
bool any_of( Container&& cont, PredicateCallable&&
f = std::identity() )
324 auto res =
find_if( cont, std::forward< PredicateCallable >(
f ) );
326 if constexpr ( is_std_tuple_v< Container > )
327 return res != std::tuple_size_v< std::decay_t< Container > >;
329 return res != cont.end();
334 template < container Container, container_invocable< Container > PredicateCallable = std::
identity >
335 [[nodiscard]] constexpr
bool none_of( Container&& cont, PredicateCallable&&
f = std::identity() )
337 return !
any_of( cont, std::forward< PredicateCallable >(
f ) );
341 template < container Container, container_invocable< Container > PredicateCallable = std::
identity >
342 [[nodiscard]] constexpr
bool all_of( Container&& cont, PredicateCallable&&
f = std::identity() )
344 return !
any_of( cont, [&](
auto& item ) {
354 typename BinaryPredicateCallable = std::equal_to< void > >
355 [[nodiscard]] constexpr
bool
356 equal( LhContainer&& lh, RhContainer&& rh, BinaryPredicateCallable&&
f = std::equal_to< void >{} )
360 auto rbeg = std::begin( rh );
361 for (
auto& item : lh ) {
362 if ( !
f( item, *rbeg ) )
379 container_invocable< Container >
UnaryCallable = std::identity >
382 ResultContainer
res{};
385 for_each( std::forward< Container >( cont ), [&]<
typename Item >( Item&& item ) {
386 collector.collect(
res,
f( std::forward< Item >( item ) ) );
396 container_invocable< Container >
UnaryCallable = std::identity,
397 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
401 return impl::map_f_to_a_impl< T, N >(
402 std::forward< Container >( cont ),
403 std::forward< UnaryCallable >(
f ),
404 std::make_index_sequence< N >() );
411 std::size_t
N = std::tuple_size< std::decay_t< Container > >::value,
412 container_invocable< Container >
UnaryCallable = std::identity,
413 typename T = std::decay_t< mapped_t< Container, UnaryCallable > > >
417 return impl::map_f_to_a_impl< T, N >(
418 std::forward< Container >( cont ),
419 std::forward< UnaryCallable >(
f ),
420 std::make_index_sequence< N >() );
425 template <
typename T >
428 template <
typename U >
430 noexcept( noexcept( T{ std::forward< U >( src ) } ) )
432 return T{ std::forward< U >( src ) };
442 container_invocable< Container >
UnaryCallable = std::identity >
443 [[nodiscard]] constexpr T
448 T
res =
f( *std::begin( cont ) );
449 for (
auto& item :
tail( cont ) )
450 res += val +
f( item );
454 template < container Container,
typename Iterator >
455 void copy( Container&& cont, Iterator iter )
465 template < std::
size_t N,
typename NullCallable >
468 impl::index_seq< 0, N >(
f );
474 template < std::
size_t N,
typename PredicateCallable >
478 until_index< N >( [&
f, &
res]< std::size_t i >() {
480 return f.template operator()< i >();
487 template < std::
size_t i,
typename PredicateCallable >
490 return impl::index_until< 0, i >(
f );
498 template < bounded_derived IndexType,
typename Callable >
500 {
f.template operator()< 0 >() } -> std::same_as< void >;
504 using T = std::decay_t< decltype(
f.template
operator()< 0 >() ) >;
507 res =
f.template operator()< i >();
512 template < bounded_derived IndexType,
typename Callable >
514 {
f.template operator()< 0 >() } -> std::same_as< void >;
518 static_assert( IndexType::min_val == 0 );
519 impl::index_switch< 0, IndexType::max_val + 1 >( *i,
f );
523 template <
typename... Args, std::size_t
N =
sizeof...( Args ) >
524 constexpr std::array< std::byte, N >
bytes( Args
const&...
args )
526 return std::array< std::byte, N >{
static_cast< std::byte >(
args )... };
530 template <
typename Arr,
typename... Arrs >
538 "All arrays have to provide similar types" );
540 constexpr std::size_t size =
541 ( std::tuple_size_v< std::decay_t< Arrs > > + ... +
542 std::tuple_size_v< std::decay_t< Arr > > );
544 auto f = [&]< std::size_t... Is >( std::index_sequence< Is... > ) {
545 return std::array< value_type, size >{
546 impl::get_ith_item_from_arrays< Is >( first, arrs... )... };
548 return f( std::make_index_sequence< size >{} );
552 template < std::
size_t N,
typename T >
553 constexpr std::array< T, N >
filled( T
const& item )
555 return map_f_to_a< N >(
range(
N ), [&](
auto ) -> T {
Generic class to represent view of some container.
Definition: view.h:41
hdr_state next(hdr_state cs) noexcept
Definition: page.h:43
constexpr std::size_t find_if_impl(std::tuple< Args... > const &t, UnaryPredicate &&f, std::index_sequence< Idx... >)
Definition: impl.h:35
concept map_f_collectable
Definition: impl.h:130
std::variant< int64_t, float, bool, string_buffer > value_type
Definition: base.h:51
MIT License.
Definition: impl.h:31
constexpr std::size_t count(Container &&cont, UnaryCallable &&f=std::identity())
Applies the predicate 'f(x)' to each element of container 'cont' and returns the count of items,...
Definition: algorithm.h:234
constexpr T min_elem(Container &&cont, UnaryCallable &&f=std::identity())
Applies unary callable 'f(x) to each element of container 'cont‘, returns the smallest return value o...
Definition: algorithm.h:222
constexpr U map_range(T input, T from_min, T from_max, U to_min, U to_max)
maps input value 'input' from input range to equivalent value in output range
Definition: algorithm.h:66
std::array< T, N > map_f_to_a(Container &&cont, UnaryCallable &&f=std::identity()) requires(!static_sized< Container >)
Calls callable f(cont[i]) for i = 0...N and stores the result in array of an size N.
Definition: algorithm.h:398
constexpr float default_epsilon
Definition: algorithm.h:40
select_index(i, [&res, &f]< std::size_t i >() { res=f.template operator()< i >();})
constexpr view< Iterator > tail(Container &&cont, int const step=1)
Returns range over Container, which skips first item of container.
Definition: algorithm.h:94
constexpr std::size_t find_if_index(PredicateCallable &&f)
Executes unary callable f() with template argument of type 'std::size_t', which ranges from 0 to N un...
Definition: algorithm.h:475
constexpr T accumulate(Container &&cont, T init, BinaryCallable &&f)
Applies callable 'f(init,x)' to each element of container 'x' and actual value of 'init' in iteration...
Definition: algorithm.h:261
constexpr bool until_index(PredicateCallable &&f)
Executes predicate f() with template argument of type 'std::size_t', which ranges from 0 to i until f...
Definition: algorithm.h:488
constexpr bool none_of(Container &&cont, PredicateCallable &&f=std::identity())
Returns true if call to predicate 'f(x)' returns false for all items in 'cont'.
Definition: algorithm.h:335
constexpr bool all_of(Container &&cont, PredicateCallable &&f=std::identity())
Returns true if call to predicate 'f(x)' returns true for all items in 'cont'.
Definition: algorithm.h:342
constexpr T max_elem(Container &&cont, UnaryCallable &&f=std::identity())
Applies unary callable 'f(x)' to each element of container 'cont', returns the largest return value o...
Definition: algorithm.h:206
constexpr T ceil_to(T val, T base)
takes an value val and rounds it up to nearest multiply of base
Definition: algorithm.h:56
constexpr T sum(Container &&cont, UnaryCallable &&f=std::identity(), T init={})
Applies f(x) to each item of container 'cont', returns the sum of all the return values of each call ...
Definition: algorithm.h:250
constexpr T joined(Container &&cont, T const &val, UnaryCallable &&f=std::identity())
Function applies callable f to each item in container cont and contacts results with operator+,...
Definition: algorithm.h:444
constexpr T variance(Container &&cont, UnaryCallable &&f=std::identity())
Applies callable 'f(x)' to each element of container 'cont' and returns the variance of values return...
Definition: algorithm.h:293
constexpr bool equal(LhContainer &&lh, RhContainer &&rh, BinaryPredicateCallable &&f=std::equal_to< void >{})
Returns true if containers 'lh' and 'rh' has same size and calls to predicate f - f(lh[i],...
Definition: algorithm.h:356
constexpr T avg(Container &&cont, UnaryCallable &&f=std::identity())
Applies callable 'f(x)' to each element of container 'cont' and returns the average value of each cal...
Definition: algorithm.h:275
constexpr void for_each_index(NullCallable &&f)
Executes unary callable f() with template argument of type 'std::size_t', which ranges from 0 to N.
Definition: algorithm.h:466
constexpr std::array< std::byte, N > bytes(Args const &... args)
Conveft the provided arguments into array of std::byte.
Definition: algorithm.h:524
void copy(Container &&cont, Iterator iter)
Definition: algorithm.h:455
constexpr view< Iterator > init(Container &&cont, int const step=1)
Returns range over Container, which skips last item of container.
Definition: algorithm.h:101
constexpr bool any_of(Container &&cont, PredicateCallable &&f=std::identity())
Returns true if call to predicate 'f(x)' returns true for at least one item x in 'cont'.
Definition: algorithm.h:322
Args const & args
Definition: min_max.h:83
T value_type
Definition: static_storage.h:100
constexpr Derived abs(vec_point_base< Derived, N > const &a)
Creates absolute version of A - removing signs on all dimensions.
Definition: vec_point_base.h:219
constexpr void for_each(Container &&cont, UnaryCallable &&f)
Applies unary callable 'f' to each element of container 'cont'.
Definition: algorithm.h:171
constexpr std::array< T, N > filled(T const &item)
Constructs an array filled with value x
Definition: algorithm.h:553
concept static_sized
Definition: concepts.h:116
constexpr int sign(T const &val)
returns sign of variable T: -1,0,1
Definition: algorithm.h:44
concept range_container
so, std::ranges::range is meh because it expects return of begin() being input_output_iterator,...
Definition: concepts.h:78
constexpr void for_cross_joint(LhContainer &&lh_cont, RhContainer &&rh_cont, BinaryCallable &&f)
Applies binary callable 'f(x,y)' to each combination of items x from lh_cont and y from rh_cont
Definition: algorithm.h:310
T res
Definition: algorithm.h:505
requires(!range_container< Container >) const expr std
Returns index of an element in tuple 't', for which call to predicate f(x) holds true,...
Definition: algorithm.h:127
constexpr min_max< T > min_max_elem(Container &&cont, UnaryCallable &&f=std::identity())
Applies unary callable 'f(x)' to each element of container 'cont', returns the largest and the smalle...
Definition: algorithm.h:185
constexpr Derived max(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:229
UnaryCallable
Definition: types.h:54
constexpr auto find(Container &&cont, T const &item)
Finds first item in container 'cont' that is equal to 'item', returns iterator for container,...
Definition: algorithm.h:140
constexpr Derived const & min(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:236
concept container
Definition: concepts.h:93
constexpr bool contains(Container const &cont, T const &item)
Checks if container cont contains at least one occurence of item, returns true/false.
Definition: algorithm.h:149
constexpr view< iterators::numeric_iterator< Numeric > > range(Numeric from, Numeric to)
Builds numeric view over interval [from, to)
Definition: range.h:34
constexpr auto merge_arrays(Arr &&first, Arrs &&... arrs)
Expects multiple std::arrays on input, and merges all together into one std::array instance.
Definition: algorithm.h:531
constexpr auto find_if(Container &&cont, PredicateCallable &&f=std::identity())
Returns iterator for first item, for which call to predicate f(*iter) holds true.
Definition: algorithm.h:112
concept gettable_container
Definition: concepts.h:71
constexpr bool almost_equal(T const &lh, T const &rh, float const eps=default_epsilon)
Two items 'lh' and 'rh' are almost equal if their difference is smaller than value 'eps'.
Definition: algorithm.h:87
ResultContainer map_f(Container &&cont, UnaryCallable &&f=std::identity())
Calls callable f(x) for each item in container 'cont' (or tuple) and stores result in 'ResultContaine...
Definition: algorithm.h:380
UnaryCallable && f
Definition: algorithm.h:161
N
Definition: static_storage.h:97
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118
constexpr std::size_t cont_size(Container const &cont) noexcept
Returns the size of the container, regardless of what it is.
Definition: algorithm.h:75
Object with operator() that constructs object of type Tout of passed-in value.
Definition: algorithm.h:427
constexpr T operator()(U &&src) const noexcept(noexcept(T{ std::forward< U >(src) }))
Definition: algorithm.h:429
A structure representing a range defined by a minimum and a maximum value.
Definition: min_max.h:37
constexpr static T lowest()
Definition: quantity.h:288