26 #if defined (HAVE_CONFIG_H)
46 if (old_dims.
ndims () == 2 && old_dims(0) == 1)
48 else if (old_dims.
ndims () == 2 && old_dims(0) == 0 && old_dims(1) == 0)
54 template <
typename ArrayType>
66 data =
reinterpret_cast<const void *
> (array.data ());
67 byte_size = array.byte_size ();
69 old_dims = array.dims ();
72 template <
typename ArrayType>
77 typedef typename ArrayType::element_type T;
80 if (
n *
static_cast<int> (
sizeof (T)) != byte_size)
81 error (
"typecast: incorrect number of input values to make output value");
84 T *dest = retval.fortran_vec ();
85 std::memcpy (dest, data,
n *
sizeof (T));
90 template <
typename ArrayType>
95 typedef typename ArrayType::element_type T;
96 typedef typename T::val_type VT;
99 if (
n *
static_cast<int> (
sizeof (T)) != byte_size)
100 error (
"typecast: incorrect number of input values to make output value");
103 VT *dest =
reinterpret_cast<VT *
> (retval.fortran_vec ());
104 std::memcpy (dest, data,
n *
sizeof (VT));
109 DEFUN (typecast, args, ,
164 if (args.length () != 2)
171 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),
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);
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");
325 const bool *bits = bitp.
data ();
326 char *packed =
reinterpret_cast<char *
> (retval.fortran_vec ());
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")
394 retval =
octave_value (do_bitpack<charNDArray> (bitp),
'\'');
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);
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.data ());
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");
565 error (
"bitunpack: invalid input class: %s",
OCTARRAY_OVERRIDABLE_FUNC_API const T * data(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
OCTARRAY_API T * fortran_vec(void)
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
bool is_undefined(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
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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()) ? '\'' :'"'))
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)
static void get_data_and_bytesize(const ArrayType &array, const void *&data, octave_idx_type &byte_size, dim_vector &old_dims, unwind_protect &frame)
boolNDArray do_bitunpack(const ArrayType &array)