25#ifndef INCLUDE_CORE_KICAD_ALGO_H_
26#define INCLUDE_CORE_KICAD_ALGO_H_
43template <
typename _Type,
typename _Function>
44void run_on_pair( std::pair<_Type, _Type>& __pair, _Function __f )
60template <
typename _InputIterator,
typename _Function>
61void adjacent_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
63 if( __first != __last )
65 _InputIterator __follow = __first;
67 for( ; __first != __last; ++__first, ++__follow )
68 __f( *__follow, *__first );
82template <
typename _InputIterator,
typename _Function>
83void for_all_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
85 if( __first != __last )
87 _InputIterator __follow = __first;
89 for( ; __first != __last; ++__first, ++__follow )
90 for( _InputIterator __it = __first; __it != __last; ++__it )
91 __f( *__follow, *__it );
98template <
class _Container,
typename _Value>
99bool contains(
const _Container& __container, _Value __value )
101 return std::find( __container.begin(), __container.end(), __value ) != __container.end();
111template <
typename _Type,
typename _Value>
114 return __pair.first ==
static_cast<_Type
>( __value )
115 || __pair.second ==
static_cast<_Type
>( __value );
130 wxCHECK_MSG( __wrap > 0,
false, wxT(
"Wrap must be positive!" ) );
132 while( __maxval >= __wrap )
135 while( __maxval < 0 )
138 while( __minval >= __wrap )
141 while( __minval < 0 )
147 while( __val >= __wrap )
150 if( __maxval > __minval )
151 return __val >= __minval && __val <= __maxval;
153 return __val >= __minval || __val <= __maxval;
163template <
class _Container,
typename _Value>
166 __c.erase( std::remove( __c.begin(), __c.end(), __value ), __c.end() );
172template <
class _Container,
class _Function>
175 __c.erase( std::remove_if( __c.begin(), __c.end(), std::forward<_Function>( __f ) ), __c.end() );
181template <
class _Container>
184 __c.erase( std::unique( __c.begin(), __c.end() ), __c.end() );
187template <
class _Container,
class _Function>
190 __c.erase( std::unique( __c.begin(), __c.end(), std::forward<_Function>( __f ) ), __c.end() );
196template <typename T, std::enable_if_t<std::is_integral<T>::value,
int> = 0>
206 return std::max( min, std::min( value, max ) );
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
T clamp(T min, T value, T max)
void delete_matching(_Container &__c, _Value __value)
Covers for the horrifically named std::remove and std::remove_if (neither of which remove anything).
bool signbit(T v)
Integral version of std::signbit that works all compilers.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
bool pair_contains(const std::pair< _Type, _Type > __pair, _Value __value)
Returns true if either of the elements in an std::pair contains the given value.
bool within_wrapped_range(T __val, T __minval, T __maxval, T __wrap)
Test if __val lies within __minval and __maxval in a wrapped range.
void run_on_pair(std::pair< _Type, _Type > &__pair, _Function __f)
Apply a function to the first and second element of a std::pair.
void adjacent_pairs(_InputIterator __first, _InputIterator __last, _Function __f)
Apply a function to every sequential pair of elements of a sequence.
void for_all_pairs(_InputIterator __first, _InputIterator __last, _Function __f)
Apply a function to every possible pair of elements of a sequence.