2019-09-12 20:53:24 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
// Copyright (c) 2019 Lawnjelly
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
2019-09-16 15:23:10 +02:00
|
|
|
// just a light wrapper around a vector until we get the Godot vector allocation issues sorted
|
2019-09-12 20:53:24 +02:00
|
|
|
#include "core/vector.h"
|
|
|
|
#include <assert.h>
|
2019-09-15 16:39:01 +02:00
|
|
|
#include <vector>
|
2019-09-12 20:53:24 +02:00
|
|
|
|
|
|
|
template <class T> class LVector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
// array subscript access
|
2019-09-16 15:23:10 +02:00
|
|
|
// note this is not available in Godot Vector
|
2019-09-12 20:53:24 +02:00
|
|
|
T& operator[](unsigned int ui)
|
|
|
|
{
|
|
|
|
assert (ui < m_iSize);
|
|
|
|
return m_Vec[ui];
|
|
|
|
}
|
2019-09-15 16:39:01 +02:00
|
|
|
|
2019-09-12 20:53:24 +02:00
|
|
|
const T& operator[](unsigned int ui) const
|
|
|
|
{
|
2019-09-13 20:15:25 +02:00
|
|
|
assert (ui < (unsigned int) m_iSize);
|
2019-09-12 20:53:24 +02:00
|
|
|
return m_Vec[ui];
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear(bool bCompact = false)
|
|
|
|
{
|
|
|
|
m_iSize = 0;
|
|
|
|
if (bCompact)
|
|
|
|
compact();
|
|
|
|
}
|
|
|
|
|
|
|
|
void compact()
|
|
|
|
{
|
|
|
|
m_Vec.resize(m_iSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reserve(int s)
|
|
|
|
{
|
|
|
|
m_Vec.resize(s);
|
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
void resize(int s, bool bCompact = false)
|
|
|
|
{
|
|
|
|
// new size
|
|
|
|
m_iSize = s;
|
|
|
|
|
|
|
|
// if compacting is not desired, no need to shrink vector
|
|
|
|
if (m_iSize < m_Vec.size())
|
|
|
|
{
|
|
|
|
if (!bCompact)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Vec.resize(s);
|
|
|
|
}
|
|
|
|
|
2019-09-12 20:53:24 +02:00
|
|
|
void set(unsigned int ui, const T &t)
|
|
|
|
{
|
2019-09-13 20:15:25 +02:00
|
|
|
assert (ui < (unsigned int) m_iSize);
|
2019-09-15 16:39:01 +02:00
|
|
|
m_Vec[ui] = t;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:23:10 +02:00
|
|
|
// efficient unsorted
|
|
|
|
void remove_unsorted(unsigned int ui)
|
|
|
|
{
|
|
|
|
// just swap the end element and decrement count
|
|
|
|
m_Vec[ui] = m_Vec[m_iSize-1];
|
|
|
|
m_iSize--;
|
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
T * request()
|
|
|
|
{
|
|
|
|
m_iSize++;
|
|
|
|
if (m_iSize >= m_Vec.size())
|
|
|
|
grow();
|
|
|
|
|
|
|
|
return &m_Vec[m_iSize-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void grow()
|
|
|
|
{
|
|
|
|
int new_size = m_Vec.size() * 2;
|
|
|
|
if (!new_size) new_size = 1;
|
|
|
|
|
|
|
|
reserve(new_size);
|
2019-09-12 20:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(const T &t)
|
|
|
|
{
|
|
|
|
int size_p1 = m_iSize+1;
|
|
|
|
|
|
|
|
if (size_p1 < m_Vec.size())
|
|
|
|
{
|
|
|
|
int size = m_iSize;
|
|
|
|
m_iSize = size_p1;
|
2019-09-15 16:39:01 +02:00
|
|
|
set(size, t);
|
2019-09-12 20:53:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// need more space
|
2019-09-15 16:39:01 +02:00
|
|
|
grow();
|
2019-09-12 20:53:24 +02:00
|
|
|
|
|
|
|
// call recursive
|
|
|
|
push_back(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_from(const LVector<T> &o)
|
|
|
|
{
|
|
|
|
// make sure enough space
|
|
|
|
if (o.size() > m_Vec.size())
|
|
|
|
{
|
|
|
|
reserve(o.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
m_iSize = o.size();
|
|
|
|
|
|
|
|
for (int n=0; n<o.size(); n++)
|
|
|
|
{
|
|
|
|
set(n, o[n]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_from(const Vector<T> &o)
|
|
|
|
{
|
|
|
|
// make sure enough space
|
|
|
|
if (o.size() > m_Vec.size())
|
|
|
|
{
|
|
|
|
reserve(o.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
m_iSize = o.size();
|
|
|
|
|
|
|
|
for (int n=0; n<o.size(); n++)
|
|
|
|
{
|
|
|
|
set(n, o[n]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LVector()
|
|
|
|
{
|
|
|
|
m_iSize = 0;
|
|
|
|
}
|
|
|
|
|
2019-09-15 16:39:01 +02:00
|
|
|
|
2019-09-12 20:53:24 +02:00
|
|
|
int size() const {return m_iSize;}
|
|
|
|
|
|
|
|
private:
|
2019-09-15 16:39:01 +02:00
|
|
|
std::vector<T> m_Vec;
|
2019-09-12 20:53:24 +02:00
|
|
|
|
|
|
|
// working size
|
|
|
|
int m_iSize;
|
|
|
|
};
|
|
|
|
|