Mercurial > hg > octave-nkf
annotate src/ov-base-scalar.cc @ 10784:ca2df6737d6b
generalize cell2mat optimization to n dimensions
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Jul 2010 21:32:18 +0200 |
parents | 654fbde5dceb |
children | 7fa044155982 |
rev | line source |
---|---|
3277 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2004, 2005, 2007, 2008, 2009 |
7017 | 4 John W. Eaton |
3277 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3277 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3277 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
3277 | 29 |
4055 | 30 #include "oct-obj.h" |
3277 | 31 #include "ov-base.h" |
3933 | 32 #include "ov-cx-mat.h" |
33 #include "ov-re-mat.h" | |
3277 | 34 #include "ov-base-scalar.h" |
35 #include "pr-output.h" | |
36 | |
37 template <class ST> | |
3933 | 38 octave_value |
4247 | 39 octave_base_scalar<ST>::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
40 const std::list<octave_value_list>& idx) |
3933 | 41 { |
42 octave_value retval; | |
43 | |
44 switch (type[0]) | |
45 { | |
46 case '(': | |
47 retval = do_index_op (idx.front ()); | |
48 break; | |
49 | |
50 case '{': | |
51 case '.': | |
52 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
53 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
54 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
3933 | 55 } |
56 break; | |
57 | |
58 default: | |
59 panic_impossible (); | |
60 } | |
61 | |
62 return retval.next_subsref (type, idx); | |
63 } | |
64 | |
65 template <class ST> | |
66 octave_value | |
4247 | 67 octave_base_scalar<ST>::subsasgn (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
68 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
69 const octave_value& rhs) |
3933 | 70 { |
71 octave_value retval; | |
72 | |
73 switch (type[0]) | |
74 { | |
75 case '(': | |
76 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
77 if (type.length () == 1) |
8437
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
78 retval = numeric_assign (type, idx, rhs); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
79 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
80 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
81 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
82 error ("in indexed assignment of %s, last rhs index must be ()", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
83 nm.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
84 } |
3933 | 85 } |
86 break; | |
87 | |
88 case '{': | |
89 case '.': | |
90 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
91 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
3933 | 93 } |
94 break; | |
95 | |
96 default: | |
97 panic_impossible (); | |
98 } | |
99 | |
100 return retval; | |
101 } | |
102 | |
103 template <class ST> | |
10545
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
104 octave_value |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
105 octave_base_scalar<ST>::permute (const Array<int>& vec, bool inv) const |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
106 { |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
107 return Array<ST> (1, 1, scalar).permute (vec, inv); |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
108 } |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
109 |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
110 template <class ST> |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
111 octave_value |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
112 octave_base_scalar<ST>::reshape (const dim_vector& new_dims) const |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
113 { |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
114 return Array<ST> (1, 1, scalar).reshape (new_dims); |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
115 } |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
116 |
ffe28cdc6fe2
fix reshape() and permute() for scalars
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
117 template <class ST> |
8626
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
118 bool |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
119 octave_base_scalar<ST>::is_true (void) const |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
120 { |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
121 bool retval = false; |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
122 |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
123 if (xisnan (scalar)) |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
124 error ("invalid conversion from NaN to logical"); |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
125 else |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
126 retval = (scalar != ST ()); |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
127 |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
128 return retval; |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
129 } |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
130 |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8437
diff
changeset
|
131 template <class ST> |
3277 | 132 void |
3523 | 133 octave_base_scalar<ST>::print (std::ostream& os, bool pr_as_read_syntax) const |
3277 | 134 { |
135 print_raw (os, pr_as_read_syntax); | |
136 newline (os); | |
137 } | |
138 | |
139 template <class ST> | |
140 void | |
3933 | 141 octave_base_scalar<ST>::print_raw (std::ostream& os, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
142 bool pr_as_read_syntax) const |
3277 | 143 { |
144 indent (os); | |
145 octave_print_internal (os, scalar, pr_as_read_syntax); | |
146 } | |
147 | |
148 template <class ST> | |
149 bool | |
3933 | 150 octave_base_scalar<ST>::print_name_tag (std::ostream& os, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
151 const std::string& name) const |
3277 | 152 { |
153 indent (os); | |
154 os << name << " = "; | |
155 return false; | |
156 } | |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
157 |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
158 template <class ST> |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
159 bool |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
160 octave_base_scalar<ST>::fast_elem_insert_self (void *where, builtin_type_t btyp) const |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
161 { |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
162 |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
163 // Don't use builtin_type () here to avoid an extra VM call. |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
164 if (btyp == class_to_btyp<ST>::btyp) |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
165 { |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
166 *(reinterpret_cast<ST *>(where)) = scalar; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
167 return true; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
168 } |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
169 else |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
170 return false; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10545
diff
changeset
|
171 } |