godot/core/vector.h

185 lines
5.5 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* vector.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 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. */
/*************************************************************************/
2014-02-10 01:10:30 +00:00
#ifndef VECTOR_H
#define VECTOR_H
/**
* @class Vector
* @author Juan Linietsky
* Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use PoolVector for large arrays.
2014-02-10 01:10:30 +00:00
*/
#include "core/cowdata.h"
#include "core/error_macros.h"
#include "core/os/memory.h"
#include "core/sort.h"
2014-02-10 01:10:30 +00:00
template <class T>
class VectorWriteProxy {
friend class Vector<T>;
CowData<T> *_parent;
2016-03-08 23:00:52 +00:00
_FORCE_INLINE_ VectorWriteProxy(CowData<T> *parent) :
_parent(parent){};
2016-03-08 23:00:52 +00:00
public:
_FORCE_INLINE_ T &operator[](int p_index) {
CRASH_BAD_INDEX(p_index, _parent->size());
return _parent->ptrw()[p_index];
}
};
2016-03-08 23:00:52 +00:00
template <class T>
class Vector {
friend class VectorWriteProxy<T>;
2016-03-08 23:00:52 +00:00
CowData<T> *_cowdata;
2014-02-10 01:10:30 +00:00
public:
VectorWriteProxy<T> write;
2016-03-08 23:00:52 +00:00
2017-08-12 11:04:30 +00:00
bool push_back(const T &p_elem);
2016-03-08 23:00:52 +00:00
void remove(int p_index) { _cowdata->remove(p_index); }
void erase(const T &p_val) {
int idx = find(p_val);
if (idx >= 0) remove(idx);
};
2014-02-10 01:10:30 +00:00
void invert();
_FORCE_INLINE_ T *ptrw() { return _cowdata->ptrw(); }
_FORCE_INLINE_ const T *ptr() const { return _cowdata->ptr(); }
_FORCE_INLINE_ void clear() { resize(0); }
_FORCE_INLINE_ bool empty() const { return _cowdata->empty(); }
2016-03-08 23:00:52 +00:00
_FORCE_INLINE_ T get(int p_index) { return _cowdata->get(p_index); }
_FORCE_INLINE_ const T get(int p_index) const { return _cowdata->get(p_index); }
_FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata->set(p_index, p_elem); }
_FORCE_INLINE_ int size() const { return _cowdata->size(); }
Error resize(int p_size) { return _cowdata->resize(p_size); }
_FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata->get(p_index); }
Error insert(int p_pos, const T &p_val) { return _cowdata->insert(p_pos, p_val); }
2014-02-10 01:10:30 +00:00
2017-11-21 00:11:39 +00:00
void append_array(const Vector<T> &p_other);
template <class C>
2014-02-10 01:10:30 +00:00
void sort_custom() {
int len = _cowdata->size();
if (len == 0)
2014-02-10 01:10:30 +00:00
return;
T *data = ptrw();
SortArray<T, C> sorter;
sorter.sort(data, len);
2014-02-10 01:10:30 +00:00
}
void sort() {
sort_custom<_DefaultComparator<T> >();
}
void ordered_insert(const T &p_val) {
int i;
for (i = 0; i < _cowdata->size(); i++) {
2015-03-19 23:53:54 +00:00
if (p_val < operator[](i)) {
break;
2015-03-19 23:53:54 +00:00
};
};
insert(i, p_val);
2015-03-19 23:53:54 +00:00
}
2014-02-10 01:10:30 +00:00
int find(const T &p_val, int p_from = 0) const {
int ret = -1;
if (p_from < 0 || size() == 0)
return ret;
2014-02-10 01:10:30 +00:00
for (int i = p_from; i < size(); i++) {
2014-02-10 01:10:30 +00:00
if (ptr()[i] == p_val) {
ret = i;
break;
};
2014-02-10 01:10:30 +00:00
};
2016-03-08 23:00:52 +00:00
return ret;
2014-02-10 01:10:30 +00:00
}
2016-03-08 23:00:52 +00:00
_FORCE_INLINE_ Vector() :
_cowdata(new CowData<T>()),
write(VectorWriteProxy<T>(_cowdata)) {}
_FORCE_INLINE_ Vector(const Vector &p_from) :
_cowdata(new CowData<T>()),
write(VectorWriteProxy<T>(_cowdata)) { _cowdata->_ref(p_from._cowdata); }
inline Vector &operator=(const Vector &p_from) {
_cowdata->_ref(p_from._cowdata);
return *this;
2014-02-10 01:10:30 +00:00
}
_FORCE_INLINE_ ~Vector() {
delete _cowdata;
}
};
2014-02-10 01:10:30 +00:00
template <class T>
2014-02-10 01:10:30 +00:00
void Vector<T>::invert() {
2016-03-08 23:00:52 +00:00
for (int i = 0; i < size() / 2; i++) {
T *p = ptrw();
SWAP(p[i], p[size() - i - 1]);
}
2014-02-10 01:10:30 +00:00
}
2017-11-21 00:11:39 +00:00
template <class T>
void Vector<T>::append_array(const Vector<T> &p_other) {
const int ds = p_other.size();
if (ds == 0)
return;
const int bs = size();
resize(bs + ds);
for (int i = 0; i < ds; ++i)
ptrw()[bs + i] = p_other[i];
2014-02-10 01:10:30 +00:00
}
template <class T>
bool Vector<T>::push_back(const T &p_elem) {
2014-02-10 01:10:30 +00:00
Error err = resize(size() + 1);
ERR_FAIL_COND_V(err, true)
set(size() - 1, p_elem);
2014-02-10 01:10:30 +00:00
return false;
2014-02-10 01:10:30 +00:00
}
#endif