Du bist hier: Snippet-Verzeichnis » C/C++ (495)
Sprache:

2d array template

Sprache: English
Programmiersprache: C++
Veröffentlicht von: rigelf [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1097


Beschreibung

I'm a bit confused about this licensing issue, but if it doesn't say so, this snippet is *LGPL*. You can distribute or modify it in any way, and even call it your own, though I'd prefer you not take credit where it's not due.The template param is whatever you want an array of, but must have an = operator. The code here is not as fast as if you wrote a custom class, but is fairly generic and should be portable (yell at me if it's not).The copy constr doesn't work, use =.

Code

1 // Very simple dynamic 2d array by Rigel Freden 2 // <rigelf@angelfire.com> 3 // Designed for use in game programming (namely a 2d map). 4 // the template parameter is whatever type you're using for a 5 // map tile. 6 // REMEMBER that resize is destructive! It destroys the previous 7 // map before it allocates space for the new one. 8 // Sorry I couldn't figure out how to double index the thing: 9 // dyn_2d<type> foo; foo[i][p]=x; 10 // if you know how to do this, please do. 11 12 #ifndef DYN_2D_HPP 13 #define DYN_2D_HPP 14 15 template<class T> 16 class dyn_2d 17 { 18 public: 19 dyn_2d() 20 :element(NULL) 21 {} 22 dyn_2d(unsigned int x,unsigned int y); 23 dyn_2d(const dyn_2d<T>&); 24 ~dyn_2d(); 25 dyn_2d<T>& operator =(const dyn_2d<T>&); 26 bool resize(unsigned int x,unsigned int y); 27 T& get(unsigned int x,unsigned int y); 28 unsigned int ySize() const; 29 unsigned int xSize() const; 30 unsigned int Size() const; 31 protected: 32 T* element; 33 unsigned int xsize,ysize; 34 unsigned int size; 35 }; 36 37 template<class T> 38 dyn_2d<T>::dyn_2d(unsigned int x,unsigned int y) 39 { 40 xsize=x; 41 ysize=y; 42 size=xsize*ysize; 43 element=new T[size]; 44 } 45 46 template<class T> 47 dyn_2d<T>::dyn_2d(const dyn_2d<T>& d) 48 { 49 // unimplemented... 50 } 51 52 template<class T> 53 dyn_2d<T>::~dyn_2d() 54 { 55 if(element) 56 delete [] element; 57 } 58 59 template<class T> 60 dyn_2d<T>& dyn_2d<T>::operator =(const dyn_2d<T>& d) 61 { 62 if(d.element) 63 { 64 if(size!=d.size) 65 resize(d.xsize,d.ysize); 66 67 for(int i=0;i<size;i++) 68 element[i]=d.element[i]; 69 } 70 } 71 72 template<class T> 73 bool dyn_2d<T>::resize(unsigned int x,unsigned int y) 74 { 75 if(element) 76 delete [] element; 77 xsize=x; 78 ysize=y; 79 size=xsize*ysize; 80 element=new T[size]; 81 } 82 83 template<class T> 84 T& dyn_2d<T>::get(unsigned int x,unsigned int y) 85 { 86 return element[y*xsize+x]; 87 } 88 89 template<class T> 90 unsigned int dyn_2d<T>::xSize() const 91 { 92 return xsize; 93 } 94 95 template<class T> 96 unsigned int dyn_2d<T>::ySize() const 97 { 98 return ysize; 99 } 100 101 template<class T> 102 unsigned int dyn_2d<T>::Size() const 103 { 104 return size; 105 } 106 107 #endif // DYN_2D_HPP 108

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS