pair.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #pragma once
  31. #include "core/templates/hashfuncs.h"
  32. #include "core/typedefs.h"
  33. template <typename F, typename S>
  34. struct Pair {
  35. F first;
  36. S second;
  37. Pair() :
  38. first(),
  39. second() {
  40. }
  41. Pair(F p_first, const S &p_second) :
  42. first(p_first),
  43. second(p_second) {
  44. }
  45. };
  46. template <typename F, typename S>
  47. bool operator==(const Pair<F, S> &pair, const Pair<F, S> &other) {
  48. return (pair.first == other.first) && (pair.second == other.second);
  49. }
  50. template <typename F, typename S>
  51. bool operator!=(const Pair<F, S> &pair, const Pair<F, S> &other) {
  52. return (pair.first != other.first) || (pair.second != other.second);
  53. }
  54. template <typename F, typename S>
  55. struct PairSort {
  56. bool operator()(const Pair<F, S> &A, const Pair<F, S> &B) const {
  57. if (A.first != B.first) {
  58. return A.first < B.first;
  59. }
  60. return A.second < B.second;
  61. }
  62. };
  63. template <typename F, typename S>
  64. struct PairHash {
  65. static uint32_t hash(const Pair<F, S> &P) {
  66. uint64_t h1 = HashMapHasherDefault::hash(P.first);
  67. uint64_t h2 = HashMapHasherDefault::hash(P.second);
  68. return hash_one_uint64((h1 << 32) | h2);
  69. }
  70. };
  71. // Pair is zero-constructible if and only if both constrained types are zero-constructible.
  72. template <typename F, typename S>
  73. struct is_zero_constructible<Pair<F, S>> : std::conjunction<is_zero_constructible<F>, is_zero_constructible<S>> {};
  74. template <typename K, typename V>
  75. struct KeyValue {
  76. const K key;
  77. V value;
  78. void operator=(const KeyValue &p_kv) = delete;
  79. _FORCE_INLINE_ KeyValue(const KeyValue &p_kv) :
  80. key(p_kv.key),
  81. value(p_kv.value) {
  82. }
  83. _FORCE_INLINE_ KeyValue(const K &p_key, const V &p_value) :
  84. key(p_key),
  85. value(p_value) {
  86. }
  87. };
  88. template <typename K, typename V>
  89. bool operator==(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
  90. return (pair.key == other.key) && (pair.value == other.value);
  91. }
  92. template <typename K, typename V>
  93. bool operator!=(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
  94. return (pair.key != other.key) || (pair.value != other.value);
  95. }
  96. template <typename K, typename V>
  97. struct KeyValueSort {
  98. bool operator()(const KeyValue<K, V> &A, const KeyValue<K, V> &B) const {
  99. return A.key < B.key;
  100. }
  101. };
  102. // KeyValue is zero-constructible if and only if both constrained types are zero-constructible.
  103. template <typename K, typename V>
  104. struct is_zero_constructible<KeyValue<K, V>> : std::conjunction<is_zero_constructible<K>, is_zero_constructible<V>> {};