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 CHANGELOG.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 {
43 template <typename _Type, typename _Function>
44 void run_on_pair( std::pair<_Type, _Type>& __pair, _Function __f )
45 {
46  __f( __pair.first );
47  __f( __pair.second );
48 }
49 
60 template <typename _InputIterator, typename _Function>
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 
82 template <typename _InputIterator, typename _Function>
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 
98 template <class _Container, typename _Value>
99 bool contains( const _Container& __container, _Value __value )
100 {
101  return std::find( __container.begin(), __container.end(), __value ) != __container.end();
102 }
103 
111 template <typename _Type, typename _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 
127 template <class T>
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 } // namespace alg
158 
159 #endif /* INCLUDE_CORE_KICAD_ALGO_H_ */
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
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
Definition: kicad_algo.h:33
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
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
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