//========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============ // // Purpose: // // $NoKeywords: $ //============================================================================= #ifndef VGUI_DAR_H #define VGUI_DAR_H #include #include #include namespace vgui { //Simple lightweight dynamic array implementation template class VGUIAPI Dar { public: Dar() { _count=0; _capacity=0; _data=null; ensureCapacity(4); } Dar(int initialCapacity) { _count=0; _capacity=0; _data=null; ensureCapacity(initialCapacity); } public: void ensureCapacity(int wantedCapacity) { if(wantedCapacity<=_capacity){return;} //double capacity until it is >= wantedCapacity //this could be done with math, but iterative is just so much more fun int newCapacity=_capacity; if(newCapacity==0){newCapacity=1;} while(newCapacity_capacity)) { return; } _count=count; } int getCount() { return _count; } void addElement(ELEMTYPE elem) { ensureCapacity(_count+1); _data[_count]=elem; _count++; } bool hasElement(ELEMTYPE elem) { for(int i=0;i<_count;i++) { if(_data[i]==elem) { return true; } } return false; } void putElement(ELEMTYPE elem) { if(hasElement(elem)) { return; } addElement(elem); } void insertElementAt(ELEMTYPE elem,int index) { if((index<0)||(index>_count)) { return; } if((index==_count)||(_count==0)) { addElement(elem); } else { addElement(elem); //just to make sure it is big enough for(int i=_count-1;i>index;i--) { _data[i]=_data[i-1]; } _data[index]=elem; } } void setElementAt(ELEMTYPE elem,int index) { if((index<0)||(index>=_count)) { return; } _data[index]=elem; } void removeElementAt(int index) { if((index<0)||(index>=_count)) { return; } //slide everything to the right of index, left one. for(int i=index;i<(_count-1);i++) { _data[i]=_data[i+1]; } _count--; } void removeElement(ELEMTYPE elem) { for(int i=0;i<_count;i++) { if(_data[i]==elem) { removeElementAt(i); break; } } } void removeAll() { _count=0; } ELEMTYPE operator[](int index) { if((index<0)||(index>=_count)) { return null; } return _data[index]; } protected: int _count; int _capacity; ELEMTYPE* _data; }; #ifdef _WIN32 //forward referencing all the template types used so they get exported template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar*>; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; template class VGUIAPI Dar; #endif } #endif