source:
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Array.h
@
2291
Revision 2291, 1.6 KB checked in by gumbau, 17 years ago (diff) |
---|
Rev | Line | |
---|---|---|
[774] | 1 | #ifndef GFXTOOLS_ARRAY_INCLUDED // -*- C++ -*- |
2 | #define GFXTOOLS_ARRAY_INCLUDED | |
3 | ||
4 | #include <string.h> | |
5 | #include "../std.h" | |
6 | ||
[1025] | 7 | namespace simplif |
[774] | 8 | { |
9 | template<class T> | |
[1526] | 10 | class array |
11 | { | |
12 | protected: | |
13 | T *data; | |
14 | int len; | |
15 | public: | |
16 | array() { data=NULL; len=0; } | |
17 | array(int l) { init(l); } | |
18 | ~array() { f_r_e_e(); } | |
[774] | 19 | |
[1526] | 20 | inline void init(int l); |
21 | inline void f_r_e_e(); | |
22 | inline void resize(int l); | |
[774] | 23 | |
[1526] | 24 | inline T& ref(int i); |
25 | inline T& operator[](int i) { return data[i]; } | |
26 | inline T& operator()(int i) { return ref(i); } | |
[774] | 27 | |
[1526] | 28 | inline const T& ref(int i) const; |
29 | inline const T& operator[](int i) const { return data[i]; } | |
30 | inline const T& operator()(int i) const { return ref(i); } | |
[774] | 31 | |
[1526] | 32 | inline int length() const { return len; } |
33 | inline int maxLength() const { return len; } | |
34 | }; | |
[774] | 35 | |
36 | template<class T> | |
[1526] | 37 | inline void array<T>::init(int l) |
38 | { | |
[2291] | 39 | data = new T[l]; |
40 | len = l; | |
[1526] | 41 | } |
[774] | 42 | |
43 | template<class T> | |
[1526] | 44 | inline void array<T>::f_r_e_e() |
[774] | 45 | { |
[2291] | 46 | if(data) |
[1526] | 47 | { |
[2291] | 48 | delete [] data; |
[1526] | 49 | data = NULL; |
50 | } | |
[774] | 51 | } |
52 | ||
53 | template<class T> | |
[1526] | 54 | inline T& array<T>::ref(int i) |
55 | { | |
56 | #ifdef SAFETY | |
[2291] | 57 | assert(data); |
58 | assert(i >= 0 && i < len); | |
[1526] | 59 | #endif |
60 | return data[i]; | |
61 | } | |
[774] | 62 | |
63 | template<class T> | |
[1526] | 64 | inline const T& array<T>::ref(int i) const |
65 | { | |
66 | #ifdef SAFETY | |
[2291] | 67 | assert(data); |
68 | assert(i >= 0 && i < len); | |
[1526] | 69 | #endif |
70 | return data[i]; | |
71 | } | |
[774] | 72 | |
73 | template<class T> | |
[1526] | 74 | inline void array<T>::resize(int l) |
75 | { | |
[2291] | 76 | T *old = data; |
77 | data = new T[l]; | |
78 | data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T)); | |
79 | len = l; | |
80 | ||
81 | delete [] old; | |
[1526] | 82 | } |
[774] | 83 | } |
84 | ||
85 | // GFXTOOLS_ARRAY_INCLUDED | |
86 | #endif |
Note: See TracBrowser
for help on using the repository browser.