Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __fullmetalVector_hpp
- #include <cstring>
- #include <stdexcept>
- /* Vector class is used for dynamic memory management
- This is more efficent than the std::vector<> class, because
- objects are only created *once*, and are only destroyed, *once*
- And also uses pointers rather than references, which simplifies
- the process significantly. The object doesn't need to be a class,
- because it only has one member, everything else is a member-function.
- It also uses a C-string style of notating the end of the array.
- The C++ 'delete' method is also used for each element, to ensure an
- objects destructor *is* called.
- NumElements() - Number of array slots in or occupied.
- Add ( ) - Add a new element.
- Remove ( ) -
- Do not steal, or take credit for this work.
- Leave this comment in this file.
- */
- class Range{
- private:
- int mStart,mSize,myID;
- public:
- Range(int aStart,int aSize):mStart(aStart),mSize(aSize){}
- Range(Range& r):mStart(r.mStart),mSize(r.mSize){}
- Range(const Range& r):mStart(r.mStart),mSize(r.mSize){} //needed to return 'by value'
- bool operator () (int x){
- return x>=0? (x<mSize || mSize==0):(-x <= mSize || mSize==0);
- }
- int operator [] ( int x ) {
- if(!operator()(x)) throw std::range_error("Range throwing exception; invalid indice number.");
- return x + mStart;
- }
- int Size( ){ return mSize; }
- int Size( int aSize ){ return mSize = aSize; }
- int Start( ) { return mStart; }
- int Start( int aStart ){ return mStart = aStart; }
- };
- template <class T,bool construct = false>
- class Vector{
- public:
- class CallBack{
- public:
- virtual void operator () (Vector<T>& a,unsigned int indice) = 0;
- };
- Range All( int size = -1 ) {
- if(size==-1) size = arraySize;
- const Range ret(0,size);
- return ret;
- }
- void Each(CallBack& cb,Range range= All()){ for(unsigned int i;range(i);i++)cb(*this,range[i]); }
- //We DO NOT delete const pointers...
- Vector<T>& Remove(Range& ran){
- for(int i=0,indice=ran[0];;i++,indice=ran[i]){
- if(indice<0) indice += arraySize;
- if(construct){
- if(sizeof(T)<=sizeof(T*)) delete &(((T*)tElements)[indice]);
- else delete tElements[indice];
- }
- memcpy( &(tElements[indice]),
- &(tElements[indice+1]),
- (sizeof(T)<=sizeof(T*)?sizeof(T)*--arraySize:sizeof(T*)*--arraySize) );
- if(!ran(i+1)) break;
- }
- }
- T& Add( const T* nElement= new T, int indice = -1){
- Allocate();
- ++arraySize;
- Range ran = All();
- indice = ran[indice];
- if ( indice < 0 ) indice = arraySize + indice;
- if(sizeof(T)<=sizeof(T*)){
- memcpy(&(tElements[indice]),nElement,sizeof(T));
- return (((T*)tElements)[indice]);
- } else {
- return *(tElements[indice] = (T*)nElement);
- }
- }
- T& Add( T* nElement= new T, int indice = -1){
- Allocate();
- ++arraySize;
- Range ran = All();
- indice = ran[indice];
- if ( indice < 0 ) indice = arraySize + indice;
- if(sizeof(T)<=sizeof(T*)){
- memcpy(&(tElements[indice]),nElement,sizeof(T));
- delete nElement;
- return (((T*)tElements)[indice]);
- } else {
- return *(tElements[indice] = nElement);
- }
- }
- T& Add(T& nElement, int indice = -1){
- Allocate();
- ++arraySize;
- Range ran = All();
- indice = ran[indice];
- if ( indice < 0 ) indice = arraySize + indice;
- if(sizeof(T)<=sizeof(T*)){
- memcpy(&(tElements[indice]),&nElement,sizeof(T));
- return (((T*)tElements)[indice]);
- } else {
- if(construct)return *(tElements[indice] = new T(nElement));
- else{
- T* nPtr = (T*)new char[sizeof(T)];
- memcpy(nPtr,&nElement,sizeof(T));
- return *(tElements[indice] = nPtr);
- }
- }
- }
- T& Add(const T& nElement, int indice = -1){
- Allocate();
- ++arraySize;
- Range ran = All();
- indice = ran[indice];
- if ( indice < 0 ) indice = arraySize + indice;
- if(sizeof(T)<=sizeof(T*)){
- if(static_cast<unsigned int>(indice+1)!=arraySize) memcpy(&(((T*)tElements)[indice+1]),&(((T*)tElements)[indice]),sizeof(T)*(arraySize-indice-1));
- memcpy(&(((T*)tElements)[indice]),&nElement,sizeof(T));
- return (((T*)tElements)[indice]);
- } else {
- memcpy( &(tElements[indice+1]),&(tElements[indice]),sizeof(T*)*(arraySize-indice));
- if(construct) return *(tElements[indice] = new T(nElement));
- else{
- T* nPtr = (T*)new char[sizeof(T)];
- memcpy(nPtr,&nElement,sizeof(T));
- return *(tElements[indice] = nPtr);
- }
- }
- }
- T& operator [] ( int indice ) throw(std::range_error) {
- try{
- indice = All()[indice];
- if(indice<0) indice = arraySize + indice;
- if(sizeof(T)<=sizeof(T*)) return *((T*)&(tElements[ indice ]));
- else return *tElements[ indice ];
- } catch(std::range_error& e) {
- throw std::range_error("Indice out of bounds of Vector<>");
- }
- }
- Vector():bufferSize(5),arraySize(0),allocatedMemory(0),tElements(NULL){};
- protected:
- T** tElements;
- unsigned int bufferSize,arraySize,allocatedMemory;
- void Allocate(unsigned int number = 1){
- if(arraySize+number <= allocatedMemory) return;
- unsigned int nSize = allocatedMemory,required = arraySize+number;
- while(nSize<required)nSize+=bufferSize;
- if(sizeof(T)<=sizeof(T*)){
- T* ntElements = (T*)new char[nSize*sizeof(T)];
- memset(ntElements,0,sizeof(T)*nSize);
- if(tElements) memcpy(ntElements,tElements,arraySize*sizeof(T)), delete[] tElements;
- tElements = (T**) ntElements;
- } else {
- T** ntElements = new T*[nSize*sizeof(T*)];
- memset(ntElements,0,sizeof(T*)*nSize);
- if(tElements) memcpy(ntElements,tElements,arraySize*sizeof(T*)), delete[] tElements;
- tElements = ntElements;
- }
- allocatedMemory = nSize;
- }
- bool validIndice(int i){ return All()(i); }
- };
- #define __fullmetalVector_hpp
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement