00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027 #include <iostream>
00028
00029 #include "mx-base.h"
00030
00031 #include "gripes.h"
00032 #include "oct-obj.h"
00033 #include "ops.h"
00034 #include "ov-bool.h"
00035 #include "ov-bool-mat.h"
00036 #include "ov-base.h"
00037 #include "ov-base-scalar.h"
00038 #include "ov-base-scalar.cc"
00039 #include "ov-re-mat.h"
00040 #include "ov-scalar.h"
00041 #include "pr-output.h"
00042
00043 #include "ls-oct-ascii.h"
00044 #include "ls-hdf5.h"
00045
00046 template class octave_base_scalar<bool>;
00047
00048 DEFINE_OCTAVE_ALLOCATOR (octave_bool);
00049
00050 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool, "bool", "logical");
00051
00052 static octave_base_value *
00053 default_numeric_conversion_function (const octave_base_value& a)
00054 {
00055 CAST_CONV_ARG (const octave_bool&);
00056
00057 return new octave_scalar (v.bool_value ());
00058 }
00059
00060 octave_base_value::type_conv_info
00061 octave_bool::numeric_conversion_function (void) const
00062 {
00063 return octave_base_value::type_conv_info (default_numeric_conversion_function,
00064 octave_scalar::static_type_id ());
00065
00066 }
00067
00068 octave_value
00069 octave_bool::do_index_op (const octave_value_list& idx, bool resize_ok)
00070 {
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 octave_value tmp (new octave_bool_matrix (bool_matrix_value ()));
00082
00083 return tmp.do_index_op (idx, resize_ok);
00084 }
00085
00086 octave_value
00087 octave_bool::resize (const dim_vector& dv, bool fill) const
00088 {
00089 if (fill)
00090 {
00091 boolNDArray retval (dv, false);
00092 if (dv.numel())
00093 retval(0) = scalar;
00094 return retval;
00095 }
00096 else
00097 {
00098 boolNDArray retval (dv);
00099 if (dv.numel())
00100 retval(0) = scalar;
00101 return retval;
00102 }
00103 }
00104
00105 octave_value
00106 octave_bool::convert_to_str_internal (bool, bool, char type) const
00107 {
00108 char s[2];
00109 s[0] = static_cast<char> (scalar);
00110 s[1] = '\0';
00111
00112 return octave_value (s, type);
00113 }
00114
00115 bool
00116 octave_bool::save_ascii (std::ostream& os)
00117 {
00118 double d = double_value ();
00119
00120 octave_write_double (os, d);
00121 os << "\n";
00122
00123 return true;
00124 }
00125
00126 bool
00127 octave_bool::load_ascii (std::istream& is)
00128 {
00129 scalar = (octave_read_value<double> (is) != 0.);
00130
00131 if (!is)
00132 {
00133 error ("load: failed to load scalar constant");
00134 return false;
00135 }
00136
00137 return true;
00138 }
00139
00140 bool
00141 octave_bool::save_binary (std::ostream& os, bool& )
00142 {
00143 char tmp = (scalar ? 1 : 0);
00144 os.write (reinterpret_cast<char *> (&tmp), 1);
00145
00146 return true;
00147 }
00148
00149 bool
00150 octave_bool::load_binary (std::istream& is, bool ,
00151 oct_mach_info::float_format )
00152 {
00153 char tmp;
00154 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
00155 return false;
00156 scalar = (tmp ? 1 : 0);
00157 return true;
00158 }
00159
00160 #if defined (HAVE_HDF5)
00161
00162 bool
00163 octave_bool::save_hdf5 (hid_t loc_id, const char *name,
00164 bool )
00165 {
00166 hsize_t dimens[3];
00167 hid_t space_hid = -1, data_hid = -1;
00168 bool retval = true;
00169
00170 space_hid = H5Screate_simple (0, dimens, 0);
00171 if (space_hid < 0) return false;
00172 #if HAVE_HDF5_18
00173 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
00174 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00175 #else
00176 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
00177 H5P_DEFAULT);
00178 #endif
00179 if (data_hid < 0)
00180 {
00181 H5Sclose (space_hid);
00182 return false;
00183 }
00184
00185 double tmp = double_value ();
00186 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
00187 H5P_DEFAULT, &tmp) >= 0;
00188
00189 H5Dclose (data_hid);
00190 H5Sclose (space_hid);
00191
00192 return retval;
00193 }
00194
00195 bool
00196 octave_bool::load_hdf5 (hid_t loc_id, const char *name)
00197 {
00198 #if HAVE_HDF5_18
00199 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
00200 #else
00201 hid_t data_hid = H5Dopen (loc_id, name);
00202 #endif
00203 hid_t space_id = H5Dget_space (data_hid);
00204
00205 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
00206
00207 if (rank != 0)
00208 {
00209 H5Dclose (data_hid);
00210 return false;
00211 }
00212
00213 double dtmp;
00214 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
00215 H5P_DEFAULT, &dtmp) < 0)
00216 {
00217 H5Dclose (data_hid);
00218 return false;
00219 }
00220
00221 scalar = (dtmp != 0.);
00222
00223 H5Dclose (data_hid);
00224
00225 return true;
00226 }
00227
00228 #endif
00229
00230 mxArray *
00231 octave_bool::as_mxArray (void) const
00232 {
00233 mxArray *retval = new mxArray (mxLOGICAL_CLASS, 1, 1, mxREAL);
00234
00235 bool *pr = static_cast<bool *> (retval->get_data ());
00236
00237 pr[0] = scalar;
00238
00239 return retval;
00240 }