2014-02-09 22:10:30 -03:00
|
|
|
/**************************************************************************/
|
|
|
|
|
/* pair.h */
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
/* This file is part of: */
|
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
|
/* https://godotengine.org */
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
|
/* the following conditions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 12:31:19 -03:00
|
|
|
#include "core/typedefs.h"
|
2024-08-31 10:22:57 -05:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
template <typename F, typename S>
|
|
|
|
|
struct Pair {
|
2024-08-31 10:22:57 -05:00
|
|
|
F first{};
|
|
|
|
|
S second{};
|
2016-10-10 18:31:01 -03:00
|
|
|
|
2024-08-31 10:22:57 -05:00
|
|
|
constexpr Pair() = default;
|
|
|
|
|
constexpr Pair(const F &p_first, const S &p_second) :
|
|
|
|
|
first(p_first), second(p_second) {}
|
2019-01-30 02:12:41 +01:00
|
|
|
|
2024-08-31 10:22:57 -05:00
|
|
|
constexpr bool operator==(const Pair &p_other) const { return first == p_other.first && second == p_other.second; }
|
|
|
|
|
constexpr bool operator!=(const Pair &p_other) const { return first != p_other.first || second != p_other.second; }
|
|
|
|
|
constexpr bool operator<(const Pair &p_other) const { return first == p_other.first ? (second < p_other.second) : (first < p_other.first); }
|
|
|
|
|
constexpr bool operator<=(const Pair &p_other) const { return first == p_other.first ? (second <= p_other.second) : (first < p_other.first); }
|
|
|
|
|
constexpr bool operator>(const Pair &p_other) const { return first == p_other.first ? (second > p_other.second) : (first > p_other.first); }
|
|
|
|
|
constexpr bool operator>=(const Pair &p_other) const { return first == p_other.first ? (second >= p_other.second) : (first > p_other.first); }
|
2014-02-09 22:10:30 -03:00
|
|
|
};
|
|
|
|
|
|
2017-02-01 09:45:45 -03:00
|
|
|
template <typename F, typename S>
|
|
|
|
|
struct PairSort {
|
2024-08-31 10:22:57 -05:00
|
|
|
constexpr bool operator()(const Pair<F, S> &p_lhs, const Pair<F, S> &p_rhs) const {
|
|
|
|
|
return p_lhs < p_rhs;
|
2022-05-13 15:04:37 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-15 11:09:01 -05:00
|
|
|
// Pair is zero-constructible if and only if both constrained types are zero-constructible.
|
|
|
|
|
template <typename F, typename S>
|
|
|
|
|
struct is_zero_constructible<Pair<F, S>> : std::conjunction<is_zero_constructible<F>, is_zero_constructible<S>> {};
|
|
|
|
|
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 12:31:19 -03:00
|
|
|
template <typename K, typename V>
|
|
|
|
|
struct KeyValue {
|
2024-08-31 10:22:57 -05:00
|
|
|
const K key{};
|
|
|
|
|
V value{};
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 12:31:19 -03:00
|
|
|
|
2024-08-31 10:22:57 -05:00
|
|
|
KeyValue &operator=(const KeyValue &p_kv) = delete;
|
|
|
|
|
KeyValue &operator=(KeyValue &&p_kv) = delete;
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 12:31:19 -03:00
|
|
|
|
2024-08-31 10:22:57 -05:00
|
|
|
constexpr KeyValue(const KeyValue &p_kv) = default;
|
|
|
|
|
constexpr KeyValue(KeyValue &&p_kv) = default;
|
|
|
|
|
constexpr KeyValue(const K &p_key, const V &p_value) :
|
|
|
|
|
key(p_key), value(p_value) {}
|
|
|
|
|
constexpr KeyValue(const Pair<K, V> &p_pair) :
|
|
|
|
|
key(p_pair.first), value(p_pair.second) {}
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 12:31:19 -03:00
|
|
|
|
2024-08-31 10:22:57 -05:00
|
|
|
constexpr bool operator==(const KeyValue &p_other) const { return key == p_other.key && value == p_other.value; }
|
|
|
|
|
constexpr bool operator!=(const KeyValue &p_other) const { return key != p_other.key || value != p_other.value; }
|
|
|
|
|
constexpr bool operator<(const KeyValue &p_other) const { return key == p_other.key ? (value < p_other.value) : (key < p_other.key); }
|
|
|
|
|
constexpr bool operator<=(const KeyValue &p_other) const { return key == p_other.key ? (value <= p_other.value) : (key < p_other.key); }
|
|
|
|
|
constexpr bool operator>(const KeyValue &p_other) const { return key == p_other.key ? (value > p_other.value) : (key > p_other.key); }
|
|
|
|
|
constexpr bool operator>=(const KeyValue &p_other) const { return key == p_other.key ? (value >= p_other.value) : (key > p_other.key); }
|
|
|
|
|
};
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 12:31:19 -03:00
|
|
|
|
|
|
|
|
template <typename K, typename V>
|
|
|
|
|
struct KeyValueSort {
|
2024-08-31 10:22:57 -05:00
|
|
|
constexpr bool operator()(const KeyValue<K, V> &p_lhs, const KeyValue<K, V> &p_rhs) const {
|
|
|
|
|
return p_lhs.key < p_rhs.key;
|
Implement Range Iterators
This PR implements range iterators in the base containers (Vector, Map, List, Pair Set).
Given several of these data structures will be replaced by more efficient versions, having a common iterator API will make this simpler.
Iterating can be done as follows (examples):
```C++
//Vector<String>
for(const String& I: vector) {
}
//List<String>
for(const String& I: list) {
}
//Map<String,int>
for(const KeyValue<String,int>&I : map) {
print_line("key: "+I.key+" value: "+itos(I.value));
}
//if intending to write the elements, reference can be used
//Map<String,int>
for(KeyValue<String,int>& I: map) {
I.value = 25;
//this will fail because key is always const
//I.key = "hello"
}
```
The containers are (for now) not STL compatible, since this would mean changing how they work internally (STL uses a special head/tail allocation for end(), while Godot Map/Set/List do not).
The idea is to change the Godot versions to be more compatible with STL, but this will happen after conversion to new iterators have taken place.
2021-07-08 12:31:19 -03:00
|
|
|
}
|
|
|
|
|
};
|
2025-03-15 11:09:01 -05:00
|
|
|
|
|
|
|
|
// KeyValue is zero-constructible if and only if both constrained types are zero-constructible.
|
|
|
|
|
template <typename K, typename V>
|
|
|
|
|
struct is_zero_constructible<KeyValue<K, V>> : std::conjunction<is_zero_constructible<K>, is_zero_constructible<V>> {};
|