2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* list.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifndef GLOBALS_LIST_H
|
|
|
|
#define GLOBALS_LIST_H
|
|
|
|
|
2019-10-08 08:17:11 +00:00
|
|
|
#include "core/error_macros.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/memory.h"
|
2019-02-12 12:30:56 +00:00
|
|
|
#include "core/sort_array.h"
|
2014-02-10 01:10:30 +00: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.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T, class A = DefaultAllocator>
|
2014-02-10 01:10:30 +00:00
|
|
|
class List {
|
|
|
|
struct _Data;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2014-02-10 01:10:30 +00:00
|
|
|
class Element {
|
|
|
|
|
|
|
|
private:
|
2017-03-05 15:44:50 +00:00
|
|
|
friend class List<T, A>;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
T value;
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *next_ptr;
|
|
|
|
Element *prev_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
_Data *data;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
|
|
|
* Get NEXT Element iterator, for constant lists.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const Element *next() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return next_ptr;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Get NEXT Element iterator,
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ Element *next() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return next_ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get PREV Element iterator, for constant lists.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const Element *prev() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return prev_ptr;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Get PREV Element iterator,
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ Element *prev() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return prev_ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* * operator, for using as *iterator, when iterators are defined on stack.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const T &operator*() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const T *operator->() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return &value;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* * operator, for using as *iterator, when iterators are defined on stack,
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ T &operator*() {
|
2014-02-10 01:10:30 +00:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ T *operator->() {
|
2014-02-10 01:10:30 +00:00
|
|
|
return &value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the value stored in this element.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ T &get() {
|
2014-02-10 01:10:30 +00:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* get the value stored in this element, for constant lists
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const T &get() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return value;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* set the value stored in this element.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set(const T &p_value) {
|
|
|
|
value = (T &)p_value;
|
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void erase() {
|
|
|
|
|
|
|
|
data->erase(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Element() {
|
|
|
|
next_ptr = 0;
|
|
|
|
prev_ptr = 0;
|
2017-03-05 15:44:50 +00:00
|
|
|
data = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct _Data {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *first;
|
|
|
|
Element *last;
|
2014-02-10 01:10:30 +00:00
|
|
|
int size_cache;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool erase(const Element *p_I) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!p_I, false);
|
|
|
|
ERR_FAIL_COND_V(p_I->data != this, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (first == p_I) {
|
|
|
|
first = p_I->next_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (last == p_I)
|
|
|
|
last = p_I->prev_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_I->prev_ptr)
|
2017-03-05 15:44:50 +00:00
|
|
|
p_I->prev_ptr->next_ptr = p_I->next_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_I->next_ptr)
|
2017-03-05 15:44:50 +00:00
|
|
|
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete_allocator<Element, A>(const_cast<Element *>(p_I));
|
2014-02-10 01:10:30 +00:00
|
|
|
size_cache--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_Data *_data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
2017-09-02 14:19:06 +00:00
|
|
|
* return a const iterator to the beginning of the list.
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const Element *front() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return _data ? _data->first : 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-24 20:45:31 +00:00
|
|
|
* return an iterator to the beginning of the list.
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ Element *front() {
|
|
|
|
return _data ? _data->first : 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-09-02 14:19:06 +00:00
|
|
|
* return a const iterator to the last member of the list.
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const Element *back() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return _data ? _data->last : 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return an iterator to the last member of the list.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ Element *back() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return _data ? _data->last : 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store a new element at the end of the list
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *push_back(const T &value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!_data) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data = memnew_allocator(_Data, A);
|
|
|
|
_data->first = NULL;
|
|
|
|
_data->last = NULL;
|
|
|
|
_data->size_cache = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *n = memnew_allocator(Element, A);
|
|
|
|
n->value = (T &)value;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
n->prev_ptr = _data->last;
|
|
|
|
n->next_ptr = 0;
|
|
|
|
n->data = _data;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (_data->last) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->last->next_ptr = n;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_data->last = n;
|
|
|
|
|
|
|
|
if (!_data->first)
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->first = n;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_data->size_cache++;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return n;
|
|
|
|
};
|
|
|
|
|
|
|
|
void pop_back() {
|
|
|
|
|
|
|
|
if (_data && _data->last)
|
|
|
|
erase(_data->last);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-24 20:45:31 +00:00
|
|
|
* store a new element at the beginning of the list
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *push_front(const T &value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!_data) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data = memnew_allocator(_Data, A);
|
|
|
|
_data->first = NULL;
|
|
|
|
_data->last = NULL;
|
|
|
|
_data->size_cache = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *n = memnew_allocator(Element, A);
|
|
|
|
n->value = (T &)value;
|
2014-02-10 01:10:30 +00:00
|
|
|
n->prev_ptr = 0;
|
|
|
|
n->next_ptr = _data->first;
|
2017-03-05 15:44:50 +00:00
|
|
|
n->data = _data;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (_data->first) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->first->prev_ptr = n;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_data->first = n;
|
|
|
|
|
|
|
|
if (!_data->last)
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->last = n;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_data->size_cache++;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return n;
|
|
|
|
};
|
|
|
|
|
|
|
|
void pop_front() {
|
|
|
|
|
|
|
|
if (_data && _data->first)
|
|
|
|
erase(_data->first);
|
|
|
|
}
|
|
|
|
|
2017-08-13 08:37:56 +00: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 16:51:38 +00:00
|
|
|
} else {
|
|
|
|
p_element->next_ptr->prev_ptr = n;
|
2017-08-13 08:37:56 +00: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 16:51:38 +00:00
|
|
|
} else {
|
|
|
|
p_element->prev_ptr->next_ptr = n;
|
2017-08-13 08:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_element->prev_ptr = n;
|
|
|
|
|
|
|
|
_data->size_cache++;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
|
|
|
* find an element in the list,
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class T_v>
|
|
|
|
Element *find(const T_v &p_val) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *it = front();
|
2014-02-10 01:10:30 +00:00
|
|
|
while (it) {
|
|
|
|
if (it->value == p_val) return it;
|
|
|
|
it = it->next();
|
|
|
|
};
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* erase an element in the list, by iterator pointing to it. Return true if it was found/erased.
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
bool erase(const Element *p_I) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (_data) {
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ret = _data->erase(p_I);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_data->size_cache == 0) {
|
|
|
|
memdelete_allocator<_Data, A>(_data);
|
|
|
|
_data = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* erase the first element in the list, that contains value
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
bool erase(const T &value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *I = find(value);
|
2014-02-10 01:10:30 +00:00
|
|
|
return erase(I);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-01-18 20:37:17 +00:00
|
|
|
* return whether the list is empty
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ bool empty() const {
|
|
|
|
|
|
|
|
return (!_data || !_data->size_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clear the list
|
|
|
|
*/
|
|
|
|
void clear() {
|
|
|
|
|
|
|
|
while (front()) {
|
|
|
|
erase(front());
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ int size() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return _data ? _data->size_cache : 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void swap(Element *p_A, Element *p_B) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!p_A || !p_B);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_A->data != _data);
|
|
|
|
ERR_FAIL_COND(p_B->data != _data);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-09-12 20:58:56 +00:00
|
|
|
if (p_A == p_B) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *A_prev = p_A->prev_ptr;
|
|
|
|
Element *A_next = p_A->next_ptr;
|
2020-09-12 20:58:56 +00:00
|
|
|
Element *B_prev = p_B->prev_ptr;
|
|
|
|
Element *B_next = p_B->next_ptr;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-09-12 20:58:56 +00:00
|
|
|
if (A_prev) {
|
|
|
|
A_prev->next_ptr = p_B;
|
|
|
|
} else {
|
|
|
|
_data->first = p_B;
|
|
|
|
}
|
|
|
|
if (B_prev) {
|
|
|
|
B_prev->next_ptr = p_A;
|
|
|
|
} else {
|
|
|
|
_data->first = p_A;
|
|
|
|
}
|
|
|
|
if (A_next) {
|
|
|
|
A_next->prev_ptr = p_B;
|
|
|
|
} else {
|
|
|
|
_data->last = p_B;
|
|
|
|
}
|
|
|
|
if (B_next) {
|
|
|
|
B_next->prev_ptr = p_A;
|
|
|
|
} else {
|
|
|
|
_data->last = p_A;
|
|
|
|
}
|
|
|
|
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 01:10:30 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* copy the list
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
void operator=(const List &p_list) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
clear();
|
2017-03-05 15:44:50 +00:00
|
|
|
const Element *it = p_list.front();
|
2014-02-10 01:10:30 +00:00
|
|
|
while (it) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
push_back(it->get());
|
|
|
|
it = it->next();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
T &operator[](int p_index) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-26 19:11:16 +00:00
|
|
|
CRASH_BAD_INDEX(p_index, size());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *I = front();
|
|
|
|
int c = 0;
|
|
|
|
while (I) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (c == p_index) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return I->get();
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
I = I->next();
|
2014-02-10 01:10:30 +00:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2017-05-26 19:11:16 +00:00
|
|
|
CRASH_NOW(); // bug!!
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const T &operator[](int p_index) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-26 19:11:16 +00:00
|
|
|
CRASH_BAD_INDEX(p_index, size());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const Element *I = front();
|
|
|
|
int c = 0;
|
|
|
|
while (I) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (c == p_index) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return I->get();
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
I = I->next();
|
2014-02-10 01:10:30 +00:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2017-05-26 19:11:16 +00:00
|
|
|
CRASH_NOW(); // bug!!
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void move_to_back(Element *p_I) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_I->data != _data);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!p_I->next_ptr)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_data->first == p_I) {
|
|
|
|
_data->first = p_I->next_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_data->last == p_I)
|
|
|
|
_data->last = p_I->prev_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_I->prev_ptr)
|
2017-03-05 15:44:50 +00:00
|
|
|
p_I->prev_ptr->next_ptr = p_I->next_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-04-04 20:00:16 +00:00
|
|
|
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->last->next_ptr = p_I;
|
|
|
|
p_I->prev_ptr = _data->last;
|
|
|
|
p_I->next_ptr = NULL;
|
|
|
|
_data->last = p_I;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void invert() {
|
|
|
|
|
|
|
|
int s = size() / 2;
|
|
|
|
Element *F = front();
|
|
|
|
Element *B = back();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < s; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(F->value, B->value);
|
|
|
|
F = F->next();
|
|
|
|
B = B->prev();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void move_to_front(Element *p_I) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_I->data != _data);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!p_I->prev_ptr)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_data->first == p_I) {
|
|
|
|
_data->first = p_I->next_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_data->last == p_I)
|
|
|
|
_data->last = p_I->prev_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-04-04 20:00:16 +00:00
|
|
|
p_I->prev_ptr->next_ptr = p_I->next_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_I->next_ptr)
|
2017-03-05 15:44:50 +00:00
|
|
|
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->first->prev_ptr = p_I;
|
|
|
|
p_I->next_ptr = _data->first;
|
|
|
|
p_I->prev_ptr = NULL;
|
|
|
|
_data->first = p_I;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void move_before(Element *value, Element *where) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (value->prev_ptr) {
|
|
|
|
value->prev_ptr->next_ptr = value->next_ptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2015-07-10 19:33:44 +00:00
|
|
|
_data->first = value->next_ptr;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
if (value->next_ptr) {
|
|
|
|
value->next_ptr->prev_ptr = value->prev_ptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2015-07-10 19:33:44 +00:00
|
|
|
_data->last = value->prev_ptr;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
value->next_ptr = where;
|
|
|
|
if (!where) {
|
|
|
|
value->prev_ptr = _data->last;
|
|
|
|
_data->last = value;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
value->prev_ptr = where->prev_ptr;
|
|
|
|
|
|
|
|
if (where->prev_ptr) {
|
|
|
|
where->prev_ptr->next_ptr = value;
|
|
|
|
} else {
|
|
|
|
_data->first = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
where->prev_ptr = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* simple insertion sort
|
|
|
|
*/
|
|
|
|
|
|
|
|
void sort() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
sort_custom<Comparator<T> >();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class C>
|
2015-02-15 15:38:25 +00:00
|
|
|
void sort_custom_inplace() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (size() < 2)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *from = front();
|
|
|
|
Element *current = from;
|
|
|
|
Element *to = from;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (current) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *next = current->next_ptr;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (from != current) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
current->prev_ptr = NULL;
|
|
|
|
current->next_ptr = from;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element *find = from;
|
2014-02-10 01:10:30 +00:00
|
|
|
C less;
|
2017-03-05 15:44:50 +00:00
|
|
|
while (find && less(find->value, current->value)) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
current->prev_ptr = find;
|
|
|
|
current->next_ptr = find->next_ptr;
|
|
|
|
find = find->next_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (current->prev_ptr)
|
2017-03-05 15:44:50 +00:00
|
|
|
current->prev_ptr->next_ptr = current;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
from = current;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (current->next_ptr)
|
2017-03-05 15:44:50 +00:00
|
|
|
current->next_ptr->prev_ptr = current;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
to = current;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
current->prev_ptr = NULL;
|
|
|
|
current->next_ptr = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
current = next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->first = from;
|
|
|
|
_data->last = to;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class C>
|
2015-02-15 15:38:25 +00:00
|
|
|
struct AuxiliaryComparator {
|
|
|
|
|
|
|
|
C compare;
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ bool operator()(const Element *a, const Element *b) const {
|
2015-02-15 15:38:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return compare(a->value, b->value);
|
2015-02-15 15:38:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class C>
|
2015-02-15 15:38:25 +00:00
|
|
|
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();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (s < 2)
|
2015-02-15 15:38:25 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Element **aux_buffer = memnew_arr(Element *, s);
|
2015-02-15 15:38:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = 0;
|
|
|
|
for (Element *E = front(); E; E = E->next_ptr) {
|
2015-02-15 15:38:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
aux_buffer[idx] = E;
|
2015-02-15 15:38:25 +00:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SortArray<Element *, AuxiliaryComparator<C> > sort;
|
|
|
|
sort.sort(aux_buffer, s);
|
2015-02-15 15:38:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->first = aux_buffer[0];
|
|
|
|
aux_buffer[0]->prev_ptr = NULL;
|
|
|
|
aux_buffer[0]->next_ptr = aux_buffer[1];
|
2015-02-15 15:38:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data->last = aux_buffer[s - 1];
|
|
|
|
aux_buffer[s - 1]->prev_ptr = aux_buffer[s - 2];
|
|
|
|
aux_buffer[s - 1]->next_ptr = NULL;
|
2015-02-15 15:38:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 1; i < s - 1; i++) {
|
2015-02-15 15:38:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
aux_buffer[i]->prev_ptr = aux_buffer[i - 1];
|
|
|
|
aux_buffer[i]->next_ptr = aux_buffer[i + 1];
|
2015-02-15 15:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memdelete_arr(aux_buffer);
|
|
|
|
}
|
|
|
|
|
2019-04-19 23:57:29 +00:00
|
|
|
const void *id() const {
|
|
|
|
return (void *)_data;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
|
|
|
* copy constructor for the list
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
List(const List &p_list) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_data = NULL;
|
|
|
|
const Element *it = p_list.front();
|
2014-02-10 01:10:30 +00:00
|
|
|
while (it) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
push_back(it->get());
|
|
|
|
it = it->next();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List() {
|
2017-03-05 15:44:50 +00:00
|
|
|
_data = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
~List() {
|
|
|
|
clear();
|
|
|
|
if (_data) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(_data->size_cache);
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete_allocator<_Data, A>(_data);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|