KiCad PCB EDA Suite
kicad_algo.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019 CERN
5 * Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
20 * or you may search the http://www.gnu.org website for the version 3 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#ifndef INCLUDE_CORE_KICAD_ALGO_H_
26#define INCLUDE_CORE_KICAD_ALGO_H_
27
28#include <algorithm>
29#include <functional> // std::function
30#include <utility> // std::pair
31#include <wx/debug.h> // wxCHECK_MSG
32
33namespace alg
34{
43template <typename _Type, typename _Function>
44void run_on_pair( std::pair<_Type, _Type>& __pair, _Function __f )
45{
46 __f( __pair.first );
47 __f( __pair.second );
48}
49
60template <typename _InputIterator, typename _Function>
61void adjacent_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
62{
63 if( __first != __last )
64 {
65 _InputIterator __follow = __first;
66 ++__first;
67 for( ; __first != __last; ++__first, ++__follow )
68 __f( *__follow, *__first );
69 }
70}
71
82template <typename _InputIterator, typename _Function>
83void for_all_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
84{
85 if( __first != __last )
86 {
87 _InputIterator __follow = __first;
88 ++__first;
89 for( ; __first != __last; ++__first, ++__follow )
90 for( _InputIterator __it = __first; __it != __last; ++__it )
91 __f( *__follow, *__it );
92 }
93}
94
98template <class _Container, typename _Value>
99bool contains( const _Container& __container, _Value __value )
100{
101 return std::find( __container.begin(), __container.end(), __value ) != __container.end();
102}
103
111template <typename _Type, typename _Value>
112bool pair_contains( const std::pair<_Type, _Type> __pair, _Value __value )
113{
114 return __pair.first == static_cast<_Type>( __value )
115 || __pair.second == static_cast<_Type>( __value );
116}
117
127template <class T>
128bool within_wrapped_range( T __val, T __minval, T __maxval, T __wrap )
129{
130 wxCHECK_MSG( __wrap > 0, false, wxT( "Wrap must be positive!" ) );
131
132 while( __maxval >= __wrap )
133 __maxval -= __wrap;
134
135 while( __maxval < 0 )
136 __maxval += __wrap;
137
138 while( __minval >= __wrap )
139 __minval -= __wrap;
140
141 while( __minval < 0 )
142 __minval += __wrap;
143
144 while( __val < 0 )
145 __val += __wrap;
146
147 while( __val >= __wrap )
148 __val -= __wrap;
149
150 if( __maxval > __minval )
151 return __val >= __minval && __val <= __maxval;
152 else
153 return __val >= __minval || __val <= __maxval;
154}
155
163template <class _Container, typename _Value>
164void delete_matching( _Container& __c, _Value __value )
165{
166 __c.erase( std::remove( __c.begin(), __c.end(), __value ), __c.end() );
167}
168
172template <class _Container, class _Function>
173void delete_if( _Container& __c, _Function&& __f )
174{
175 __c.erase( std::remove_if( __c.begin(), __c.end(), std::forward<_Function>( __f ) ), __c.end() );
176}
177
181template <class _Container>
182void remove_duplicates( _Container& __c )
183{
184 __c.erase( std::unique( __c.begin(), __c.end() ), __c.end() );
185}
186
187template <class _Container, class _Function>
188void remove_duplicates( _Container& __c, _Function&& __f )
189{
190 __c.erase( std::unique( __c.begin(), __c.end(), std::forward<_Function>( __f ) ), __c.end() );
191}
192
196template <typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
197bool signbit( T v )
198{
199 return v < 0;
200}
201
202
203} // namespace alg
204
205#endif /* INCLUDE_CORE_KICAD_ALGO_H_ */
Definition: kicad_algo.h:34
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:173
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
Definition: kicad_algo.h:182
void delete_matching(_Container &__c, _Value __value)
Covers for the horrifically named std::remove and std::remove_if (neither of which remove anything).
Definition: kicad_algo.h:164
bool signbit(T v)
Integral version of std::signbit that works all compilers.
Definition: kicad_algo.h:197
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:99
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.
Definition: kicad_algo.h:112
bool within_wrapped_range(T __val, T __minval, T __maxval, T __wrap)
Test if __val lies within __minval and __maxval in a wrapped range.
Definition: kicad_algo.h:128
void run_on_pair(std::pair< _Type, _Type > &__pair, _Function __f)
Apply a function to the first and second element of a std::pair.
Definition: kicad_algo.h:44
void adjacent_pairs(_InputIterator __first, _InputIterator __last, _Function __f)
Apply a function to every sequential pair of elements of a sequence.
Definition: kicad_algo.h:61
void for_all_pairs(_InputIterator __first, _InputIterator __last, _Function __f)
Apply a function to every possible pair of elements of a sequence.
Definition: kicad_algo.h:83