Advertisement
Guest User

Vector.hpp

a guest
Oct 20th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #ifndef __fullmetalVector_hpp
  2. #include <cstring>
  3. #include <stdexcept>
  4. /* Vector class is used for dynamic memory management
  5. This is more efficent than the std::vector<> class, because
  6. objects are only created *once*, and are only destroyed, *once*
  7. And also uses pointers rather than references, which simplifies
  8. the process significantly. The object doesn't need to be a class,
  9. because it only has one member, everything else is a member-function.
  10. It also uses a C-string style of notating the end of the array.
  11. The C++ 'delete' method is also used for each element, to ensure an
  12. objects destructor *is* called.
  13. NumElements() - Number of array slots in or occupied.
  14. Add ( ) - Add a new element.
  15. Remove ( ) -
  16. Do not steal, or take credit for this work.
  17. Leave this comment in this file.
  18. */
  19.  
  20. class Range{
  21.     private:
  22.         int mStart,mSize,myID;
  23.     public:
  24.         Range(int aStart,int aSize):mStart(aStart),mSize(aSize){}
  25.         Range(Range& r):mStart(r.mStart),mSize(r.mSize){}
  26.         Range(const Range& r):mStart(r.mStart),mSize(r.mSize){} //needed to return 'by value'
  27.         bool operator () (int x){
  28.             return x>=0? (x<mSize || mSize==0):(-x <= mSize || mSize==0);
  29.         }
  30.         int operator [] ( int x ) {
  31.             if(!operator()(x)) throw std::range_error("Range throwing exception; invalid indice number.");
  32.             return x + mStart;
  33.         }
  34.         int Size( ){ return mSize; }
  35.         int Size( int aSize ){ return mSize = aSize; }
  36.         int Start( ) { return mStart; }
  37.         int Start( int aStart ){ return mStart = aStart; }
  38. };
  39.  
  40. template <class T,bool construct = false>
  41. class Vector{
  42.     public:
  43.         class CallBack{
  44.             public:
  45.                 virtual void operator () (Vector<T>& a,unsigned int indice) = 0;
  46.         };
  47.         Range All( int size = -1 ) {
  48.             if(size==-1) size = arraySize;
  49.             const Range ret(0,size);
  50.             return ret;
  51.         }
  52.         void Each(CallBack& cb,Range range= All()){ for(unsigned int i;range(i);i++)cb(*this,range[i]); }
  53.         //We DO NOT delete const pointers...
  54.         Vector<T>& Remove(Range& ran){
  55.             for(int i=0,indice=ran[0];;i++,indice=ran[i]){
  56.                 if(indice<0) indice += arraySize;
  57.  
  58.                 if(construct){
  59.                     if(sizeof(T)<=sizeof(T*)) delete &(((T*)tElements)[indice]);
  60.                     else delete tElements[indice];
  61.                 }
  62.  
  63.                 memcpy( &(tElements[indice]),
  64.                         &(tElements[indice+1]),
  65.                        (sizeof(T)<=sizeof(T*)?sizeof(T)*--arraySize:sizeof(T*)*--arraySize) );
  66.                 if(!ran(i+1)) break;
  67.             }
  68.         }
  69.         T& Add( const T* nElement= new T, int indice = -1){
  70.             Allocate();
  71.             ++arraySize;
  72.             Range ran = All();
  73.             indice = ran[indice];
  74.             if ( indice < 0 ) indice = arraySize + indice;
  75.             if(sizeof(T)<=sizeof(T*)){
  76.                 memcpy(&(tElements[indice]),nElement,sizeof(T));
  77.                 return (((T*)tElements)[indice]);
  78.             } else {
  79.                 return *(tElements[indice] = (T*)nElement);
  80.             }
  81.         }
  82.         T& Add( T* nElement= new T, int indice = -1){
  83.             Allocate();
  84.             ++arraySize;
  85.             Range ran = All();
  86.             indice = ran[indice];
  87.             if ( indice < 0 ) indice = arraySize + indice;
  88.             if(sizeof(T)<=sizeof(T*)){
  89.                 memcpy(&(tElements[indice]),nElement,sizeof(T));
  90.                 delete nElement;
  91.                 return (((T*)tElements)[indice]);
  92.             } else {
  93.                 return *(tElements[indice] = nElement);
  94.             }
  95.         }
  96.         T& Add(T& nElement, int indice = -1){
  97.             Allocate();
  98.             ++arraySize;
  99.             Range ran = All();
  100.             indice = ran[indice];
  101.             if ( indice < 0 ) indice = arraySize + indice;
  102.             if(sizeof(T)<=sizeof(T*)){
  103.                 memcpy(&(tElements[indice]),&nElement,sizeof(T));
  104.                 return (((T*)tElements)[indice]);
  105.             } else {
  106.                 if(construct)return *(tElements[indice] = new T(nElement));
  107.                 else{
  108.                     T* nPtr = (T*)new char[sizeof(T)];
  109.                     memcpy(nPtr,&nElement,sizeof(T));
  110.                     return *(tElements[indice] = nPtr);
  111.                 }
  112.             }
  113.         }
  114.         T& Add(const T& nElement, int indice = -1){
  115.             Allocate();
  116.             ++arraySize;
  117.             Range ran = All();
  118.             indice = ran[indice];
  119.             if ( indice < 0 ) indice = arraySize + indice;
  120.             if(sizeof(T)<=sizeof(T*)){
  121.                 if(static_cast<unsigned int>(indice+1)!=arraySize) memcpy(&(((T*)tElements)[indice+1]),&(((T*)tElements)[indice]),sizeof(T)*(arraySize-indice-1));
  122.                 memcpy(&(((T*)tElements)[indice]),&nElement,sizeof(T));
  123.                 return (((T*)tElements)[indice]);
  124.             } else {
  125.                 memcpy( &(tElements[indice+1]),&(tElements[indice]),sizeof(T*)*(arraySize-indice));
  126.                 if(construct) return *(tElements[indice] = new T(nElement));
  127.                 else{
  128.                     T* nPtr = (T*)new char[sizeof(T)];
  129.                     memcpy(nPtr,&nElement,sizeof(T));
  130.                     return *(tElements[indice] = nPtr);
  131.                 }
  132.             }
  133.         }
  134.         T& operator [] ( int indice ) throw(std::range_error) {
  135.             try{
  136.                 indice = All()[indice];
  137.                 if(indice<0) indice = arraySize + indice;
  138.                 if(sizeof(T)<=sizeof(T*)) return *((T*)&(tElements[ indice ]));
  139.                 else return *tElements[ indice ];
  140.             } catch(std::range_error& e) {
  141.                 throw std::range_error("Indice out of bounds of Vector<>");
  142.             }
  143.         }
  144.         Vector():bufferSize(5),arraySize(0),allocatedMemory(0),tElements(NULL){};
  145.     protected:
  146.         T** tElements;
  147.         unsigned int bufferSize,arraySize,allocatedMemory;
  148.         void Allocate(unsigned int number = 1){
  149.             if(arraySize+number <= allocatedMemory) return;
  150.             unsigned int nSize = allocatedMemory,required = arraySize+number;
  151.             while(nSize<required)nSize+=bufferSize;
  152.             if(sizeof(T)<=sizeof(T*)){
  153.                 T* ntElements = (T*)new char[nSize*sizeof(T)];
  154.                 memset(ntElements,0,sizeof(T)*nSize);
  155.                 if(tElements) memcpy(ntElements,tElements,arraySize*sizeof(T)), delete[] tElements;
  156.                 tElements = (T**) ntElements;
  157.             } else {
  158.                 T** ntElements = new T*[nSize*sizeof(T*)];
  159.                 memset(ntElements,0,sizeof(T*)*nSize);
  160.                 if(tElements) memcpy(ntElements,tElements,arraySize*sizeof(T*)), delete[] tElements;
  161.                 tElements = ntElements;
  162.             }
  163.             allocatedMemory = nSize;
  164.         }
  165.         bool validIndice(int i){ return All()(i); }
  166. };
  167.  
  168.  
  169. #define __fullmetalVector_hpp
  170. #endif
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement