25#ifndef INCLUDE_CORE_KICAD_ALGO_H_
26#define INCLUDE_CORE_KICAD_ALGO_H_
31#include "../wx_compat.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>
112bool pair_contains(
const std::pair<_Type, _Type> __pair, _Value __value )
114 return __pair.first ==
static_cast<_Type
>( __value )
115 || __pair.second ==
static_cast<_Type
>( __value );
128bool within_wrapped_range( T __val, T __minval, T __maxval, T __wrap )
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>
164void delete_matching( _Container& __c, _Value __value )
166 __c.erase( std::remove( __c.begin(), __c.end(), __value ), __c.end() );
172template <
class _Container,
class _Function>
173void delete_if( _Container& __c, _Function&& __f )
175 __c.erase( std::remove_if( __c.begin(), __c.end(), std::forward<_Function>( __f ) ), __c.end() );