3353
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3353
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
4513
|
28 #include "idx-vector.h" |
|
29 |
3353
|
30 #include "Cell.h" |
4919
|
31 #include "error.h" |
5602
|
32 #include "gripes.h" |
3353
|
33 |
5805
|
34 Cell::Cell (const string_vector& sv, bool trim) |
4513
|
35 : ArrayN<octave_value> () |
4216
|
36 { |
5275
|
37 octave_idx_type n = sv.length (); |
4216
|
38 |
|
39 if (n > 0) |
|
40 { |
4625
|
41 resize (dim_vector (n, 1)); |
4216
|
42 |
5275
|
43 for (octave_idx_type i = 0; i < n; i++) |
5805
|
44 { |
|
45 std::string s = sv[i]; |
|
46 |
|
47 if (trim) |
|
48 { |
5814
|
49 size_t pos = s.find_last_not_of (' '); |
5805
|
50 |
5814
|
51 s = (pos == NPOS) ? "" : s.substr (0, pos+1); |
5805
|
52 } |
|
53 |
|
54 elem(i,0) = s; |
|
55 } |
4216
|
56 } |
|
57 } |
3354
|
58 |
6116
|
59 // Set size to DV, filling with []. Then fill with as many elements of |
|
60 // SV as possible. |
|
61 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim) |
|
62 : ArrayN<octave_value> (dv) |
|
63 { |
|
64 octave_idx_type n = sv.length (); |
|
65 |
|
66 if (n > 0) |
|
67 { |
|
68 octave_idx_type m = numel (); |
|
69 |
|
70 octave_idx_type len = n > m ? m : n; |
|
71 |
|
72 for (octave_idx_type i = 0; i < len; i++) |
|
73 { |
|
74 std::string s = sv[i]; |
|
75 |
|
76 if (trim) |
|
77 { |
|
78 size_t pos = s.find_last_not_of (' '); |
|
79 |
|
80 s = (pos == NPOS) ? "" : s.substr (0, pos+1); |
|
81 } |
|
82 |
|
83 elem(i) = s; |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 bool |
|
89 Cell::is_cellstr (void) const |
|
90 { |
|
91 bool retval = true; |
|
92 |
|
93 for (int i = 0; i < numel (); i++) |
|
94 { |
|
95 if (! elem(i).is_string ()) |
|
96 { |
|
97 retval = false; |
|
98 break; |
|
99 } |
|
100 } |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
4513
|
105 Cell |
4587
|
106 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const |
4513
|
107 { |
|
108 Cell retval; |
|
109 |
5275
|
110 octave_idx_type n = idx_arg.length (); |
4513
|
111 |
|
112 switch (n) |
|
113 { |
5539
|
114 case 0: |
|
115 retval = *this; |
|
116 break; |
|
117 |
4513
|
118 case 1: |
|
119 { |
4587
|
120 idx_vector i = idx_arg(0).index_vector (); |
4513
|
121 |
4919
|
122 if (! error_state) |
|
123 retval = index (i, resize_ok); |
4513
|
124 } |
|
125 break; |
|
126 |
|
127 case 2: |
|
128 { |
4587
|
129 idx_vector i = idx_arg(0).index_vector (); |
4513
|
130 |
4919
|
131 if (! error_state) |
|
132 { |
|
133 idx_vector j = idx_arg(1).index_vector (); |
|
134 |
|
135 if (! error_state) |
|
136 retval = index (i, j, resize_ok); |
|
137 } |
4513
|
138 } |
|
139 break; |
|
140 |
|
141 default: |
|
142 { |
|
143 Array<idx_vector> iv (n); |
|
144 |
5275
|
145 for (octave_idx_type i = 0; i < n; i++) |
4919
|
146 { |
|
147 iv(i) = idx_arg(i).index_vector (); |
4513
|
148 |
4919
|
149 if (error_state) |
|
150 break; |
|
151 } |
|
152 |
|
153 if (!error_state) |
|
154 retval = index (iv, resize_ok); |
4513
|
155 } |
|
156 break; |
|
157 } |
|
158 |
|
159 return retval; |
|
160 } |
|
161 |
|
162 Cell& |
4587
|
163 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs, |
4513
|
164 const octave_value& fill_val) |
|
165 |
|
166 { |
5275
|
167 for (octave_idx_type i = 0; i < idx_arg.length (); i++) |
4587
|
168 set_index (idx_arg(i).index_vector ()); |
4513
|
169 |
|
170 ::assign (*this, rhs, fill_val); |
|
171 |
|
172 return *this; |
|
173 } |
|
174 |
5602
|
175 octave_idx_type |
|
176 Cell::nnz (void) const |
|
177 { |
|
178 gripe_wrong_type_arg ("nnz", "cell array"); |
|
179 return -1; |
|
180 } |
|
181 |
4915
|
182 Cell |
5570
|
183 Cell::column (octave_idx_type i) const |
|
184 { |
|
185 Cell retval; |
|
186 |
|
187 if (ndims () < 3) |
|
188 { |
|
189 if (i < 0 || i >= cols ()) |
|
190 error ("invalid column selection"); |
|
191 else |
|
192 { |
|
193 octave_idx_type nr = rows (); |
|
194 |
|
195 retval.resize (dim_vector (nr, 1)); |
|
196 |
|
197 for (octave_idx_type j = 0; j < nr; j++) |
|
198 retval.xelem (j) = elem (j, i); |
|
199 } |
|
200 } |
|
201 else |
|
202 error ("Cell::column: requires 2-d cell array"); |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
|
207 Cell |
5275
|
208 Cell::concat (const Cell& rb, const Array<octave_idx_type>& ra_idx) |
4806
|
209 { |
5073
|
210 return insert (rb, ra_idx); |
4915
|
211 } |
|
212 |
|
213 Cell& |
5275
|
214 Cell::insert (const Cell& a, octave_idx_type r, octave_idx_type c) |
4915
|
215 { |
|
216 Array<octave_value>::insert (a, r, c); |
|
217 return *this; |
|
218 } |
|
219 |
|
220 Cell& |
5275
|
221 Cell::insert (const Cell& a, const Array<octave_idx_type>& ra_idx) |
4915
|
222 { |
|
223 Array<octave_value>::insert (a, ra_idx); |
|
224 return *this; |
4806
|
225 } |
|
226 |
3353
|
227 /* |
|
228 ;;; Local Variables: *** |
|
229 ;;; mode: C++ *** |
|
230 ;;; End: *** |
|
231 */ |