3353
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1999, 2002, 2003, 2004, 2005, 2006, 2007 John W. Eaton |
3353
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3353
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3353
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
4513
|
27 #include "idx-vector.h" |
|
28 |
3353
|
29 #include "Cell.h" |
4919
|
30 #include "error.h" |
5602
|
31 #include "gripes.h" |
3353
|
32 |
5805
|
33 Cell::Cell (const string_vector& sv, bool trim) |
4513
|
34 : ArrayN<octave_value> () |
4216
|
35 { |
5275
|
36 octave_idx_type n = sv.length (); |
4216
|
37 |
|
38 if (n > 0) |
|
39 { |
4625
|
40 resize (dim_vector (n, 1)); |
4216
|
41 |
5275
|
42 for (octave_idx_type i = 0; i < n; i++) |
5805
|
43 { |
|
44 std::string s = sv[i]; |
|
45 |
|
46 if (trim) |
|
47 { |
5814
|
48 size_t pos = s.find_last_not_of (' '); |
5805
|
49 |
5814
|
50 s = (pos == NPOS) ? "" : s.substr (0, pos+1); |
5805
|
51 } |
|
52 |
|
53 elem(i,0) = s; |
|
54 } |
4216
|
55 } |
|
56 } |
3354
|
57 |
6116
|
58 // Set size to DV, filling with []. Then fill with as many elements of |
|
59 // SV as possible. |
7209
|
60 |
6116
|
61 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim) |
7209
|
62 : ArrayN<octave_value> (dv, resize_fill_value ()) |
6116
|
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 */ |