86 lines
2.3 KiB
C++
86 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include "Vectors.h"
|
|
#include <vector>
|
|
#include <assert.h>
|
|
#include <list>
|
|
|
|
#define MAX(x, y) x > y ? x : y
|
|
#define MIN(x, y) x < y ? x : y
|
|
|
|
#define List std::list
|
|
|
|
template<typename T>
|
|
class Array
|
|
{
|
|
public:
|
|
Array() : m_allocated(1000), m_dynamic(true)
|
|
{
|
|
m_values = (T*)malloc(sizeof(T)*m_allocated);
|
|
}
|
|
Array(int size, bool dynamic) : m_allocated(size), m_dynamic(dynamic)
|
|
{
|
|
m_values = (T*)malloc(sizeof(T)*m_allocated);
|
|
}
|
|
Array(const Array<T>& other) : m_allocated(other.m_filled), m_dynamic(other.m_dynamic)
|
|
{
|
|
m_filled = other.m_filled;
|
|
m_allocated = other.m_filled;
|
|
m_values = (T*)malloc(sizeof(T)*m_allocated);
|
|
memcpy((T*)m_values, (T*)other.m_values, sizeof(T)*m_filled);
|
|
}
|
|
~Array() { free(m_values); }
|
|
void Prealloc(int size)
|
|
{
|
|
m_allocated = size;
|
|
m_values = (T*)realloc(m_values, sizeof(T)*m_allocated);
|
|
}
|
|
const int push_back(const T& value)
|
|
{
|
|
if (m_filled >= m_allocated) {
|
|
if (not m_dynamic) {assert(false); }
|
|
if (m_allocated == 0) m_allocated = 1; else m_allocated*=2;
|
|
m_values = (T*)realloc(m_values, sizeof(T)*m_allocated);
|
|
}
|
|
m_values[m_filled++] = value;
|
|
|
|
return m_filled-1;
|
|
}
|
|
void remove_simple(int index)
|
|
{
|
|
assert(index < m_filled);
|
|
m_filled--;
|
|
if (index != m_filled) m_values[index] = m_values[m_filled];
|
|
}
|
|
const int exists(const T& value) const
|
|
{
|
|
for (int i = 0; i < m_filled; i++) if (m_values[i] == value) return i;
|
|
return -1;
|
|
}
|
|
void clear() { m_filled = 0; }
|
|
const unsigned size() const { return m_filled; }
|
|
const bool empty() const { return m_filled == 0; }
|
|
T operator[](int index) const { return m_values[index]; }
|
|
T& operator[](int index) { return m_values[index]; }
|
|
Array<T>& operator=( const Array<T>& other ) {
|
|
if (other.m_filled > m_allocated) {
|
|
m_values = (T*)realloc(m_values, sizeof(T)*other.m_filled);
|
|
}
|
|
m_allocated = other.m_filled;
|
|
m_filled = other.m_filled;
|
|
m_dynamic = other.m_dynamic;
|
|
memcpy(m_values, other.m_values, sizeof(T)*m_filled);
|
|
return *this;
|
|
}
|
|
private:
|
|
unsigned m_allocated{0};
|
|
unsigned m_filled{0};
|
|
bool m_dynamic{true};
|
|
T* m_values{nullptr};
|
|
};
|
|
|
|
Vector3 calculate_normal(const Vector3& v1, const Vector3& v2, const Vector3& v3);
|
|
|
|
const float random(const float& rangeMin, const float& rangeMax);
|
|
const float random_int(const int& rangeMin, const int& rangeMax);
|