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
33 namespace alg
34 {
35 /**
36 * @brief Apply a function to the first and second element of a std::pair
37 * @param __pair A pair of elements (both elements must be the same type).
38 * @param __f A unary function object.
39 *
40 * Applies the function object @p __f to @p __pair.first and @p __pair.second
41 * If @p __f has a return value it is ignored.
42 */
43 template <typename _Type, typename _Function>
run_on_pair(std::pair<_Type,_Type> & __pair,_Function __f)44 void run_on_pair( std::pair<_Type, _Type>& __pair, _Function __f )
45 {
46 __f( __pair.first );
47 __f( __pair.second );
48 }
49
50 /**
51 * @brief Apply a function to every sequential pair of elements of a sequence.
52 * @param __first An input iterator.
53 * @param __last An input iterator.
54 * @param __f A unary function object.
55 *
56 * Applies the function object @p __f to each sequential pair of elements in the range
57 * @p [first,last). @p __f must not modify the order of the sequence.
58 * If @p __f has a return value it is ignored.
59 */
60 template <typename _InputIterator, typename _Function>
adjacent_pairs(_InputIterator __first,_InputIterator __last,_Function __f)61 void 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
72 /**
73 * @brief Apply a function to every possible pair of elements of a sequence.
74 * @param __first An input iterator.
75 * @param __last An input iterator.
76 * @param __f A unary function object.
77 *
78 * Applies the function object @p __f to every possible pair of elements in the range
79 * @p [first,last). @p __f must not modify the order of the sequence.
80 * If @p __f has a return value it is ignored.
81 */
82 template <typename _InputIterator, typename _Function>
for_all_pairs(_InputIterator __first,_InputIterator __last,_Function __f)83 void 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
95 /**
96 * @brief Returns true if the container contains the given value.
97 */
98 template <class _Container, typename _Value>
contains(const _Container & __container,_Value __value)99 bool contains( const _Container& __container, _Value __value )
100 {
101 return std::find( __container.begin(), __container.end(), __value ) != __container.end();
102 }
103
104 /**
105 * @brief Returns true if either of the elements in an std::pair contains the given value
106 *
107 * @param __pair A pair of elements (both elements must be the same type).
108 * @param __value A value to test
109 * @return true if @p __value is contained in @p __pair
110 */
111 template <typename _Type, typename _Value>
pair_contains(const std::pair<_Type,_Type> __pair,_Value __value)112 bool 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
118 /**
119 * @brief Test if __val lies within __minval and __maxval in a wrapped range.
120 *
121 * @param __val A value to test
122 * @param __minval Lowest permissible value within the wrapped range
123 * @param __maxval Highest permissible value within the wrapped range
124 * @param __wrap Value at which the range wraps around itself (must be positive)
125 * @return true if @p __val lies in the wrapped range
126 */
127 template <class T>
within_wrapped_range(T __val,T __minval,T __maxval,T __wrap)128 bool within_wrapped_range( T __val, T __minval, T __maxval, T __wrap )
129 {
130 wxCHECK_MSG( __wrap > 0, false, "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
156 /**
157 * Covers for the horrifically named std::remove and std::remove_if (neither of which remove
158 * anything).
159 */
160 /**
161 * @brief Deletes all values from \a __c which match \a __value.
162 */
163 template <class _Container, typename _Value>
delete_matching(_Container & __c,_Value __value)164 void delete_matching( _Container& __c, _Value __value )
165 {
166 __c.erase( std::remove( __c.begin(), __c.end(), __value ), __c.end() );
167 }
168
169 /**
170 * @brief Deletes all values from \a __c for which \a __f returns true.
171 */
172 template <class _Container, class _Function>
delete_if(_Container & __c,_Function && __f)173 void 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
178
179 } // namespace alg
180
181 #endif /* INCLUDE_CORE_KICAD_ALGO_H_ */
182