24 #if defined (HAVE_CONFIG_H) 41 if (old_dims.
ndims () == 2 && old_dims(0) == 1)
43 else if (old_dims.
ndims () == 2 && old_dims(0) == 0 && old_dims(1) == 0)
49 template <
typename ArrayType>
61 data =
reinterpret_cast<const void *
> (
array.data ());
62 byte_size =
array.byte_size ();
64 old_dims =
array.dims ();
67 template <
typename ArrayType>
72 typedef typename ArrayType::element_type T;
75 if (n * static_cast<int> (
sizeof (T)) != byte_size)
76 error (
"typecast: incorrect number of input values to make output value");
79 T *dest =
retval.fortran_vec ();
80 std::memcpy (dest, data, n *
sizeof (T));
85 template <
typename ArrayType>
90 typedef typename ArrayType::element_type T;
91 typedef typename T::val_type VT;
94 if (n * static_cast<int> (
sizeof (T)) != byte_size)
95 error (
"typecast: incorrect number of input values to make output value");
98 VT *dest =
reinterpret_cast<VT *
> (
retval.fortran_vec ());
99 std::memcpy (dest, data, n *
sizeof (VT));
159 if (args.length () != 2)
166 const void *data =
nullptr;
172 if (
array.islogical ())
175 else if (
array.is_string ())
178 else if (
array.isinteger ())
180 if (
array.is_int8_type ())
183 else if (
array.is_int16_type ())
186 else if (
array.is_int32_type ())
189 else if (
array.is_int64_type ())
192 else if (
array.is_uint8_type ())
195 else if (
array.is_uint16_type ())
198 else if (
array.is_uint32_type ())
201 else if (
array.is_uint64_type ())
207 else if (
array.iscomplex ())
209 if (
array.is_single_type ())
211 byte_size, old_dims,
frame);
214 byte_size, old_dims,
frame);
216 else if (
array.isreal ())
218 if (
array.is_single_type ())
226 error (
"typecast: invalid input CLASS: %s",
227 array.class_name ().c_str ());
231 if (numclass.size () == 0)
233 else if (numclass ==
"char")
235 (data, byte_size, old_dims),
array.is_dq_string () ?
'"' 237 else if (numclass[0] ==
'i')
239 if (numclass ==
"int8")
240 retval = reinterpret_int_copy<int8NDArray> (data, byte_size, old_dims);
241 else if (numclass ==
"int16")
242 retval = reinterpret_int_copy<int16NDArray> (data, byte_size, old_dims);
243 else if (numclass ==
"int32")
244 retval = reinterpret_int_copy<int32NDArray> (data, byte_size, old_dims);
245 else if (numclass ==
"int64")
246 retval = reinterpret_int_copy<int64NDArray> (data, byte_size, old_dims);
248 else if (numclass[0] ==
'u')
250 if (numclass ==
"uint8")
251 retval = reinterpret_int_copy<uint8NDArray> (data, byte_size, old_dims);
252 else if (numclass ==
"uint16")
253 retval = reinterpret_int_copy<uint16NDArray> (data, byte_size,
255 else if (numclass ==
"uint32")
256 retval = reinterpret_int_copy<uint32NDArray> (data, byte_size,
258 else if (numclass ==
"uint64")
259 retval = reinterpret_int_copy<uint64NDArray> (data, byte_size,
262 else if (numclass ==
"single")
263 retval = reinterpret_copy<FloatNDArray> (data, byte_size, old_dims);
264 else if (numclass ==
"double")
265 retval = reinterpret_copy<NDArray> (data, byte_size, old_dims);
266 else if (numclass ==
"single complex")
267 retval = reinterpret_copy<FloatComplexNDArray> (data, byte_size,
269 else if (numclass ==
"double complex")
270 retval = reinterpret_copy<ComplexNDArray> (data, byte_size, old_dims);
273 error (
"typecast: cannot convert to %s class", numclass.c_str ());
306 template <
typename ArrayType>
310 typedef typename ArrayType::element_type T;
312 = bitp.
numel () / (
sizeof (T) * std::numeric_limits<unsigned char>::digits);
314 if (n * static_cast<int> (
sizeof (T)) *
315 std::numeric_limits<unsigned char>::digits != bitp.
numel ())
316 error (
"bitpack: incorrect number of bits to make up output value");
321 char *packed =
reinterpret_cast<char *
> (
retval.fortran_vec ());
328 for (
int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
332 bits += std::numeric_limits<unsigned char>::digits;
338 DEFUN (bitpack, args, ,
374 if (args.length () != 2)
377 if (! args(0).islogical ())
378 error (
"bitpack: X must be a logical array");
386 if (numclass.size () == 0)
388 else if (numclass ==
"char")
390 else if (numclass[0] ==
'i')
392 if (numclass ==
"int8")
393 retval = do_bitpack<int8NDArray> (bitp);
394 else if (numclass ==
"int16")
395 retval = do_bitpack<int16NDArray> (bitp);
396 else if (numclass ==
"int32")
397 retval = do_bitpack<int32NDArray> (bitp);
398 else if (numclass ==
"int64")
399 retval = do_bitpack<int64NDArray> (bitp);
401 else if (numclass[0] ==
'u')
403 if (numclass ==
"uint8")
404 retval = do_bitpack<uint8NDArray> (bitp);
405 else if (numclass ==
"uint16")
406 retval = do_bitpack<uint16NDArray> (bitp);
407 else if (numclass ==
"uint32")
408 retval = do_bitpack<uint32NDArray> (bitp);
409 else if (numclass ==
"uint64")
410 retval = do_bitpack<uint64NDArray> (bitp);
412 else if (numclass ==
"single")
413 retval = do_bitpack<FloatNDArray> (bitp);
414 else if (numclass ==
"double")
415 retval = do_bitpack<NDArray> (bitp);
416 else if (numclass ==
"single complex")
417 retval = do_bitpack<FloatComplexNDArray> (bitp);
418 else if (numclass ==
"double complex")
419 retval = do_bitpack<ComplexNDArray> (bitp);
422 error (
"bitpack: cannot pack to %s class", numclass.c_str ());
451 template <
typename ArrayType>
453 do_bitunpack (
const ArrayType&
array)
455 typedef typename ArrayType::element_type T;
457 * std::numeric_limits<unsigned char>::digits;
461 const char *packed =
reinterpret_cast<const char *
> (
array.fortran_vec ());
462 bool *bits =
retval.fortran_vec ();
470 for (
int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
471 bits[j] = (
c >>= 1) & 1;
472 bits += std::numeric_limits<unsigned char>::digits;
478 DEFUN (bitunpack, args, ,
507 if (args.length () != 1)
510 if (! (args(0).
isnumeric () || args(0).is_string ()))
511 error (
"bitunpack: argument must be a number or a string");
517 if (
array.is_string ())
519 else if (
array.isinteger ())
521 if (
array.is_int8_type ())
523 else if (
array.is_int16_type ())
524 retval = do_bitunpack (
array.int16_array_value ());
525 else if (
array.is_int32_type ())
526 retval = do_bitunpack (
array.int32_array_value ());
527 else if (
array.is_int64_type ())
528 retval = do_bitunpack (
array.int64_array_value ());
529 else if (
array.is_uint8_type ())
530 retval = do_bitunpack (
array.uint8_array_value ());
531 else if (
array.is_uint16_type ())
532 retval = do_bitunpack (
array.uint16_array_value ());
533 else if (
array.is_uint32_type ())
534 retval = do_bitunpack (
array.uint32_array_value ());
535 else if (
array.is_uint64_type ())
536 retval = do_bitunpack (
array.uint64_array_value ());
540 else if (
array.iscomplex ())
542 if (
array.is_single_type ())
543 retval = do_bitunpack (
array.float_complex_array_value ());
545 retval = do_bitunpack (
array.complex_array_value ());
547 else if (
array.isreal ())
549 if (
array.is_single_type ())
550 retval = do_bitunpack (
array.float_array_value ());
555 error (
"bitunpack: invalid input class: %s",
556 array.class_name ().c_str ());
typecast(ar{x}, "uint8") esult
OCTINTERP_API void print_usage(void)
const T * fortran_vec(void) const
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error(const char *fmt,...)
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
static void get_data_and_bytesize(const ArrayType &array, const void *&data, octave_idx_type &byte_size, dim_vector &old_dims, octave::unwind_protect &frame)
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
OCTAVE_EXPORT octave_value_list isnumeric
then the function must return scalars which will be concatenated into the return array(s). If code
octave::unwind_protect frame
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
bool is_undefined(void) const
static ArrayType reinterpret_copy(const void *data, octave_idx_type byte_size, const dim_vector &old_dims)
static ArrayType reinterpret_int_copy(const void *data, octave_idx_type byte_size, const dim_vector &old_dims)
static dim_vector get_vec_dims(const dim_vector &old_dims, octave_idx_type n)
octave_idx_type ndims(void) const
Number of dimensions.
octave_idx_type numel(void) const
Number of elements in the array.
Vector representing the dimensions (size) of an Array.
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string