Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/pair.h
9973 views
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
#pragma once
32
33
#include "core/typedefs.h"
34
35
template <typename F, typename S>
36
struct Pair {
37
F first{};
38
S second{};
39
40
constexpr Pair() = default;
41
constexpr Pair(const F &p_first, const S &p_second) :
42
first(p_first), second(p_second) {}
43
44
constexpr bool operator==(const Pair &p_other) const { return first == p_other.first && second == p_other.second; }
45
constexpr bool operator!=(const Pair &p_other) const { return first != p_other.first || second != p_other.second; }
46
constexpr bool operator<(const Pair &p_other) const { return first == p_other.first ? (second < p_other.second) : (first < p_other.first); }
47
constexpr bool operator<=(const Pair &p_other) const { return first == p_other.first ? (second <= p_other.second) : (first < p_other.first); }
48
constexpr bool operator>(const Pair &p_other) const { return first == p_other.first ? (second > p_other.second) : (first > p_other.first); }
49
constexpr bool operator>=(const Pair &p_other) const { return first == p_other.first ? (second >= p_other.second) : (first > p_other.first); }
50
};
51
52
template <typename F, typename S>
53
struct PairSort {
54
constexpr bool operator()(const Pair<F, S> &p_lhs, const Pair<F, S> &p_rhs) const {
55
return p_lhs < p_rhs;
56
}
57
};
58
59
// Pair is zero-constructible if and only if both constrained types are zero-constructible.
60
template <typename F, typename S>
61
struct is_zero_constructible<Pair<F, S>> : std::conjunction<is_zero_constructible<F>, is_zero_constructible<S>> {};
62
63
template <typename K, typename V>
64
struct KeyValue {
65
const K key{};
66
V value{};
67
68
KeyValue &operator=(const KeyValue &p_kv) = delete;
69
KeyValue &operator=(KeyValue &&p_kv) = delete;
70
71
constexpr KeyValue(const KeyValue &p_kv) = default;
72
constexpr KeyValue(KeyValue &&p_kv) = default;
73
constexpr KeyValue(const K &p_key, const V &p_value) :
74
key(p_key), value(p_value) {}
75
constexpr KeyValue(const Pair<K, V> &p_pair) :
76
key(p_pair.first), value(p_pair.second) {}
77
78
constexpr bool operator==(const KeyValue &p_other) const { return key == p_other.key && value == p_other.value; }
79
constexpr bool operator!=(const KeyValue &p_other) const { return key != p_other.key || value != p_other.value; }
80
constexpr bool operator<(const KeyValue &p_other) const { return key == p_other.key ? (value < p_other.value) : (key < p_other.key); }
81
constexpr bool operator<=(const KeyValue &p_other) const { return key == p_other.key ? (value <= p_other.value) : (key < p_other.key); }
82
constexpr bool operator>(const KeyValue &p_other) const { return key == p_other.key ? (value > p_other.value) : (key > p_other.key); }
83
constexpr bool operator>=(const KeyValue &p_other) const { return key == p_other.key ? (value >= p_other.value) : (key > p_other.key); }
84
};
85
86
template <typename K, typename V>
87
struct KeyValueSort {
88
constexpr bool operator()(const KeyValue<K, V> &p_lhs, const KeyValue<K, V> &p_rhs) const {
89
return p_lhs.key < p_rhs.key;
90
}
91
};
92
93
// KeyValue is zero-constructible if and only if both constrained types are zero-constructible.
94
template <typename K, typename V>
95
struct is_zero_constructible<KeyValue<K, V>> : std::conjunction<is_zero_constructible<K>, is_zero_constructible<V>> {};
96
97