2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* vector.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef VECTOR_H
|
|
|
|
#define VECTOR_H
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Vector
|
|
|
|
* @author Juan Linietsky
|
2017-01-07 22:25:37 +01:00
|
|
|
* Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use PoolVector for large arrays.
|
2014-02-10 02:10:30 +01:00
|
|
|
*/
|
|
|
|
#include "error_macros.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "os/memory.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "safe_refcount.h"
|
|
|
|
#include "sort.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
class Vector {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
mutable T *_ptr;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
// internal helpers
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ uint32_t *_get_refcount() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!_ptr)
|
2017-03-05 16:44:50 +01:00
|
|
|
return NULL;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return reinterpret_cast<uint32_t *>(_ptr) - 2;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ uint32_t *_get_size() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!_ptr)
|
2017-01-06 14:15:44 +01:00
|
|
|
return NULL;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return reinterpret_cast<uint32_t *>(_ptr) - 1;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ T *_get_data() const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!_ptr)
|
2017-03-05 16:44:50 +01:00
|
|
|
return NULL;
|
|
|
|
return reinterpret_cast<T *>(_ptr);
|
|
|
|
}
|
2016-02-19 07:13:16 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const {
|
2016-04-11 08:34:10 +02:00
|
|
|
//return nearest_power_of_2_templated(p_elements*sizeof(T)+sizeof(SafeRefCount)+sizeof(int));
|
2017-08-17 23:35:55 +02:00
|
|
|
return next_power_of_2(p_elements * sizeof(T));
|
2016-02-19 07:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const {
|
|
|
|
#if defined(_add_overflow) && defined(_mul_overflow)
|
|
|
|
size_t o;
|
|
|
|
size_t p;
|
|
|
|
if (_mul_overflow(p_elements, sizeof(T), &o)) return false;
|
2017-08-17 23:35:55 +02:00
|
|
|
*out = next_power_of_2(o);
|
2017-01-31 03:46:30 +01:00
|
|
|
if (_add_overflow(o, static_cast<size_t>(32), &p)) return false; //no longer allocated here
|
2016-02-19 07:13:16 +01:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
// Speed is more important than correctness here, do the operations unchecked
|
|
|
|
// and hope the best
|
|
|
|
*out = _get_alloc_size(p_elements);
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void _unref(void *p_data);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void _copy_from(const Vector &p_from);
|
2014-02-10 02:10:30 +01:00
|
|
|
void _copy_on_write();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
|
|
|
_FORCE_INLINE_ T *ptr() {
|
|
|
|
if (!_ptr) return NULL;
|
|
|
|
_copy_on_write();
|
|
|
|
return (T *)_get_data();
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const T *ptr() const {
|
|
|
|
if (!_ptr) return NULL;
|
|
|
|
return _get_data();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void clear() { resize(0); }
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ int size() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *size = (uint32_t *)_get_size();
|
2015-12-04 05:43:48 +01:00
|
|
|
if (size)
|
|
|
|
return *size;
|
2016-03-09 00:00:52 +01:00
|
|
|
else
|
2015-12-04 05:43:48 +01:00
|
|
|
return 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ bool empty() const { return _ptr == 0; }
|
|
|
|
Error resize(int p_size);
|
2017-08-12 13:04:30 +02:00
|
|
|
bool push_back(const T &p_elem);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void remove(int p_index);
|
2017-03-05 16:44:50 +01:00
|
|
|
void erase(const T &p_val) {
|
|
|
|
int idx = find(p_val);
|
|
|
|
if (idx >= 0) remove(idx);
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
void invert();
|
|
|
|
|
|
|
|
template <class T_val>
|
2017-03-05 16:44:50 +01:00
|
|
|
int find(const T_val &p_val, int p_from = 0) const;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-12 13:04:30 +02:00
|
|
|
void set(int p_index, const T &p_elem);
|
2014-02-10 02:10:30 +01:00
|
|
|
T get(int p_index) const;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
inline T &operator[](int p_index) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-26 21:11:16 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, size());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_copy_on_write(); // wants to write, so copy on write.
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return _get_data()[p_index];
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
inline const T &operator[](int p_index) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-26 21:11:16 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, size());
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// no cow needed, since it's reading
|
|
|
|
return _get_data()[p_index];
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error insert(int p_pos, const T &p_val);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class C>
|
2014-02-10 02:10:30 +01:00
|
|
|
void sort_custom() {
|
|
|
|
|
|
|
|
int len = size();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
T *data = &operator[](0);
|
2017-03-05 16:44:50 +01:00
|
|
|
SortArray<T, C> sorter;
|
|
|
|
sorter.sort(data, len);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sort() {
|
|
|
|
|
|
|
|
sort_custom<_DefaultComparator<T> >();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ordered_insert(const T &p_val) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < size(); i++) {
|
2015-03-20 00:53:54 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_val < operator[](i)) {
|
|
|
|
break;
|
2015-03-20 00:53:54 +01:00
|
|
|
};
|
2017-03-05 16:44:50 +01:00
|
|
|
};
|
|
|
|
insert(i, p_val);
|
2015-03-20 00:53:54 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void operator=(const Vector &p_from);
|
|
|
|
Vector(const Vector &p_from);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector();
|
|
|
|
_FORCE_INLINE_ ~Vector();
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
void Vector<T>::_unref(void *p_data) {
|
|
|
|
|
|
|
|
if (!p_data)
|
|
|
|
return;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-01-06 14:15:44 +01:00
|
|
|
uint32_t *refc = _get_refcount();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (atomic_decrement(refc) > 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return; // still in use
|
|
|
|
// clean up
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-01-06 14:15:44 +01:00
|
|
|
uint32_t *count = _get_size();
|
2017-03-05 16:44:50 +01:00
|
|
|
T *data = (T *)(count + 1);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (uint32_t i = 0; i < *count; i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
// call destructors
|
2014-02-10 02:10:30 +01:00
|
|
|
data[i].~T();
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// free mem
|
2017-03-05 16:44:50 +01:00
|
|
|
Memory::free_static((uint8_t *)p_data, true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
void Vector<T>::_copy_on_write() {
|
|
|
|
|
|
|
|
if (!_ptr)
|
|
|
|
return;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-01-06 14:15:44 +01:00
|
|
|
uint32_t *refc = _get_refcount();
|
|
|
|
|
|
|
|
if (*refc > 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
/* in use by more than me */
|
2017-01-06 14:15:44 +01:00
|
|
|
uint32_t current_size = *_get_size();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*(mem_new - 2) = 1; //refcount
|
|
|
|
*(mem_new - 1) = current_size; //size
|
2017-01-06 14:15:44 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
T *_data = (T *)(mem_new);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// initialize new elements
|
2017-03-05 16:44:50 +01:00
|
|
|
for (uint32_t i = 0; i < current_size; i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
memnew_placement(&_data[i], T(_get_data()[i]));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_unref(_ptr);
|
2017-03-05 16:44:50 +01:00
|
|
|
_ptr = _data;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
template <class T_val>
|
2016-06-10 19:57:56 +02:00
|
|
|
int Vector<T>::find(const T_val &p_val, int p_from) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int ret = -1;
|
2016-06-10 19:57:56 +02:00
|
|
|
if (p_from < 0 || size() == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return ret;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = p_from; i < size(); i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (operator[](i) == p_val) {
|
|
|
|
ret = i;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2016-06-10 19:57:56 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
Error Vector<T>::resize(int p_size) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_size == size())
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_size == 0) {
|
2016-03-09 00:00:52 +01:00
|
|
|
// wants to clean up
|
2014-02-10 02:10:30 +01:00
|
|
|
_unref(_ptr);
|
2017-03-05 16:44:50 +01:00
|
|
|
_ptr = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// possibly changing size, copy on write
|
|
|
|
_copy_on_write();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-02-19 07:13:16 +01:00
|
|
|
size_t alloc_size;
|
|
|
|
ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_size > size()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (size() == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// alloc from scratch
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true);
|
|
|
|
ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY);
|
|
|
|
*(ptr - 1) = 0; //size, currently none
|
|
|
|
*(ptr - 2) = 1; //refcount
|
2017-01-06 14:15:44 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_ptr = (T *)ptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
|
|
|
|
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
|
|
|
|
_ptr = (T *)(_ptrnew);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// construct the newly created elements
|
2017-03-05 16:44:50 +01:00
|
|
|
T *elems = _get_data();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = *_get_size(); i < p_size; i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
memnew_placement(&elems[i], T);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*_get_size() = p_size;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p_size < size()) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// deinitialize no longer needed elements
|
2017-03-05 16:44:50 +01:00
|
|
|
for (uint32_t i = p_size; i < *_get_size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
T *t = &_get_data()[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
t->~T();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void *_ptrnew = (T *)Memory::realloc_static(_ptr, alloc_size, true);
|
|
|
|
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_ptr = (T *)(_ptrnew);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*_get_size() = p_size;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
void Vector<T>::invert() {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < size() / 2; i++) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
SWAP(operator[](i), operator[](size() - i - 1));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2017-08-12 13:04:30 +02:00
|
|
|
void Vector<T>::set(int p_index, const T &p_elem) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
operator[](p_index) = p_elem;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
T Vector<T>::get(int p_index) const {
|
|
|
|
|
|
|
|
return operator[](p_index);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2017-08-12 13:04:30 +02:00
|
|
|
bool Vector<T>::push_back(const T &p_elem) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error err = resize(size() + 1);
|
|
|
|
ERR_FAIL_COND_V(err, true)
|
|
|
|
set(size() - 1, p_elem);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
void Vector<T>::remove(int p_index) {
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX(p_index, size());
|
2017-03-05 16:44:50 +01:00
|
|
|
T *p = ptr();
|
|
|
|
int len = size();
|
|
|
|
for (int i = p_index; i < len - 1; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p[i] = p[i + 1];
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
resize(len - 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
void Vector<T>::_copy_from(const Vector &p_from) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (_ptr == p_from._ptr)
|
|
|
|
return; // self assign, do nothing.
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_unref(_ptr);
|
2017-03-05 16:44:50 +01:00
|
|
|
_ptr = NULL;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!p_from._ptr)
|
|
|
|
return; //nothing to do
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (atomic_conditional_increment(p_from._get_refcount()) > 0) { // could reference
|
|
|
|
_ptr = p_from._ptr;
|
2017-01-06 14:15:44 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
void Vector<T>::operator=(const Vector &p_from) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_copy_from(p_from);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
Error Vector<T>::insert(int p_pos, const T &p_val) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
|
|
|
|
resize(size() + 1);
|
|
|
|
for (int i = (size() - 1); i > p_pos; i--)
|
|
|
|
set(i, get(i - 1));
|
|
|
|
set(p_pos, p_val);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
Vector<T>::Vector(const Vector &p_from) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_ptr = NULL;
|
|
|
|
_copy_from(p_from);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<T>::Vector() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_ptr = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<T>::~Vector() {
|
|
|
|
|
|
|
|
_unref(_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|