1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1988
|
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 |
3503
|
34 #include <iostream> |
1988
|
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 |
3492
|
69 int ts = get_size (get_size (r, c), p); |
|
70 |
|
71 rep = new ArrayRep (ts); |
2477
|
72 |
|
73 d1 = r; |
|
74 d2 = c; |
|
75 d3 = p; |
|
76 |
|
77 if (old_data && old_len > 0) |
|
78 { |
|
79 int min_r = old_d1 < r ? old_d1 : r; |
|
80 int min_c = old_d2 < c ? old_d2 : c; |
|
81 int min_p = old_d3 < p ? old_d3 : p; |
|
82 |
|
83 for (int k = 0; k < min_p; k++) |
|
84 for (int j = 0; j < min_c; j++) |
|
85 for (int i = 0; i < min_r; i++) |
|
86 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
|
87 } |
|
88 |
|
89 if (--old_rep->count <= 0) |
|
90 delete old_rep; |
1988
|
91 } |
|
92 |
|
93 template <class T> |
|
94 void |
2478
|
95 Array3<T>::resize (int r, int c, int p, const T& val) |
1988
|
96 { |
2477
|
97 if (r < 0 || c < 0 || p < 0) |
|
98 { |
|
99 (*current_liboctave_error_handler) |
|
100 ("can't resize to negative dimension"); |
|
101 return; |
|
102 } |
|
103 |
|
104 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
|
105 return; |
|
106 |
|
107 ArrayRep *old_rep = rep; |
|
108 const T *old_data = data (); |
|
109 |
|
110 int old_d1 = dim1 (); |
|
111 int old_d2 = dim2 (); |
|
112 int old_d3 = dim3 (); |
2478
|
113 |
2477
|
114 int old_len = length (); |
|
115 |
3492
|
116 int ts = get_size (get_size (r, c), p); |
|
117 |
|
118 rep = new ArrayRep (ts); |
2477
|
119 |
|
120 d1 = r; |
|
121 d2 = c; |
|
122 d3 = p; |
|
123 |
2478
|
124 int min_r = old_d1 < r ? old_d1 : r; |
|
125 int min_c = old_d2 < c ? old_d2 : c; |
|
126 int min_p = old_d3 < p ? old_d3 : p; |
2477
|
127 |
2478
|
128 if (old_data && old_len > 0) |
|
129 for (int k = 0; k < min_p; k++) |
|
130 for (int j = 0; j < min_c; j++) |
|
131 for (int i = 0; i < min_r; i++) |
|
132 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
2477
|
133 |
|
134 // If the copy constructor is expensive, this may win. Otherwise, |
|
135 // it may make more sense to just copy the value everywhere when |
|
136 // making the new ArrayRep. |
|
137 |
|
138 for (int k = 0; k < min_p; k++) |
|
139 for (int j = min_c; j < c; j++) |
|
140 for (int i = 0; i < min_r; i++) |
|
141 xelem (i, j, k) = val; |
|
142 |
|
143 for (int k = 0; k < min_p; k++) |
|
144 for (int j = 0; j < c; j++) |
|
145 for (int i = min_r; i < r; i++) |
|
146 xelem (i, j, k) = val; |
|
147 |
|
148 for (int k = min_p; k < p; k++) |
|
149 for (int j = 0; j < c; j++) |
|
150 for (int i = 0; i < r; i++) |
|
151 xelem (i, j, k) = val; |
|
152 |
|
153 if (--old_rep->count <= 0) |
|
154 delete old_rep; |
1988
|
155 } |
|
156 |
|
157 /* |
|
158 ;;; Local Variables: *** |
|
159 ;;; mode: C++ *** |
|
160 ;;; End: *** |
|
161 */ |