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