1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <cassert> |
|
33 |
|
34 #include <iostream.h> |
|
35 |
|
36 #include "Array3.h" |
|
37 |
|
38 #if defined (HEAVYWEIGHT_INDEXING) |
|
39 #include "idx-vector.h" |
|
40 #include "Array3-idx.h" |
|
41 #endif |
|
42 |
|
43 #include "lo-error.h" |
|
44 |
|
45 // Three dimensional array class. |
|
46 |
|
47 template <class T> |
|
48 void |
2477
|
49 Array3<T>::resize (int r, int c, int p) |
1988
|
50 { |
2477
|
51 if (r < 0 || c < 0 || p < 0) |
|
52 { |
|
53 (*current_liboctave_error_handler) |
|
54 ("can't resize to negative dimension"); |
|
55 return; |
|
56 } |
|
57 |
|
58 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
|
59 return; |
|
60 |
|
61 ArrayRep *old_rep = rep; |
|
62 const T *old_data = data (); |
|
63 |
|
64 int old_d1 = dim1 (); |
|
65 int old_d2 = dim2 (); |
|
66 int old_d3 = dim3 (); |
|
67 int old_len = length (); |
|
68 |
|
69 rep = new ArrayRep (r*c*p); |
|
70 |
|
71 d1 = r; |
|
72 d2 = c; |
|
73 d3 = p; |
|
74 |
|
75 if (old_data && old_len > 0) |
|
76 { |
|
77 int min_r = old_d1 < r ? old_d1 : r; |
|
78 int min_c = old_d2 < c ? old_d2 : c; |
|
79 int min_p = old_d3 < p ? old_d3 : p; |
|
80 |
|
81 for (int k = 0; k < min_p; k++) |
|
82 for (int j = 0; j < min_c; j++) |
|
83 for (int i = 0; i < min_r; i++) |
|
84 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
|
85 } |
|
86 |
|
87 if (--old_rep->count <= 0) |
|
88 delete old_rep; |
1988
|
89 } |
|
90 |
|
91 template <class T> |
|
92 void |
2478
|
93 Array3<T>::resize (int r, int c, int p, const T& val) |
1988
|
94 { |
2477
|
95 if (r < 0 || c < 0 || p < 0) |
|
96 { |
|
97 (*current_liboctave_error_handler) |
|
98 ("can't resize to negative dimension"); |
|
99 return; |
|
100 } |
|
101 |
|
102 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
|
103 return; |
|
104 |
|
105 ArrayRep *old_rep = rep; |
|
106 const T *old_data = data (); |
|
107 |
|
108 int old_d1 = dim1 (); |
|
109 int old_d2 = dim2 (); |
|
110 int old_d3 = dim3 (); |
2478
|
111 |
2477
|
112 int old_len = length (); |
|
113 |
|
114 rep = new ArrayRep (r*c*p); |
|
115 |
|
116 d1 = r; |
|
117 d2 = c; |
|
118 d3 = p; |
|
119 |
2478
|
120 int min_r = old_d1 < r ? old_d1 : r; |
|
121 int min_c = old_d2 < c ? old_d2 : c; |
|
122 int min_p = old_d3 < p ? old_d3 : p; |
2477
|
123 |
2478
|
124 if (old_data && old_len > 0) |
|
125 for (int k = 0; k < min_p; k++) |
|
126 for (int j = 0; j < min_c; j++) |
|
127 for (int i = 0; i < min_r; i++) |
|
128 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
2477
|
129 |
|
130 // If the copy constructor is expensive, this may win. Otherwise, |
|
131 // it may make more sense to just copy the value everywhere when |
|
132 // making the new ArrayRep. |
|
133 |
|
134 for (int k = 0; k < min_p; k++) |
|
135 for (int j = min_c; j < c; j++) |
|
136 for (int i = 0; i < min_r; i++) |
|
137 xelem (i, j, k) = val; |
|
138 |
|
139 for (int k = 0; k < min_p; k++) |
|
140 for (int j = 0; j < c; j++) |
|
141 for (int i = min_r; i < r; i++) |
|
142 xelem (i, j, k) = val; |
|
143 |
|
144 for (int k = min_p; k < p; k++) |
|
145 for (int j = 0; j < c; j++) |
|
146 for (int i = 0; i < r; i++) |
|
147 xelem (i, j, k) = val; |
|
148 |
|
149 if (--old_rep->count <= 0) |
|
150 delete old_rep; |
1988
|
151 } |
|
152 |
|
153 /* |
|
154 ;;; Local Variables: *** |
|
155 ;;; mode: C++ *** |
|
156 ;;; End: *** |
|
157 */ |