#ifndef FixedVector_h
#define FixedVector_h
#include "BAssert.h"
#include <array>
#include <cstddef>
#include <type_traits>
namespace bmalloc {
template<typename T, size_t Capacity>
class FixedVector {
static_assert(std::is_trivially_destructible<T>::value, "FixedVector must have a trivial destructor.");
public:
FixedVector();
const T* begin() const { return &m_buffer[0]; }
const T* end() const { return begin() + size(); }
size_t size() const { return m_size; }
size_t capacity() const { return Capacity; }
T& operator[](size_t);
void push(const T&);
void push(const T*, const T*);
T pop();
void shrink(T*);
void shrink(size_t);
void clear() { shrink(static_cast<size_t>(0)); }
bool isEmpty() { return !m_size; }
private:
size_t m_size;
std::array<T, Capacity> m_buffer;
};
template<typename T, size_t Capacity>
inline FixedVector<T, Capacity>::FixedVector()
: m_size(0)
{
}
template<typename T, size_t Capacity>
inline T& FixedVector<T, Capacity>::operator[](size_t i)
{
BASSERT(i < m_size);
return m_buffer[i];
}
template<typename T, size_t Capacity>
inline void FixedVector<T, Capacity>::push(const T& value)
{
BASSERT(m_size < Capacity);
m_buffer[m_size++] = value;
}
template<typename T, size_t Capacity>
inline void FixedVector<T, Capacity>::push(const T* begin, const T* end)
{
for (const T* it = begin; it != end; ++it)
push(*it);
}
template<typename T, size_t Capacity>
inline T FixedVector<T, Capacity>::pop()
{
BASSERT(m_size);
return m_buffer[--m_size];
}
template<typename T, size_t Capacity>
inline void FixedVector<T, Capacity>::shrink(size_t size)
{
BASSERT(size <= m_size);
m_size = size;
}
template<typename T, size_t Capacity>
inline void FixedVector<T, Capacity>::shrink(T* end)
{
shrink(end - begin());
}
}
#endif // FixedVector_h