KiCad PCB EDA Suite
wx_stl_compat.cpp
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) 2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <ostream>
21 #include <core/wx_stl_compat.h>
22 
23 // add this only if it is not in wxWidgets (for instance before 3.1.0)
24 #ifdef USE_KICAD_WXSTRING_HASH
25 size_t std::hash<wxString>::operator()( const wxString& s ) const
26 {
27  return std::hash<std::wstring>{}( s.ToStdWstring() );
28 }
29 #endif
30 
31 #ifdef USE_KICAD_WXPOINT_LESS_AND_HASH
32 size_t std::hash<wxPoint>::operator() ( const wxPoint& k ) const
33 {
34  auto xhash = std::hash<int>()( k.x );
35 
36  // 0x9e3779b9 is 2^33 / ( 1 + sqrt(5) )
37  // Adding this value ensures that consecutive bits of y will not be close to each other
38  // decreasing the likelihood of hash collision in similar values of x and y
39  return xhash ^ ( std::hash<int>()( k.y ) + 0x9e3779b9 + ( xhash << 6 ) + ( xhash >> 2 ) );
40 }
41 
42 bool std::less<wxPoint>::operator()( const wxPoint& aA, const wxPoint& aB ) const
43 {
44  if( aA.x == aB.x )
45  return aA.y < aB.y;
46 
47  return aA.x < aB.x;
48 }
49 #endif
50 
51 
52 std::ostream& operator<<( std::ostream& out, const wxSize& size )
53 {
54  out << " width=\"" << size.GetWidth() << "\" height=\"" << size.GetHeight() << "\"";
55  return out;
56 }
57 
58 
59 std::ostream& operator<<( std::ostream& out, const wxPoint& pt )
60 {
61  out << " x=\"" << pt.x << "\" y=\"" << pt.y << "\"";
62  return out;
63 }
std::ostream & operator<<(std::ostream &out, const wxSize &size)
Helper function to print the given wxSize to a stream.
size_t operator()(const wxPoint &k) const
bool operator()(const wxPoint &aA, const wxPoint &aB) const
size_t operator()(const wxString &s) const