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