Mercurial > hg > octave-jordi
changeset 20981:d27f66b4b8e6
maint: invert if/else/error instances.
* oct-map.cc, oct-stream.cc, sparse-xpow.cc, symtab.h, typecast.cc,
urlwrite.cc: Invert if/else/error instances.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 25 Dec 2015 00:48:01 -0500 |
parents | c11cea70b638 |
children | 004fa31be0b7 |
files | libinterp/corefcn/oct-map.cc libinterp/corefcn/oct-stream.cc libinterp/corefcn/sparse-xpow.cc libinterp/corefcn/symtab.h libinterp/corefcn/typecast.cc libinterp/corefcn/urlwrite.cc |
diffstat | 6 files changed, 282 insertions(+), 327 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/oct-map.cc +++ b/libinterp/corefcn/oct-map.cc @@ -265,16 +265,14 @@ if (nfields () == 0) dimensions = val.dims (); - if (val.dims () == dimensions) - { - octave_idx_type idx = xkeys.getfield (k); - if (idx < static_cast<octave_idx_type> (xvals.size ())) - xvals[idx] = val; - else - xvals.push_back (val); - } + if (val.dims () != dimensions) + error ("octave_map::setfield: internal error"); + + octave_idx_type idx = xkeys.getfield (k); + if (idx < static_cast<octave_idx_type> (xvals.size ())) + xvals[idx] = val; else - error ("octave_map::setfield: internal error"); + xvals.push_back (val); } void
--- a/libinterp/corefcn/oct-stream.cc +++ b/libinterp/corefcn/oct-stream.cc @@ -119,11 +119,11 @@ retval = -1; else { - if (d >= 0.0) - retval = NINT (d); - else + if (d < 0.0) ::error ("%s: negative value invalid as size specification", who.c_str ()); + + retval = NINT (d); } return retval; @@ -155,10 +155,10 @@ { dnr = size(0); - if (! xisinf (dnr)) - dnc = size(1); - else + if (xisinf (dnr)) ::error ("%s: invalid size specification", who.c_str ()); + + dnc = size(1); } else ::error ("%s: invalid size specification", who.c_str ()); @@ -2623,81 +2623,79 @@ { octave_quit (); - if (elt) + if (! elt) + ::error ("%s: internal error handling format", who.c_str ()); + + // NSA is the number of 'star' args to convert. + int nsa = (elt->fw == -2) + (elt->prec == -2); + + int sa_1 = 0; + int sa_2 = 0; + + if (nsa > 0) { - // NSA is the number of 'star' args to convert. - int nsa = (elt->fw == -2) + (elt->prec == -2); - - int sa_1 = 0; - int sa_2 = 0; - - if (nsa > 0) + sa_1 = val_cache.int_value (); + + if (! val_cache) + break; + else { - sa_1 = val_cache.int_value (); - - if (! val_cache) - break; - else + if (nsa > 1) { - if (nsa > 1) - { - sa_2 = val_cache.int_value (); - - if (! val_cache) - break; - } + sa_2 = val_cache.int_value (); + + if (! val_cache) + break; } } - - if (elt->type == '%') - { - os << "%"; - retval++; - } - else if (elt->args == 0 && elt->text) - { - os << elt->text; - retval += strlen (elt->text); - } - else if (elt->type == 's' || elt->type == 'c') + } + + if (elt->type == '%') + { + os << "%"; + retval++; + } + else if (elt->args == 0 && elt->text) + { + os << elt->text; + retval += strlen (elt->text); + } + else if (elt->type == 's' || elt->type == 'c') + { + octave_value val = val_cache.get_next_value (elt->type); + + if (val_cache) { - octave_value val = val_cache.get_next_value (elt->type); - - if (val_cache) + if (val.is_string ()) { - if (val.is_string ()) - { - std::string sval = val.string_value (); - - retval += do_printf_string (os, elt, nsa, sa_1, - sa_2, sval, who); - } - else - retval += do_numeric_printf_conv (os, elt, nsa, sa_1, - sa_2, val, who); + std::string sval = val.string_value (); + + retval += do_printf_string (os, elt, nsa, sa_1, + sa_2, sval, who); } else - break; - } - else - { - octave_value val = val_cache.get_next_value (); - - if (val_cache) retval += do_numeric_printf_conv (os, elt, nsa, sa_1, sa_2, val, who); - else - break; } - - if (! os) - { - error ("%s: write error", who.c_str ()); - break; - } + else + break; } else - ::error ("%s: internal error handling format", who.c_str ()); + { + octave_value val = val_cache.get_next_value (); + + if (val_cache) + retval += do_numeric_printf_conv (os, elt, nsa, sa_1, + sa_2, val, who); + else + break; + } + + if (! os) + { + error ("%s: write error", who.c_str ()); + break; + } elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); @@ -3044,17 +3042,15 @@ } } - if (! conv_err) - { - retval = seek (xoffset, origin); - - if (retval != 0) - // Note: error is member fcn from octave_stream, not ::error. - error ("fseek: failed to seek to requested position"); - } - else + if (conv_err) ::error ("fseek: invalid value for origin"); + retval = seek (xoffset, origin); + + if (retval != 0) + // Note: error is member fcn from octave_stream, not ::error. + error ("fseek: failed to seek to requested position"); + return retval; } @@ -4151,16 +4147,18 @@ // overwrite this entry, although the wrong entry might have done harm // before. - if (list.size () < list.max_size ()) - list[stream_number] = os; - else + if (list.size () >= list.max_size ()) ::error ("could not create file id"); + list[stream_number] = os; + return stream_number; - } static void +gripe_invalid_file_id (int fid, const std::string& who) GCC_ATTR_NORETURN; + +static void gripe_invalid_file_id (int fid, const std::string& who) { if (who.empty ()) @@ -4174,25 +4172,21 @@ { octave_stream retval; - if (fid >= 0) + if (fid < 0) + gripe_invalid_file_id (fid, who); + + if (lookup_cache != list.end () && lookup_cache->first == fid) + retval = lookup_cache->second; + else { - if (lookup_cache != list.end () && lookup_cache->first == fid) - retval = lookup_cache->second; - else - { - ostrl_map::const_iterator iter = list.find (fid); - - if (iter != list.end ()) - { - retval = iter->second; - lookup_cache = iter; - } - else - gripe_invalid_file_id (fid, who); - } + ostrl_map::const_iterator iter = list.find (fid); + + if (iter == list.end ()) + gripe_invalid_file_id (fid, who); + + retval = iter->second; + lookup_cache = iter; } - else - gripe_invalid_file_id (fid, who); return retval; } @@ -4209,35 +4203,26 @@ int octave_stream_list::do_remove (int fid, const std::string& who) { - int retval = -1; - // Can't remove stdin (std::cin), stdout (std::cout), or stderr (std::cerr). if (fid < 3) gripe_invalid_file_id (fid, who); - else - { - ostrl_map::iterator iter = list.find (fid); - - if (iter == list.end ()) - gripe_invalid_file_id (fid, who); - else - { - octave_stream os = iter->second; - list.erase (iter); - lookup_cache = list.end (); - - // FIXME: is this check redundant? - if (os.is_valid ()) - { - os.close (); - retval = 0; - } - else - gripe_invalid_file_id (fid, who); - } - } - - return retval; + + ostrl_map::iterator iter = list.find (fid); + + if (iter == list.end ()) + gripe_invalid_file_id (fid, who); + + octave_stream os = iter->second; + list.erase (iter); + lookup_cache = list.end (); + + // FIXME: is this check redundant? + if (! os.is_valid ()) + gripe_invalid_file_id (fid, who); + + os.close (); + + return 0; } int
--- a/libinterp/corefcn/sparse-xpow.cc +++ b/libinterp/corefcn/sparse-xpow.cc @@ -65,64 +65,60 @@ if (nr == 0 || nc == 0 || nr != nc) error ("for A^b, A must be a square matrix. Use .^ for elementwise power."); + + if (static_cast<int> (b) != b) + error ("use full(a) ^ full(b)"); + + int btmp = static_cast<int> (b); + if (btmp == 0) + { + SparseMatrix tmp = SparseMatrix (nr, nr, nr); + for (octave_idx_type i = 0; i < nr; i++) + { + tmp.data (i) = 1.0; + tmp.ridx (i) = i; + } + for (octave_idx_type i = 0; i < nr + 1; i++) + tmp.cidx (i) = i; + + retval = tmp; + } else { - if (static_cast<int> (b) == b) + SparseMatrix atmp; + if (btmp < 0) { - int btmp = static_cast<int> (b); - if (btmp == 0) - { - SparseMatrix tmp = SparseMatrix (nr, nr, nr); - for (octave_idx_type i = 0; i < nr; i++) - { - tmp.data (i) = 1.0; - tmp.ridx (i) = i; - } - for (octave_idx_type i = 0; i < nr + 1; i++) - tmp.cidx (i) = i; - - retval = tmp; - } - else - { - SparseMatrix atmp; - if (btmp < 0) - { - btmp = -btmp; - - octave_idx_type info; - double rcond = 0.0; - MatrixType mattyp (a); + btmp = -btmp; - atmp = a.inverse (mattyp, info, rcond, 1); - - if (info == -1) - warning ("inverse: matrix singular to machine\ - precision, rcond = %g", rcond); - } - else - atmp = a; - - SparseMatrix result (atmp); - - btmp--; + octave_idx_type info; + double rcond = 0.0; + MatrixType mattyp (a); - while (btmp > 0) - { - if (btmp & 1) - result = result * atmp; - - btmp >>= 1; + atmp = a.inverse (mattyp, info, rcond, 1); - if (btmp > 0) - atmp = atmp * atmp; - } - - retval = result; - } + if (info == -1) + warning ("inverse: matrix singular to machine\ + precision, rcond = %g", rcond); } else - error ("use full(a) ^ full(b)"); + atmp = a; + + SparseMatrix result (atmp); + + btmp--; + + while (btmp > 0) + { + if (btmp & 1) + result = result * atmp; + + btmp >>= 1; + + if (btmp > 0) + atmp = atmp * atmp; + } + + retval = result; } return retval; @@ -138,64 +134,60 @@ if (nr == 0 || nc == 0 || nr != nc) error ("for A^b, A must be a square matrix. Use .^ for elementwise power."); + + if (static_cast<int> (b) != b) + error ("use full(a) ^ full(b)"); + + int btmp = static_cast<int> (b); + if (btmp == 0) + { + SparseMatrix tmp = SparseMatrix (nr, nr, nr); + for (octave_idx_type i = 0; i < nr; i++) + { + tmp.data (i) = 1.0; + tmp.ridx (i) = i; + } + for (octave_idx_type i = 0; i < nr + 1; i++) + tmp.cidx (i) = i; + + retval = tmp; + } else { - if (static_cast<int> (b) == b) + SparseComplexMatrix atmp; + if (btmp < 0) { - int btmp = static_cast<int> (b); - if (btmp == 0) - { - SparseMatrix tmp = SparseMatrix (nr, nr, nr); - for (octave_idx_type i = 0; i < nr; i++) - { - tmp.data (i) = 1.0; - tmp.ridx (i) = i; - } - for (octave_idx_type i = 0; i < nr + 1; i++) - tmp.cidx (i) = i; - - retval = tmp; - } - else - { - SparseComplexMatrix atmp; - if (btmp < 0) - { - btmp = -btmp; - - octave_idx_type info; - double rcond = 0.0; - MatrixType mattyp (a); + btmp = -btmp; - atmp = a.inverse (mattyp, info, rcond, 1); - - if (info == -1) - warning ("inverse: matrix singular to machine\ - precision, rcond = %g", rcond); - } - else - atmp = a; - - SparseComplexMatrix result (atmp); - - btmp--; + octave_idx_type info; + double rcond = 0.0; + MatrixType mattyp (a); - while (btmp > 0) - { - if (btmp & 1) - result = result * atmp; - - btmp >>= 1; + atmp = a.inverse (mattyp, info, rcond, 1); - if (btmp > 0) - atmp = atmp * atmp; - } - - retval = result; - } + if (info == -1) + warning ("inverse: matrix singular to machine\ + precision, rcond = %g", rcond); } else - error ("use full(a) ^ full(b)"); + atmp = a; + + SparseComplexMatrix result (atmp); + + btmp--; + + while (btmp > 0) + { + if (btmp & 1) + result = result * atmp; + + btmp >>= 1; + + if (btmp > 0) + atmp = atmp * atmp; + } + + retval = result; } return retval;
--- a/libinterp/corefcn/symtab.h +++ b/libinterp/corefcn/symtab.h @@ -142,13 +142,11 @@ { set_iterator p = in_use.find (scope); - if (p != in_use.end ()) - { - in_use.erase (p); - free_list.insert (scope); - } - else + if (p == in_use.end ()) error ("free_scope: scope %d not found!", scope); + + in_use.erase (p); + free_list.insert (scope); } std::list<scope_id> do_scopes (void) const @@ -395,15 +393,15 @@ { if (is_persistent ()) error ("can't make persistent variable %s global", name.c_str ()); - else - storage_class |= global; + + storage_class |= global; } void mark_persistent (void) { if (is_global ()) error ("can't make global variable %s persistent", name.c_str ()); - else - storage_class |= persistent; + + storage_class |= persistent; } void mark_added_static (void) { storage_class |= added_static; } @@ -1168,7 +1166,8 @@ { if (scope == xglobal_scope) error ("can't set scope to global"); - else if (scope != xcurrent_scope) + + if (scope != xcurrent_scope) { all_instances_iterator p = all_instances.find (scope); @@ -1191,26 +1190,22 @@ { if (scope == xglobal_scope) error ("can't set scope to global"); - else + + if (scope != xcurrent_scope) { - if (scope != xcurrent_scope) - { - all_instances_iterator p = all_instances.find (scope); - - if (p == all_instances.end ()) - error ("scope not found!"); - else - { - instance = p->second; - - xcurrent_scope = scope; - - xcurrent_context = context; - } - } - else - xcurrent_context = context; + all_instances_iterator p = all_instances.find (scope); + + if (p == all_instances.end ()) + error ("scope not found!"); + + instance = p->second; + + xcurrent_scope = scope; + + xcurrent_context = context; } + else + xcurrent_context = context; } static void erase_scope (scope_id scope) @@ -1909,26 +1904,22 @@ { if (xcurrent_scope == xglobal_scope || xcurrent_scope == xtop_scope) error ("invalid call to xymtab::push_context"); - else - { - symbol_table *inst = get_instance (xcurrent_scope); - - if (inst) - inst->do_push_context (); - } + + symbol_table *inst = get_instance (xcurrent_scope); + + if (inst) + inst->do_push_context (); } static void pop_context (void) { if (xcurrent_scope == xglobal_scope || xcurrent_scope == xtop_scope) error ("invalid call to xymtab::pop_context"); - else - { - symbol_table *inst = get_instance (xcurrent_scope); - - if (inst) - inst->do_pop_context (); - } + + symbol_table *inst = get_instance (xcurrent_scope); + + if (inst) + inst->do_pop_context (); } // For unwind_protect. @@ -2245,8 +2236,8 @@ { if (scope == xglobal_scope || scope == xtop_scope) error ("can't free global or top-level scopes!"); - else - symbol_table::scope_id_cache::free (scope); + + symbol_table::scope_id_cache::free (scope); } static void stash_dir_name_for_subfunctions (scope_id scope,
--- a/libinterp/corefcn/typecast.cc +++ b/libinterp/corefcn/typecast.cc @@ -72,18 +72,14 @@ typedef typename ArrayType::element_type T; octave_idx_type n = byte_size / sizeof (T); - if (n * static_cast<int> (sizeof (T)) == byte_size) - { - ArrayType retval (get_vec_dims (old_dims, n)); - T *dest = retval.fortran_vec (); - std::memcpy (dest, data, n * sizeof (T)); - - return retval; - } - else + if (n * static_cast<int> (sizeof (T)) != byte_size) error ("typecast: incorrect number of input values to make output value"); - return ArrayType (); + ArrayType retval (get_vec_dims (old_dims, n)); + T *dest = retval.fortran_vec (); + std::memcpy (dest, data, n * sizeof (T)); + + return retval; } @@ -268,32 +264,27 @@ octave_idx_type n = bitp.numel () / (sizeof (T) * std::numeric_limits<unsigned char>::digits); - if (n * static_cast<int> (sizeof (T)) * std::numeric_limits<unsigned char>::digits == bitp.numel ()) - { - - ArrayType retval (get_vec_dims (bitp.dims (), n)); - - const bool *bits = bitp.fortran_vec (); - char *packed = reinterpret_cast<char *> (retval.fortran_vec ()); - - octave_idx_type m = n * sizeof (T); - - for (octave_idx_type i = 0; i < m; i++) - { - char c = bits[0]; - for (int j = 1; j < std::numeric_limits<unsigned char>::digits; j++) - c |= bits[j] << j; - - packed[i] = c; - bits += std::numeric_limits<unsigned char>::digits; - } - - return retval; - } - else + if (n * static_cast<int> (sizeof (T)) * std::numeric_limits<unsigned char>::digits != bitp.numel ()) error ("bitpack: incorrect number of bits to make up output value"); - return ArrayType (); + ArrayType retval (get_vec_dims (bitp.dims (), n)); + + const bool *bits = bitp.fortran_vec (); + char *packed = reinterpret_cast<char *> (retval.fortran_vec ()); + + octave_idx_type m = n * sizeof (T); + + for (octave_idx_type i = 0; i < m; i++) + { + char c = bits[0]; + for (int j = 1; j < std::numeric_limits<unsigned char>::digits; j++) + c |= bits[j] << j; + + packed[i] = c; + bits += std::numeric_limits<unsigned char>::digits; + } + + return retval; } DEFUN (bitpack, args, ,
--- a/libinterp/corefcn/urlwrite.cc +++ b/libinterp/corefcn/urlwrite.cc @@ -185,11 +185,11 @@ url_transfer obj (host, user, passwd, os); - if (obj.is_valid ()) - handle_map[h] = obj; - else + if (! obj.is_valid ()) error ("support for URL transfers was disabled when Octave was built"); + handle_map[h] = obj; + return h; } @@ -259,21 +259,19 @@ { iterator p = handle_map.find (h); - if (p != handle_map.end ()) - { - // Curl handles are negative integers plus some random - // fractional part. To avoid running out of integers, we - // recycle the integer part but tack on a new random part - // each time. + if (p == handle_map.end ()) + error ("ch_manager::free: invalid object %g", h.value ()); - handle_map.erase (p); + // Curl handles are negative integers plus some random + // fractional part. To avoid running out of integers, we + // recycle the integer part but tack on a new random part + // each time. - if (h.value () < 0) - handle_free_list.insert - (std::ceil (h.value ()) - make_handle_fraction ()); - } - else - error ("ch_manager::free: invalid object %g", h.value ()); + handle_map.erase (p); + + if (h.value () < 0) + handle_free_list.insert + (std::ceil (h.value ()) - make_handle_fraction ()); } }