Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __ARRAY_H_
- #define __ARRAY_H_
- #include <cstdlib>
- #include "../debug/errors.h"
- #include "../debug/memory.h"
- template <class T> class array_t {
- public:
- unsigned long int size;
- T * data;
- array_t();
- ~array_t();
- void insert(unsigned int n);
- void clear();
- unsigned int realsize();
- T& operator[] (int i);
- /*
- void copy(array<T> source);
- void copy(T* source, unsigned int length);
- */
- };
- // todo: use template function here
- /*
- namespace array_t{
- extern void inset(unsigned int n);
- }
- */
- template <class T> array_t<T>::array_t(){
- this->data = NULL;
- this->size = 0;
- }
- template <class T> array_t<T>::~array_t(){
- //if (this->data && this->size) free(this->data);
- this->clear();
- }
- template <class T> void array_t<T>::insert(unsigned int n)
- {
- if (this->size == 0)
- {
- //while (!this->data) this->data = (T*)malloc(n*sizeof(T));
- this->data = new T[n];
- this->size = n;
- //FWdebug::globalMem += (unsigned int) sizeof(T)*size; // debugger
- FWdebug::globalMem->add(sizeof(T)*size,(void*)this->data);
- }
- else
- {
- T* tmp = NULL;
- //while (!tmp) tmp = (T*) malloc((this->size+n)*sizeof(T));
- tmp = new T[this->size+n];
- memcpy(tmp, this->data, this->size*sizeof(T));
- //free(this->data);
- try{
- delete this->data;
- }catch (...){
- delete this->data;
- }
- this->data = tmp;
- this->size += n;
- //FWdebug::globalMem += (unsigned int) sizeof(T)*n; // debugger
- FWdebug::globalMem->add(-sizeof(T)*size, (void*)this->data );
- }
- }
- template <class T> void array_t<T>::clear()
- {
- if (this->data && this->size)
- {
- try{
- delete[] this->data;
- }catch (...){
- delete[] this->data;
- }
- //FWdebug::globalMem -= (unsigned int) sizeof(T)*size; // debugger
- FWdebug::globalMem->add(-sizeof(T)*size,(void*)this->data);
- this->size = 0;
- }
- }
- template <class T> unsigned int array_t<T>::realsize()
- {
- return sizeof(T)*this->size;
- }
- template <class T> T& array_t<T>::operator [] (int i)
- {
- if ( i<0 ||(i>=this->size-1)) throw (FW_ERROR_INVALID_ARRAY_INDEX);
- return this->data[i];
- }
- template <class T> class double_buffer {
- public:
- unsigned long int size;
- //T * data;
- array_t<T> v1, v2;
- double_buffer();
- ~double_buffer();
- void insert(unsigned int n);
- int realsize();
- void swap();
- };
- template <class T> double_buffer<T>::double_buffer()
- {
- // hopefully the constructors of v1 and v2 were executed.
- }
- template <class T> ~double_buffer<T>::double_buffer()
- {
- // hopefully the destructors of v1 and v2 were executed.
- }
- template <class T> double_buffer<T>::insert(unsigned int n)
- {
- this->v1.insert(n);
- this->v2.insert(n);
- }
- template <class T> double_buffer<T>::swap()
- {
- register unsigned int v_tmp = v1.size;
- register T* p_tmp = v1.data; // swap size if the buffers size not match
- v1.size = v2.size;
- v1.data = v2.data;
- v2.size = v_tmp;
- v2.data = p_tmp;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement