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. |
|
60 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim) |
|
61 : ArrayN<octave_value> (dv) |
|
62 { |
|
63 octave_idx_type n = sv.length (); |
|
64 |
|
65 if (n > 0) |
|
66 { |
|
67 octave_idx_type m = numel (); |
|
68 |
|
69 octave_idx_type len = n > m ? m : n; |
|
70 |
|
71 for (octave_idx_type i = 0; i < len; i++) |
|
72 { |
|
73 std::string s = sv[i]; |
|
74 |
|
75 if (trim) |
|
76 { |
|
77 size_t pos = s.find_last_not_of (' '); |
|
78 |
|
79 s = (pos == NPOS) ? "" : s.substr (0, pos+1); |
|
80 } |
|
81 |
|
82 elem(i) = s; |
|
83 } |
|
84 } |
|
85 } |
|
86 |
|
87 bool |
|
88 Cell::is_cellstr (void) const |
|
89 { |
|
90 bool retval = true; |
|
91 |
|
92 for (int i = 0; i < numel (); i++) |
|
93 { |
|
94 if (! elem(i).is_string ()) |
|
95 { |
|
96 retval = false; |
|
97 break; |
|
98 } |
|
99 } |
|
100 |
|
101 return retval; |
|
102 } |
|
103 |
4513
|
104 Cell |
4587
|
105 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const |
4513
|
106 { |
|
107 Cell retval; |
|
108 |
5275
|
109 octave_idx_type n = idx_arg.length (); |
4513
|
110 |
|
111 switch (n) |
|
112 { |
5539
|
113 case 0: |
|
114 retval = *this; |
|
115 break; |
|
116 |
4513
|
117 case 1: |
|
118 { |
4587
|
119 idx_vector i = idx_arg(0).index_vector (); |
4513
|
120 |
4919
|
121 if (! error_state) |
|
122 retval = index (i, resize_ok); |
4513
|
123 } |
|
124 break; |
|
125 |
|
126 case 2: |
|
127 { |
4587
|
128 idx_vector i = idx_arg(0).index_vector (); |
4513
|
129 |
4919
|
130 if (! error_state) |
|
131 { |
|
132 idx_vector j = idx_arg(1).index_vector (); |
|
133 |
|
134 if (! error_state) |
|
135 retval = index (i, j, resize_ok); |
|
136 } |
4513
|
137 } |
|
138 break; |
|
139 |
|
140 default: |
|
141 { |
|
142 Array<idx_vector> iv (n); |
|
143 |
5275
|
144 for (octave_idx_type i = 0; i < n; i++) |
4919
|
145 { |
|
146 iv(i) = idx_arg(i).index_vector (); |
4513
|
147 |
4919
|
148 if (error_state) |
|
149 break; |
|
150 } |
|
151 |
|
152 if (!error_state) |
|
153 retval = index (iv, resize_ok); |
4513
|
154 } |
|
155 break; |
|
156 } |
|
157 |
|
158 return retval; |
|
159 } |
|
160 |
|
161 Cell& |
4587
|
162 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs, |
4513
|
163 const octave_value& fill_val) |
|
164 |
|
165 { |
5275
|
166 for (octave_idx_type i = 0; i < idx_arg.length (); i++) |
4587
|
167 set_index (idx_arg(i).index_vector ()); |
4513
|
168 |
|
169 ::assign (*this, rhs, fill_val); |
|
170 |
|
171 return *this; |
|
172 } |
|
173 |
5602
|
174 octave_idx_type |
|
175 Cell::nnz (void) const |
|
176 { |
|
177 gripe_wrong_type_arg ("nnz", "cell array"); |
|
178 return -1; |
|
179 } |
|
180 |
4915
|
181 Cell |
5570
|
182 Cell::column (octave_idx_type i) const |
|
183 { |
|
184 Cell retval; |
|
185 |
|
186 if (ndims () < 3) |
|
187 { |
|
188 if (i < 0 || i >= cols ()) |
|
189 error ("invalid column selection"); |
|
190 else |
|
191 { |
|
192 octave_idx_type nr = rows (); |
|
193 |
|
194 retval.resize (dim_vector (nr, 1)); |
|
195 |
|
196 for (octave_idx_type j = 0; j < nr; j++) |
|
197 retval.xelem (j) = elem (j, i); |
|
198 } |
|
199 } |
|
200 else |
|
201 error ("Cell::column: requires 2-d cell array"); |
|
202 |
|
203 return retval; |
|
204 } |
|
205 |
|
206 Cell |
5275
|
207 Cell::concat (const Cell& rb, const Array<octave_idx_type>& ra_idx) |
4806
|
208 { |
5073
|
209 return insert (rb, ra_idx); |
4915
|
210 } |
|
211 |
|
212 Cell& |
5275
|
213 Cell::insert (const Cell& a, octave_idx_type r, octave_idx_type c) |
4915
|
214 { |
|
215 Array<octave_value>::insert (a, r, c); |
|
216 return *this; |
|
217 } |
|
218 |
|
219 Cell& |
5275
|
220 Cell::insert (const Cell& a, const Array<octave_idx_type>& ra_idx) |
4915
|
221 { |
|
222 Array<octave_value>::insert (a, ra_idx); |
|
223 return *this; |
4806
|
224 } |
|
225 |
3353
|
226 /* |
|
227 ;;; Local Variables: *** |
|
228 ;;; mode: C++ *** |
|
229 ;;; End: *** |
|
230 */ |