You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 CERN
  5. * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #ifndef HASH_EDA_H_
  26. #define HASH_EDA_H_
  27. /**
  28. * @brief Hashing functions for EDA_ITEMs.
  29. */
  30. #include <cstdlib>
  31. #include <functional>
  32. class EDA_ITEM;
  33. ///> Enables/disables properties that will be used for calculating the hash.
  34. ///> The properties might be combined using the bitwise 'or' operator.
  35. enum HASH_FLAGS
  36. {
  37. HASH_POS = 0x01,
  38. ///> use coordinates relative to the parent object
  39. REL_COORD = 0x02,
  40. HASH_ROT = 0x04,
  41. HASH_LAYER = 0x08,
  42. HASH_NET = 0x10,
  43. HASH_REF = 0x20,
  44. HASH_VALUE = 0x40,
  45. HASH_ALL = 0xff
  46. };
  47. /**
  48. * Calculates hash of an EDA_ITEM.
  49. * @param aItem is the item for which the hash will be computed.
  50. * @return Hash value.
  51. */
  52. std::size_t hash_eda( const EDA_ITEM* aItem, int aFlags = HASH_FLAGS::HASH_ALL );
  53. /**
  54. * This is a dummy function to take the final case of hash_combine below
  55. * @param seed
  56. */
  57. static inline void hash_combine( std::size_t &seed ) {}
  58. /**
  59. * @brief Combine multiple hashes utilizing previous hash result
  60. * @tparam T A hashable type
  61. * @param seed A seed value input and output for the result.
  62. * @param val A hashable object of type T
  63. */
  64. template< typename T, typename ... Types >
  65. static inline void hash_combine( std::size_t &seed, const T &val, const Types &... args )
  66. {
  67. seed ^= std::hash<T>()( val ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
  68. hash_combine( seed, args... );
  69. }
  70. template <typename... Types>
  71. static inline std::size_t hash_val( const Types &... args )
  72. {
  73. std::size_t seed = 0xa82de1c0;
  74. hash_combine( seed, args... );
  75. return seed;
  76. }
  77. #endif