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