Advertisement
Caiwan

array.h

Apr 6th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #ifndef __ARRAY_H_
  2. #define __ARRAY_H_
  3.  
  4. #include <cstdlib>
  5. #include "../debug/errors.h"
  6. #include "../debug/memory.h"
  7.  
  8. template <class T> class array_t {
  9.     public:
  10.         unsigned long int size;
  11.         T * data;
  12.  
  13.         array_t();
  14.         ~array_t();
  15.  
  16.         void insert(unsigned int n);
  17.         void clear();
  18.         unsigned int realsize();
  19.  
  20.         T& operator[] (int i);
  21.         /*
  22.         void copy(array<T> source);
  23.         void copy(T* source, unsigned int length);
  24.         */
  25. };
  26.  
  27. // todo: use template function here
  28. /*
  29. namespace array_t{
  30.     extern void inset(unsigned int n);
  31. }
  32. */
  33. template <class T> array_t<T>::array_t(){
  34.     this->data = NULL;
  35.     this->size = 0;
  36. }
  37.  
  38. template <class T> array_t<T>::~array_t(){
  39.     //if (this->data && this->size) free(this->data);
  40.     this->clear();
  41. }
  42.  
  43. template <class T> void array_t<T>::insert(unsigned int n)
  44. {
  45.     if (this->size == 0)
  46.     {
  47.         //while (!this->data) this->data = (T*)malloc(n*sizeof(T));
  48.         this->data = new T[n];
  49.         this->size = n;
  50.         //FWdebug::globalMem += (unsigned int) sizeof(T)*size; // debugger
  51.         FWdebug::globalMem->add(sizeof(T)*size,(void*)this->data);
  52.     }
  53.     else
  54.     {
  55.         T* tmp = NULL;
  56.         //while (!tmp) tmp = (T*) malloc((this->size+n)*sizeof(T));
  57.         tmp = new T[this->size+n];
  58.        
  59.         memcpy(tmp, this->data, this->size*sizeof(T));
  60.         //free(this->data);
  61.        
  62.         try{
  63.             delete this->data;
  64.         }catch (...){
  65.             delete this->data;
  66.         }
  67.        
  68.         this->data = tmp;
  69.         this->size += n;
  70.         //FWdebug::globalMem += (unsigned int) sizeof(T)*n; // debugger
  71.         FWdebug::globalMem->add(-sizeof(T)*size, (void*)this->data );
  72.     }
  73. }
  74.  
  75. template <class T> void array_t<T>::clear()
  76. {
  77.     if (this->data && this->size)
  78.     {
  79.         try{
  80.             delete[] this->data;
  81.         }catch (...){
  82.             delete[] this->data;
  83.         }
  84.         //FWdebug::globalMem -= (unsigned int) sizeof(T)*size; // debugger
  85.         FWdebug::globalMem->add(-sizeof(T)*size,(void*)this->data);
  86.         this->size = 0;
  87.     }
  88. }
  89.  
  90. template <class T> unsigned int array_t<T>::realsize()
  91. {
  92.     return sizeof(T)*this->size;
  93. }
  94.  
  95. template <class T> T& array_t<T>::operator [] (int i)
  96. {
  97.     if ( i<0 ||(i>=this->size-1)) throw (FW_ERROR_INVALID_ARRAY_INDEX);
  98.     return this->data[i];
  99. }
  100.  
  101. template <class T> class double_buffer {
  102.     public:
  103.         unsigned long int size;
  104.         //T * data;
  105.         array_t<T> v1, v2;
  106.  
  107.         double_buffer();
  108.         ~double_buffer();
  109.        
  110.         void insert(unsigned int n);
  111.         int realsize();
  112.  
  113.         void swap();
  114. };
  115.  
  116. template <class T> double_buffer<T>::double_buffer()
  117. {
  118.     // hopefully the constructors of v1 and v2 were executed.
  119. }
  120.  
  121. template <class T> ~double_buffer<T>::double_buffer()
  122. {
  123.     // hopefully the destructors of v1 and v2 were executed.
  124. }
  125.  
  126. template <class T> double_buffer<T>::insert(unsigned int n)
  127. {
  128.     this->v1.insert(n);
  129.     this->v2.insert(n);
  130. }
  131.  
  132. template <class T> double_buffer<T>::swap()
  133. {
  134.     register unsigned int v_tmp = v1.size;
  135.     register T* p_tmp = v1.data;    // swap size if the buffers size not match
  136.    
  137.     v1.size = v2.size;
  138.     v1.data = v2.data;
  139.  
  140.     v2.size = v_tmp;
  141.     v2.data = p_tmp;
  142. }
  143.  
  144. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement