#ifndef __A_DYNARRAY_H #define __A_DYNARRAY_H #include
#include
#ifdef LINUX #define __fastcall #define _fastcall #endif //========================================================= // simple step allocated linear array template class aDynArray { public: aDynArray() { elem = NULL; no = alloc_no = 0; } ~aDynArray() { if(elem) aMemFreeOnly(elem); } void Release() { if(elem) { aMemFreeOnly(elem); elem = NULL; } no = alloc_no = 0; } void Reset() { no = 0; } void Repack() { if(alloc_no != no) { alloc_no = no; elem = (TYPE*)aMemRealloc(elem, alloc_no*sizeof(TYPE), "aDynArray::elem"); } } void __fastcall Add(TYPE e) { _AddNewElem(STEP); elem[no-1] = e; } void __fastcall AddArray(TYPE* vect, uint n_elems) { int prior_no = no; no += n_elems; if(no > alloc_no) { //compute the new number of alloc steps (because n_elems can be greater than step) alloc_no = ((no - 1) / STEP + 1) * STEP; elem = (TYPE*)aMemRealloc(elem, alloc_no* sizeof(TYPE), "aDynArray::elem"); } memcpy(&elem[no], vect, n_elems * sizeof(TYPE)); } // add a pointer to a class that has a member called alloc_array_index that points to the index in the array void __fastcall AddIndexedPnt(TYPE e) { int prior_no = no; _AddNewElem(STEP); e->alloc_array_index = prior_no; elem[prior_no] = e; //e->alloc_array_index = prior_no; } // add a class that has a member called alloc_array_index that points to the index in the array void __fastcall AddIndexedElem(TYPE& e) { int prior_no = no; _AddNewElem(STEP); e.alloc_array_index = prior_no; elem[prior_no] = e; //e.alloc_array_index = prior_no; } void AddBlank() { _AddNewElem(STEP); } void AddIndexedBlank() { int prior_no = no; _AddNewElem(STEP); elem[prior_no].alloc_array_index = prior_no; } void __fastcall _AddNewElem(const int step) { no ++; if(no > alloc_no) { alloc_no += step; elem = (TYPE*)aMemRealloc(elem, alloc_no*sizeof(TYPE), "aDynArray::elem"); } } void __fastcall InsertAtPos(TYPE e, int pos) { no ++; if(no > alloc_no) { alloc_no += STEP; elem = (TYPE*)aMemRealloc(elem, alloc_no*sizeof(TYPE), "aDynArray::elem"); } for (int i = no - 1; i > pos; i--) elem[i] = elem[i-1]; elem[pos] = e; } void __fastcall InsertIndexedPntAtPos(TYPE e, int pos) { no ++; if(no > alloc_no) { alloc_no += STEP; elem = (TYPE*)aMemRealloc(elem, alloc_no*sizeof(TYPE), "aDynArray::elem"); } for (int i = no - 1; i > pos; i--) elem[i] = elem[i-1]; elem[pos] = e; e->alloc_array_index = pos; } void __fastcall InsertIndexedElemAtPos(TYPE e, int pos) { no ++; if(no > alloc_no) { alloc_no += STEP; elem = (TYPE*)aMemRealloc(elem, alloc_no*sizeof(TYPE), "aDynArray::elem"); } for (int i = no - 1; i > pos; i--) elem[i] = elem[i-1]; elem[pos] = e; e.alloc_array_index = pos; } void __fastcall Del(int pos) { # ifndef A_EDIT // temporary - TerrainEd: unknown A_ASSERT in Markers.h/.cpp A_ASSERT(pos < no && pos >= 0) # endif no --; if(pos < no) // move last in this position elem[pos] = elem[no]; } void __fastcall Del(TYPE e) { for(int i=0; i < no; i++) if(elem[i] == e) { no --; if(i < no) elem[i] = elem[no]; } } // called when you know for sure that there is a last element void __fastcall DelWithLast(int pos) { # ifndef A_EDIT // temporary - TerrainEd: unknown A_ASSERT in Markers.h/.cpp A_ASSERT(pos < no && pos >= 0) # endif no --; // move last in this position without check elem[pos] = elem[no]; } // called when you have a vector of pointers to classes with a member called alloc_array_index that points to the index void __fastcall DelIndexedPnt(TYPE e) { int pos = e->alloc_array_index; # ifndef A_EDIT // temporary - TerrainEd: unknown A_ASSERT in Markers.h/.cpp A_ASSERT(pos < no && pos >= 0) # endif no --; if(pos < no) // move last in this position { elem[pos] = elem[no]; elem[pos]->alloc_array_index = pos; } } // called when you have a vector of classes with a member called alloc_array_index that points to the index void __fastcall DelIndexedElem(TYPE e) { int pos = e.alloc_array_index; # ifndef A_EDIT // temporary - TerrainEd: unknown A_ASSERT in Markers.h/.cpp A_ASSERT(pos < no && pos >= 0) # endif no --; if(pos < no) // move last in this position { elem[pos] = elem[no]; elem[pos].alloc_array_index = pos; } } void _fastcall DelIndexedElemAndKeepOrder(int pos) { # ifndef A_EDIT // temporary - TerrainEd: unknown A_ASSERT in Markers.h/.cpp A_ASSERT(pos < no && pos >= 0) # endif for(int i=pos; i= 0) # endif for(int i=pos; ialloc_array_index = i; } no --; } void __fastcall DelAndKeepOrder(int pos) { # ifndef A_EDIT // temporary - TerrainEd: unknown A_ASSERT in Markers.h/.cpp A_ASSERT(pos < no && pos >= 0) # endif for(int i=pos; i class aDynStepArray : public aDynArray { public: aDynStepArray() { _step = STEP; } void __fastcall SetStep(const int step) { _step = step; } void __fastcall Add(TYPE e) { _AddNewElem(_step); elem[no-1] = e; } // add a pointer to a class that has a member called alloc_array_index that points to the index in the array void __fastcall AddIndexedPnt(TYPE e) { int prior_no = no; _AddNewElem(_step); elem[prior_no] = e; e->alloc_array_index = prior_no; } // add a class that has a member called alloc_array_index that points to the index in the array void __fastcall AddIndexedElem(TYPE e) { int prior_no = no; _AddNewElem(_step); elem[prior_no] = e; e.alloc_array_index = prior_no; } void AddBlank() { _AddNewElem(_step); } protected: int _step; }; //========================================================= #endif