2012-05-16 02:00:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2021-10-06 02:46:53 +00:00
|
|
|
* Copyright (C) 2012-2020 KiCad Developers, see AUTHORS.TXT for contributors.
|
2012-05-16 02:00:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2016-01-12 16:33:33 +00:00
|
|
|
#ifndef HASHTABLES_H_
|
|
|
|
#define HASHTABLES_H_
|
|
|
|
|
2021-06-02 14:21:06 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2021-06-06 23:11:35 +00:00
|
|
|
#include <eda_rect.h>
|
2012-05-16 02:00:25 +00:00
|
|
|
#include <wx/string.h>
|
|
|
|
|
2021-06-02 14:21:06 +00:00
|
|
|
// First some utility classes and functions
|
2012-05-16 02:00:25 +00:00
|
|
|
|
2013-07-01 01:05:40 +00:00
|
|
|
/// Equality test for "const char*" type used in very specialized KEYWORD_MAP below
|
2022-01-04 23:58:42 +00:00
|
|
|
struct iequal_to
|
2013-07-01 01:05:40 +00:00
|
|
|
{
|
|
|
|
bool operator()( const char* x, const char* y ) const
|
|
|
|
{
|
|
|
|
return !strcmp( x, y );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Very fast and efficient hash function for "const char*" type, used in specialized
|
|
|
|
/// KEYWORD_MAP below.
|
|
|
|
/// taken from: http://www.boost.org/doc/libs/1_53_0/libs/unordered/examples/fnv1.hpp
|
|
|
|
struct fnv_1a
|
|
|
|
{
|
|
|
|
/* not used, std::string is too slow:
|
|
|
|
std::size_t operator()( std::string const& text ) const
|
|
|
|
{
|
|
|
|
std::size_t hash = 2166136261u;
|
|
|
|
|
|
|
|
for( std::string::const_iterator it = text.begin(), end = text.end();
|
|
|
|
it != end; ++it )
|
|
|
|
{
|
|
|
|
hash ^= *it;
|
|
|
|
hash *= 16777619;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::size_t operator()( const char* it ) const
|
|
|
|
{
|
|
|
|
std::size_t hash = 2166136261u;
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
for( ; *it; ++it )
|
2013-07-01 01:05:40 +00:00
|
|
|
{
|
2013-07-09 05:18:03 +00:00
|
|
|
hash ^= (unsigned char) *it;
|
2013-07-01 01:05:40 +00:00
|
|
|
hash *= 16777619;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-10 17:04:07 +00:00
|
|
|
/// Hash function for wxString, counterpart of std::string hash
|
2022-01-04 23:58:42 +00:00
|
|
|
struct WXSTRING_HASH
|
2014-01-10 17:04:07 +00:00
|
|
|
{
|
|
|
|
std::size_t operator()( const wxString& aString ) const
|
|
|
|
{
|
|
|
|
std::size_t hash = 2166136261u;
|
|
|
|
|
2021-03-20 00:56:22 +00:00
|
|
|
for( const wxUniChar c : aString )
|
2014-01-10 17:04:07 +00:00
|
|
|
{
|
2021-03-06 09:27:41 +00:00
|
|
|
unsigned ch = static_cast<unsigned>( c );
|
2015-07-04 19:53:37 +00:00
|
|
|
hash ^= ch;
|
2014-01-10 17:04:07 +00:00
|
|
|
hash *= 16777619;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
Pcbnew: major swig fix.
* Switched hashtables.h over to std::undordered_map from boost version.
* Added new macros DECL_VEC_FOR_SWIG() and DECL_MAP_FOR_SWIG() in macros.h.
These along with future DECL_HASH_FOR_SWIG() unify the declaration to swig
and C++ so that the resultant type name is common in both languages, and
the types AGREE.
* Fixed swigging of NETINFO_ITEM and NETINFO_LIST via magic.
* Newly exposed (python wrapped) are: D_PADS, TRACKS (was TRACK_PTRS),
NETNAME_MAP, NETCODE_MAP, wxString (without constructor purposely, read
comment in wx.i), MARKERS, ZONE_CONTAINERS, NETCLASSPTR, KICAD_T types.
* std::vector<SOMETHING*> tends to end up named SOMETHINGS in C++ and python.
Having the name consistent between like types is helpful, and between
languages. std::map<> ends up as SOMETHING_MAP.
* NETINFO_LIST::m_netNames and NETINFO_LIST::m_netCodes are now std::map
instead of hashtables, because swig does not yet support std::unordered_map.
* You can now get to any netclass or net info. NETNAMES_MAP and NETCODES_MAP
are traversable basically the same as a python dictionary using a python
string (not wsString) as the key! The wxString typemap converts python
string to wxString before the lookup happens. Iteration also works.
2016-07-18 17:23:09 +00:00
|
|
|
#ifdef SWIG
|
|
|
|
/// Declare a std::unordered_map and also the swig %template in unison
|
2020-12-19 16:00:52 +00:00
|
|
|
#define DECL_HASH_FOR_SWIG( TypeName, KeyType, ValueType ) \
|
|
|
|
namespace std \
|
|
|
|
{ \
|
|
|
|
% template( TypeName ) unordered_map<KeyType, ValueType>; \
|
|
|
|
} \
|
|
|
|
typedef std::unordered_map<KeyType, ValueType> TypeName;
|
Pcbnew: major swig fix.
* Switched hashtables.h over to std::undordered_map from boost version.
* Added new macros DECL_VEC_FOR_SWIG() and DECL_MAP_FOR_SWIG() in macros.h.
These along with future DECL_HASH_FOR_SWIG() unify the declaration to swig
and C++ so that the resultant type name is common in both languages, and
the types AGREE.
* Fixed swigging of NETINFO_ITEM and NETINFO_LIST via magic.
* Newly exposed (python wrapped) are: D_PADS, TRACKS (was TRACK_PTRS),
NETNAME_MAP, NETCODE_MAP, wxString (without constructor purposely, read
comment in wx.i), MARKERS, ZONE_CONTAINERS, NETCLASSPTR, KICAD_T types.
* std::vector<SOMETHING*> tends to end up named SOMETHINGS in C++ and python.
Having the name consistent between like types is helpful, and between
languages. std::map<> ends up as SOMETHING_MAP.
* NETINFO_LIST::m_netNames and NETINFO_LIST::m_netCodes are now std::map
instead of hashtables, because swig does not yet support std::unordered_map.
* You can now get to any netclass or net info. NETNAMES_MAP and NETCODES_MAP
are traversable basically the same as a python dictionary using a python
string (not wsString) as the key! The wxString typemap converts python
string to wxString before the lookup happens. Iteration also works.
2016-07-18 17:23:09 +00:00
|
|
|
#else
|
|
|
|
/// Declare a std::unordered_map but no swig %template
|
2020-12-19 16:00:52 +00:00
|
|
|
#define DECL_HASH_FOR_SWIG( TypeName, KeyType, ValueType ) \
|
|
|
|
typedef std::unordered_map<KeyType, ValueType> TypeName;
|
Pcbnew: major swig fix.
* Switched hashtables.h over to std::undordered_map from boost version.
* Added new macros DECL_VEC_FOR_SWIG() and DECL_MAP_FOR_SWIG() in macros.h.
These along with future DECL_HASH_FOR_SWIG() unify the declaration to swig
and C++ so that the resultant type name is common in both languages, and
the types AGREE.
* Fixed swigging of NETINFO_ITEM and NETINFO_LIST via magic.
* Newly exposed (python wrapped) are: D_PADS, TRACKS (was TRACK_PTRS),
NETNAME_MAP, NETCODE_MAP, wxString (without constructor purposely, read
comment in wx.i), MARKERS, ZONE_CONTAINERS, NETCLASSPTR, KICAD_T types.
* std::vector<SOMETHING*> tends to end up named SOMETHINGS in C++ and python.
Having the name consistent between like types is helpful, and between
languages. std::map<> ends up as SOMETHING_MAP.
* NETINFO_LIST::m_netNames and NETINFO_LIST::m_netCodes are now std::map
instead of hashtables, because swig does not yet support std::unordered_map.
* You can now get to any netclass or net info. NETNAMES_MAP and NETCODES_MAP
are traversable basically the same as a python dictionary using a python
string (not wsString) as the key! The wxString typemap converts python
string to wxString before the lookup happens. Iteration also works.
2016-07-18 17:23:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-19 16:00:52 +00:00
|
|
|
* A hashtable made of a const char* and an int.
|
|
|
|
*
|
|
|
|
* @note The use of this type outside very specific circumstances is foolish since there is
|
|
|
|
* no storage provided for the actual C string itself.
|
|
|
|
*
|
|
|
|
* This type assumes use with type #KEYWORD that is created by CMake and that table creates
|
|
|
|
* *constant* storage for C strings (and pointers to those C strings). Here we are only
|
|
|
|
* interested in the C strings themselves and only the pointers are duplicated within the
|
|
|
|
* hashtable. If the strings were not constant and fixed, this type would not work. Also
|
|
|
|
* note that normally a hashtable (i.e. unordered_map) using a const char* key would simply
|
|
|
|
* compare the 32 bit or 64 bit pointers themselves, rather than the C strings which they
|
|
|
|
* are known to point to in this context. I force the latter behavior by supplying both
|
|
|
|
* "hash" and "equality" overloads to the hashtable (unordered_map) template.
|
|
|
|
*
|
Pcbnew: major swig fix.
* Switched hashtables.h over to std::undordered_map from boost version.
* Added new macros DECL_VEC_FOR_SWIG() and DECL_MAP_FOR_SWIG() in macros.h.
These along with future DECL_HASH_FOR_SWIG() unify the declaration to swig
and C++ so that the resultant type name is common in both languages, and
the types AGREE.
* Fixed swigging of NETINFO_ITEM and NETINFO_LIST via magic.
* Newly exposed (python wrapped) are: D_PADS, TRACKS (was TRACK_PTRS),
NETNAME_MAP, NETCODE_MAP, wxString (without constructor purposely, read
comment in wx.i), MARKERS, ZONE_CONTAINERS, NETCLASSPTR, KICAD_T types.
* std::vector<SOMETHING*> tends to end up named SOMETHINGS in C++ and python.
Having the name consistent between like types is helpful, and between
languages. std::map<> ends up as SOMETHING_MAP.
* NETINFO_LIST::m_netNames and NETINFO_LIST::m_netCodes are now std::map
instead of hashtables, because swig does not yet support std::unordered_map.
* You can now get to any netclass or net info. NETNAMES_MAP and NETCODES_MAP
are traversable basically the same as a python dictionary using a python
string (not wsString) as the key! The wxString typemap converts python
string to wxString before the lookup happens. Iteration also works.
2016-07-18 17:23:09 +00:00
|
|
|
* @author Dick Hollenbeck
|
|
|
|
*/
|
2020-12-19 16:00:52 +00:00
|
|
|
typedef std::unordered_map< const char*, int, fnv_1a, iequal_to > KEYWORD_MAP;
|
Pcbnew: major swig fix.
* Switched hashtables.h over to std::undordered_map from boost version.
* Added new macros DECL_VEC_FOR_SWIG() and DECL_MAP_FOR_SWIG() in macros.h.
These along with future DECL_HASH_FOR_SWIG() unify the declaration to swig
and C++ so that the resultant type name is common in both languages, and
the types AGREE.
* Fixed swigging of NETINFO_ITEM and NETINFO_LIST via magic.
* Newly exposed (python wrapped) are: D_PADS, TRACKS (was TRACK_PTRS),
NETNAME_MAP, NETCODE_MAP, wxString (without constructor purposely, read
comment in wx.i), MARKERS, ZONE_CONTAINERS, NETCLASSPTR, KICAD_T types.
* std::vector<SOMETHING*> tends to end up named SOMETHINGS in C++ and python.
Having the name consistent between like types is helpful, and between
languages. std::map<> ends up as SOMETHING_MAP.
* NETINFO_LIST::m_netNames and NETINFO_LIST::m_netCodes are now std::map
instead of hashtables, because swig does not yet support std::unordered_map.
* You can now get to any netclass or net info. NETNAMES_MAP and NETCODES_MAP
are traversable basically the same as a python dictionary using a python
string (not wsString) as the key! The wxString typemap converts python
string to wxString before the lookup happens. Iteration also works.
2016-07-18 17:23:09 +00:00
|
|
|
|
|
|
|
|
2012-01-09 08:35:06 +00:00
|
|
|
#endif // HASHTABLES_H_
|