source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Array.h @ 1526

Revision 1526, 1.6 KB checked in by gumbau, 18 years ago (diff)

Updated modules to the new interface and the new simplification algorithm improvements.

Line 
1#ifndef GFXTOOLS_ARRAY_INCLUDED // -*- C++ -*-
2#define GFXTOOLS_ARRAY_INCLUDED
3
4#include <string.h>
5#include "../std.h"
6
7namespace simplif
8{
9        template<class T>
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(); }
19
20                                inline void init(int l);
21                                inline void f_r_e_e();
22                                inline void resize(int l);
23
24                                inline T& ref(int i);
25                                inline T& operator[](int i) { return data[i]; }
26                                inline T& operator()(int i) { return ref(i); }
27
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); }
31
32                                inline int length() const { return len; }
33                                inline int maxLength() const { return len; }
34                };
35
36        template<class T>
37                inline void array<T>::init(int l)
38                {
39                        data = new T[l];
40                        len = l;
41                }
42
43        template<class T>
44                inline void array<T>::f_r_e_e()
45                {
46                        if( data )
47                        {
48                                delete[] data;
49                                data = NULL;
50                        }
51                }
52
53        template<class T>
54                inline T& array<T>::ref(int i)
55                {
56#ifdef SAFETY
57                        assert( data );
58                        assert( i>=0 && i<len );
59#endif
60                        return data[i];
61                }
62
63        template<class T>
64                inline const T& array<T>::ref(int i) const
65                {
66#ifdef SAFETY
67                        assert( data );
68                        assert( i>=0 && i<len );
69#endif
70                        return data[i];
71                }
72
73        template<class T>
74                inline void array<T>::resize(int l)
75                {
76                        T *old = data;
77                        data = new T[l];
78                        data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T));
79                        len = l;
80                        delete[] old;
81                }
82}
83
84// GFXTOOLS_ARRAY_INCLUDED
85#endif
Note: See TracBrowser for help on using the repository browser.