1 //////////////////////////////////////////////////////////////////////////////
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
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
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef two_dimensional_array_h
26 #define two_dimensional_array_h
28 #include <AD/generic/generic.h>
34 int M
, N
; // rows and columns
36 T
* array
; // array in flatten form
40 ///////////////////////////////////////////////////////////////////////
41 // Constructor and destructors
42 ///////////////////////////////////////////////////////////////////////
43 Array2() : M(0), N(0), len(0), array(0) {}
45 Array2(int m
, int n
, const T
&);
46 Array2(const Array2
&);
47 ~Array2() { delete [] array
; }
49 ///////////////////////////////////////////////////////////////////////
51 ///////////////////////////////////////////////////////////////////////
52 Array2
& operator = (const Array2
&);
53 Array2
& operator = (const T
&);
55 ///////////////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////////////////////
71 Array2
<T
>::Array2(int m
, int n
)
72 : M(m
), N(n
), len(m
* n
), array(new T
[len
]) {}
75 Array2
<T
>::Array2(int m
, int n
, const T
& e
)
76 : M(m
), N(n
), len(m
* n
), array(new T
[len
])
80 Array2
<T
>::Array2(const Array2
<T
>& A
) : M(0), N(0), array(0)
84 Array2
<T
>& Array2
<T
>::operator = (const Array2
<T
>& 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
];
93 for (i
= len
, q
= array
, p
= A
.array
; i
> 0; i
--)
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
;