2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* list.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
|
|
|
|
2020-03-25 11:10:34 +01:00
|
|
|
#ifndef LIST_H
|
|
|
|
#define LIST_H
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/error/error_macros.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/memory.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/templates/sort_array.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic Templatized Linked List Implementation.
|
|
|
|
* The implementation differs from the STL one because
|
|
|
|
* a compatible preallocated linked list can be written
|
|
|
|
* using the same API, or features such as erasing an element
|
|
|
|
* from the iterator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <class T, class A = DefaultAllocator>
|
|
|
|
class List {
|
|
|
|
struct _Data;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
2014-02-10 02:10:30 +01:00
|
|
|
class Element {
|
|
|
|
private:
|
|
|
|
friend class List<T, A>;
|
|
|
|
|
|
|
|
T value;
|
2020-05-12 17:01:17 +02:00
|
|
|
Element *next_ptr = nullptr;
|
|
|
|
Element *prev_ptr = nullptr;
|
|
|
|
_Data *data = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* Get NEXT Element iterator, for constant lists.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ const Element *next() const {
|
|
|
|
return next_ptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* Get NEXT Element iterator,
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ Element *next() {
|
|
|
|
return next_ptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get PREV Element iterator, for constant lists.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ const Element *prev() const {
|
|
|
|
return prev_ptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* Get PREV Element iterator,
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ Element *prev() {
|
|
|
|
return prev_ptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* * operator, for using as *iterator, when iterators are defined on stack.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ const T &operator*() const {
|
|
|
|
return value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ const T *operator->() const {
|
|
|
|
return &value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* * operator, for using as *iterator, when iterators are defined on stack,
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ T &operator*() {
|
|
|
|
return value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ T *operator->() {
|
|
|
|
return &value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the value stored in this element.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ T &get() {
|
|
|
|
return value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* get the value stored in this element, for constant lists
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ const T &get() const {
|
|
|
|
return value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* set the value stored in this element.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ void set(const T &p_value) {
|
|
|
|
value = (T &)p_value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void erase() {
|
|
|
|
data->erase(this);
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
_FORCE_INLINE_ Element() {}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
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 17:31:19 +02:00
|
|
|
typedef T ValueType;
|
|
|
|
|
|
|
|
struct Iterator {
|
|
|
|
_FORCE_INLINE_ T &operator*() const {
|
|
|
|
return E->get();
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ T *operator->() const { return &E->get(); }
|
|
|
|
_FORCE_INLINE_ Iterator &operator++() {
|
|
|
|
E = E->next();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator &operator--() {
|
|
|
|
E = E->prev();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
|
|
|
|
|
|
|
|
Iterator(Element *p_E) { E = p_E; }
|
|
|
|
Iterator() {}
|
|
|
|
Iterator(const Iterator &p_it) { E = p_it.E; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Element *E = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstIterator {
|
|
|
|
_FORCE_INLINE_ const T &operator*() const {
|
|
|
|
return E->get();
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const T *operator->() const { return &E->get(); }
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator++() {
|
|
|
|
E = E->next();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator--() {
|
|
|
|
E = E->prev();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-07-15 10:47:20 +02:00
|
|
|
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
|
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 17:31:19 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
|
|
|
|
_FORCE_INLINE_ ConstIterator() {}
|
|
|
|
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Element *E = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Iterator begin() {
|
|
|
|
return Iterator(front());
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator end() {
|
|
|
|
return Iterator(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
//to use when replacing find()
|
|
|
|
_FORCE_INLINE_ Iterator find(const K &p_key) {
|
|
|
|
return Iterator(find(p_key));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
_FORCE_INLINE_ ConstIterator begin() const {
|
|
|
|
return ConstIterator(front());
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator end() const {
|
|
|
|
return ConstIterator(nullptr);
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
//to use when replacing find()
|
|
|
|
_FORCE_INLINE_ ConstIterator find(const K &p_key) const {
|
|
|
|
return ConstIterator(find(p_key));
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
private:
|
|
|
|
struct _Data {
|
2020-11-23 17:38:46 +01:00
|
|
|
Element *first = nullptr;
|
|
|
|
Element *last = nullptr;
|
|
|
|
int size_cache = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
bool erase(const Element *p_I) {
|
|
|
|
ERR_FAIL_COND_V(!p_I, false);
|
|
|
|
ERR_FAIL_COND_V(p_I->data != this, false);
|
|
|
|
|
|
|
|
if (first == p_I) {
|
|
|
|
first = p_I->next_ptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (last == p_I) {
|
2014-02-10 02:10:30 +01:00
|
|
|
last = p_I->prev_ptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_I->prev_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_I->prev_ptr->next_ptr = p_I->next_ptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_I->next_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
memdelete_allocator<Element, A>(const_cast<Element *>(p_I));
|
|
|
|
size_cache--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
_Data *_data = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
2021-10-28 15:43:36 +02:00
|
|
|
* return a const iterator to the beginning of the list.
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ const Element *front() const {
|
2020-05-14 10:15:48 +02:00
|
|
|
return _data ? _data->first : nullptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
2021-10-28 15:43:36 +02:00
|
|
|
* return an iterator to the beginning of the list.
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ Element *front() {
|
2020-05-14 10:15:48 +02:00
|
|
|
return _data ? _data->first : nullptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
2021-10-28 15:43:36 +02:00
|
|
|
* return a const iterator to the last member of the list.
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ const Element *back() const {
|
2020-05-14 10:15:48 +02:00
|
|
|
return _data ? _data->last : nullptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
2021-10-28 15:43:36 +02:00
|
|
|
* return an iterator to the last member of the list.
|
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ Element *back() {
|
2020-05-14 10:15:48 +02:00
|
|
|
return _data ? _data->last : nullptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* store a new element at the end of the list
|
|
|
|
*/
|
|
|
|
Element *push_back(const T &value) {
|
|
|
|
if (!_data) {
|
|
|
|
_data = memnew_allocator(_Data, A);
|
2020-04-02 01:20:12 +02:00
|
|
|
_data->first = nullptr;
|
|
|
|
_data->last = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->size_cache = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element *n = memnew_allocator(Element, A);
|
|
|
|
n->value = (T &)value;
|
|
|
|
|
|
|
|
n->prev_ptr = _data->last;
|
2020-05-14 10:15:48 +02:00
|
|
|
n->next_ptr = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
n->data = _data;
|
|
|
|
|
|
|
|
if (_data->last) {
|
|
|
|
_data->last->next_ptr = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_data->last = n;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data->first) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->first = n;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_data->size_cache++;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return n;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void pop_back() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_data && _data->last) {
|
2014-02-10 02:10:30 +01:00
|
|
|
erase(_data->last);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-24 21:45:31 +01:00
|
|
|
* store a new element at the beginning of the list
|
2014-02-10 02:10:30 +01:00
|
|
|
*/
|
|
|
|
Element *push_front(const T &value) {
|
|
|
|
if (!_data) {
|
|
|
|
_data = memnew_allocator(_Data, A);
|
2020-04-02 01:20:12 +02:00
|
|
|
_data->first = nullptr;
|
|
|
|
_data->last = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->size_cache = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element *n = memnew_allocator(Element, A);
|
|
|
|
n->value = (T &)value;
|
2020-05-14 10:15:48 +02:00
|
|
|
n->prev_ptr = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
n->next_ptr = _data->first;
|
|
|
|
n->data = _data;
|
|
|
|
|
|
|
|
if (_data->first) {
|
|
|
|
_data->first->prev_ptr = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_data->first = n;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!_data->last) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->last = n;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_data->size_cache++;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return n;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void pop_front() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_data && _data->first) {
|
2014-02-10 02:10:30 +01:00
|
|
|
erase(_data->first);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 10:37:56 +02:00
|
|
|
Element *insert_after(Element *p_element, const T &p_value) {
|
|
|
|
CRASH_COND(p_element && (!_data || p_element->data != _data));
|
|
|
|
|
|
|
|
if (!p_element) {
|
|
|
|
return push_back(p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Element *n = memnew_allocator(Element, A);
|
|
|
|
n->value = (T &)p_value;
|
|
|
|
n->prev_ptr = p_element;
|
|
|
|
n->next_ptr = p_element->next_ptr;
|
|
|
|
n->data = _data;
|
|
|
|
|
|
|
|
if (!p_element->next_ptr) {
|
|
|
|
_data->last = n;
|
2017-11-21 17:51:38 +01:00
|
|
|
} else {
|
|
|
|
p_element->next_ptr->prev_ptr = n;
|
2017-08-13 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p_element->next_ptr = n;
|
|
|
|
|
|
|
|
_data->size_cache++;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element *insert_before(Element *p_element, const T &p_value) {
|
|
|
|
CRASH_COND(p_element && (!_data || p_element->data != _data));
|
|
|
|
|
|
|
|
if (!p_element) {
|
|
|
|
return push_back(p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Element *n = memnew_allocator(Element, A);
|
|
|
|
n->value = (T &)p_value;
|
|
|
|
n->prev_ptr = p_element->prev_ptr;
|
|
|
|
n->next_ptr = p_element;
|
|
|
|
n->data = _data;
|
|
|
|
|
|
|
|
if (!p_element->prev_ptr) {
|
|
|
|
_data->first = n;
|
2017-11-21 17:51:38 +01:00
|
|
|
} else {
|
|
|
|
p_element->prev_ptr->next_ptr = n;
|
2017-08-13 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p_element->prev_ptr = n;
|
|
|
|
|
|
|
|
_data->size_cache++;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* find an element in the list,
|
|
|
|
*/
|
|
|
|
template <class T_v>
|
|
|
|
Element *find(const T_v &p_val) {
|
|
|
|
Element *it = front();
|
|
|
|
while (it) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (it->value == p_val) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return it;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
it = it->next();
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* erase an element in the list, by iterator pointing to it. Return true if it was found/erased.
|
|
|
|
*/
|
|
|
|
bool erase(const Element *p_I) {
|
2019-11-30 20:20:01 +01:00
|
|
|
if (_data && p_I) {
|
2014-02-10 02:10:30 +01:00
|
|
|
bool ret = _data->erase(p_I);
|
|
|
|
|
|
|
|
if (_data->size_cache == 0) {
|
|
|
|
memdelete_allocator<_Data, A>(_data);
|
2020-04-02 01:20:12 +02:00
|
|
|
_data = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* erase the first element in the list, that contains value
|
|
|
|
*/
|
|
|
|
bool erase(const T &value) {
|
|
|
|
Element *I = find(value);
|
|
|
|
return erase(I);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-18 21:37:17 +01:00
|
|
|
* return whether the list is empty
|
2014-02-10 02:10:30 +01:00
|
|
|
*/
|
2020-12-15 13:04:21 +01:00
|
|
|
_FORCE_INLINE_ bool is_empty() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return (!_data || !_data->size_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clear the list
|
|
|
|
*/
|
|
|
|
void clear() {
|
|
|
|
while (front()) {
|
|
|
|
erase(front());
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ int size() const {
|
|
|
|
return _data ? _data->size_cache : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(Element *p_A, Element *p_B) {
|
|
|
|
ERR_FAIL_COND(!p_A || !p_B);
|
|
|
|
ERR_FAIL_COND(p_A->data != _data);
|
|
|
|
ERR_FAIL_COND(p_B->data != _data);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-09-12 22:58:56 +02:00
|
|
|
if (p_A == p_B) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *A_prev = p_A->prev_ptr;
|
|
|
|
Element *A_next = p_A->next_ptr;
|
2020-09-12 22:58:56 +02:00
|
|
|
Element *B_prev = p_B->prev_ptr;
|
|
|
|
Element *B_next = p_B->next_ptr;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-09-12 22:58:56 +02:00
|
|
|
if (A_prev) {
|
|
|
|
A_prev->next_ptr = p_B;
|
|
|
|
} else {
|
|
|
|
_data->first = p_B;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-09-12 22:58:56 +02:00
|
|
|
if (B_prev) {
|
|
|
|
B_prev->next_ptr = p_A;
|
|
|
|
} else {
|
|
|
|
_data->first = p_A;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-09-12 22:58:56 +02:00
|
|
|
if (A_next) {
|
|
|
|
A_next->prev_ptr = p_B;
|
|
|
|
} else {
|
|
|
|
_data->last = p_B;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-09-12 22:58:56 +02:00
|
|
|
if (B_next) {
|
|
|
|
B_next->prev_ptr = p_A;
|
|
|
|
} else {
|
|
|
|
_data->last = p_A;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-09-12 22:58:56 +02:00
|
|
|
p_A->prev_ptr = A_next == p_B ? p_B : B_prev;
|
|
|
|
p_A->next_ptr = B_next == p_A ? p_B : B_next;
|
|
|
|
p_B->prev_ptr = B_next == p_A ? p_A : A_prev;
|
|
|
|
p_B->next_ptr = A_next == p_B ? p_A : A_next;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* copy the list
|
|
|
|
*/
|
|
|
|
void operator=(const List &p_list) {
|
|
|
|
clear();
|
|
|
|
const Element *it = p_list.front();
|
|
|
|
while (it) {
|
|
|
|
push_back(it->get());
|
|
|
|
it = it->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T &operator[](int p_index) {
|
2017-05-26 21:11:16 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, size());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Element *I = front();
|
|
|
|
int c = 0;
|
2020-02-11 07:53:33 +01:00
|
|
|
while (c < p_index) {
|
2014-02-10 02:10:30 +01:00
|
|
|
I = I->next();
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:53:33 +01:00
|
|
|
return I->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const T &operator[](int p_index) const {
|
2017-05-26 21:11:16 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, size());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
const Element *I = front();
|
|
|
|
int c = 0;
|
2020-02-11 07:53:33 +01:00
|
|
|
while (c < p_index) {
|
2014-02-10 02:10:30 +01:00
|
|
|
I = I->next();
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:53:33 +01:00
|
|
|
return I->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void move_to_back(Element *p_I) {
|
|
|
|
ERR_FAIL_COND(p_I->data != _data);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!p_I->next_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (_data->first == p_I) {
|
|
|
|
_data->first = p_I->next_ptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_data->last == p_I) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->last = p_I->prev_ptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_I->prev_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_I->prev_ptr->next_ptr = p_I->next_ptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-04-04 22:00:16 +02:00
|
|
|
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_data->last->next_ptr = p_I;
|
|
|
|
p_I->prev_ptr = _data->last;
|
2020-04-02 01:20:12 +02:00
|
|
|
p_I->next_ptr = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->last = p_I;
|
|
|
|
}
|
|
|
|
|
2021-03-14 08:21:32 +01:00
|
|
|
void reverse() {
|
2014-02-10 02:10:30 +01:00
|
|
|
int s = size() / 2;
|
|
|
|
Element *F = front();
|
|
|
|
Element *B = back();
|
|
|
|
for (int i = 0; i < s; i++) {
|
|
|
|
SWAP(F->value, B->value);
|
|
|
|
F = F->next();
|
|
|
|
B = B->prev();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void move_to_front(Element *p_I) {
|
|
|
|
ERR_FAIL_COND(p_I->data != _data);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!p_I->prev_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (_data->first == p_I) {
|
|
|
|
_data->first = p_I->next_ptr;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_data->last == p_I) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->last = p_I->prev_ptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-04-04 22:00:16 +02:00
|
|
|
p_I->prev_ptr->next_ptr = p_I->next_ptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_I->next_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_data->first->prev_ptr = p_I;
|
|
|
|
p_I->next_ptr = _data->first;
|
2020-04-02 01:20:12 +02:00
|
|
|
p_I->prev_ptr = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
_data->first = p_I;
|
|
|
|
}
|
|
|
|
|
|
|
|
void move_before(Element *value, Element *where) {
|
|
|
|
if (value->prev_ptr) {
|
|
|
|
value->prev_ptr->next_ptr = value->next_ptr;
|
2015-07-10 21:33:44 +02:00
|
|
|
} else {
|
|
|
|
_data->first = value->next_ptr;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
if (value->next_ptr) {
|
|
|
|
value->next_ptr->prev_ptr = value->prev_ptr;
|
2015-07-10 21:33:44 +02:00
|
|
|
} else {
|
|
|
|
_data->last = value->prev_ptr;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
value->next_ptr = where;
|
|
|
|
if (!where) {
|
|
|
|
value->prev_ptr = _data->last;
|
|
|
|
_data->last = value;
|
|
|
|
return;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
value->prev_ptr = where->prev_ptr;
|
|
|
|
|
|
|
|
if (where->prev_ptr) {
|
|
|
|
where->prev_ptr->next_ptr = value;
|
|
|
|
} else {
|
|
|
|
_data->first = value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
where->prev_ptr = value;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* simple insertion sort
|
|
|
|
*/
|
|
|
|
|
|
|
|
void sort() {
|
2020-03-17 07:33:00 +01:00
|
|
|
sort_custom<Comparator<T>>();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
template <class C>
|
2015-02-15 16:38:25 +01:00
|
|
|
void sort_custom_inplace() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (size() < 2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Element *from = front();
|
|
|
|
Element *current = from;
|
|
|
|
Element *to = from;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
while (current) {
|
|
|
|
Element *next = current->next_ptr;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (from != current) {
|
2020-04-02 01:20:12 +02:00
|
|
|
current->prev_ptr = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
current->next_ptr = from;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Element *find = from;
|
|
|
|
C less;
|
|
|
|
while (find && less(find->value, current->value)) {
|
|
|
|
current->prev_ptr = find;
|
|
|
|
current->next_ptr = find->next_ptr;
|
|
|
|
find = find->next_ptr;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (current->prev_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
current->prev_ptr->next_ptr = current;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
from = current;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (current->next_ptr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
current->next_ptr->prev_ptr = current;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
to = current;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2020-04-02 01:20:12 +02:00
|
|
|
current->prev_ptr = nullptr;
|
|
|
|
current->next_ptr = nullptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
_data->first = from;
|
|
|
|
_data->last = to;
|
|
|
|
}
|
|
|
|
|
2015-02-15 16:38:25 +01:00
|
|
|
template <class C>
|
|
|
|
struct AuxiliaryComparator {
|
|
|
|
C compare;
|
2015-02-15 19:09:11 +01:00
|
|
|
_FORCE_INLINE_ bool operator()(const Element *a, const Element *b) const {
|
|
|
|
return compare(a->value, b->value);
|
2015-02-15 16:38:25 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class C>
|
|
|
|
void sort_custom() {
|
|
|
|
//this version uses auxiliary memory for speed.
|
|
|
|
//if you don't want to use auxiliary memory, use the in_place version
|
|
|
|
|
|
|
|
int s = size();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (s < 2) {
|
2015-02-15 16:38:25 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-02-15 16:38:25 +01:00
|
|
|
|
|
|
|
Element **aux_buffer = memnew_arr(Element *, s);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = 0;
|
2015-02-15 16:38:25 +01:00
|
|
|
for (Element *E = front(); E; E = E->next_ptr) {
|
|
|
|
aux_buffer[idx] = E;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2020-03-17 07:33:00 +01:00
|
|
|
SortArray<Element *, AuxiliaryComparator<C>> sort;
|
2015-02-15 16:38:25 +01:00
|
|
|
sort.sort(aux_buffer, s);
|
|
|
|
|
|
|
|
_data->first = aux_buffer[0];
|
2020-04-02 01:20:12 +02:00
|
|
|
aux_buffer[0]->prev_ptr = nullptr;
|
2015-02-15 16:38:25 +01:00
|
|
|
aux_buffer[0]->next_ptr = aux_buffer[1];
|
|
|
|
|
|
|
|
_data->last = aux_buffer[s - 1];
|
|
|
|
aux_buffer[s - 1]->prev_ptr = aux_buffer[s - 2];
|
2020-04-02 01:20:12 +02:00
|
|
|
aux_buffer[s - 1]->next_ptr = nullptr;
|
2015-02-15 16:38:25 +01:00
|
|
|
|
|
|
|
for (int i = 1; i < s - 1; i++) {
|
|
|
|
aux_buffer[i]->prev_ptr = aux_buffer[i - 1];
|
|
|
|
aux_buffer[i]->next_ptr = aux_buffer[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete_arr(aux_buffer);
|
|
|
|
}
|
|
|
|
|
2019-04-20 01:57:29 +02:00
|
|
|
const void *id() const {
|
|
|
|
return (void *)_data;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
/**
|
|
|
|
* copy constructor for the list
|
|
|
|
*/
|
|
|
|
List(const List &p_list) {
|
|
|
|
const Element *it = p_list.front();
|
|
|
|
while (it) {
|
|
|
|
push_back(it->get());
|
|
|
|
it = it->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
List() {}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
~List() {
|
|
|
|
clear();
|
|
|
|
if (_data) {
|
|
|
|
ERR_FAIL_COND(_data->size_cache);
|
|
|
|
memdelete_allocator<_Data, A>(_data);
|
|
|
|
}
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 11:10:34 +01:00
|
|
|
#endif // LIST_H
|