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: 1435


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 void set(unsigned int x,unsigned int y, T elem); 29 unsigned int ySize() const; 30 unsigned int xSize() const; 31 unsigned int Size() const; 32 protected: 33 T* element; 34 unsigned int xsize,ysize; 35 unsigned int size; 36 }; 37 38 template<class T> 39 dyn_2d<T>::dyn_2d(unsigned int x,unsigned int y) 40 { 41 xsize=x; 42 ysize=y; 43 size=xsize*ysize; 44 element=new T[size]; 45 } 46 47 template<class T> 48 dyn_2d<T>::dyn_2d(const dyn_2d<T>& d) 49 { 50 // unimplemented... 51 } 52 53 template<class T> 54 dyn_2d<T>::~dyn_2d() 55 { 56 if(element) 57 delete [] element; 58 } 59 60 template<class T> 61 dyn_2d<T>& dyn_2d<T>::operator =(const dyn_2d<T>& d) 62 { 63 if(d.element) 64 { 65 if(size!=d.size) 66 resize(d.xsize,d.ysize); 67 68 for(int i=0;i<size;i++) 69 element[i]=d.element[i]; 70 } 71 } 72 73 template<class T> 74 bool dyn_2d<T>::resize(unsigned int x,unsigned int y) 75 { 76 if(element) 77 delete [] element; 78 xsize=x; 79 ysize=y; 80 size=xsize*ysize; 81 element=new T[size]; 82 return true; 83 } 84 85 template<class T> 86 T& dyn_2d<T>::get(unsigned int x,unsigned int y) 87 { 88 return element[y*xsize+x]; 89 } 90 91 template<class T> 92 void dyn_2d<T>::set(unsigned int x,unsigned int y, T elem) 93 { 94 element[y*xsize+x] = elem; 95 } 96 97 template<class T> 98 unsigned int dyn_2d<T>::xSize() const 99 { 100 return xsize; 101 } 102 103 template<class T> 104 unsigned int dyn_2d<T>::ySize() const 105 { 106 return ysize; 107 } 108 109 template<class T> 110 unsigned int dyn_2d<T>::Size() const 111 { 112 return size; 113 } 114 115 #endif // DYN_2D_HPP 116

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS