view liboctave/util/direction.h @ 19006:2e0613dadfee draft

All calls to "find" use the same generic implementation (bug #42408, 42421) * find.cc: Rewrite. Move generic "find" logic to find.h (Ffind) : Changed calls to find_nonzero_elem_idx to find_templated Added unit test for bug #42421 * Array.cc (and .h) (Array::find): Deleted function. Replaced with find::find(Array) from find.h * Array.h: Added typedef for array_iterator (in nz-iterators.h) as Array::iter_type * DiagArray2.h: Added typedef for diag_iterator (in nz-iterators.h) as DiagArray2::iter_type * PermMatrix.h: Added typedef for perm_iterator (in nz-iterators.h) as PermMatrix::iter_type Also added typedef for bool as PermMatrix::element_type (not octave_idx_type) Added an nnz() function (which is an alias for perm_length) and a perm_elem(i) function for retrieving the ith element of the permutation * Sparse.h: Added typedef for sparse_iterator (in nz-iterators.h) as Sparse::iter_type Added a short comment documenting the the argument to the numel function * idx-vector.cc (idx_vector::idx_mask_rep::as_array): Changed Array.find to find::find(Array) (in find.h) * (new file) find.h * (new file) interp-idx.h: Simple methods for converting between interpreter index type and internal octave_idx_type/row-col pair * (new file) min-with-nnz.h: Fast methods for taking an arbitrary matrix M and an octave_idx_type n and finding min(M.nnz(), n) * (new file) nz-iterators.h: Iterators for traversing (in column-major order) the nonzero elements of any array or matrix backwards or forwards * (new file) direction.h: Generic methods for simplifying code has to deal with a "backwards or forwards" template argument * build-sparse-tests.sh: Removed 5-return-value calls to "find" in unit-tests; Admittedly this commit breaks this "feature" which was undocumented and only partially supported to begin with (ie never worked for full matrices, permutation matrices, or diagonal matrices)
author David Spies <dnspies@gmail.com>
date Tue, 17 Jun 2014 16:41:11 -0600
parents
children 3fb030666878
line wrap: on
line source

/*

Copyright (C) 2014 David Spies

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<http://www.gnu.org/licenses/>.

*/
#if !defined (octave_direction_h)
#define octave_direction_h 1

// Simple generic parameterized functions for stepping "forward" or "backward"
// FORWARD and BACKWARD are elements of the "direction enum
//
enum direction
{
  FORWARD = 1, BACKWARD = -1
};

// A struct with two overloaded functions: begin(lo, hi) and
// is_ended (i, lo, hi) where i is assumed to be stepping through the range
// [lo, hi) (inclusive, exclusive).  begin() returns the initial value for i
// and is_ended (i, lo, hi) returns true if i has stepped past the end of the
// range.  To increment i in the proper direction, one can simply say i += dir
// (dir is implicitly cast to an int either 1 or -1 for FORWARD and BACKWARD
// respectively).
// When lo = 0, one can instead use the 1- and 2- argument variants of begin
// and is_ended respectively
template<direction dir>
struct dir_handler
{
  octave_idx_type
  begin (octave_idx_type size) const
  {
    return begin (0, size);
  }

  octave_idx_type
  begin (octave_idx_type lo, octave_idx_type hi) const
  {
    switch (dir) {
      case FORWARD: return lo;
      case BACKWARD: return hi - 1;
    }
  }

  bool
  is_ended (octave_idx_type i, octave_idx_type size) const
  {
    return is_ended (i, 0, size);
  }

  bool
  is_ended (octave_idx_type i, octave_idx_type lo, octave_idx_type hi) const
  {
    switch (dir) {
      case FORWARD: return i >= hi;
      case BACKWARD: return i < lo;
    }
  }
};

#endif