comparison src/ov-bool-mat.cc @ 10315:57a59eae83cc

untabify src C++ source files
author John W. Eaton <jwe@octave.org>
date Thu, 11 Feb 2010 12:41:46 -0500
parents cd96d29c5efa
children 4d1fc073fbb7
comparison
equal deleted inserted replaced
10314:07ebe522dac2 10315:57a59eae83cc
52 template class octave_base_matrix<boolNDArray>; 52 template class octave_base_matrix<boolNDArray>;
53 53
54 DEFINE_OCTAVE_ALLOCATOR (octave_bool_matrix); 54 DEFINE_OCTAVE_ALLOCATOR (octave_bool_matrix);
55 55
56 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool_matrix, 56 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool_matrix,
57 "bool matrix", "logical"); 57 "bool matrix", "logical");
58 58
59 static octave_base_value * 59 static octave_base_value *
60 default_numeric_conversion_function (const octave_base_value& a) 60 default_numeric_conversion_function (const octave_base_value& a)
61 { 61 {
62 CAST_CONV_ARG (const octave_bool_matrix&); 62 CAST_CONV_ARG (const octave_bool_matrix&);
82 82
83 octave_idx_type nr = bm.rows (); 83 octave_idx_type nr = bm.rows ();
84 octave_idx_type nc = bm.cols (); 84 octave_idx_type nc = bm.cols ();
85 85
86 if (nr == 1 && nc == 1) 86 if (nr == 1 && nc == 1)
87 retval = new octave_bool (bm (0, 0)); 87 retval = new octave_bool (bm (0, 0));
88 } 88 }
89 89
90 return retval; 90 return retval;
91 } 91 }
92 92
96 double retval = lo_ieee_nan_value (); 96 double retval = lo_ieee_nan_value ();
97 97
98 if (rows () > 0 && columns () > 0) 98 if (rows () > 0 && columns () > 0)
99 { 99 {
100 gripe_implicit_conversion ("Octave:array-as-scalar", 100 gripe_implicit_conversion ("Octave:array-as-scalar",
101 "bool matrix", "real scalar"); 101 "bool matrix", "real scalar");
102 102
103 retval = matrix (0, 0); 103 retval = matrix (0, 0);
104 } 104 }
105 else 105 else
106 gripe_invalid_conversion ("bool matrix", "real scalar"); 106 gripe_invalid_conversion ("bool matrix", "real scalar");
114 float retval = lo_ieee_float_nan_value (); 114 float retval = lo_ieee_float_nan_value ();
115 115
116 if (rows () > 0 && columns () > 0) 116 if (rows () > 0 && columns () > 0)
117 { 117 {
118 gripe_implicit_conversion ("Octave:array-as-scalar", 118 gripe_implicit_conversion ("Octave:array-as-scalar",
119 "bool matrix", "real scalar"); 119 "bool matrix", "real scalar");
120 120
121 retval = matrix (0, 0); 121 retval = matrix (0, 0);
122 } 122 }
123 else 123 else
124 gripe_invalid_conversion ("bool matrix", "real scalar"); 124 gripe_invalid_conversion ("bool matrix", "real scalar");
134 Complex retval (tmp, tmp); 134 Complex retval (tmp, tmp);
135 135
136 if (rows () > 0 && columns () > 0) 136 if (rows () > 0 && columns () > 0)
137 { 137 {
138 gripe_implicit_conversion ("Octave:array-as-scalar", 138 gripe_implicit_conversion ("Octave:array-as-scalar",
139 "bool matrix", "complex scalar"); 139 "bool matrix", "complex scalar");
140 140
141 retval = matrix (0, 0); 141 retval = matrix (0, 0);
142 } 142 }
143 else 143 else
144 gripe_invalid_conversion ("bool matrix", "complex scalar"); 144 gripe_invalid_conversion ("bool matrix", "complex scalar");
154 FloatComplex retval (tmp, tmp); 154 FloatComplex retval (tmp, tmp);
155 155
156 if (rows () > 0 && columns () > 0) 156 if (rows () > 0 && columns () > 0)
157 { 157 {
158 gripe_implicit_conversion ("Octave:array-as-scalar", 158 gripe_implicit_conversion ("Octave:array-as-scalar",
159 "bool matrix", "complex scalar"); 159 "bool matrix", "complex scalar");
160 160
161 retval = matrix (0, 0); 161 retval = matrix (0, 0);
162 } 162 }
163 else 163 else
164 gripe_invalid_conversion ("bool matrix", "complex scalar"); 164 gripe_invalid_conversion ("bool matrix", "complex scalar");
166 return retval; 166 return retval;
167 } 167 }
168 168
169 octave_value 169 octave_value
170 octave_bool_matrix::convert_to_str_internal (bool pad, bool force, 170 octave_bool_matrix::convert_to_str_internal (bool pad, bool force,
171 char type) const 171 char type) const
172 { 172 {
173 octave_value tmp = octave_value (array_value ()); 173 octave_value tmp = octave_value (array_value ());
174 return tmp.convert_to_str (pad, force, type); 174 return tmp.convert_to_str (pad, force, type);
175 } 175 }
176 176
177 void 177 void
178 octave_bool_matrix::print_raw (std::ostream& os, 178 octave_bool_matrix::print_raw (std::ostream& os,
179 bool pr_as_read_syntax) const 179 bool pr_as_read_syntax) const
180 { 180 {
181 octave_print_internal (os, matrix, pr_as_read_syntax, 181 octave_print_internal (os, matrix, pr_as_read_syntax,
182 current_print_indent_level ()); 182 current_print_indent_level ());
183 } 183 }
184 184
185 bool 185 bool
186 octave_bool_matrix::save_ascii (std::ostream& os) 186 octave_bool_matrix::save_ascii (std::ostream& os)
187 { 187 {
190 { 190 {
191 NDArray tmp = array_value (); 191 NDArray tmp = array_value ();
192 os << "# ndims: " << d.length () << "\n"; 192 os << "# ndims: " << d.length () << "\n";
193 193
194 for (int i = 0; i < d.length (); i++) 194 for (int i = 0; i < d.length (); i++)
195 os << " " << d (i); 195 os << " " << d (i);
196 196
197 os << "\n" << tmp; 197 os << "\n" << tmp;
198 } 198 }
199 else 199 else
200 { 200 {
201 // Keep this case, rather than use generic code above for backward 201 // Keep this case, rather than use generic code above for backward
202 // compatiability. Makes load_ascii much more complex!! 202 // compatiability. Makes load_ascii much more complex!!
203 os << "# rows: " << rows () << "\n" 203 os << "# rows: " << rows () << "\n"
204 << "# columns: " << columns () << "\n"; 204 << "# columns: " << columns () << "\n";
205 205
206 Matrix tmp = matrix_value (); 206 Matrix tmp = matrix_value ();
207 207
208 os << tmp; 208 os << tmp;
209 } 209 }
225 octave_idx_type val = 0; 225 octave_idx_type val = 0;
226 226
227 if (extract_keyword (is, keywords, kw, val, true)) 227 if (extract_keyword (is, keywords, kw, val, true))
228 { 228 {
229 if (kw == "ndims") 229 if (kw == "ndims")
230 { 230 {
231 int mdims = static_cast<int> (val); 231 int mdims = static_cast<int> (val);
232 232
233 if (mdims >= 0) 233 if (mdims >= 0)
234 { 234 {
235 dim_vector dv; 235 dim_vector dv;
236 dv.resize (mdims); 236 dv.resize (mdims);
237 237
238 for (int i = 0; i < mdims; i++) 238 for (int i = 0; i < mdims; i++)
239 is >> dv(i); 239 is >> dv(i);
240 240
241 if (is) 241 if (is)
242 { 242 {
243 boolNDArray btmp (dv); 243 boolNDArray btmp (dv);
244 244
245 if (btmp.is_empty ()) 245 if (btmp.is_empty ())
246 matrix = btmp; 246 matrix = btmp;
247 else 247 else
248 { 248 {
249 NDArray tmp(dv); 249 NDArray tmp(dv);
250 is >> tmp; 250 is >> tmp;
251 251
252 if (is) 252 if (is)
253 { 253 {
254 for (octave_idx_type i = 0; i < btmp.nelem (); i++) 254 for (octave_idx_type i = 0; i < btmp.nelem (); i++)
255 btmp.elem (i) = (tmp.elem (i) != 0.); 255 btmp.elem (i) = (tmp.elem (i) != 0.);
256 256
257 matrix = btmp; 257 matrix = btmp;
258 } 258 }
259 else 259 else
260 { 260 {
261 error ("load: failed to load matrix constant"); 261 error ("load: failed to load matrix constant");
262 success = false; 262 success = false;
263 } 263 }
264 } 264 }
265 } 265 }
266 else 266 else
267 { 267 {
268 error ("load: failed to extract dimensions"); 268 error ("load: failed to extract dimensions");
269 success = false; 269 success = false;
270 } 270 }
271 } 271 }
272 else 272 else
273 { 273 {
274 error ("load: failed to extract number of dimensions"); 274 error ("load: failed to extract number of dimensions");
275 success = false; 275 success = false;
276 } 276 }
277 } 277 }
278 else if (kw == "rows") 278 else if (kw == "rows")
279 { 279 {
280 octave_idx_type nr = val; 280 octave_idx_type nr = val;
281 octave_idx_type nc = 0; 281 octave_idx_type nc = 0;
282 282
283 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) 283 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0)
284 { 284 {
285 if (nr > 0 && nc > 0) 285 if (nr > 0 && nc > 0)
286 { 286 {
287 Matrix tmp (nr, nc); 287 Matrix tmp (nr, nc);
288 is >> tmp; 288 is >> tmp;
289 if (is) 289 if (is)
290 { 290 {
291 boolMatrix btmp (nr, nc); 291 boolMatrix btmp (nr, nc);
292 for (octave_idx_type j = 0; j < nc; j++) 292 for (octave_idx_type j = 0; j < nc; j++)
293 for (octave_idx_type i = 0; i < nr; i++) 293 for (octave_idx_type i = 0; i < nr; i++)
294 btmp.elem (i,j) = (tmp.elem (i, j) != 0.); 294 btmp.elem (i,j) = (tmp.elem (i, j) != 0.);
295 295
296 matrix = btmp; 296 matrix = btmp;
297 } 297 }
298 else 298 else
299 { 299 {
300 error ("load: failed to load matrix constant"); 300 error ("load: failed to load matrix constant");
301 success = false; 301 success = false;
302 } 302 }
303 } 303 }
304 else if (nr == 0 || nc == 0) 304 else if (nr == 0 || nc == 0)
305 matrix = boolMatrix (nr, nc); 305 matrix = boolMatrix (nr, nc);
306 else 306 else
307 panic_impossible (); 307 panic_impossible ();
308 } 308 }
309 else 309 else
310 { 310 {
311 error ("load: failed to extract number of rows and columns"); 311 error ("load: failed to extract number of rows and columns");
312 success = false; 312 success = false;
313 } 313 }
314 } 314 }
315 else 315 else
316 panic_impossible (); 316 panic_impossible ();
317 } 317 }
318 else 318 else
319 { 319 {
320 error ("load: failed to extract number of rows and columns"); 320 error ("load: failed to extract number of rows and columns");
321 success = false; 321 success = false;
354 return true; 354 return true;
355 } 355 }
356 356
357 bool 357 bool
358 octave_bool_matrix::load_binary (std::istream& is, bool swap, 358 octave_bool_matrix::load_binary (std::istream& is, bool swap,
359 oct_mach_info::float_format /* fmt */) 359 oct_mach_info::float_format /* fmt */)
360 { 360 {
361 int32_t mdims; 361 int32_t mdims;
362 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) 362 if (! is.read (reinterpret_cast<char *> (&mdims), 4))
363 return false; 363 return false;
364 if (swap) 364 if (swap)
375 dv.resize (mdims); 375 dv.resize (mdims);
376 376
377 for (int i = 0; i < mdims; i++) 377 for (int i = 0; i < mdims; i++)
378 { 378 {
379 if (! is.read (reinterpret_cast<char *> (&di), 4)) 379 if (! is.read (reinterpret_cast<char *> (&di), 4))
380 return false; 380 return false;
381 if (swap) 381 if (swap)
382 swap_bytes<4> (&di); 382 swap_bytes<4> (&di);
383 dv(i) = di; 383 dv(i) = di;
384 } 384 }
385 385
386 // Convert an array with a single dimension to be a row vector. 386 // Convert an array with a single dimension to be a row vector.
387 // Octave should never write files like this, other software 387 // Octave should never write files like this, other software
410 410
411 #if defined (HAVE_HDF5) 411 #if defined (HAVE_HDF5)
412 412
413 bool 413 bool
414 octave_bool_matrix::save_hdf5 (hid_t loc_id, const char *name, 414 octave_bool_matrix::save_hdf5 (hid_t loc_id, const char *name,
415 bool /* save_as_floats */) 415 bool /* save_as_floats */)
416 { 416 {
417 dim_vector dv = dims (); 417 dim_vector dv = dims ();
418 int empty = save_hdf5_empty (loc_id, name, dv); 418 int empty = save_hdf5_empty (loc_id, name, dv);
419 if (empty) 419 if (empty)
420 return (empty > 0); 420 return (empty > 0);
432 432
433 space_hid = H5Screate_simple (rank, hdims, 0); 433 space_hid = H5Screate_simple (rank, hdims, 0);
434 if (space_hid < 0) return false; 434 if (space_hid < 0) return false;
435 #if HAVE_HDF5_18 435 #if HAVE_HDF5_18
436 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid, 436 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid,
437 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); 437 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
438 #else 438 #else
439 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid, 439 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_HBOOL, space_hid,
440 H5P_DEFAULT); 440 H5P_DEFAULT);
441 #endif 441 #endif
442 if (data_hid < 0) 442 if (data_hid < 0)
443 { 443 {
444 H5Sclose (space_hid); 444 H5Sclose (space_hid);
445 return false; 445 return false;
451 451
452 for (octave_idx_type i = 0; i < nel; i++) 452 for (octave_idx_type i = 0; i < nel; i++)
453 htmp[i] = mtmp[i]; 453 htmp[i] = mtmp[i];
454 454
455 retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, 455 retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL,
456 H5P_DEFAULT, htmp) >= 0; 456 H5P_DEFAULT, htmp) >= 0;
457 457
458 H5Dclose (data_hid); 458 H5Dclose (data_hid);
459 H5Sclose (space_hid); 459 H5Sclose (space_hid);
460 460
461 return retval; 461 return retval;
502 } 502 }
503 else 503 else
504 { 504 {
505 dv.resize (rank); 505 dv.resize (rank);
506 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) 506 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--)
507 dv(j) = hdims[i]; 507 dv(j) = hdims[i];
508 } 508 }
509 509
510 octave_idx_type nel = dv.numel (); 510 octave_idx_type nel = dv.numel ();
511 OCTAVE_LOCAL_BUFFER (hbool_t, htmp, nel); 511 OCTAVE_LOCAL_BUFFER (hbool_t, htmp, nel);
512 if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, H5P_DEFAULT, htmp) >= 0) 512 if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, H5P_DEFAULT, htmp) >= 0)
513 { 513 {
514 retval = true; 514 retval = true;
515 515
516 boolNDArray btmp (dv); 516 boolNDArray btmp (dv);
517 for (octave_idx_type i = 0; i < nel; i++) 517 for (octave_idx_type i = 0; i < nel; i++)
518 btmp.elem (i) = htmp[i]; 518 btmp.elem (i) = htmp[i];
519 519
520 matrix = btmp; 520 matrix = btmp;
521 } 521 }
522 522
523 H5Dclose (data_hid); 523 H5Dclose (data_hid);