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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream.h> |
|
32 #include <strstream.h> |
|
33 |
|
34 #include "lo-utils.h" |
|
35 |
|
36 #include "defun.h" |
|
37 #include "error.h" |
|
38 #include "ov-cell.h" |
3354
|
39 #include "oct-obj.h" |
3353
|
40 #include "unwind-prot.h" |
3354
|
41 #include "utils.h" |
3353
|
42 |
|
43 DEFINE_OCTAVE_ALLOCATOR (octave_cell); |
|
44 |
|
45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell"); |
|
46 |
|
47 octave_value |
|
48 octave_cell::do_index_op (const octave_value_list& idx) |
|
49 { |
|
50 octave_value retval; |
|
51 |
3354
|
52 int len = idx.length (); |
|
53 |
|
54 switch (len) |
3353
|
55 { |
3354
|
56 case 2: |
|
57 { |
|
58 idx_vector i = idx (0).index_vector (); |
|
59 idx_vector j = idx (1).index_vector (); |
|
60 |
|
61 retval = cell_val.index (i, j); |
|
62 } |
|
63 break; |
3353
|
64 |
3354
|
65 case 1: |
|
66 { |
|
67 idx_vector i = idx (0).index_vector (); |
|
68 |
|
69 retval = cell_val.index (i); |
|
70 } |
|
71 break; |
|
72 |
|
73 default: |
|
74 { |
|
75 string n = type_name (); |
|
76 |
|
77 error ("invalid number of indices (%d) for %s value", |
|
78 len, n.c_str ()); |
|
79 } |
|
80 break; |
3353
|
81 } |
|
82 |
|
83 return retval; |
|
84 } |
|
85 |
|
86 void |
|
87 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
|
88 { |
|
89 #if 0 |
|
90 if (idx.length () == 1) |
|
91 { |
|
92 int i = idx(0).int_value (true); |
|
93 |
|
94 if (! error_state) |
|
95 { |
|
96 int n = lst.length (); |
|
97 |
|
98 if (i > 0 && (Vresize_on_range_error || i <= n)) |
|
99 lst(i-1) = rhs; |
|
100 else |
|
101 error ("list index = %d out of range", i); |
|
102 } |
|
103 else |
|
104 error ("list index must be an integer"); |
|
105 } |
|
106 else |
|
107 error ("lists may only be indexed by a single scalar"); |
|
108 #endif |
|
109 } |
|
110 |
|
111 void |
|
112 octave_cell::print (ostream& os, bool) const |
|
113 { |
|
114 print_raw (os); |
|
115 } |
|
116 |
|
117 void |
|
118 octave_cell::print_raw (ostream& os, bool) const |
|
119 { |
|
120 unwind_protect::begin_frame ("octave_cell_print"); |
|
121 |
|
122 int nr = cell_val.rows (); |
|
123 int nc = cell_val.columns(); |
|
124 |
|
125 if (nr > 0 && nc > 0) |
|
126 { |
|
127 indent (os); |
|
128 os << "{"; |
|
129 newline (os); |
|
130 |
|
131 increment_indent_level (); |
|
132 |
|
133 for (int j = 0; j < nc; j++) |
|
134 { |
|
135 for (int i = 0; i < nr; i++) |
|
136 { |
|
137 ostrstream buf; |
|
138 buf << "[" << i+1 << "," << j+1 << "]" << ends; |
|
139 const char *nm = buf.str (); |
|
140 |
|
141 octave_value val = cell_val(i,j); |
|
142 |
|
143 val.print_with_name (os, nm); |
|
144 |
|
145 delete [] nm; |
|
146 } |
|
147 } |
|
148 |
|
149 decrement_indent_level (); |
|
150 |
|
151 indent (os); |
|
152 |
|
153 os << "}"; |
|
154 } |
|
155 else |
|
156 os << "{}"; |
|
157 |
|
158 newline (os); |
|
159 |
|
160 unwind_protect::run_frame ("octave_cell_print"); |
|
161 } |
|
162 |
|
163 bool |
|
164 octave_cell::print_name_tag (ostream& os, const string& name) const |
|
165 { |
|
166 indent (os); |
|
167 if (is_empty ()) |
|
168 os << name << " = "; |
|
169 else |
|
170 { |
|
171 os << name << " ="; |
|
172 newline (os); |
|
173 } |
|
174 return false; |
|
175 } |
|
176 |
3354
|
177 DEFUN (iscell, args, , |
|
178 "iscell (x): return nonzero if x is a cell array") |
|
179 { |
|
180 octave_value retval; |
|
181 |
|
182 if (args.length () == 1) |
|
183 retval = args(0).is_cell (); |
|
184 else |
|
185 print_usage ("iscell"); |
|
186 |
|
187 return retval; |
|
188 } |
|
189 |
|
190 DEFUN (cell, args, , |
|
191 "cell (N)\n\ |
|
192 cell (M, N)\n\ |
|
193 cell (size (A))") |
|
194 { |
|
195 octave_value retval; |
|
196 |
|
197 int nargin = args.length (); |
|
198 |
|
199 switch (nargin) |
|
200 { |
|
201 case 1: |
|
202 { |
|
203 int nr, nc; |
|
204 get_dimensions (args(0), "cell", nr, nc); |
|
205 |
|
206 if (! error_state) |
|
207 retval = Cell (nr, nc, Matrix ()); |
|
208 } |
|
209 break; |
|
210 |
|
211 case 2: |
|
212 { |
|
213 int nr, nc; |
|
214 get_dimensions (args(0), args(1), "cell", nr, nc); |
|
215 |
|
216 if (! error_state) |
|
217 retval = Cell (nr, nc, Matrix ()); |
|
218 } |
|
219 break; |
|
220 |
|
221 default: |
|
222 print_usage ("cell"); |
|
223 break; |
|
224 } |
|
225 |
|
226 return retval; |
|
227 } |
|
228 |
3353
|
229 /* |
|
230 ;;; Local Variables: *** |
|
231 ;;; mode: C++ *** |
|
232 ;;; End: *** |
|
233 */ |