KiCad PCB EDA Suite
hashtables.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) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5  * Copyright (C) 2012-2020 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 2
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-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 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 HASHTABLES_H_
26 #define HASHTABLES_H_
27 
28 #include <unordered_map>
29 
30 #include <eda_rect.h>
31 #include <wx/string.h>
32 
33 // First some utility classes and functions
34 
36 struct iequal_to : std::binary_function< const char*, const char*, bool >
37 {
38  bool operator()( const char* x, const char* y ) const
39  {
40  return !strcmp( x, y );
41  }
42 };
43 
44 
48 struct fnv_1a
49 {
50  /* not used, std::string is too slow:
51  std::size_t operator()( std::string const& text ) const
52  {
53  std::size_t hash = 2166136261u;
54 
55  for( std::string::const_iterator it = text.begin(), end = text.end();
56  it != end; ++it )
57  {
58  hash ^= *it;
59  hash *= 16777619;
60  }
61  return hash;
62  }
63  */
64 
65  std::size_t operator()( const char* it ) const
66  {
67  std::size_t hash = 2166136261u;
68 
69  for( ; *it; ++it )
70  {
71  hash ^= (unsigned char) *it;
72  hash *= 16777619;
73  }
74  return hash;
75  }
76 };
77 
78 
80 struct WXSTRING_HASH : std::unary_function<wxString, std::size_t>
81 {
82  std::size_t operator()( const wxString& aString ) const
83  {
84  std::size_t hash = 2166136261u;
85 
86  for( const wxUniChar c : aString )
87  {
88  unsigned ch = static_cast<unsigned>( c );
89  hash ^= ch;
90  hash *= 16777619;
91  }
92  return hash;
93  }
94 };
95 
96 
97 #ifdef SWIG
98 #define DECL_HASH_FOR_SWIG( TypeName, KeyType, ValueType ) \
100  namespace std \
101  { \
102  % template( TypeName ) unordered_map<KeyType, ValueType>; \
103  } \
104  typedef std::unordered_map<KeyType, ValueType> TypeName;
105 #else
106 #define DECL_HASH_FOR_SWIG( TypeName, KeyType, ValueType ) \
108  typedef std::unordered_map<KeyType, ValueType> TypeName;
109 #endif
110 
111 
129 typedef std::unordered_map< const char*, int, fnv_1a, iequal_to > KEYWORD_MAP;
130 
133 typedef std::unordered_map< std::string, EDA_RECT > RECT_MAP;
134 
135 
136 #endif // HASHTABLES_H_
std::size_t operator()(const wxString &aString) const
Definition: hashtables.h:82
Equality test for "const char*" type used in very specialized KEYWORD_MAP below.
Definition: hashtables.h:36
std::unordered_map< const char *, int, fnv_1a, iequal_to > KEYWORD_MAP
A hashtable made of a const char* and an int.
Definition: hashtables.h:129
bool operator()(const char *x, const char *y) const
Definition: hashtables.h:38
Hash function for wxString, counterpart of std::string hash.
Definition: hashtables.h:80
std::size_t operator()(const char *it) const
Definition: hashtables.h:65
Very fast and efficient hash function for "const char*" type, used in specialized KEYWORD_MAP below.
Definition: hashtables.h:48
std::unordered_map< std::string, EDA_RECT > RECT_MAP
Map a C string to an EDA_RECT.
Definition: hashtables.h:133