21#ifndef INCLUDE_CORE_KICAD_ALGO_H_
22#define INCLUDE_CORE_KICAD_ALGO_H_
40template <
typename _Type,
typename _Function>
41void run_on_pair( std::pair<_Type, _Type>& __pair, _Function __f )
57template <
typename _InputIterator,
typename _Function>
58void adjacent_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
60 if( __first != __last )
62 _InputIterator __follow = __first;
64 for( ; __first != __last; ++__first, ++__follow )
65 __f( *__follow, *__first );
79template <
typename _InputIterator,
typename _Function>
80void for_all_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
82 if( __first != __last )
84 _InputIterator __follow = __first;
86 for( ; __first != __last; ++__first, ++__follow )
87 for( _InputIterator __it = __first; __it != __last; ++__it )
88 __f( *__follow, *__it );
95template <
class _Container,
typename _Value>
96bool contains(
const _Container& __container, _Value __value )
98 return std::find( __container.begin(), __container.end(), __value ) != __container.end();
108template <
typename _Type,
typename _Value>
111 return __pair.first ==
static_cast<_Type
>( __value )
112 || __pair.second ==
static_cast<_Type
>( __value );
127 wxCHECK_MSG( __wrap > 0,
false, wxT(
"Wrap must be positive!" ) );
129 while( __maxval >= __wrap )
132 while( __maxval < 0 )
135 while( __minval >= __wrap )
138 while( __minval < 0 )
144 while( __val >= __wrap )
147 if( __maxval > __minval )
148 return __val >= __minval && __val <= __maxval;
150 return __val >= __minval || __val <= __maxval;
156template <
class _Container>
159 __c.erase( std::unique( __c.begin(), __c.end() ), __c.end() );
162template <
class _Container,
class _Function>
165 __c.erase( std::unique( __c.begin(), __c.end(), std::forward<_Function>( __f ) ), __c.end() );
171template <typename T, std::enable_if_t<std::is_integral<T>::value,
int> = 0>
181template <
class _Container>
184 size_t __c1_size = __c1.size();
185 size_t __c2_size = __c2.size();
187 if( __c1_size == 0 || __c2_size == 0 )
191 std::vector<std::vector<size_t>>
table( __c1_size + 1, std::vector<size_t>( __c2_size + 1, 0 ) );
195 for(
size_t i = 1; i <= __c1_size; ++i )
197 for(
size_t j = 1; j <= __c2_size; ++j )
199 if( __c1[i - 1] == __c2[j - 1] )
202 longest = std::max( longest,
static_cast<size_t>(
table[i][j] ) );
219template <
class Container1Iter,
class Container2Iter>
221 Container2Iter aC2_first, Container2Iter aC2_last )
223#ifdef __cpp_lib_three_way_comparison
225 std::lexicographical_compare_three_way( aC1_first, aC1_last, aC2_first, aC2_last );
226 return retval == std::strong_ordering::equal
228 : ( retval == std::strong_ordering::less ? -1 : 1 );
230 Container1Iter it1 = aC1_first;
231 Container2Iter it2 = aC2_first;
233 while( it1 != aC1_last && it2 != aC2_last )
243 if( it2 == aC2_last )
244 return !( it1 == aC1_last );
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
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.
int lexicographical_compare_three_way(Container1Iter aC1_first, Container1Iter aC1_last, Container2Iter aC2_first, Container2Iter aC2_last)
Compares two containers lexicographically.
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.
size_t longest_common_subset(const _Container &__c1, const _Container &__c2)
Returns the length of the longest common subset of values between two containers.
std::vector< std::vector< std::string > > table