simple.cc - generated code example
[prop.git] / include / AD / contain / array2.h
blobb820e1549a932879e0d6858831d5e489062e1d0b
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef two_dimensional_array_h
26 #define two_dimensional_array_h
28 #include <AD/generic/generic.h>
30 template <class T>
31 class Array2 {
32 protected:
34 int M, N; // rows and columns
35 int len; // M * N
36 T * array; // array in flatten form
38 public:
40 ///////////////////////////////////////////////////////////////////////
41 // Constructor and destructors
42 ///////////////////////////////////////////////////////////////////////
43 Array2() : M(0), N(0), len(0), array(0) {}
44 Array2(int m, int n);
45 Array2(int m, int n, const T&);
46 Array2(const Array2&);
47 ~Array2() { delete [] array; }
49 ///////////////////////////////////////////////////////////////////////
50 // Assignment
51 ///////////////////////////////////////////////////////////////////////
52 Array2& operator = (const Array2&);
53 Array2& operator = (const T&);
55 ///////////////////////////////////////////////////////////////////////
56 // Selectors
57 ///////////////////////////////////////////////////////////////////////
58 int rows() const { return M; }
59 int columns() const { return N; }
60 int length() const { return len; }
61 const T * operator [] (int i) const { return array + i * N; }
62 T * operator [] (int i) { return array + i * N; }
63 const T operator () (int i) const { return array[i]; }
64 T& operator () (int i) { return array[i]; }
67 /////////////////////////////////////////////////////////////////////////////
68 // Implementation of additional template methods
69 /////////////////////////////////////////////////////////////////////////////
70 template <class T>
71 Array2<T>::Array2(int m, int n)
72 : M(m), N(n), len(m * n), array(new T [len]) {}
74 template <class T>
75 Array2<T>::Array2(int m, int n, const T& e)
76 : M(m), N(n), len(m * n), array(new T [len])
77 { *this = e; }
79 template <class T>
80 Array2<T>::Array2(const Array2<T>& A) : M(0), N(0), array(0)
81 { *this = A; }
83 template <class T>
84 Array2<T>& Array2<T>::operator = (const Array2<T>& A)
85 { if (this != &A) {
86 if (M != A.M || N != A.N) {
87 if (array) delete [] array;
88 M = A.M; N = A.N; len = A.len; array = new T [len];
90 register const T * p;
91 register T * q;
92 register int i;
93 for (i = len, q = array, p = A.array; i > 0; i--)
94 *q++ = *p++;
96 return *this;
99 template <class T>
100 Array2<T>& Array2<T>::operator = (const T& e)
101 { register T * p, * q;
102 for (p = array, q = array + len; p < q; ) *p++ = e;
103 return *this;
106 #endif