KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef INCLUDE_CORE_KICAD_ALGO_H_
22#define INCLUDE_CORE_KICAD_ALGO_H_
23
24#include <algorithm>
25#include <functional> // std::function
26#include <utility> // std::pair
27#include <vector>
28#include <wx/debug.h> // wxCHECK_MSG
29
30namespace alg
31{
40template <typename _Type, typename _Function>
41void run_on_pair( std::pair<_Type, _Type>& __pair, _Function __f )
42{
43 __f( __pair.first );
44 __f( __pair.second );
45}
46
57template <typename _InputIterator, typename _Function>
58void adjacent_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
59{
60 if( __first != __last )
61 {
62 _InputIterator __follow = __first;
63 ++__first;
64 for( ; __first != __last; ++__first, ++__follow )
65 __f( *__follow, *__first );
66 }
67}
68
79template <typename _InputIterator, typename _Function>
80void for_all_pairs( _InputIterator __first, _InputIterator __last, _Function __f )
81{
82 if( __first != __last )
83 {
84 _InputIterator __follow = __first;
85 ++__first;
86 for( ; __first != __last; ++__first, ++__follow )
87 for( _InputIterator __it = __first; __it != __last; ++__it )
88 __f( *__follow, *__it );
89 }
90}
91
95template <class _Container, typename _Value>
96bool contains( const _Container& __container, _Value __value )
97{
98 return std::find( __container.begin(), __container.end(), __value ) != __container.end();
99}
100
108template <typename _Type, typename _Value>
109bool pair_contains( const std::pair<_Type, _Type> __pair, _Value __value )
110{
111 return __pair.first == static_cast<_Type>( __value )
112 || __pair.second == static_cast<_Type>( __value );
113}
114
124template <class T>
125bool within_wrapped_range( T __val, T __minval, T __maxval, T __wrap )
126{
127 wxCHECK_MSG( __wrap > 0, false, wxT( "Wrap must be positive!" ) );
128
129 while( __maxval >= __wrap )
130 __maxval -= __wrap;
131
132 while( __maxval < 0 )
133 __maxval += __wrap;
134
135 while( __minval >= __wrap )
136 __minval -= __wrap;
137
138 while( __minval < 0 )
139 __minval += __wrap;
140
141 while( __val < 0 )
142 __val += __wrap;
143
144 while( __val >= __wrap )
145 __val -= __wrap;
146
147 if( __maxval > __minval )
148 return __val >= __minval && __val <= __maxval;
149 else
150 return __val >= __minval || __val <= __maxval;
151}
152
156template <class _Container>
157void remove_duplicates( _Container& __c )
158{
159 __c.erase( std::unique( __c.begin(), __c.end() ), __c.end() );
160}
161
162template <class _Container, class _Function>
163void remove_duplicates( _Container& __c, _Function&& __f )
164{
165 __c.erase( std::unique( __c.begin(), __c.end(), std::forward<_Function>( __f ) ), __c.end() );
166}
167
171template <typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
172bool signbit( T v )
173{
174 return v < 0;
175}
176
177
181template <class _Container>
182size_t longest_common_subset( const _Container& __c1, const _Container& __c2 )
183{
184 size_t __c1_size = __c1.size();
185 size_t __c2_size = __c2.size();
186
187 if( __c1_size == 0 || __c2_size == 0 )
188 return 0;
189
190 // Create a 2D table to store the lengths of common subsets
191 std::vector<std::vector<size_t>> table( __c1_size + 1, std::vector<size_t>( __c2_size + 1, 0 ) );
192
193 size_t longest = 0;
194
195 for( size_t i = 1; i <= __c1_size; ++i )
196 {
197 for( size_t j = 1; j <= __c2_size; ++j )
198 {
199 if( __c1[i - 1] == __c2[j - 1] )
200 {
201 table[i][j] = table[i - 1][j - 1] + 1;
202 longest = std::max( longest, static_cast<size_t>( table[i][j] ) );
203 }
204 }
205 }
206
207 return longest;
208}
209
219template <class Container1Iter, class Container2Iter>
220int lexicographical_compare_three_way( Container1Iter aC1_first, Container1Iter aC1_last,
221 Container2Iter aC2_first, Container2Iter aC2_last )
222{
223#ifdef __cpp_lib_three_way_comparison // Check to see if we have an optimized version
224 auto retval =
225 std::lexicographical_compare_three_way( aC1_first, aC1_last, aC2_first, aC2_last );
226 return retval == std::strong_ordering::equal
227 ? 0
228 : ( retval == std::strong_ordering::less ? -1 : 1 );
229#else
230 Container1Iter it1 = aC1_first;
231 Container2Iter it2 = aC2_first;
232
233 while( it1 != aC1_last && it2 != aC2_last )
234 {
235 if( *it1 < *it2 )
236 return -1;
237 if( *it1 > *it2 )
238 return 1;
239 ++it1;
240 ++it2;
241 }
242
243 if( it2 == aC2_last )
244 return !( it1 == aC1_last );
245 else
246 return -1;
247#endif // __cpp_lib_three_way_comparison
248}
249
250
251} // namespace alg
252
253#endif /* INCLUDE_CORE_KICAD_ALGO_H_ */
void remove_duplicates(_Container &__c)
Deletes all duplicate values from __c.
Definition kicad_algo.h:157
bool signbit(T v)
Integral version of std::signbit that works all compilers.
Definition kicad_algo.h:172
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
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:109
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:125
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:41
int lexicographical_compare_three_way(Container1Iter aC1_first, Container1Iter aC1_last, Container2Iter aC2_first, Container2Iter aC2_last)
Compares two containers lexicographically.
Definition kicad_algo.h:220
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:58
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:80
size_t longest_common_subset(const _Container &__c1, const _Container &__c2)
Returns the length of the longest common subset of values between two containers.
Definition kicad_algo.h:182
std::vector< std::vector< std::string > > table