Mercurial > hg > octave-nkf
comparison src/ov-lazy-idx.h @ 10325:8b3cfc1288e2
implement lazy index conversions
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 16 Feb 2010 15:28:53 +0100 |
parents | |
children | de2d43bcb083 |
comparison
equal
deleted
inserted
replaced
10324:7673850d6adf | 10325:8b3cfc1288e2 |
---|---|
1 /* | |
2 | |
3 Copyright (C) 2010 VZLU Prague | |
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 3 of the License, or (at your | |
10 option) any 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, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 #if !defined (octave_lazy_idx_h) | |
24 #define octave_lazy_idx_h 1 | |
25 | |
26 #include "ov-re-mat.h" | |
27 | |
28 // Lazy indices that stay in idx_vector form until the conversion to NDArray is | |
29 // actually needed. | |
30 | |
31 class | |
32 OCTINTERP_API | |
33 octave_lazy_index : public octave_base_value | |
34 { | |
35 public: | |
36 | |
37 octave_lazy_index (void) | |
38 : octave_base_value () { } | |
39 | |
40 octave_lazy_index (const idx_vector& idx) | |
41 : octave_base_value (), index (idx) { } | |
42 | |
43 octave_lazy_index (const octave_lazy_index& i) | |
44 : octave_base_value (), index (i.index), value (i.value) { } | |
45 | |
46 ~octave_lazy_index (void) { } | |
47 | |
48 octave_base_value *clone (void) const { return new octave_lazy_index (*this); } | |
49 octave_base_value *empty_clone (void) const { return new octave_matrix (); } | |
50 | |
51 type_conv_info numeric_conversion_function (void) const; | |
52 | |
53 octave_base_value *try_narrowing_conversion (void); | |
54 | |
55 size_t byte_size (void) const { return numel () * sizeof (octave_idx_type); } | |
56 | |
57 // FIXME: should avoid conversion. | |
58 octave_value squeeze (void) const { return make_value ().squeeze (); } | |
59 | |
60 octave_value full_value (void) const { return make_value (); } | |
61 | |
62 idx_vector index_vector (void) const | |
63 { return index; } | |
64 | |
65 builtin_type_t builtin_type (void) const { return btyp_double; } | |
66 | |
67 bool is_real_matrix (void) const { return true; } | |
68 | |
69 bool is_real_type (void) const { return true; } | |
70 | |
71 bool is_double_type (void) const { return true; } | |
72 | |
73 bool is_float_type (void) const { return true; } | |
74 | |
75 octave_value subsref (const std::string& type, | |
76 const std::list<octave_value_list>& idx) | |
77 { return make_value ().subsref (type, idx); } | |
78 | |
79 octave_value_list subsref (const std::string& type, | |
80 const std::list<octave_value_list>& idx, int) | |
81 { return subsref (type, idx); } | |
82 | |
83 octave_value subsasgn (const std::string& type, | |
84 const std::list<octave_value_list>& idx, | |
85 const octave_value& rhs) | |
86 { return make_value ().subsasgn (type, idx, rhs); } | |
87 | |
88 octave_value do_index_op (const octave_value_list& idx, | |
89 bool resize_ok = false) | |
90 { return make_value ().do_index_op (idx, resize_ok); } | |
91 | |
92 dim_vector dims (void) const { return index.orig_dimensions (); } | |
93 | |
94 octave_idx_type numel (void) const { return index.length (0); } | |
95 | |
96 octave_idx_type nnz (void) const { return numel (); } | |
97 | |
98 // FIXME: should avoid conversion. | |
99 octave_value reshape (const dim_vector& new_dims) const | |
100 { return make_value ().reshape (new_dims); } | |
101 | |
102 octave_value permute (const Array<int>& vec, bool inv = false) const | |
103 { return make_value ().permute (vec, inv); } | |
104 | |
105 octave_value resize (const dim_vector& dv, bool fill = false) const | |
106 { return make_value ().resize (dv, fill); } | |
107 | |
108 octave_value all (int dim = 0) const { return make_value ().all (dim); } | |
109 octave_value any (int dim = 0) const { return make_value ().any (dim); } | |
110 | |
111 MatrixType matrix_type (void) const { return make_value ().matrix_type (); } | |
112 MatrixType matrix_type (const MatrixType& _typ) const | |
113 { return make_value ().matrix_type (_typ); } | |
114 | |
115 // FIXME: should avoid conversion. | |
116 sortmode is_sorted (sortmode mode = UNSORTED) const | |
117 { return make_value ().is_sorted (mode); } | |
118 | |
119 Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const | |
120 { return make_value ().sort_rows_idx (mode); } | |
121 | |
122 sortmode is_sorted_rows (sortmode mode = UNSORTED) const | |
123 { return make_value ().is_sorted_rows (mode); } | |
124 | |
125 bool is_matrix_type (void) const { return true; } | |
126 | |
127 bool is_numeric_type (void) const { return true; } | |
128 | |
129 bool is_defined (void) const { return true; } | |
130 | |
131 bool is_constant (void) const { return true; } | |
132 | |
133 bool is_true (void) const | |
134 { return make_value ().is_true (); } | |
135 | |
136 bool print_as_scalar (void) const | |
137 { return make_value ().print_as_scalar (); } | |
138 | |
139 void print (std::ostream& os, bool pr_as_read_syntax = false) const | |
140 { make_value ().print (os, pr_as_read_syntax); } | |
141 | |
142 void print_info (std::ostream& os, const std::string& prefix) const | |
143 { make_value ().print_info (os, prefix); } | |
144 | |
145 #define FORWARD_VALUE_QUERY(TYPE,NAME) \ | |
146 TYPE \ | |
147 NAME (void) const { return make_value ().NAME (); } | |
148 | |
149 FORWARD_VALUE_QUERY (int8NDArray, int8_array_value) | |
150 FORWARD_VALUE_QUERY (int16NDArray, int16_array_value) | |
151 FORWARD_VALUE_QUERY (int32NDArray, int32_array_value) | |
152 FORWARD_VALUE_QUERY (int64NDArray, int64_array_value) | |
153 FORWARD_VALUE_QUERY (uint8NDArray, uint8_array_value) | |
154 FORWARD_VALUE_QUERY (uint16NDArray, uint16_array_value) | |
155 FORWARD_VALUE_QUERY (uint32NDArray, uint32_array_value) | |
156 FORWARD_VALUE_QUERY (uint64NDArray, uint64_array_value) | |
157 | |
158 #define FORWARD_VALUE_QUERY1(TYPE,NAME) \ | |
159 TYPE \ | |
160 NAME (bool flag = false) const { return make_value ().NAME (flag); } | |
161 | |
162 FORWARD_VALUE_QUERY1 (double, double_value) | |
163 | |
164 FORWARD_VALUE_QUERY1 (float, float_value) | |
165 | |
166 FORWARD_VALUE_QUERY1 (double, scalar_value) | |
167 | |
168 FORWARD_VALUE_QUERY1 (Matrix, matrix_value) | |
169 | |
170 FORWARD_VALUE_QUERY1 (FloatMatrix, float_matrix_value) | |
171 | |
172 FORWARD_VALUE_QUERY1 (Complex, complex_value) | |
173 | |
174 FORWARD_VALUE_QUERY1 (FloatComplex, float_complex_value) | |
175 | |
176 FORWARD_VALUE_QUERY1 (ComplexMatrix, complex_matrix_value) | |
177 | |
178 FORWARD_VALUE_QUERY1 (FloatComplexMatrix, float_complex_matrix_value) | |
179 | |
180 FORWARD_VALUE_QUERY1 (ComplexNDArray, complex_array_value) | |
181 | |
182 FORWARD_VALUE_QUERY1 (FloatComplexNDArray, float_complex_array_value) | |
183 | |
184 FORWARD_VALUE_QUERY1 (boolNDArray, bool_array_value) | |
185 | |
186 FORWARD_VALUE_QUERY1 (charNDArray, char_array_value) | |
187 | |
188 FORWARD_VALUE_QUERY1 (NDArray, array_value) | |
189 | |
190 FORWARD_VALUE_QUERY1 (FloatNDArray, float_array_value) | |
191 | |
192 FORWARD_VALUE_QUERY1 (SparseMatrix, sparse_matrix_value) | |
193 | |
194 FORWARD_VALUE_QUERY1 (SparseComplexMatrix, sparse_complex_matrix_value) | |
195 | |
196 octave_value diag (octave_idx_type k = 0) const | |
197 { return make_value ().diag (k); } | |
198 | |
199 octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const | |
200 { return make_value ().sort (dim, mode); } | |
201 | |
202 octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, | |
203 sortmode mode = ASCENDING) const | |
204 { return make_value ().sort (sidx, dim, mode); } | |
205 | |
206 octave_value convert_to_str_internal (bool pad, bool force, char type) const | |
207 { return make_value ().convert_to_str_internal (pad, force, type); } | |
208 | |
209 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const | |
210 { return make_value ().print_raw (os, pr_as_read_syntax); } | |
211 | |
212 bool save_ascii (std::ostream& os); | |
213 | |
214 bool load_ascii (std::istream& is); | |
215 | |
216 bool save_binary (std::ostream& os, bool& save_as_floats); | |
217 | |
218 bool load_binary (std::istream& is, bool swap, | |
219 oct_mach_info::float_format fmt); | |
220 | |
221 // HDF5 functions not defined. | |
222 | |
223 int write (octave_stream& os, int block_size, | |
224 oct_data_conv::data_type output_type, int skip, | |
225 oct_mach_info::float_format flt_fmt) const | |
226 { return make_value ().write (os, block_size, output_type, skip, flt_fmt); } | |
227 | |
228 // Unsafe. This function exists to support the MEX interface. | |
229 // You should not use it anywhere else. | |
230 void *mex_get_data (void) const | |
231 { return make_value ().mex_get_data (); } | |
232 | |
233 mxArray *as_mxArray (void) const | |
234 { return make_value ().as_mxArray (); } | |
235 | |
236 octave_value map (unary_mapper_t umap) const | |
237 { return make_value ().map (umap); } | |
238 | |
239 private: | |
240 const octave_value& make_value (void) const | |
241 { | |
242 if (value.is_undefined ()) | |
243 value = octave_value (index, false); | |
244 | |
245 return value; | |
246 } | |
247 | |
248 octave_value& make_value (void) | |
249 { | |
250 if (value.is_undefined ()) | |
251 value = octave_value (index, false); | |
252 | |
253 return value; | |
254 } | |
255 | |
256 idx_vector index; | |
257 mutable octave_value value; | |
258 | |
259 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA | |
260 }; | |
261 | |
262 #endif | |
263 |