tracy/server/TracyVector.hpp

162 lines
3.7 KiB
C++
Raw Normal View History

#ifndef __TRACYVECTOR_HPP__
#define __TRACYVECTOR_HPP__
#include <assert.h>
#include <stdint.h>
2017-09-16 23:22:46 +00:00
#include "TracyMemory.hpp"
namespace tracy
{
2017-09-28 18:20:10 +00:00
#if 0
template<typename T>
using Vector = std::vector<T>;
#else
template<typename T>
class Vector
{
public:
Vector()
: m_ptr( nullptr )
, m_size( 0 )
, m_capacity( 0 )
{
}
2017-09-22 23:10:48 +00:00
Vector( const Vector& ) = delete;
Vector( Vector&& src )
: m_ptr( src.m_ptr )
, m_size( src.m_size )
, m_capacity( src.m_capacity )
{
src.m_ptr = nullptr;
}
~Vector()
{
2017-09-16 23:22:46 +00:00
memUsage.fetch_sub( m_capacity * sizeof( T ), std::memory_order_relaxed );
delete[] m_ptr;
}
2017-09-22 23:10:48 +00:00
Vector& operator=( const Vector& ) = delete;
Vector& operator=( Vector&& src )
{
delete[] m_ptr;
m_ptr = src.m_ptr;
m_size = src.m_size;
m_capacity = src.m_capacity;
src.m_ptr = nullptr;
return *this;
}
bool empty() const { return m_size == 0; }
2017-09-16 23:05:47 +00:00
size_t size() const { return m_size; }
const T* data() { return m_ptr; };
T* begin() { return m_ptr; }
const T* begin() const { return m_ptr; }
T* end() { return m_ptr + m_size; }
const T* end() const { return m_ptr + m_size; }
T& back() { assert( m_size > 0 ); return m_ptr[m_size - 1]; }
const T& back() const { assert( m_size > 0 ); return m_ptr[m_size - 1]; }
2017-09-16 23:05:47 +00:00
T& operator[]( size_t idx ) { return m_ptr[idx]; }
const T& operator[]( size_t idx ) const { return m_ptr[idx]; }
void push_back( const T& v )
{
if( m_size == m_capacity ) AllocMore();
m_ptr[m_size++] = v;
}
void push_back( T&& v )
{
if( m_size == m_capacity ) AllocMore();
m_ptr[m_size++] = std::move( v );
}
2017-09-25 21:01:22 +00:00
T* insert( T* it, const T& v )
2017-09-16 23:05:47 +00:00
{
assert( it >= m_ptr && it <= m_ptr + m_size );
2017-09-25 21:01:22 +00:00
const auto dist = it - m_ptr;
2017-09-16 23:05:47 +00:00
if( m_size == m_capacity ) AllocMore();
2017-09-25 21:01:22 +00:00
if( dist != m_size ) memmove( m_ptr + dist + 1, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
2017-09-16 23:05:47 +00:00
m_size++;
2017-09-25 21:01:22 +00:00
m_ptr[dist] = v;
return m_ptr + dist;
2017-09-16 23:05:47 +00:00
}
2017-09-25 21:01:22 +00:00
T* insert( T* it, T&& v )
2017-09-16 23:05:47 +00:00
{
assert( it >= m_ptr && it <= m_ptr + m_size );
2017-09-25 21:01:22 +00:00
const auto dist = it - m_ptr;
2017-09-16 23:05:47 +00:00
if( m_size == m_capacity ) AllocMore();
2017-09-25 21:01:22 +00:00
if( dist != m_size ) memmove( m_ptr + dist + 1, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
2017-09-16 23:05:47 +00:00
m_size++;
2017-09-25 21:01:22 +00:00
m_ptr[dist] = std::move( v );
return m_ptr + dist;
2017-09-16 23:05:47 +00:00
}
2017-09-25 21:11:56 +00:00
T* erase( T* begin, T* end )
{
assert( begin >= m_ptr && begin <= m_ptr + m_size );
assert( end >= m_ptr && end <= m_ptr + m_size );
assert( begin <= end );
const auto dist = end - begin;
if( dist > 0 )
{
memmove( begin, end, ( m_size - ( end - m_ptr ) ) * sizeof( T ) );
m_size -= dist;
}
return begin;
}
2017-09-30 23:51:29 +00:00
void reserve( size_t cap )
{
if( cap <= m_capacity ) return;
memUsage.fetch_add( ( cap - m_capacity ) * sizeof( T ), std::memory_order_relaxed );
m_capacity = cap;
Realloc();
}
private:
void AllocMore()
{
if( m_capacity == 0 )
{
m_capacity = 1;
2017-09-16 23:22:46 +00:00
memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed );
}
else
{
2017-09-16 23:22:46 +00:00
memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed );
m_capacity *= 2;
}
2017-09-30 23:51:29 +00:00
Realloc();
}
void Realloc()
{
T* ptr = new T[m_capacity];
2017-09-28 18:47:57 +00:00
if( m_size != 0 )
{
memcpy( ptr, m_ptr, m_size * sizeof( T ) );
delete[] m_ptr;
}
m_ptr = ptr;
}
T* m_ptr;
uint32_t m_size;
uint32_t m_capacity;
};
#endif
}
#endif