pair.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**************************************************************************/
  2. /* pair.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PAIR_H
  31. #define PAIR_H
  32. #include "core/templates/hashfuncs.h"
  33. #include "core/typedefs.h"
  34. template <typename F, typename S>
  35. struct Pair {
  36. F first;
  37. S second;
  38. Pair() :
  39. first(),
  40. second() {
  41. }
  42. Pair(F p_first, const S &p_second) :
  43. first(p_first),
  44. second(p_second) {
  45. }
  46. };
  47. template <typename F, typename S>
  48. bool operator==(const Pair<F, S> &pair, const Pair<F, S> &other) {
  49. return (pair.first == other.first) && (pair.second == other.second);
  50. }
  51. template <typename F, typename S>
  52. bool operator!=(const Pair<F, S> &pair, const Pair<F, S> &other) {
  53. return (pair.first != other.first) || (pair.second != other.second);
  54. }
  55. template <typename F, typename S>
  56. struct PairSort {
  57. bool operator()(const Pair<F, S> &A, const Pair<F, S> &B) const {
  58. if (A.first != B.first) {
  59. return A.first < B.first;
  60. }
  61. return A.second < B.second;
  62. }
  63. };
  64. template <typename F, typename S>
  65. struct PairHash {
  66. static uint32_t hash(const Pair<F, S> &P) {
  67. uint64_t h1 = HashMapHasherDefault::hash(P.first);
  68. uint64_t h2 = HashMapHasherDefault::hash(P.second);
  69. return hash_one_uint64((h1 << 32) | h2);
  70. }
  71. };
  72. template <typename K, typename V>
  73. struct KeyValue {
  74. const K key;
  75. V value;
  76. void operator=(const KeyValue &p_kv) = delete;
  77. _FORCE_INLINE_ KeyValue(const KeyValue &p_kv) :
  78. key(p_kv.key),
  79. value(p_kv.value) {
  80. }
  81. _FORCE_INLINE_ KeyValue(const K &p_key, const V &p_value) :
  82. key(p_key),
  83. value(p_value) {
  84. }
  85. };
  86. template <typename K, typename V>
  87. bool operator==(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
  88. return (pair.key == other.key) && (pair.value == other.value);
  89. }
  90. template <typename K, typename V>
  91. bool operator!=(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
  92. return (pair.key != other.key) || (pair.value != other.value);
  93. }
  94. template <typename K, typename V>
  95. struct KeyValueSort {
  96. bool operator()(const KeyValue<K, V> &A, const KeyValue<K, V> &B) const {
  97. return A.key < B.key;
  98. }
  99. };
  100. #endif // PAIR_H