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 { |
|
49 size_t n = s.find_last_not_of (' '); |
|
50 |
|
51 s = (n == NPOS) ? "" : s.substr (0, n+1); |
|
52 } |
|
53 |
|
54 elem(i,0) = s; |
|
55 } |
4216
|
56 } |
|
57 } |
3354
|
58 |
4513
|
59 Cell |
4587
|
60 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const |
4513
|
61 { |
|
62 Cell retval; |
|
63 |
5275
|
64 octave_idx_type n = idx_arg.length (); |
4513
|
65 |
|
66 switch (n) |
|
67 { |
5539
|
68 case 0: |
|
69 retval = *this; |
|
70 break; |
|
71 |
4513
|
72 case 1: |
|
73 { |
4587
|
74 idx_vector i = idx_arg(0).index_vector (); |
4513
|
75 |
4919
|
76 if (! error_state) |
|
77 retval = index (i, resize_ok); |
4513
|
78 } |
|
79 break; |
|
80 |
|
81 case 2: |
|
82 { |
4587
|
83 idx_vector i = idx_arg(0).index_vector (); |
4513
|
84 |
4919
|
85 if (! error_state) |
|
86 { |
|
87 idx_vector j = idx_arg(1).index_vector (); |
|
88 |
|
89 if (! error_state) |
|
90 retval = index (i, j, resize_ok); |
|
91 } |
4513
|
92 } |
|
93 break; |
|
94 |
|
95 default: |
|
96 { |
|
97 Array<idx_vector> iv (n); |
|
98 |
5275
|
99 for (octave_idx_type i = 0; i < n; i++) |
4919
|
100 { |
|
101 iv(i) = idx_arg(i).index_vector (); |
4513
|
102 |
4919
|
103 if (error_state) |
|
104 break; |
|
105 } |
|
106 |
|
107 if (!error_state) |
|
108 retval = index (iv, resize_ok); |
4513
|
109 } |
|
110 break; |
|
111 } |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 Cell& |
4587
|
117 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs, |
4513
|
118 const octave_value& fill_val) |
|
119 |
|
120 { |
5275
|
121 for (octave_idx_type i = 0; i < idx_arg.length (); i++) |
4587
|
122 set_index (idx_arg(i).index_vector ()); |
4513
|
123 |
|
124 ::assign (*this, rhs, fill_val); |
|
125 |
|
126 return *this; |
|
127 } |
|
128 |
5602
|
129 octave_idx_type |
|
130 Cell::nnz (void) const |
|
131 { |
|
132 gripe_wrong_type_arg ("nnz", "cell array"); |
|
133 return -1; |
|
134 } |
|
135 |
4915
|
136 Cell |
5570
|
137 Cell::column (octave_idx_type i) const |
|
138 { |
|
139 Cell retval; |
|
140 |
|
141 if (ndims () < 3) |
|
142 { |
|
143 if (i < 0 || i >= cols ()) |
|
144 error ("invalid column selection"); |
|
145 else |
|
146 { |
|
147 octave_idx_type nr = rows (); |
|
148 |
|
149 retval.resize (dim_vector (nr, 1)); |
|
150 |
|
151 for (octave_idx_type j = 0; j < nr; j++) |
|
152 retval.xelem (j) = elem (j, i); |
|
153 } |
|
154 } |
|
155 else |
|
156 error ("Cell::column: requires 2-d cell array"); |
|
157 |
|
158 return retval; |
|
159 } |
|
160 |
|
161 Cell |
5275
|
162 Cell::concat (const Cell& rb, const Array<octave_idx_type>& ra_idx) |
4806
|
163 { |
5073
|
164 return insert (rb, ra_idx); |
4915
|
165 } |
|
166 |
|
167 Cell& |
5275
|
168 Cell::insert (const Cell& a, octave_idx_type r, octave_idx_type c) |
4915
|
169 { |
|
170 Array<octave_value>::insert (a, r, c); |
|
171 return *this; |
|
172 } |
|
173 |
|
174 Cell& |
5275
|
175 Cell::insert (const Cell& a, const Array<octave_idx_type>& ra_idx) |
4915
|
176 { |
|
177 Array<octave_value>::insert (a, ra_idx); |
|
178 return *this; |
4806
|
179 } |
|
180 |
3353
|
181 /* |
|
182 ;;; Local Variables: *** |
|
183 ;;; mode: C++ *** |
|
184 ;;; End: *** |
|
185 */ |