2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 "defun.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "ov-scalar.h" |
2423
|
35 #include "ov-re-mat.h" |
2376
|
36 #include "ov-typeinfo.h" |
|
37 #include "pr-output.h" |
|
38 #include "xdiv.h" |
|
39 #include "xpow.h" |
|
40 |
2477
|
41 octave_allocator |
|
42 octave_scalar::allocator (sizeof (octave_scalar)); |
2376
|
43 |
2477
|
44 int |
|
45 octave_scalar::t_id (-1); |
|
46 |
|
47 const string |
|
48 octave_scalar::t_name ("scalar"); |
2376
|
49 |
|
50 static inline bool |
|
51 valid_scalar_indices (const octave_value_list& args) |
|
52 { |
|
53 int nargin = args.length (); |
|
54 |
|
55 for (int i = 0; i < nargin; i++) |
|
56 if (! args(i).valid_as_scalar_index ()) |
|
57 return false; |
|
58 |
|
59 return true; |
|
60 } |
|
61 |
|
62 octave_value |
|
63 octave_scalar::index (const octave_value_list& idx) const |
|
64 { |
|
65 octave_value retval; |
|
66 |
|
67 if (valid_scalar_indices (idx)) |
|
68 retval = scalar; |
|
69 else |
|
70 { |
|
71 // XXX FIXME XXX -- this doesn't solve the problem of |
|
72 // |
|
73 // a = 1; a([1,1], [1,1], [1,1]) |
|
74 // |
|
75 // and similar constructions. Hmm... |
|
76 |
2423
|
77 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
78 // 1x1 matrix back to a scalar value. Need a better solution |
|
79 // to this problem. |
|
80 |
|
81 octave_value tmp (new octave_matrix (matrix_value ())); |
2376
|
82 |
|
83 retval = tmp.index (idx); |
|
84 } |
|
85 |
|
86 return retval; |
|
87 } |
|
88 |
|
89 octave_value |
|
90 octave_scalar::convert_to_str (void) const |
|
91 { |
|
92 octave_value retval; |
|
93 |
|
94 if (xisnan (scalar)) |
|
95 ::error ("invalid conversion from NaN to character"); |
|
96 else |
|
97 { |
|
98 // XXX FIXME XXX -- warn about out of range conversions? |
|
99 |
|
100 int i = NINT (scalar); |
|
101 char s[2]; |
|
102 s[0] = (char) i; |
|
103 s[1] = '\0'; |
|
104 retval = octave_value (s); |
|
105 } |
|
106 |
|
107 return retval; |
|
108 } |
|
109 |
|
110 void |
2466
|
111 octave_scalar::print (ostream& os, bool pr_as_read_syntax) |
2376
|
112 { |
2466
|
113 octave_print_internal (os, scalar, pr_as_read_syntax); |
2376
|
114 } |
|
115 |
|
116 /* |
|
117 ;;; Local Variables: *** |
|
118 ;;; mode: C++ *** |
|
119 ;;; End: *** |
|
120 */ |