Mercurial > hg > octave-lojdl
changeset 9548:e5f7aee2ab8c
optimize &=, |= operators
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 19 Aug 2009 15:54:31 +0200 |
parents | 588c6bb6fdcb |
children | ed34b1da0e26 |
files | liboctave/ChangeLog liboctave/MArrayN.cc liboctave/boolNDArray.cc liboctave/boolNDArray.h src/ChangeLog src/OPERATORS/op-bm-bm.cc src/ops.h |
diffstat | 7 files changed, 97 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,9 @@ +2009-08-19 Jaroslav Hajek <highegg@gmail.com> + + * MArrayN.cc (operator+=, operator-=): Test matching dimensions first. + * boolNDArray.cc (mx_el_and_assign, mx_el_or_assign): New functions. + * boolNDArray.h: Declare them. + 2009-08-19 Jaroslav Hajek <highegg@gmail.com> * Array.cc (Array<T>::clear): New method.
--- a/liboctave/MArrayN.cc +++ b/liboctave/MArrayN.cc @@ -82,17 +82,18 @@ { if (a.is_shared ()) return a = a + b; - octave_idx_type l = a.length (); + + dim_vector a_dims = a.dims (); + dim_vector b_dims = b.dims (); - if (l > 0) + if (a_dims != b_dims) + gripe_nonconformant ("operator +=", a_dims, b_dims); + else { - dim_vector a_dims = a.dims (); - dim_vector b_dims = b.dims (); + octave_idx_type l = a.length (); - if (a_dims != b_dims) - gripe_nonconformant ("operator +=", a_dims, b_dims); - else - DO_VV_OP2 (T, a, +=, b); + if (l > 0) + DO_VV_OP2 (T, a, +=, b); } return a; @@ -104,18 +105,20 @@ { if (a.is_shared ()) return a = a - b; - octave_idx_type l = a.length (); + + dim_vector a_dims = a.dims (); + dim_vector b_dims = b.dims (); - if (l > 0) + if (a_dims != b_dims) + gripe_nonconformant ("operator -=", a_dims, b_dims); + else { - dim_vector a_dims = a.dims (); - dim_vector b_dims = b.dims (); + octave_idx_type l = a.length (); - if (a_dims != b_dims) - gripe_nonconformant ("operator -=", a_dims, b_dims); - else - DO_VV_OP2 (T, a, -=, b); + if (l > 0) + DO_VV_OP2 (T, a, -=, b); } + return a; }
--- a/liboctave/boolNDArray.cc +++ b/liboctave/boolNDArray.cc @@ -31,6 +31,7 @@ #include "mx-base.h" #include "lo-ieee.h" #include "mx-op-defs.h" +#include "MArray-defs.h" // unary operations @@ -154,6 +155,50 @@ SND_BOOL_OPS (bool, boolNDArray, false) SND_CMP_OPS (bool, , boolNDArray, ) +boolNDArray& +mx_el_and_assign (boolNDArray& a, const boolNDArray& b) +{ + if (a.is_shared ()) + return a = mx_el_and (a, b); + + dim_vector a_dims = a.dims (); + dim_vector b_dims = b.dims (); + + if (a_dims != b_dims) + gripe_nonconformant ("operator &=", a_dims, b_dims); + else + { + octave_idx_type l = a.length (); + + if (l > 0) + DO_VV_OP2 (bool, a, &=, b); + } + + return a; +} + +boolNDArray& +mx_el_or_assign (boolNDArray& a, const boolNDArray& b) +{ + if (a.is_shared ()) + return a = mx_el_and (a, b); + + dim_vector a_dims = a.dims (); + dim_vector b_dims = b.dims (); + + if (a_dims != b_dims) + gripe_nonconformant ("operator |=", a_dims, b_dims); + else + { + octave_idx_type l = a.length (); + + if (l > 0) + DO_VV_OP2 (bool, a, |=, b); + } + + return a; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/liboctave/boolNDArray.h +++ b/liboctave/boolNDArray.h @@ -128,6 +128,11 @@ SND_BOOL_OP_DECLS (bool, boolNDArray, OCTAVE_API) SND_CMP_OP_DECLS (bool, boolNDArray, OCTAVE_API) +extern OCTAVE_API boolNDArray& +mx_el_and_assign (boolNDArray& m, const boolNDArray& a); +extern OCTAVE_API boolNDArray& +mx_el_or_assign (boolNDArray& m, const boolNDArray& a); + #endif /*
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2009-08-17 Jaroslav Hajek <highegg@gmail.com> + + * ops.h (DEFNDASSIGNOP_FNOP): New macro. + * OPERATORS/op-bm-bm.cc: Define and install &= and |= operators. + 2009-08-17 Jaroslav Hajek <highegg@gmail.com> * OPERATORS/op-m-m.cc: Define and install += and -= operators.
--- a/src/OPERATORS/op-bm-bm.cc +++ b/src/OPERATORS/op-bm-bm.cc @@ -98,6 +98,8 @@ DEFNDCATOP_FN (fm_bm, float_matrix, bool_matrix, float_array, float_array, concat) DEFNDASSIGNOP_FN (assign, bool_matrix, bool_matrix, bool_array, assign) +DEFNDASSIGNOP_FNOP (assign_and, bool_matrix, bool_matrix, bool_array, mx_el_and_assign) +DEFNDASSIGNOP_FNOP (assign_or, bool_matrix, bool_matrix, bool_array, mx_el_or_assign) DEFNULLASSIGNOP_FN (null_assign, bool_matrix, delete_elements) @@ -173,6 +175,9 @@ INSTALL_ASSIGNOP (op_asn_eq, octave_bool_matrix, octave_null_matrix, null_assign); INSTALL_ASSIGNOP (op_asn_eq, octave_bool_matrix, octave_null_str, null_assign); INSTALL_ASSIGNOP (op_asn_eq, octave_bool_matrix, octave_null_sq_str, null_assign); + + INSTALL_ASSIGNOP (op_el_and_eq, octave_bool_matrix, octave_bool_matrix, assign_and); + INSTALL_ASSIGNOP (op_el_or_eq, octave_bool_matrix, octave_bool_matrix, assign_or); } /*
--- a/src/ops.h +++ b/src/ops.h @@ -201,7 +201,7 @@ return octave_value (); \ } -// FIXME: the following currently doesn't handle index. +// FIXME: the following currently don't handle index. #define DEFNDASSIGNOP_OP(name, t1, t2, f, op) \ ASSIGNOPDECL (name) \ { \ @@ -213,6 +213,17 @@ return octave_value (); \ } +#define DEFNDASSIGNOP_FNOP(name, t1, t2, f, fnop) \ + ASSIGNOPDECL (name) \ + { \ + CAST_BINOP_ARGS (CONCAT2(octave_, t1)&, const CONCAT2(octave_, t2)&); \ + \ + assert (idx.empty ()); \ + fnop (v1.matrix_ref (), v2.CONCAT2(f, _value) ()); \ + \ + return octave_value (); \ + } + #define DEFASSIGNANYOP_FN(name, t1, f) \ ASSIGNANYOPDECL (name) \ { \