26 #if defined (HAVE_CONFIG_H)
44 if (old_dims.
ndims () == 2 && old_dims(0) == 1)
46 else if (old_dims.
ndims () == 2 && old_dims(0) == 0 && old_dims(1) == 0)
52 template <
typename ArrayType>
64 data =
reinterpret_cast<const void *
> (array.data ());
65 byte_size = array.byte_size ();
67 old_dims = array.dims ();
70 template <
typename ArrayType>
75 typedef typename ArrayType::element_type T;
78 if (
n *
static_cast<int> (
sizeof (T)) != byte_size)
79 error (
"typecast: incorrect number of input values to make output value");
83 std::memcpy (dest, data,
n *
sizeof (T));
88 template <
typename ArrayType>
93 typedef typename ArrayType::element_type T;
94 typedef typename T::val_type VT;
97 if (
n *
static_cast<int> (
sizeof (T)) != byte_size)
98 error (
"typecast: incorrect number of input values to make output value");
102 std::memcpy (dest, data,
n *
sizeof (VT));
107 DEFUN (typecast, args, ,
162 if (args.length () != 2)
169 const void *data =
nullptr;
214 byte_size, old_dims, frame);
217 byte_size, old_dims, frame);
229 error (
"typecast: invalid input CLASS: %s",
232 std::string numclass = args(1).string_value ();
233 std::transform (numclass.begin (), numclass.end (), numclass.begin (),
236 if (numclass.size () == 0)
238 else if (numclass ==
"char")
240 (data, byte_size, old_dims), array.
is_dq_string () ?
'"'
242 else if (numclass[0] ==
'i')
244 if (numclass ==
"int8")
245 retval = reinterpret_int_copy<int8NDArray> (data, byte_size, old_dims);
246 else if (numclass ==
"int16")
247 retval = reinterpret_int_copy<int16NDArray> (data, byte_size, old_dims);
248 else if (numclass ==
"int32")
249 retval = reinterpret_int_copy<int32NDArray> (data, byte_size, old_dims);
250 else if (numclass ==
"int64")
251 retval = reinterpret_int_copy<int64NDArray> (data, byte_size, old_dims);
253 else if (numclass[0] ==
'u')
255 if (numclass ==
"uint8")
256 retval = reinterpret_int_copy<uint8NDArray> (data, byte_size, old_dims);
257 else if (numclass ==
"uint16")
258 retval = reinterpret_int_copy<uint16NDArray> (data, byte_size,
260 else if (numclass ==
"uint32")
261 retval = reinterpret_int_copy<uint32NDArray> (data, byte_size,
263 else if (numclass ==
"uint64")
264 retval = reinterpret_int_copy<uint64NDArray> (data, byte_size,
267 else if (numclass ==
"single")
268 retval = reinterpret_copy<FloatNDArray> (data, byte_size, old_dims);
269 else if (numclass ==
"double")
270 retval = reinterpret_copy<NDArray> (data, byte_size, old_dims);
271 else if (numclass ==
"single complex")
272 retval = reinterpret_copy<FloatComplexNDArray> (data, byte_size,
274 else if (numclass ==
"double complex")
275 retval = reinterpret_copy<ComplexNDArray> (data, byte_size, old_dims);
277 if (
retval.is_undefined ())
278 error (
"typecast: cannot convert to %s class", numclass.c_str ());
311 template <
typename ArrayType>
315 typedef typename ArrayType::element_type T;
317 = bitp.
numel () / (
sizeof (T) * std::numeric_limits<unsigned char>::digits);
319 if (
n *
static_cast<int> (
sizeof (T)) *
320 std::numeric_limits<unsigned char>::digits != bitp.
numel ())
321 error (
"bitpack: incorrect number of bits to make up output value");
333 for (
int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
337 bits += std::numeric_limits<unsigned char>::digits;
343 DEFUN (bitpack, args, ,
379 if (args.length () != 2)
382 if (! args(0).islogical ())
383 error (
"bitpack: X must be a logical array");
389 std::string numclass = args(1).string_value ();
391 if (numclass.size () == 0)
393 else if (numclass ==
"char")
395 else if (numclass[0] ==
'i')
397 if (numclass ==
"int8")
398 retval = do_bitpack<int8NDArray> (bitp);
399 else if (numclass ==
"int16")
400 retval = do_bitpack<int16NDArray> (bitp);
401 else if (numclass ==
"int32")
402 retval = do_bitpack<int32NDArray> (bitp);
403 else if (numclass ==
"int64")
404 retval = do_bitpack<int64NDArray> (bitp);
406 else if (numclass[0] ==
'u')
408 if (numclass ==
"uint8")
409 retval = do_bitpack<uint8NDArray> (bitp);
410 else if (numclass ==
"uint16")
411 retval = do_bitpack<uint16NDArray> (bitp);
412 else if (numclass ==
"uint32")
413 retval = do_bitpack<uint32NDArray> (bitp);
414 else if (numclass ==
"uint64")
415 retval = do_bitpack<uint64NDArray> (bitp);
417 else if (numclass ==
"single")
418 retval = do_bitpack<FloatNDArray> (bitp);
419 else if (numclass ==
"double")
420 retval = do_bitpack<NDArray> (bitp);
421 else if (numclass ==
"single complex")
422 retval = do_bitpack<FloatComplexNDArray> (bitp);
423 else if (numclass ==
"double complex")
424 retval = do_bitpack<ComplexNDArray> (bitp);
426 if (
retval.is_undefined ())
427 error (
"bitpack: cannot pack to %s class", numclass.c_str ());
463 template <
typename ArrayType>
467 typedef typename ArrayType::element_type T;
469 * std::numeric_limits<unsigned char>::digits;
473 const char *packed =
reinterpret_cast<const char *
> (array.fortran_vec ());
482 for (
int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
483 bits[j] = (c >>= 1) & 1;
484 bits += std::numeric_limits<unsigned char>::digits;
490 DEFUN (bitunpack, args, ,
519 if (args.length () != 1)
522 if (! (args(0).isnumeric () || args(0).is_string ()))
523 error (
"bitunpack: argument must be a number or a string");
567 error (
"bitunpack: invalid input class: %s",
octave_idx_type numel(void) const
Number of elements in the array.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
const T * fortran_vec(void) const
Size of the specified dimension.
Vector representing the dimensions (size) of an Array.
octave_idx_type ndims(void) const
Number of dimensions.
int32NDArray int32_array_value(void) const
boolNDArray bool_array_value(bool warn=false) const
uint16NDArray uint16_array_value(void) const
bool is_uint16_type(void) const
bool is_int8_type(void) const
bool is_dq_string(void) const
bool is_string(void) const
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
charNDArray char_array_value(bool frc_str_conv=false) const
bool isinteger(void) const
std::string class_name(void) const
bool is_uint32_type(void) const
int8NDArray int8_array_value(void) const
bool is_int64_type(void) const
int64NDArray int64_array_value(void) const
uint8NDArray uint8_array_value(void) const
bool is_int32_type(void) const
bool is_uint64_type(void) const
bool is_int16_type(void) const
uint64NDArray uint64_array_value(void) const
NDArray array_value(bool frc_str_conv=false) const
bool is_single_type(void) const
uint32NDArray uint32_array_value(void) const
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
FloatNDArray float_array_value(bool frc_str_conv=false) const
bool is_uint8_type(void) const
int16NDArray int16_array_value(void) const
bool iscomplex(void) const
bool islogical(void) const
OCTINTERP_API void print_usage(void)
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error(const char *fmt,...)
ColumnVector transform(const Matrix &m, double x, double y, double z)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value::octave_value(const Array< char > &chm, char type) return retval
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)
ArrayType do_bitpack(const boolNDArray &bitp)
static dim_vector get_vec_dims(const dim_vector &old_dims, octave_idx_type n)
static ArrayType reinterpret_int_copy(const void *data, octave_idx_type byte_size, const dim_vector &old_dims)
static ArrayType reinterpret_copy(const void *data, octave_idx_type byte_size, const dim_vector &old_dims)
boolNDArray do_bitunpack(const ArrayType &array)