mudgangster

Tiny, scriptable MUD client
Log | Files | Refs | README

TracyFastVector.hpp (2599B)


      1 #ifndef __TRACYFASTVECTOR_HPP__
      2 #define __TRACYFASTVECTOR_HPP__
      3 
      4 #include <stddef.h>
      5 
      6 #include "../common/TracyAlloc.hpp"
      7 #include "../common/TracyForceInline.hpp"
      8 
      9 namespace tracy
     10 {
     11 
     12 template<typename T>
     13 class FastVector
     14 {
     15 public:
     16     using iterator = T*;
     17     using const_iterator = const T*;
     18 
     19     FastVector( size_t capacity )
     20         : m_ptr( (T*)tracy_malloc( sizeof( T ) * capacity ) )
     21         , m_write( m_ptr )
     22         , m_end( m_ptr + capacity )
     23     {
     24     }
     25 
     26     FastVector( const FastVector& ) = delete;
     27     FastVector( FastVector&& ) = delete;
     28 
     29     ~FastVector()
     30     {
     31         tracy_free( m_ptr );
     32     }
     33 
     34     FastVector& operator=( const FastVector& ) = delete;
     35     FastVector& operator=( FastVector&& ) = delete;
     36 
     37     bool empty() const { return m_ptr == m_write; }
     38     size_t size() const { return m_write - m_ptr; }
     39 
     40     T* data() { return m_ptr; }
     41     const T* data() const { return m_ptr; };
     42 
     43     T* begin() { return m_ptr; }
     44     const T* begin() const { return m_ptr; }
     45     T* end() { return m_write; }
     46     const T* end() const { return m_write; }
     47 
     48     T& front() { assert( !empty() ); return m_ptr[0]; }
     49     const T& front() const { assert( !empty() ); return m_ptr[0]; }
     50 
     51     T& back() { assert( !empty() ); return m_write[-1]; }
     52     const T& back() const { assert( !empty() ); return m_write[-1]; }
     53 
     54     T& operator[]( size_t idx ) { return m_ptr[idx]; }
     55     const T& operator[]( size_t idx ) const { return m_ptr[idx]; }
     56 
     57     T* push_next()
     58     {
     59         if( m_write == m_end ) AllocMore();
     60         return m_write++;
     61     }
     62 
     63     T* prepare_next()
     64     {
     65         if( m_write == m_end ) AllocMore();
     66         return m_write;
     67     }
     68 
     69     void commit_next()
     70     {
     71         m_write++;
     72     }
     73 
     74     void clear()
     75     {
     76         m_write = m_ptr;
     77     }
     78 
     79     void swap( FastVector& vec )
     80     {
     81         const auto ptr1 = m_ptr;
     82         const auto ptr2 = vec.m_ptr;
     83         const auto write1 = m_write;
     84         const auto write2 = vec.m_write;
     85         const auto end1 = m_end;
     86         const auto end2 = vec.m_end;
     87 
     88         m_ptr = ptr2;
     89         vec.m_ptr = ptr1;
     90         m_write = write2;
     91         vec.m_write = write1;
     92         m_end = end2;
     93         vec.m_end = end1;
     94     }
     95 
     96 private:
     97     tracy_no_inline void AllocMore()
     98     {
     99         const auto cap = ( m_end - m_ptr ) * 2;
    100         const auto size = m_write - m_ptr;
    101         T* ptr = (T*)tracy_malloc( sizeof( T ) * cap );
    102         memcpy( ptr, m_ptr, size * sizeof( T ) );
    103         tracy_free( m_ptr );
    104         m_ptr = ptr;
    105         m_write = m_ptr + size;
    106         m_end = m_ptr + cap;
    107     }
    108 
    109     T* m_ptr;
    110     T* m_write;
    111     T* m_end;
    112 };
    113 
    114 }
    115 
    116 #endif