annotate liboctave/array/Array-base.cc @ 31771:21f9b34eb893

maint: Eliminate "(void)" in C++ function prototypes/declarations. * mk-opts.pl, external.txi, embedded.cc, make_int.cc, standalone.cc, standalonebuiltin.cc, BaseControl.cc, BaseControl.h, ButtonControl.cc, ButtonControl.h, ButtonGroup.cc, ButtonGroup.h, Canvas.cc, Canvas.h, CheckBoxControl.cc, CheckBoxControl.h, Container.cc, Container.h, ContextMenu.cc, ContextMenu.h, EditControl.cc, EditControl.h, Figure.cc, Figure.h, FigureWindow.cc, FigureWindow.h, GLCanvas.cc, GLCanvas.h, GenericEventNotify.h, KeyMap.cc, ListBoxControl.cc, ListBoxControl.h, Logger.cc, Logger.h, Menu.cc, Menu.h, MenuContainer.h, Object.cc, Object.h, ObjectProxy.cc, ObjectProxy.h, Panel.cc, Panel.h, PopupMenuControl.cc, PopupMenuControl.h, PushButtonControl.cc, PushButtonControl.h, PushTool.cc, PushTool.h, RadioButtonControl.cc, RadioButtonControl.h, SliderControl.cc, SliderControl.h, Table.cc, Table.h, TextControl.cc, TextControl.h, TextEdit.h, ToggleButtonControl.cc, ToggleButtonControl.h, ToggleTool.cc, ToggleTool.h, ToolBar.cc, ToolBar.h, ToolBarButton.cc, ToolBarButton.h, gl-select.cc, gl-select.h, qopengl-functions.h, qt-graphics-toolkit.h, qdialog.cpp, qfontdialog.cpp, qprintdialog_win.cpp, liboctgui-build-info.h, liboctgui-build-info.in.cc, color-picker.cc, color-picker.h, command-widget.cc, command-widget.h, community-news.cc, community-news.h, dialog.cc, dialog.h, documentation-bookmarks.cc, documentation-bookmarks.h, documentation-dock-widget.cc, documentation-dock-widget.h, documentation.cc, documentation.h, dw-main-window.cc, dw-main-window.h, external-editor-interface.cc, external-editor-interface.h, files-dock-widget.cc, files-dock-widget.h, find-files-dialog.cc, find-files-dialog.h, find-files-model.cc, find-files-model.h, gui-preferences.cc, gui-preferences.h, gui-settings.cc, gui-settings.h, history-dock-widget.cc, history-dock-widget.h, interpreter-qobject.cc, interpreter-qobject.h, file-editor-interface.h, file-editor-tab.cc, file-editor-tab.h, file-editor.cc, file-editor.h, find-dialog.cc, find-dialog.h, marker.cc, marker.h, octave-qscintilla.cc, octave-qscintilla.h, octave-txt-lexer.cc, octave-txt-lexer.h, main-window.cc, main-window.h, news-reader.cc, news-reader.h, octave-dock-widget.cc, octave-dock-widget.h, octave-qobject.cc, octave-qobject.h, qt-application.cc, qt-application.h, qt-interpreter-events.cc, qt-interpreter-events.h, release-notes.cc, release-notes.h, set-path-dialog.cc, set-path-dialog.h, set-path-model.cc, set-path-model.h, settings-dialog.cc, settings-dialog.h, shortcuts-tree-widget.cc, shortcuts-tree-widget.h, tab-bar.cc, tab-bar.h, terminal-dock-widget.cc, terminal-dock-widget.h, variable-editor-model.cc, variable-editor-model.h, variable-editor.cc, variable-editor.h, welcome-wizard.cc, welcome-wizard.h, workspace-model.cc, workspace-model.h, workspace-view.cc, workspace-view.h, build-env.h, Cell.cc, Cell.h, __contourc__.cc, __magick_read__.cc, auto-shlib.cc, auto-shlib.h, base-text-renderer.h, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, debug.cc, defaults.cc, defaults.h, defun.cc, display.cc, display.h, dynamic-ld.cc, dynamic-ld.h, environment.cc, environment.h, error.cc, error.h, errwarn.cc, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, ft-text-renderer.cc, ft-text-renderer.h, genprops.awk, gh-manager.cc, gh-manager.h, gl-render.cc, gl-render.h, gl2ps-print.cc, graphics-toolkit.h, graphics.cc, graphics.in.h, gtk-manager.cc, gtk-manager.h, help.cc, help.h, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, jsondecode.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, ls-hdf5.cc, ls-hdf5.h, mxarray.h, oct-errno.h, oct-errno.in.cc, oct-fstrm.cc, oct-fstrm.h, oct-handle.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-map.cc, oct-map.h, oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, pager.cc, pager.h, pr-flt-fmt.cc, pr-flt-fmt.h, pr-output.cc, pr-output.h, procstream.cc, procstream.h, settings.cc, settings.h, sighandlers.cc, sighandlers.h, stack-frame.cc, stack-frame.h, svd.cc, syminfo.cc, syminfo.h, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, toplev.cc, url-handle-manager.cc, url-handle-manager.h, variables.cc, xpow.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, audiodevinfo.cc, gzip.cc, liboctinterp-build-info.h, liboctinterp-build-info.in.cc, mk-build-env-features.sh, mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base-diag.cc, ov-base-diag.h, ov-base-int.cc, ov-base-int.h, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base-scalar.h, ov-base-sparse.cc, ov-base-sparse.h, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-bool-mat.h, ov-bool-sparse.cc, ov-bool-sparse.h, ov-bool.cc, ov-bool.h, ov-builtin.cc, ov-builtin.h, ov-cell.cc, ov-cell.h, ov-ch-mat.cc, ov-ch-mat.h, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-colon.h, ov-complex.cc, ov-complex.h, ov-cs-list.h, ov-cx-diag.cc, ov-cx-diag.h, ov-cx-mat.cc, ov-cx-mat.h, ov-cx-sparse.cc, ov-cx-sparse.h, ov-dld-fcn.cc, ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.cc, ov-fcn.h, ov-float.cc, ov-float.h, ov-flt-complex.cc, ov-flt-complex.h, ov-flt-cx-diag.cc, ov-flt-cx-diag.h, ov-flt-cx-mat.cc, ov-flt-cx-mat.h, ov-flt-re-diag.cc, ov-flt-re-diag.h, ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-intx.h, ov-java.cc, ov-java.h, ov-lazy-idx.cc, ov-lazy-idx.h, ov-legacy-range.cc, ov-legacy-range.h, ov-magic-int.cc, ov-magic-int.h, ov-mex-fcn.cc, ov-mex-fcn.h, ov-null-mat.cc, ov-null-mat.h, ov-oncleanup.cc, ov-oncleanup.h, ov-perm.cc, ov-perm.h, ov-range.cc, ov-range.h, ov-re-diag.cc, ov-re-diag.h, ov-re-mat.cc, ov-re-mat.h, ov-re-sparse.cc, ov-re-sparse.h, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc, ov-str-mat.h, ov-struct.cc, ov-struct.h, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.cc, ovl.h, octave.cc, octave.h, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc, oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.h, pt-cbinop.h, pt-cell.h, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.h, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-walk.h, pt.cc, pt.h, token.cc, token.h, usage.h, Array-base.cc, Array.h, CColVector.cc, CColVector.h, CDiagMatrix.cc, CDiagMatrix.h, CMatrix.cc, CMatrix.h, CNDArray.cc, CNDArray.h, CRowVector.cc, CRowVector.h, CSparse.cc, CSparse.h, DiagArray2.cc, DiagArray2.h, MArray.cc, MArray.h, MDiagArray2.h, MSparse.h, MatrixType.cc, MatrixType.h, PermMatrix.cc, PermMatrix.h, Range.cc, Range.h, Sparse-b.cc, Sparse.cc, Sparse.h, boolMatrix.cc, boolMatrix.h, boolNDArray.cc, boolNDArray.h, boolSparse.cc, boolSparse.h, chMatrix.h, chNDArray.h, dColVector.cc, dColVector.h, dDiagMatrix.cc, dDiagMatrix.h, dMatrix.cc, dMatrix.h, dNDArray.cc, dNDArray.h, dRowVector.cc, dRowVector.h, dSparse.cc, dSparse.h, dim-vector.cc, dim-vector.h, fCColVector.cc, fCColVector.h, fCDiagMatrix.cc, fCDiagMatrix.h, fCMatrix.cc, fCMatrix.h, fCNDArray.cc, fCNDArray.h, fCRowVector.cc, fCRowVector.h, fColVector.cc, fColVector.h, fDiagMatrix.cc, fDiagMatrix.h, fMatrix.cc, fMatrix.h, fNDArray.cc, fNDArray.h, fRowVector.cc, fRowVector.h, idx-vector.cc, idx-vector.h, intNDArray.cc, intNDArray.h, liboctave-build-info.h, liboctave-build-info.in.cc, CollocWt.cc, CollocWt.h, DAE.h, DAEFunc.h, DAERT.h, DAERTFunc.h, DASPK.cc, DASPK.h, DASRT.cc, DASRT.h, DASSL.cc, DASSL.h, DET.h, EIG.h, LSODE.cc, LSODE.h, ODE.h, ODEFunc.h, ODES.h, ODESFunc.h, Quad.h, aepbalance.cc, aepbalance.h, base-dae.h, base-de.h, chol.cc, chol.h, eigs-base.cc, fEIG.h, gepbalance.h, gsvd.cc, gsvd.h, hess.h, lu.cc, lu.h, oct-fftw.cc, oct-fftw.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randmtzig.cc, randmtzig.h, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.h, base-list.h, caseless-str.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, data-conv.cc, file-info.h, glob-match.cc, glob-match.h, kpse.cc, kpse.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.cc, lo-ieee.h, lo-regexp.cc, lo-regexp.h, oct-inttypes.cc, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sort.cc, oct-sort.h, oct-sparse.cc, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, singleton-cleanup.cc, singleton-cleanup.h, str-vec.cc, str-vec.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h, version.cc, version.in.h, cxx-signal-helpers.cc, acinclude.m4, main-cli.cc, main-gui.cc, main.in.cc, mkoctfile.in.cc, octave-build-info.h, octave-build-info.in.cc, octave-config.in.cc, octave-svgconvert.cc, shared-fcns.h: maint: Eliminate "(void)" in C++ function prototypes/declarations.
author Rik <rik@octave.org>
date Tue, 24 Jan 2023 17:19:44 -0800
parents 597f3ee61a48
children febd82d1a8de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
31706
597f3ee61a48 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31404
diff changeset
3 // Copyright (C) 1993-2023 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
237
5a9e23307fb0 [project @ 1993-11-30 20:23:04 by jwe]
jwe
parents:
diff changeset
25
31396
7e60506a5428 Prevent implicit instantiations of the Array class template (bug #61711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31383
diff changeset
26 // Do not include this file directly to instantiate the Array<T> template
7e60506a5428 Prevent implicit instantiations of the Array class template (bug #61711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31383
diff changeset
27 // class in code outside liboctave. Include "Array-oct.cc" or "Array.cc"
7e60506a5428 Prevent implicit instantiations of the Array class template (bug #61711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31383
diff changeset
28 // instead.
7e60506a5428 Prevent implicit instantiations of the Array class template (bug #61711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31383
diff changeset
29
21690
b6a686543080 Only include config.h in files that are compiled separately.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
30 // This file should not include config.h. It is only included in other
b6a686543080 Only include config.h in files that are compiled separately.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
31 // C++ source files that should have included config.h before including
b6a686543080 Only include config.h in files that are compiled separately.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
32 // this file.
237
5a9e23307fb0 [project @ 1993-11-30 20:23:04 by jwe]
jwe
parents:
diff changeset
33
25438
cb1606f78f6b prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
34 #include <ostream>
23443
3f1bf237908b maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents: 23420
diff changeset
35
3f1bf237908b maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents: 23420
diff changeset
36 #include "Array-util.h"
237
5a9e23307fb0 [project @ 1993-11-30 20:23:04 by jwe]
jwe
parents:
diff changeset
37 #include "Array.h"
21785
c9f8a7f7915e use forward declarations instead of including oct-inttypes.h where possible
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
38 #include "lo-mappers.h"
8377
25bc2d31e1bf improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents: 8333
diff changeset
39 #include "oct-locbuf.h"
1560
27a03373de41 [project @ 1995-10-12 07:22:26 by jwe]
jwe
parents: 1550
diff changeset
40
1360
7eb93d12654c [project @ 1995-09-05 21:51:54 by jwe]
jwe
parents: 1321
diff changeset
41 // One dimensional array class. Handles the reference counting for
7eb93d12654c [project @ 1995-09-05 21:51:54 by jwe]
jwe
parents: 1321
diff changeset
42 // all the derived classes.
237
5a9e23307fb0 [project @ 1993-11-30 20:23:04 by jwe]
jwe
parents:
diff changeset
43
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
44 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
45 typename Array<T, Alloc>::ArrayRep *
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
46 Array<T, Alloc>::nil_rep ()
21573
f3f8e1d3e399 avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
47 {
f3f8e1d3e399 avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
48 static ArrayRep nr;
f3f8e1d3e399 avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
49 return &nr;
f3f8e1d3e399 avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
50 }
f3f8e1d3e399 avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
51
22139
f8212eb6da00 Array: add constructor from std sequence containers (patch 9047)
Carnë Draug <carandraug@octave.org>
parents: 22022
diff changeset
52 // This is similar to the template for containers but specialized for Array.
f8212eb6da00 Array: add constructor from std sequence containers (patch 9047)
Carnë Draug <carandraug@octave.org>
parents: 22022
diff changeset
53 // Note we can't specialize a member without also specializing the class.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
54 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
55 Array<T, Alloc>::Array (const Array<T, Alloc>& a, const dim_vector& dv)
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
56 : m_dimensions (dv), m_rep (a.m_rep),
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
57 m_slice_data (a.m_slice_data), m_slice_len (a.m_slice_len)
4834
8f669cc5a901 [project @ 2004-03-11 18:49:17 by jwe]
jwe
parents: 4832
diff changeset
58 {
31282
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
59 bool invalid_size = false;
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
60
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
61 octave_idx_type new_numel = 0;
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
62
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
63 try
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
64 {
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
65 // Safe numel may throw a bad_alloc error because the new
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
66 // dimensions are too large to allocate. Trap that here so
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
67 // we can issue a more helpful diagnostic that is consistent
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
68 // with other size mismatch failures.
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
69
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
70 new_numel = m_dimensions.safe_numel ();
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
71 }
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
72 catch (const std::bad_alloc&)
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
73 {
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
74 invalid_size = true;
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
75 }
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
76
4707df477065 reshape: Show more specific error message if target size is large (bug #62870).
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
77 if (invalid_size || new_numel != a.numel ())
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
78 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
79 std::string dimensions_str = a.m_dimensions.str ();
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
80 std::string new_dims_str = m_dimensions.str ();
10352
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
81
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
82 (*current_liboctave_error_handler)
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
83 ("reshape: can't reshape %s array to %s array",
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
84 dimensions_str.c_str (), new_dims_str.c_str ());
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
85 }
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
86
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
87 // This goes here because if an exception is thrown by the above,
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
88 // destructor will be never called.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
89 m_rep->m_count++;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
90 m_dimensions.chop_trailing_singletons ();
10352
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
91 }
a3635bc1ea19 remove Array2
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
92
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
93 template <typename T, typename Alloc>
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
94 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
95 Array<T, Alloc>::fill (const T& val)
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
96 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
97 if (m_rep->m_count > 1)
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
98 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
99 --m_rep->m_count;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
100 m_rep = new ArrayRep (numel (), val);
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
101 m_slice_data = m_rep->m_data;
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
102 }
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
103 else
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
104 std::fill_n (m_slice_data, m_slice_len, val);
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
105 }
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
106
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
107 template <typename T, typename Alloc>
9546
1beb23d2b892 optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
108 void
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
109 Array<T, Alloc>::clear ()
9546
1beb23d2b892 optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
110 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
111 if (--m_rep->m_count == 0)
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
112 delete m_rep;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
113
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
114 m_rep = nil_rep ();
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
115 m_rep->m_count++;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
116 m_slice_data = m_rep->m_data;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
117 m_slice_len = m_rep->m_len;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
118
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
119 m_dimensions = dim_vector ();
9546
1beb23d2b892 optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
120 }
1beb23d2b892 optimize op= in common cases
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
121
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
122 template <typename T, typename Alloc>
9624
3fc7272937ce implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents: 9601
diff changeset
123 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
124 Array<T, Alloc>::clear (const dim_vector& dv)
9624
3fc7272937ce implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents: 9601
diff changeset
125 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
126 if (--m_rep->m_count == 0)
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
127 delete m_rep;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
128
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
129 m_rep = new ArrayRep (dv.safe_numel ());
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
130 m_slice_data = m_rep->m_data;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
131 m_slice_len = m_rep->m_len;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
132
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
133 m_dimensions = dv;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
134 m_dimensions.chop_trailing_singletons ();
9624
3fc7272937ce implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents: 9601
diff changeset
135 }
3fc7272937ce implement Array<T>::clear overloads
Jaroslav Hajek <highegg@gmail.com>
parents: 9601
diff changeset
136
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
137 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
138 Array<T, Alloc>
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
139 Array<T, Alloc>::squeeze () const
4532
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
140 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
141 Array<T, Alloc> retval = *this;
4532
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
142
4929
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
143 if (ndims () > 2)
4532
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
144 {
4929
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
145 bool dims_changed = false;
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
146
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
147 dim_vector new_dimensions = m_dimensions;
4929
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
148
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
149 int k = 0;
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
150
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
151 for (int i = 0; i < ndims (); i++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
152 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
153 if (m_dimensions(i) == 1)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
154 dims_changed = true;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
155 else
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
156 new_dimensions(k++) = m_dimensions(i);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
157 }
4929
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
158
585e9a8c2ef8 [project @ 2004-08-03 20:45:34 by jwe]
jwe
parents: 4916
diff changeset
159 if (dims_changed)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
160 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
161 switch (k)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
162 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
163 case 0:
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
164 new_dimensions = dim_vector (1, 1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
165 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
166
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
167 case 1:
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
168 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
169 octave_idx_type tmp = new_dimensions(0);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
170
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
171 new_dimensions.resize (2);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
172
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
173 new_dimensions(0) = tmp;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
174 new_dimensions(1) = 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
175 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
176 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
177
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
178 default:
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
179 new_dimensions.resize (k);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
180 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
181 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
182 }
4532
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
183
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
184 retval = Array<T, Alloc> (*this, new_dimensions);
4532
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
185 }
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
186
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
187 return retval;
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
188 }
01ee68d18069 [project @ 2003-10-10 00:49:54 by jwe]
jwe
parents: 4530
diff changeset
189
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
190 template <typename T, typename Alloc>
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5265
diff changeset
191 octave_idx_type
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
192 Array<T, Alloc>::compute_index (octave_idx_type i, octave_idx_type j) const
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
193 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
194 return ::compute_index (i, j, m_dimensions);
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
195 }
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
196
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
197 template <typename T, typename Alloc>
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
198 octave_idx_type
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
199 Array<T, Alloc>::compute_index (octave_idx_type i, octave_idx_type j,
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
200 octave_idx_type k) const
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
201 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
202 return ::compute_index (i, j, k, m_dimensions);
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
203 }
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
204
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
205 template <typename T, typename Alloc>
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
206 octave_idx_type
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
207 Array<T, Alloc>::compute_index (const Array<octave_idx_type>& ra_idx) const
237
5a9e23307fb0 [project @ 1993-11-30 20:23:04 by jwe]
jwe
parents:
diff changeset
208 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
209 return ::compute_index (ra_idx, m_dimensions);
237
5a9e23307fb0 [project @ 1993-11-30 20:23:04 by jwe]
jwe
parents:
diff changeset
210 }
5a9e23307fb0 [project @ 1993-11-30 20:23:04 by jwe]
jwe
parents:
diff changeset
211
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
212 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
213 T&
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
214 Array<T, Alloc>::checkelem (octave_idx_type n)
2049
468a96dd03fc [project @ 1996-03-29 20:09:05 by jwe]
jwe
parents: 2006
diff changeset
215 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
216 // Do checks directly to avoid recomputing m_slice_len.
10370
9c4daf174387 implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents: 10366
diff changeset
217 if (n < 0)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
218 octave::err_invalid_index (n);
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
219 if (n >= m_slice_len)
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
220 octave::err_index_out_of_range (1, 1, n+1, m_slice_len, m_dimensions);
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
221
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
222 return elem (n);
2049
468a96dd03fc [project @ 1996-03-29 20:09:05 by jwe]
jwe
parents: 2006
diff changeset
223 }
468a96dd03fc [project @ 1996-03-29 20:09:05 by jwe]
jwe
parents: 2006
diff changeset
224
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
225 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
226 T&
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
227 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
228 {
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
229 return elem (compute_index (i, j));
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
230 }
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
231
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
232 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
233 T&
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
234 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k)
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
235 {
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
236 return elem (compute_index (i, j, k));
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
237 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
238
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
239 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
240 T&
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
241 Array<T, Alloc>::checkelem (const Array<octave_idx_type>& ra_idx)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
242 {
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
243 return elem (compute_index (ra_idx));
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
244 }
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
245
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
246 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
247 typename Array<T, Alloc>::crefT
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
248 Array<T, Alloc>::checkelem (octave_idx_type n) const
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
249 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
250 // Do checks directly to avoid recomputing m_slice_len.
10370
9c4daf174387 implement IDs for common liboctave exceptions
Jaroslav Hajek <highegg@gmail.com>
parents: 10366
diff changeset
251 if (n < 0)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
252 octave::err_invalid_index (n);
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
253 if (n >= m_slice_len)
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
254 octave::err_index_out_of_range (1, 1, n+1, m_slice_len, m_dimensions);
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
255
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
256 return elem (n);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
257 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
258
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
259 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
260 typename Array<T, Alloc>::crefT
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
261 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j) const
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
262 {
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
263 return elem (compute_index (i, j));
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
264 }
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
265
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
266 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
267 typename Array<T, Alloc>::crefT
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
268 Array<T, Alloc>::checkelem (octave_idx_type i, octave_idx_type j,
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
269 octave_idx_type k) const
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
270 {
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
271 return elem (compute_index (i, j, k));
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
272 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
273
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
274 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
275 typename Array<T, Alloc>::crefT
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
276 Array<T, Alloc>::checkelem (const Array<octave_idx_type>& ra_idx) const
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
277 {
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10636
diff changeset
278 return elem (compute_index (ra_idx));
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
279 }
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
280
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
281 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
282 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
283 Array<T, Alloc>::column (octave_idx_type k) const
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
284 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
285 octave_idx_type r = m_dimensions(0);
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
286
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
287 return Array<T, Alloc> (*this, dim_vector (r, 1), k*r, k*r + r);
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
288 }
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
289
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
290 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
291 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
292 Array<T, Alloc>::page (octave_idx_type k) const
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
293 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
294 octave_idx_type r = m_dimensions(0);
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
295 octave_idx_type c = m_dimensions(1);
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
296 octave_idx_type p = r*c;
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
297
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
298 return Array<T, Alloc> (*this, dim_vector (r, c), k*p, k*p + p);
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
299 }
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
300
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
301 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
302 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
303 Array<T, Alloc>::linear_slice (octave_idx_type lo, octave_idx_type up) const
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
304 {
23487
af2f1f3dbe06 eliminate --enable-bounds-check configure option
John W. Eaton <jwe@octave.org>
parents: 23461
diff changeset
305 if (up < lo)
af2f1f3dbe06 eliminate --enable-bounds-check configure option
John W. Eaton <jwe@octave.org>
parents: 23461
diff changeset
306 up = lo;
af2f1f3dbe06 eliminate --enable-bounds-check configure option
John W. Eaton <jwe@octave.org>
parents: 23461
diff changeset
307
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
308 return Array<T, Alloc> (*this, dim_vector (up - lo, 1), lo, up);
4567
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4553
diff changeset
309 }
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4553
diff changeset
310
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
311 // Helper class for multi-d dimension permuting (generalized transpose).
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
312 class rec_permute_helper
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
313 {
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
314 public:
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
315 rec_permute_helper (const dim_vector& dv, const Array<octave_idx_type>& perm)
11507
c3ad80f4ce36 Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents: 11242
diff changeset
316
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
317 : m_n (dv.ndims ()), m_top (0), m_dim (new octave_idx_type [2*m_n]),
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
318 m_stride (m_dim + m_n), m_use_blk (false)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
319 {
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
320 assert (m_n == perm.numel ());
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
321
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
322 // Get cumulative dimensions.
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
323 OCTAVE_LOCAL_BUFFER (octave_idx_type, cdim, m_n+1);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
324 cdim[0] = 1;
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
325 for (int i = 1; i < m_n+1; i++) cdim[i] = cdim[i-1] * dv(i-1);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
326
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
327 // Setup the permuted strides.
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
328 for (int k = 0; k < m_n; k++)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
329 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
330 int kk = perm(k);
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
331 m_dim[k] = dv(kk);
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
332 m_stride[k] = cdim[kk];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
333 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
334
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
335 // Reduce contiguous runs.
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
336 for (int k = 1; k < m_n; k++)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
337 {
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
338 if (m_stride[k] == m_stride[m_top]*m_dim[m_top])
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
339 m_dim[m_top] *= m_dim[k];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
340 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
341 {
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
342 m_top++;
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
343 m_dim[m_top] = m_dim[k];
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
344 m_stride[m_top] = m_stride[k];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
345 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
346 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
347
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
348 // Determine whether we can use block transposes.
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
349 m_use_blk = m_top >= 1 && m_stride[1] == 1 && m_stride[0] == m_dim[1];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
350
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
351 }
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
352
22869
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
353 // No copying!
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
354
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
355 rec_permute_helper (const rec_permute_helper&) = delete;
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
356
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
357 rec_permute_helper& operator = (const rec_permute_helper&) = delete;
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
358
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
359 ~rec_permute_helper () { delete [] m_dim; }
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
360
30174
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
361 template <typename T>
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
362 void permute (const T *src, T *dest) const { do_permute (src, dest, m_top); }
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
363
9012
9f5e095555fc smarter algorithm for permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
364 // Helper method for fast blocked transpose.
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
365 template <typename T>
9121
bb62bc406ea7 reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents: 9100
diff changeset
366 static T *
bb62bc406ea7 reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents: 9100
diff changeset
367 blk_trans (const T *src, T *dest, octave_idx_type nr, octave_idx_type nc)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
368 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
369 static const octave_idx_type m = 8;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
370 OCTAVE_LOCAL_BUFFER (T, blk, m*m);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
371 for (octave_idx_type kr = 0; kr < nr; kr += m)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
372 for (octave_idx_type kc = 0; kc < nc; kc += m)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
373 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
374 octave_idx_type lr = std::min (m, nr - kr);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
375 octave_idx_type lc = std::min (m, nc - kc);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
376 if (lr == m && lc == m)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
377 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
378 const T *ss = src + kc * nr + kr;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
379 for (octave_idx_type j = 0; j < m; j++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
380 for (octave_idx_type i = 0; i < m; i++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
381 blk[j*m+i] = ss[j*nr + i];
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
382 T *dd = dest + kr * nc + kc;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
383 for (octave_idx_type j = 0; j < m; j++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
384 for (octave_idx_type i = 0; i < m; i++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
385 dd[j*nc+i] = blk[i*m+j];
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
386 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
387 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
388 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
389 const T *ss = src + kc * nr + kr;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
390 for (octave_idx_type j = 0; j < lc; j++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
391 for (octave_idx_type i = 0; i < lr; i++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
392 blk[j*m+i] = ss[j*nr + i];
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
393 T *dd = dest + kr * nc + kc;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
394 for (octave_idx_type j = 0; j < lr; j++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
395 for (octave_idx_type i = 0; i < lc; i++)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
396 dd[j*nc+i] = blk[i*m+j];
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
397 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
398 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
399
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
400 return dest + nr*nc;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
401 }
12153
e0e50f48df37 Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 11590
diff changeset
402
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
403 private:
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
404
21014
b9d4c3615e89 maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents: 20974
diff changeset
405 // Recursive N-D generalized transpose
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
406 template <typename T>
23446
cd4e1ee28716 maint: Use convention 'void * fcn ()' for functions which return pointers.
Rik <rik@octave.org>
parents: 23443
diff changeset
407 T * do_permute (const T *src, T *dest, int lev) const
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
408 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
409 if (lev == 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
410 {
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
411 octave_idx_type step = m_stride[0];
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
412 octave_idx_type len = m_dim[0];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
413 if (step == 1)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
414 {
23420
1fee389bd04b Use std::copy_n rather than std::copy for clarity in liboctave/array.
Rik <rik@octave.org>
parents: 23386
diff changeset
415 std::copy_n (src, len, dest);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
416 dest += len;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
417 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
418 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
419 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
420 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
421 dest[i] = src[j];
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
422
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
423 dest += len;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
424 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
425 }
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
426 else if (m_use_blk && lev == 1)
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
427 dest = blk_trans (src, dest, m_dim[1], m_dim[0]);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
428 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
429 {
30094
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
430 octave_idx_type step = m_stride[lev];
d32f3be74563 maint: use "m_" prefix for member variables in class rec_permute_helper.
Rik <rik@octave.org>
parents: 29979
diff changeset
431 octave_idx_type len = m_dim[lev];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
432 for (octave_idx_type i = 0, j = 0; i < len; i++, j+= step)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
433 dest = do_permute (src + i * step, dest, lev-1);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
434 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
435
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
436 return dest;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
437 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
438
30174
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
439 //--------
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
440
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
441 // STRIDE occupies the last half of the space allocated for dim to
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
442 // avoid a double allocation.
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
443
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
444 int m_n;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
445 int m_top;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
446 octave_idx_type *m_dim;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
447 octave_idx_type *m_stride;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
448 bool m_use_blk;
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
449 };
7241
da540e715d05 [project @ 2007-12-03 22:55:20 by jwe]
jwe
parents: 7189
diff changeset
450
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
451 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
452 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
453 Array<T, Alloc>::permute (const Array<octave_idx_type>& perm_vec_arg, bool inv) const
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
454 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
455 Array<T, Alloc> retval;
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
456
5607
4b33d802ef3c [project @ 2006-02-08 18:56:54 by jwe]
jwe
parents: 5535
diff changeset
457 Array<octave_idx_type> perm_vec = perm_vec_arg;
4b33d802ef3c [project @ 2006-02-08 18:56:54 by jwe]
jwe
parents: 5535
diff changeset
458
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
459 dim_vector dv = dims ();
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
460
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
461 int perm_vec_len = perm_vec_arg.numel ();
5148
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
462
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
463 if (perm_vec_len < dv.ndims ())
5148
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
464 (*current_liboctave_error_handler)
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
465 ("%s: invalid permutation vector", inv ? "ipermute" : "permute");
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
466
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
467 dim_vector dv_new = dim_vector::alloc (perm_vec_len);
5148
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
468
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
469 // Append singleton dimensions as needed.
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
470 dv.resize (perm_vec_len, 1);
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
471
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
472 // Need this array to check for identical elements in permutation array.
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
473 OCTAVE_LOCAL_BUFFER_INIT (bool, checked, perm_vec_len, false);
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
474
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
475 bool identity = true;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
476
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
477 // Find dimension vector of permuted array.
5148
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
478 for (int i = 0; i < perm_vec_len; i++)
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
479 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5265
diff changeset
480 octave_idx_type perm_elt = perm_vec.elem (i);
5148
9eab94f5fadf [project @ 2005-02-18 19:04:34 by jwe]
jwe
parents: 5120
diff changeset
481 if (perm_elt >= perm_vec_len || perm_elt < 0)
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
482 (*current_liboctave_error_handler)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
483 ("%s: permutation vector contains an invalid element",
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
484 inv ? "ipermute" : "permute");
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
485
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
486 if (checked[perm_elt])
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
487 (*current_liboctave_error_handler)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
488 ("%s: permutation vector cannot contain identical elements",
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
489 inv ? "ipermute" : "permute");
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
490 else
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
491 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
492 checked[perm_elt] = true;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
493 identity = identity && perm_elt == i;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
494 }
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
495 }
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
496
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
497 if (identity)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
498 return *this;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9624
diff changeset
499
5607
4b33d802ef3c [project @ 2006-02-08 18:56:54 by jwe]
jwe
parents: 5535
diff changeset
500 if (inv)
4b33d802ef3c [project @ 2006-02-08 18:56:54 by jwe]
jwe
parents: 5535
diff changeset
501 {
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
502 for (int i = 0; i < perm_vec_len; i++)
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
503 perm_vec(perm_vec_arg(i)) = i;
5607
4b33d802ef3c [project @ 2006-02-08 18:56:54 by jwe]
jwe
parents: 5535
diff changeset
504 }
4b33d802ef3c [project @ 2006-02-08 18:56:54 by jwe]
jwe
parents: 5535
diff changeset
505
10270
6ffed9bb5598 fix ipermute
Jaroslav Hajek <highegg@gmail.com>
parents: 10258
diff changeset
506 for (int i = 0; i < perm_vec_len; i++)
6ffed9bb5598 fix ipermute
Jaroslav Hajek <highegg@gmail.com>
parents: 10258
diff changeset
507 dv_new(i) = dv(perm_vec(i));
6ffed9bb5598 fix ipermute
Jaroslav Hajek <highegg@gmail.com>
parents: 10258
diff changeset
508
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
509 retval = Array<T, Alloc> (dv_new);
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
510
5940
0dd8428bb260 [project @ 2006-08-18 17:50:23 by jwe]
jwe
parents: 5837
diff changeset
511 if (numel () > 0)
5607
4b33d802ef3c [project @ 2006-02-08 18:56:54 by jwe]
jwe
parents: 5535
diff changeset
512 {
8410
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
513 rec_permute_helper rh (dv, perm_vec);
ba24ecd4c019 optimize permute
Jaroslav Hajek <highegg@gmail.com>
parents: 8390
diff changeset
514 rh.permute (data (), retval.fortran_vec ());
4593
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
515 }
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
516
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
517 return retval;
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
518 }
77566be8b9e9 [project @ 2003-11-11 17:25:42 by jwe]
jwe
parents: 4592
diff changeset
519
11507
c3ad80f4ce36 Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents: 11242
diff changeset
520 // Helper class for multi-d index reduction and recursive
c3ad80f4ce36 Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents: 11242
diff changeset
521 // indexing/indexed assignment. Rationale: we could avoid recursion
c3ad80f4ce36 Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents: 11242
diff changeset
522 // using a state machine instead. However, using recursion is much
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
523 // more amenable to possible parallelization in the future.
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
524 // Also, the recursion solution is cleaner and more understandable.
11507
c3ad80f4ce36 Array.h, Array.cc: more constructor fixes
John W. Eaton <jwe@octave.org>
parents: 11242
diff changeset
525
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
526 class rec_index_helper
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
527 {
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
528 public:
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
529 rec_index_helper (const dim_vector& dv, const Array<octave::idx_vector>& ia)
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
530 : m_n (ia.numel ()), m_top (0), m_dim (new octave_idx_type [2*m_n]),
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
531 m_cdim (m_dim + m_n), m_idx (new octave::idx_vector [m_n])
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
532 {
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
533 assert (m_n > 0 && (dv.ndims () == std::max (m_n, 2)));
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
534
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
535 m_dim[0] = dv(0);
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
536 m_cdim[0] = 1;
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
537 m_idx[0] = ia(0);
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
538
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
539 for (int i = 1; i < m_n; i++)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
540 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
541 // Try reduction...
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
542 if (m_idx[m_top].maybe_reduce (m_dim[m_top], ia(i), dv(i)))
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
543 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
544 // Reduction successful, fold dimensions.
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
545 m_dim[m_top] *= dv(i);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
546 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
547 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
548 {
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
549 // Unsuccessful, store index & cumulative m_dim.
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
550 m_top++;
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
551 m_idx[m_top] = ia(i);
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
552 m_dim[m_top] = dv(i);
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
553 m_cdim[m_top] = m_cdim[m_top-1] * m_dim[m_top-1];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
554 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
555 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
556 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
557
22869
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
558 // No copying!
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
559
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
560 rec_index_helper (const rec_index_helper&) = delete;
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
561
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
562 rec_index_helper& operator = (const rec_index_helper&) = delete;
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
563
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
564 ~rec_index_helper () { delete [] m_idx; delete [] m_dim; }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
565
30174
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
566 template <typename T>
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
567 void index (const T *src, T *dest) const { do_index (src, dest, m_top); }
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
568
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
569 template <typename T>
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
570 void assign (const T *src, T *dest) const { do_assign (src, dest, m_top); }
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
571
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
572 template <typename T>
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
573 void fill (const T& val, T *dest) const { do_fill (val, dest, m_top); }
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
574
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
575 bool is_cont_range (octave_idx_type& l, octave_idx_type& u) const
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
576 {
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
577 return m_top == 0 && m_idx[0].is_cont_range (m_dim[0], l, u);
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
578 }
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
579
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
580 private:
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
581
21014
b9d4c3615e89 maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents: 20974
diff changeset
582 // Recursive N-D indexing
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
583 template <typename T>
23446
cd4e1ee28716 maint: Use convention 'void * fcn ()' for functions which return pointers.
Rik <rik@octave.org>
parents: 23443
diff changeset
584 T * do_index (const T *src, T *dest, int lev) const
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
585 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
586 if (lev == 0)
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
587 dest += m_idx[0].index (src, m_dim[0], dest);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
588 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
589 {
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
590 octave_idx_type nn = m_idx[lev].length (m_dim[lev]);
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
591 octave_idx_type d = m_cdim[lev];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
592 for (octave_idx_type i = 0; i < nn; i++)
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
593 dest = do_index (src + d*m_idx[lev].xelem (i), dest, lev-1);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
594 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
595
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
596 return dest;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
597 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
598
21014
b9d4c3615e89 maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents: 20974
diff changeset
599 // Recursive N-D indexed assignment
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
600 template <typename T>
23446
cd4e1ee28716 maint: Use convention 'void * fcn ()' for functions which return pointers.
Rik <rik@octave.org>
parents: 23443
diff changeset
601 const T * do_assign (const T *src, T *dest, int lev) const
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
602 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
603 if (lev == 0)
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
604 src += m_idx[0].assign (src, m_dim[0], dest);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
605 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
606 {
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
607 octave_idx_type nn = m_idx[lev].length (m_dim[lev]);
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
608 octave_idx_type d = m_cdim[lev];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
609 for (octave_idx_type i = 0; i < nn; i++)
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
610 src = do_assign (src, dest + d*m_idx[lev].xelem (i), lev-1);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
611 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
612
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
613 return src;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
614 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
615
21014
b9d4c3615e89 maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents: 20974
diff changeset
616 // Recursive N-D indexed assignment
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
617 template <typename T>
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
618 void do_fill (const T& val, T *dest, int lev) const
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
619 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
620 if (lev == 0)
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
621 m_idx[0].fill (val, m_dim[0], dest);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
622 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
623 {
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
624 octave_idx_type nn = m_idx[lev].length (m_dim[lev]);
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
625 octave_idx_type d = m_cdim[lev];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
626 for (octave_idx_type i = 0; i < nn; i++)
30095
a855aef03941 maint: use "m_" prefix for member variables in class rec_index_helper.
Rik <rik@octave.org>
parents: 30094
diff changeset
627 do_fill (val, dest + d*m_idx[lev].xelem (i), lev-1);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
628 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
629 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
630
30174
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
631 //--------
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
632
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
633 // CDIM occupies the last half of the space allocated for dim to
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
634 // avoid a double allocation.
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
635
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
636 int m_n;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
637 int m_top;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
638 octave_idx_type *m_dim;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
639 octave_idx_type *m_cdim;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
640 octave::idx_vector *m_idx;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
641 };
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
642
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
643 // Helper class for multi-d recursive resizing
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
644 // This handles resize () in an efficient manner, touching memory only
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
645 // once (apart from reinitialization)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
646 class rec_resize_helper
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
647 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
648 public:
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
649 rec_resize_helper (const dim_vector& ndv, const dim_vector& odv)
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
650 : m_cext (nullptr), m_sext (nullptr), m_dext (nullptr), m_n (0)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
651 {
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
652 int l = ndv.ndims ();
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
653 assert (odv.ndims () == l);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
654 octave_idx_type ld = 1;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
655 int i = 0;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
656 for (; i < l-1 && ndv(i) == odv(i); i++) ld *= ndv(i);
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
657 m_n = l - i;
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
658 m_cext = new octave_idx_type [3*m_n];
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
659 // Trick to avoid three allocations
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
660 m_sext = m_cext + m_n;
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
661 m_dext = m_sext + m_n;
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
662
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
663 octave_idx_type sld = ld;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
664 octave_idx_type dld = ld;
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
665 for (int j = 0; j < m_n; j++)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
666 {
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
667 m_cext[j] = std::min (ndv(i+j), odv(i+j));
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
668 m_sext[j] = sld *= odv(i+j);
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
669 m_dext[j] = dld *= ndv(i+j);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
670 }
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
671 m_cext[0] *= ld;
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
672 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
673
22869
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
674 // No copying!
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
675
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
676 rec_resize_helper (const rec_resize_helper&) = delete;
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
677
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
678 rec_resize_helper& operator = (const rec_resize_helper&) = delete;
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22865
diff changeset
679
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
680 ~rec_resize_helper () { delete [] m_cext; }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
681
30174
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
682 template <typename T>
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
683 void resize_fill (const T *src, T *dest, const T& rfv) const
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
684 { do_resize_fill (src, dest, rfv, m_n-1); }
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
685
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
686 private:
12153
e0e50f48df37 Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 11590
diff changeset
687
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
688 // recursive resizing
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
689 template <typename T>
23449
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23446
diff changeset
690 void do_resize_fill (const T *src, T *dest, const T& rfv, int lev) const
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
691 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
692 if (lev == 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
693 {
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
694 std::copy_n (src, m_cext[0], dest);
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
695 std::fill_n (dest + m_cext[0], m_dext[0] - m_cext[0], rfv);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
696 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
697 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
698 {
18099
6c706a83070f Tweak cset 8e056300994b defining 1 var per line in liboctave.
Rik <rik@octave.org>
parents: 18084
diff changeset
699 octave_idx_type sd, dd, k;
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
700 sd = m_sext[lev-1];
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
701 dd = m_dext[lev-1];
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
702 for (k = 0; k < m_cext[lev]; k++)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
703 do_resize_fill (src + k * sd, dest + k * dd, rfv, lev - 1);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
704
30096
b9ccf2213a1a maint: use "m_" prefix for member variables in class rec_resize_helper.
Rik <rik@octave.org>
parents: 30095
diff changeset
705 std::fill_n (dest + k * dd, m_dext[lev] - k * dd, rfv);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
706 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
707 }
12153
e0e50f48df37 Explicitly disallow copying in some classes
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 11590
diff changeset
708
30174
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
709 //--------
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
710
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
711 octave_idx_type *m_cext;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
712 octave_idx_type *m_sext;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
713 octave_idx_type *m_dext;
1e49b26c1ead maint: Use public/private ordering in helper class declarations in Array.cc
Rik <rik@octave.org>
parents: 30096
diff changeset
714 int m_n;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
715 };
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
716
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
717 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
718 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
719 Array<T, Alloc>::index (const octave::idx_vector& i) const
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
720 {
23386
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
721 // Colon:
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
722 //
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
723 // object | index | result orientation
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
724 // ---------+----------+-------------------
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
725 // anything | colon | column vector
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
726 //
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
727 // Numeric array or logical mask:
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
728 //
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
729 // Note that logical mask indices are always transformed to vectors
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
730 // before we see them.
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
731 //
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
732 // object | index | result orientation
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
733 // ---------+----------+-------------------
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
734 // vector | vector | indexed object
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
735 // | other | same size as index
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
736 // ---------+----------+-------------------
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
737 // array | anything | same size as index
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
738
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
739 octave_idx_type n = numel ();
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
740 Array<T, Alloc> retval;
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5265
diff changeset
741
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
742 if (i.is_colon ())
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
743 {
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
744 // A(:) produces a shallow copy as a column vector.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
745 retval = Array<T, Alloc> (*this, dim_vector (n, 1));
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
746 }
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4545
diff changeset
747 else
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
748 {
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
749 if (i.extent (n) != n)
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
750 octave::err_index_out_of_range (1, 1, i.extent (n), n, m_dimensions);
23386
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
751
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
752 dim_vector result_dims = i.orig_dimensions ();
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
753 octave_idx_type idx_len = i.length ();
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
754
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
755 if (n != 1 && is_nd_vector () && idx_len != 1
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
756 && result_dims.is_nd_vector ())
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
757 {
23386
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
758 // Indexed object and index are both vectors. Set result size
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
759 // and orientation as above.
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
760
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
761 dim_vector dv = dims ();
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
762
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
763 result_dims = dv.make_nd_vector (idx_len);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
764 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
765
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
766 octave_idx_type l, u;
23386
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
767 if (idx_len != 0 && i.is_cont_range (n, l, u))
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
768 // If suitable, produce a shallow slice.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
769 retval = Array<T, Alloc> (*this, result_dims, l, u);
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
770 else
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
771 {
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
772 // Don't use resize here to avoid useless initialization for POD
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
773 // types.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
774 retval = Array<T, Alloc> (result_dims);
23386
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
775
f1ea44e92df8 improve compatibility for single index indexing (bug #50776)
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
776 if (idx_len != 0)
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
777 i.index (data (), n, retval.fortran_vec ());
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
778 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
779 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
780
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
781 return retval;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
782 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
783
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
784 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
785 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
786 Array<T, Alloc>::index (const octave::idx_vector& i, const octave::idx_vector& j) const
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
787 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
788 // Get dimensions, allowing Fortran indexing in the 2nd dim.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
789 dim_vector dv = m_dimensions.redim (2);
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
790 octave_idx_type r = dv(0);
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
791 octave_idx_type c = dv(1);
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
792 Array<T, Alloc> retval;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
793
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
794 if (i.is_colon () && j.is_colon ())
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
795 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
796 // A(:,:) produces a shallow copy.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
797 retval = Array<T, Alloc> (*this, dv);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
798 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
799 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
800 {
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
801 if (i.extent (r) != r)
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
802 octave::err_index_out_of_range (2, 1, i.extent (r), r, m_dimensions); // throws
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
803 if (j.extent (c) != c)
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
804 octave::err_index_out_of_range (2, 2, j.extent (c), c, m_dimensions); // throws
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
805
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
806 octave_idx_type n = numel ();
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
807 octave_idx_type il = i.length (r);
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
808 octave_idx_type jl = j.length (c);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
809
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
810 octave::idx_vector ii (i);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
811
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
812 if (ii.maybe_reduce (r, j, c))
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
813 {
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
814 octave_idx_type l, u;
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
815 if (ii.length () > 0 && ii.is_cont_range (n, l, u))
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
816 // If suitable, produce a shallow slice.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
817 retval = Array<T, Alloc> (*this, dim_vector (il, jl), l, u);
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
818 else
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
819 {
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
820 // Don't use resize to avoid useless initialization for POD types.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
821 retval = Array<T, Alloc> (dim_vector (il, jl));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
822
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
823 ii.index (data (), n, retval.fortran_vec ());
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
824 }
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
825 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
826 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
827 {
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
828 // Don't use resize to avoid useless initialization for POD types.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
829 retval = Array<T, Alloc> (dim_vector (il, jl));
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
830
23449
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23446
diff changeset
831 const T *src = data ();
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
832 T *dest = retval.fortran_vec ();
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
833
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
834 for (octave_idx_type k = 0; k < jl; k++)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
835 dest += i.index (src + r * j.xelem (k), r, dest);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
836 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
837 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
838
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
839 return retval;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
840 }
4747
3f28979bbe2c [project @ 2004-02-06 23:56:46 by jwe]
jwe
parents: 4746
diff changeset
841
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
842 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
843 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
844 Array<T, Alloc>::index (const Array<octave::idx_vector>& ia) const
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
845 {
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
846 int ial = ia.numel ();
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
847 Array<T, Alloc> retval;
4587
7b957b442818 [project @ 2003-11-10 15:50:39 by jwe]
jwe
parents: 4586
diff changeset
848
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
849 // FIXME: is this dispatching necessary?
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
850 if (ial == 1)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
851 retval = index (ia(0));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
852 else if (ial == 2)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
853 retval = index (ia(0), ia(1));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
854 else if (ial > 0)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
855 {
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
856 // Get dimensions, allowing Fortran indexing in the last dim.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
857 dim_vector dv = m_dimensions.redim (ial);
4747
3f28979bbe2c [project @ 2004-02-06 23:56:46 by jwe]
jwe
parents: 4746
diff changeset
858
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
859 // Check for out of bounds conditions.
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
860 bool all_colons = true;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
861 for (int i = 0; i < ial; i++)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
862 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
863 if (ia(i).extent (dv(i)) != dv(i))
22402
4caa7b28d183 maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents: 22327
diff changeset
864 octave::err_index_out_of_range (ial, i+1, ia(i).extent (dv(i)), dv(i),
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
865 m_dimensions); // throws
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
866
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
867 all_colons = all_colons && ia(i).is_colon ();
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
868 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
869
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
870 if (all_colons)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
871 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
872 // A(:,:,...,:) produces a shallow copy.
10095
eb8ac0eed9f1 always chop dimension vector when constructing Arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 10075
diff changeset
873 dv.chop_trailing_singletons ();
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
874 retval = Array<T, Alloc> (*this, dv);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
875 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
876 else
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
877 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
878 // Form result dimensions.
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
879 dim_vector rdv = dim_vector::alloc (ial);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
880 for (int i = 0; i < ial; i++) rdv(i) = ia(i).length (dv(i));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
881 rdv.chop_trailing_singletons ();
4870
2cbc6f37b0c2 [project @ 2004-04-21 17:30:51 by jwe]
jwe
parents: 4834
diff changeset
882
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
883 // Prepare for recursive indexing
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
884 rec_index_helper rh (dv, ia);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
885
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
886 octave_idx_type l, u;
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
887 if (rh.is_cont_range (l, u))
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
888 // If suitable, produce a shallow slice.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
889 retval = Array<T, Alloc> (*this, rdv, l, u);
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
890 else
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
891 {
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
892 // Don't use resize to avoid useless initialization for POD types.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
893 retval = Array<T, Alloc> (rdv);
8523
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
894
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
895 // Do it.
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
896 rh.index (data (), retval.fortran_vec ());
ad3afaaa19c1 implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8505
diff changeset
897 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
898 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
899 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
900
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
901 return retval;
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
902 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
903
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
904 // The default fill value. Override if you want a different one.
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
905
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
906 template <typename T, typename Alloc>
14616
13cc11418393 improve handling of default resize fill value for arrays
John W. Eaton <jwe@octave.org>
parents: 14591
diff changeset
907 T
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
908 Array<T, Alloc>::resize_fill_value () const
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
909 {
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
910 static T zero = T ();
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
911 return zero;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
912 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
913
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
914 // Yes, we could do resize using index & assign. However, that would
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
915 // possibly involve a lot more memory traffic than we actually need.
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
916
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
917 template <typename T, typename Alloc>
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
918 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
919 Array<T, Alloc>::resize1 (octave_idx_type n, const T& rfv)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
920 {
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
921 if (n < 0 || ndims () != 2)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
922 octave::err_invalid_resize ();
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
923
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
924 dim_vector dv;
21308
c53bfd6d8e08 maint: Use American spelling for "behavior".
Rik <rik@octave.org>
parents: 21301
diff changeset
925 // This is driven by Matlab's behavior of giving a *row* vector
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
926 // on some out-of-bounds assignments. Specifically, Matlab
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
927 // allows a(i) with out-of-bouds i when a is either of 0x0, 1x0,
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
928 // 1x1, 0xN, and gives a row vector in all cases (yes, even the
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
929 // last one, search me why). Giving a column vector would make
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
930 // much more sense (given the way trailing singleton dims are
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
931 // treated).
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
932 bool invalid = false;
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
933 if (rows () == 0 || rows () == 1)
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
934 dv = dim_vector (1, n);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
935 else if (columns () == 1)
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
936 dv = dim_vector (n, 1);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
937 else
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
938 invalid = true;
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
939
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
940 if (invalid)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
941 octave::err_invalid_resize ();
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
942
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
943 octave_idx_type nx = numel ();
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
944 if (n == nx - 1 && n > 0)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
945 {
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
946 // Stack "pop" operation.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
947 if (m_rep->m_count == 1)
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
948 m_slice_data[m_slice_len-1] = T ();
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
949 m_slice_len--;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
950 m_dimensions = dv;
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
951 }
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
952 else if (n == nx + 1 && nx > 0)
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
953 {
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
954 // Stack "push" operation.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
955 if (m_rep->m_count == 1
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
956 && m_slice_data + m_slice_len < m_rep->m_data + m_rep->m_len)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
957 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
958 m_slice_data[m_slice_len++] = rfv;
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
959 m_dimensions = dv;
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
960 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
961 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
962 {
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
963 static const octave_idx_type max_stack_chunk = 1024;
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
964 octave_idx_type nn = n + std::min (nx, max_stack_chunk);
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
965 Array<T, Alloc> tmp (Array<T, Alloc> (dim_vector (nn, 1)), dv, 0, n);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
966 T *dest = tmp.fortran_vec ();
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
967
23420
1fee389bd04b Use std::copy_n rather than std::copy for clarity in liboctave/array.
Rik <rik@octave.org>
parents: 23386
diff changeset
968 std::copy_n (data (), nx, dest);
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
969 dest[nx] = rfv;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
970
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
971 *this = tmp;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
972 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
973 }
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
974 else if (n != nx)
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
975 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
976 Array<T, Alloc> tmp = Array<T, Alloc> (dv);
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
977 T *dest = tmp.fortran_vec ();
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
978
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
979 octave_idx_type n0 = std::min (n, nx);
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
980 octave_idx_type n1 = n - n0;
23420
1fee389bd04b Use std::copy_n rather than std::copy for clarity in liboctave/array.
Rik <rik@octave.org>
parents: 23386
diff changeset
981 std::copy_n (data (), n0, dest);
21120
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
982 std::fill_n (dest + n0, n1, rfv);
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
983
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
984 *this = tmp;
499b851fbfae Replace pattern if/err_XXX/else/code with if/err_XXX/ code.
Rik <rik@octave.org>
parents: 21118
diff changeset
985 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
986 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
987
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
988 template <typename T, typename Alloc>
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
989 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
990 Array<T, Alloc>::resize2 (octave_idx_type r, octave_idx_type c, const T& rfv)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
991 {
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
992 if (r < 0 || c < 0 || ndims () != 2)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
993 octave::err_invalid_resize ();
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
994
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
995 octave_idx_type rx = rows ();
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
996 octave_idx_type cx = columns ();
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
997 if (r != rx || c != cx)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
998 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
999 Array<T, Alloc> tmp = Array<T, Alloc> (dim_vector (r, c));
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1000 T *dest = tmp.fortran_vec ();
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1001
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1002 octave_idx_type r0 = std::min (r, rx);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1003 octave_idx_type r1 = r - r0;
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1004 octave_idx_type c0 = std::min (c, cx);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1005 octave_idx_type c1 = c - c0;
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1006 const T *src = data ();
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1007 if (r == rx)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1008 {
23420
1fee389bd04b Use std::copy_n rather than std::copy for clarity in liboctave/array.
Rik <rik@octave.org>
parents: 23386
diff changeset
1009 std::copy_n (src, r * c0, dest);
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1010 dest += r * c0;
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1011 }
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1012 else
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1013 {
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1014 for (octave_idx_type k = 0; k < c0; k++)
9773
01f897d8a130 optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 9732
diff changeset
1015 {
23420
1fee389bd04b Use std::copy_n rather than std::copy for clarity in liboctave/array.
Rik <rik@octave.org>
parents: 23386
diff changeset
1016 std::copy_n (src, r0, dest);
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1017 src += rx;
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1018 dest += r0;
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1019 std::fill_n (dest, r1, rfv);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1020 dest += r1;
9773
01f897d8a130 optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 9732
diff changeset
1021 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1022 }
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1023
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1024 std::fill_n (dest, r * c1, rfv);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1025
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1026 *this = tmp;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1027 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1028 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1029
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1030 template <typename T, typename Alloc>
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1031 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1032 Array<T, Alloc>::resize (const dim_vector& dv, const T& rfv)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1033 {
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
1034 int dvl = dv.ndims ();
8784
374cb30311a8 remove dead branch in Array.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 8760
diff changeset
1035 if (dvl == 2)
11574
a83bad07f7e3 attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1036 resize2 (dv(0), dv(1), rfv);
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1037 else if (m_dimensions != dv)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1038 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1039 if (m_dimensions.ndims () > dvl || dv.any_neg ())
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
1040 octave::err_invalid_resize ();
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1041
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1042 Array<T, Alloc> tmp (dv);
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1043 // Prepare for recursive resizing.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1044 rec_resize_helper rh (dv, m_dimensions.redim (dvl));
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1045
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1046 // Do it.
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1047 rh.resize_fill (data (), tmp.fortran_vec (), rfv);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1048 *this = tmp;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1049 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1050 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1051
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1052 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1053 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1054 Array<T, Alloc>::index (const octave::idx_vector& i, bool resize_ok, const T& rfv) const
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1055 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1056 Array<T, Alloc> tmp = *this;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1057 if (resize_ok)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1058 {
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1059 octave_idx_type n = numel ();
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1060 octave_idx_type nx = i.extent (n);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1061 if (n != nx)
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1062 {
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1063 if (i.is_scalar ())
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1064 return Array<T, Alloc> (dim_vector (1, 1), rfv);
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1065 else
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
1066 tmp.resize1 (nx, rfv);
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1067 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1068
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1069 if (tmp.numel () != nx)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1070 return Array<T, Alloc> ();
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1071 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1072
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1073 return tmp.index (i);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1074 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1075
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1076 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1077 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1078 Array<T, Alloc>::index (const octave::idx_vector& i, const octave::idx_vector& j,
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1079 bool resize_ok, const T& rfv) const
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1080 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1081 Array<T, Alloc> tmp = *this;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1082 if (resize_ok)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1083 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1084 dim_vector dv = m_dimensions.redim (2);
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1085 octave_idx_type r = dv(0);
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1086 octave_idx_type c = dv(1);
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1087 octave_idx_type rx = i.extent (r);
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1088 octave_idx_type cx = j.extent (c);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1089 if (r != rx || c != cx)
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1090 {
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1091 if (i.is_scalar () && j.is_scalar ())
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1092 return Array<T, Alloc> (dim_vector (1, 1), rfv);
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1093 else
11574
a83bad07f7e3 attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1094 tmp.resize2 (rx, cx, rfv);
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1095 }
4747
3f28979bbe2c [project @ 2004-02-06 23:56:46 by jwe]
jwe
parents: 4746
diff changeset
1096
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1097 if (tmp.rows () != rx || tmp.columns () != cx)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1098 return Array<T, Alloc> ();
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1099 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1100
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1101 return tmp.index (i, j);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1102 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1103
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1104 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1105 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1106 Array<T, Alloc>::index (const Array<octave::idx_vector>& ia,
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1107 bool resize_ok, const T& rfv) const
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1108 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1109 Array<T, Alloc> tmp = *this;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1110 if (resize_ok)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1111 {
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
1112 int ial = ia.numel ();
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1113 dim_vector dv = m_dimensions.redim (ial);
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1114 dim_vector dvx = dim_vector::alloc (ial);
21098
99d373870017 Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21014
diff changeset
1115 for (int i = 0; i < ial; i++)
99d373870017 Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21014
diff changeset
1116 dvx(i) = ia(i).extent (dv(i));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1117 if (! (dvx == dv))
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1118 {
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1119 bool all_scalars = true;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1120 for (int i = 0; i < ial; i++)
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1121 all_scalars = all_scalars && ia(i).is_scalar ();
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1122 if (all_scalars)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1123 return Array<T, Alloc> (dim_vector (1, 1), rfv);
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1124 else
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
1125 tmp.resize (dvx, rfv);
21098
99d373870017 Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21014
diff changeset
1126
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1127 if (tmp.m_dimensions != dvx)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1128 return Array<T, Alloc> ();
8563
3a3421a9f0bb optimize resizable indexing with scalars
Jaroslav Hajek <highegg@gmail.com>
parents: 8527
diff changeset
1129 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1130 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1131
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1132 return tmp.index (ia);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1133 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1134
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1135 template <typename T, typename Alloc>
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1136 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1137 Array<T, Alloc>::assign (const octave::idx_vector& i, const Array<T, Alloc>& rhs, const T& rfv)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1138 {
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1139 octave_idx_type n = numel ();
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1140 octave_idx_type rhl = rhs.numel ();
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4545
diff changeset
1141
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1142 if (rhl != 1 && i.length (n) != rhl)
30394
f3f3e3793fb5 maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30174
diff changeset
1143 octave::err_nonconformant ("=", dim_vector(i.length(n), 1), rhs.dims());
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1144
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1145 octave_idx_type nx = i.extent (n);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1146 bool colon = i.is_colon_equiv (nx);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1147 // Try to resize first if necessary.
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1148 if (nx != n)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1149 {
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1150 // Optimize case A = []; A(1:n) = X with A empty.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1151 if (m_dimensions.zero_by_zero () && colon)
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1152 {
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1153 if (rhl == 1)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1154 *this = Array<T, Alloc> (dim_vector (1, nx), rhs(0));
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1155 else
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1156 *this = Array<T, Alloc> (rhs, dim_vector (1, nx));
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1157 return;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1158 }
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1159
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1160 resize1 (nx, rfv);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1161 n = numel ();
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1162 }
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1163
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1164 if (colon)
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1165 {
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1166 // A(:) = X makes a full fill or a shallow copy.
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1167 if (rhl == 1)
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1168 fill (rhs(0));
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1169 else
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1170 *this = rhs.reshape (m_dimensions);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1171 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1172 else
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1173 {
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1174 if (rhl == 1)
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1175 i.fill (rhs(0), n, fortran_vec ());
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1176 else
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1177 i.assign (rhs.data (), n, fortran_vec ());
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21100
diff changeset
1178 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1179 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1180
20390
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1181 // Assignment to a 2-dimensional array
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1182 template <typename T, typename Alloc>
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1183 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1184 Array<T, Alloc>::assign (const octave::idx_vector& i, const octave::idx_vector& j,
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1185 const Array<T, Alloc>& rhs, const T& rfv)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1186 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1187 bool initial_dims_all_zero = m_dimensions.all_zero ();
14624
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14440
diff changeset
1188
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1189 // Get RHS extents, discarding singletons.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1190 dim_vector rhdv = rhs.dims ();
14624
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14440
diff changeset
1191
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1192 // Get LHS extents, allowing Fortran indexing in the second dim.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1193 dim_vector dv = m_dimensions.redim (2);
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1194
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1195 // Check for out-of-bounds and form resizing m_dimensions.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1196 dim_vector rdv;
14624
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14440
diff changeset
1197
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1198 // In the special when all dimensions are zero, colons are allowed
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1199 // to inquire the shape of RHS. The rules are more obscure, so we
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1200 // solve that elsewhere.
14624
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14440
diff changeset
1201 if (initial_dims_all_zero)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1202 rdv = zero_dims_inquire (i, j, rhdv);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1203 else
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1204 {
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1205 rdv(0) = i.extent (dv(0));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1206 rdv(1) = j.extent (dv(1));
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1207 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1208
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1209 bool isfill = rhs.numel () == 1;
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1210 octave_idx_type il = i.length (rdv(0));
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1211 octave_idx_type jl = j.length (rdv(1));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1212 rhdv.chop_all_singletons ();
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1213 bool match = (isfill
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
1214 || (rhdv.ndims () == 2 && il == rhdv(0) && jl == rhdv(1)));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1215 match = match || (il == 1 && jl == rhdv(0) && rhdv(1) == 1);
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4545
diff changeset
1216
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1217 if (match)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1218 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1219 bool all_colons = (i.is_colon_equiv (rdv(0))
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1220 && j.is_colon_equiv (rdv(1)));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1221 // Resize if requested.
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1222 if (rdv != dv)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1223 {
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8563
diff changeset
1224 // Optimize case A = []; A(1:m, 1:n) = X
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1225 if (dv.zero_by_zero () && all_colons)
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8563
diff changeset
1226 {
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8563
diff changeset
1227 if (isfill)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1228 *this = Array<T, Alloc> (rdv, rhs(0));
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8563
diff changeset
1229 else
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1230 *this = Array<T, Alloc> (rhs, rdv);
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8563
diff changeset
1231 return;
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8563
diff changeset
1232 }
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8563
diff changeset
1233
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1234 resize (rdv, rfv);
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1235 dv = m_dimensions;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1236 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1237
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1238 if (all_colons)
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1239 {
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1240 // A(:,:) = X makes a full fill or a shallow copy
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1241 if (isfill)
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1242 fill (rhs(0));
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1243 else
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1244 *this = rhs.reshape (m_dimensions);
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1245 }
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1246 else
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1247 {
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1248 // The actual work.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1249 octave_idx_type n = numel ();
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1250 octave_idx_type r = dv(0);
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1251 octave_idx_type c = dv(1);
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
1252 octave::idx_vector ii (i);
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1253
23449
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23446
diff changeset
1254 const T *src = rhs.data ();
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1255 T *dest = fortran_vec ();
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1256
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1257 // Try reduction first.
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1258 if (ii.maybe_reduce (r, j, c))
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1259 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1260 if (isfill)
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1261 ii.fill (*src, n, dest);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1262 else
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1263 ii.assign (src, n, dest);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1264 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1265 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1266 {
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1267 if (isfill)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1268 {
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1269 for (octave_idx_type k = 0; k < jl; k++)
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1270 i.fill (*src, r, dest + r * j.xelem (k));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1271 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1272 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1273 {
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1274 for (octave_idx_type k = 0; k < jl; k++)
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1275 src += i.assign (src, r, dest + r * j.xelem (k));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1276 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1277 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1278 }
4747
3f28979bbe2c [project @ 2004-02-06 23:56:46 by jwe]
jwe
parents: 4746
diff changeset
1279 }
20390
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1280 // any empty RHS can be assigned to an empty LHS
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1281 else if ((il != 0 && jl != 0) || (rhdv(0) != 0 && rhdv(1) != 0))
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
1282 octave::err_nonconformant ("=", il, jl, rhs.dim1 (), rhs.dim2 ());
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1283 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1284
20390
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1285 // Assignment to a multi-dimensional array
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1286 template <typename T, typename Alloc>
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1287 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1288 Array<T, Alloc>::assign (const Array<octave::idx_vector>& ia,
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1289 const Array<T, Alloc>& rhs, const T& rfv)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1290 {
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
1291 int ial = ia.numel ();
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5265
diff changeset
1292
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1293 // FIXME: is this dispatching necessary / desirable?
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1294 if (ial == 1)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1295 assign (ia(0), rhs, rfv);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1296 else if (ial == 2)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1297 assign (ia(0), ia(1), rhs, rfv);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1298 else if (ial > 0)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1299 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1300 bool initial_dims_all_zero = m_dimensions.all_zero ();
14624
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14440
diff changeset
1301
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1302 // Get RHS extents, discarding singletons.
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1303 dim_vector rhdv = rhs.dims ();
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1304
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1305 // Get LHS extents, allowing Fortran indexing in the second dim.
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1306 dim_vector dv = m_dimensions.redim (ial);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1307
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1308 // Get the extents forced by indexing.
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1309 dim_vector rdv;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1310
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1311 // In the special when all dimensions are zero, colons are
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1312 // allowed to inquire the shape of RHS. The rules are more
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1313 // obscure, so we solve that elsewhere.
14624
edf9ca8a92a8 when redimensioning, always pad dim_vector objects with 1 (bug #33216)
John W. Eaton <jwe@octave.org>
parents: 14440
diff changeset
1314 if (initial_dims_all_zero)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1315 rdv = zero_dims_inquire (ia, rhdv);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1316 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1317 {
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1318 rdv = dim_vector::alloc (ial);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1319 for (int i = 0; i < ial; i++)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1320 rdv(i) = ia(i).extent (dv(i));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1321 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1322
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1323 // Check whether LHS and RHS match, up to singleton dims.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1324 bool match = true;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1325 bool all_colons = true;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1326 bool isfill = rhs.numel () == 1;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1327
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1328 rhdv.chop_all_singletons ();
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1329 int j = 0;
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
1330 int rhdvl = rhdv.ndims ();
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1331 for (int i = 0; i < ial; i++)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1332 {
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1333 all_colons = all_colons && ia(i).is_colon_equiv (rdv(i));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1334 octave_idx_type l = ia(i).length (rdv(i));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1335 if (l == 1) continue;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1336 match = match && j < rhdvl && l == rhdv(j++);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1337 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1338
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1339 match = match && (j == rhdvl || rhdv(j) == 1);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1340 match = match || isfill;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1341
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1342 if (match)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1343 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1344 // Resize first if necessary.
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1345 if (rdv != dv)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1346 {
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1347 // Optimize case A = []; A(1:m, 1:n) = X
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1348 if (dv.zero_by_zero () && all_colons)
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1349 {
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1350 rdv.chop_trailing_singletons ();
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1351 if (isfill)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1352 *this = Array<T, Alloc> (rdv, rhs(0));
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1353 else
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1354 *this = Array<T, Alloc> (rhs, rdv);
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1355 return;
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1356 }
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9878
diff changeset
1357
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
1358 resize (rdv, rfv);
10095
eb8ac0eed9f1 always chop dimension vector when constructing Arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 10075
diff changeset
1359 dv = rdv;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1360 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1361
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1362 if (all_colons)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1363 {
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1364 // A(:,:,...,:) = X makes a full fill or a shallow copy.
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1365 if (isfill)
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1366 fill (rhs(0));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1367 else
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1368 *this = rhs.reshape (m_dimensions);
9058
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1369 }
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1370 else
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1371 {
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1372 // Do the actual work.
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1373
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1374 // Prepare for recursive indexing
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1375 rec_index_helper rh (dv, ia);
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1376
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1377 // Do it.
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1378 if (isfill)
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1379 rh.fill (rhs(0), fortran_vec ());
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1380 else
2da105bf2507 remove redundant checks from Array<T>::index
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
1381 rh.assign (rhs.data (), fortran_vec ());
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1382 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1383 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1384 else
20390
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1385 {
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1386 // dimension mismatch, unless LHS and RHS both empty
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1387 bool lhsempty, rhsempty;
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1388 lhsempty = rhsempty = false;
29419
2cfdcae08e84 Fix dimensions printed in non-conformant matrices for '=' operator (bug #59637).
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
1389 dim_vector lhs_dv = dim_vector::alloc (ial);
20390
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1390 for (int i = 0; i < ial; i++)
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1391 {
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1392 octave_idx_type l = ia(i).length (rdv(i));
29419
2cfdcae08e84 Fix dimensions printed in non-conformant matrices for '=' operator (bug #59637).
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
1393 lhs_dv(i) = l;
20390
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1394 lhsempty = lhsempty || (l == 0);
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1395 rhsempty = rhsempty || (rhdv(j++) == 0);
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1396 }
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1397 if (! lhsempty || ! rhsempty)
29419
2cfdcae08e84 Fix dimensions printed in non-conformant matrices for '=' operator (bug #59637).
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
1398 {
2cfdcae08e84 Fix dimensions printed in non-conformant matrices for '=' operator (bug #59637).
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
1399 lhs_dv.chop_trailing_singletons ();
2cfdcae08e84 Fix dimensions printed in non-conformant matrices for '=' operator (bug #59637).
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
1400 octave::err_nonconformant ("=", lhs_dv, rhdv);
2cfdcae08e84 Fix dimensions printed in non-conformant matrices for '=' operator (bug #59637).
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
1401 }
20390
41d19a6ef55a Allow assignment of an empty variable to an empty indexing slice (bug #45467).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20232
diff changeset
1402 }
4553
c7eb767505e9 [project @ 2003-10-27 21:39:55 by jwe]
jwe
parents: 4548
diff changeset
1403 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1404 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1405
20542
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1406 /*
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1407 %!shared a
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1408 %! a = [1 2; 3 4];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1409 %!error <op1 is 1x2, op2 is 1x3> a(1,:) = [1 2 3]
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1410 %!error <op1 is 2x1, op2 is 1x3> a(:,1) = [1 2 3]
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1411 %!error <op1 is 2x2, op2 is 2x1> a(1:2,2:3) = [1;2]
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1412 */
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20390
diff changeset
1413
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1414 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1415 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1416 Array<T, Alloc>::delete_elements (const octave::idx_vector& i)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1417 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1418 octave_idx_type n = numel ();
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1419 if (i.is_colon ())
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1420 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1421 *this = Array<T, Alloc> ();
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1422 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1423 else if (i.length (n) != 0)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1424 {
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
1425 if (i.extent (n) != n)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
1426 octave::err_del_index_out_of_range (true, i.extent (n), n);
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
1427
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1428 octave_idx_type l, u;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1429 bool col_vec = ndims () == 2 && columns () == 1 && rows () != 1;
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1430 if (i.is_scalar () && i(0) == n-1 && m_dimensions.isvector ())
9100
1a8bbfb2f7cf optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9058
diff changeset
1431 {
1a8bbfb2f7cf optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9058
diff changeset
1432 // Stack "pop" operation.
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
1433 resize1 (n-1);
9100
1a8bbfb2f7cf optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9058
diff changeset
1434 }
1a8bbfb2f7cf optimize simple stack operations on arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9058
diff changeset
1435 else if (i.is_cont_range (n, l, u))
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1436 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1437 // Special case deleting a contiguous range.
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1438 octave_idx_type m = n + l - u;
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1439 Array<T, Alloc> tmp (dim_vector (col_vec ? m : 1, ! col_vec ? m : 1));
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1440 const T *src = data ();
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1441 T *dest = tmp.fortran_vec ();
23420
1fee389bd04b Use std::copy_n rather than std::copy for clarity in liboctave/array.
Rik <rik@octave.org>
parents: 23386
diff changeset
1442 std::copy_n (src, l, dest);
19381
af41e41ad28e replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 18146
diff changeset
1443 std::copy (src + u, src + n, dest + l);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1444 *this = tmp;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1445 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1446 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1447 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1448 // Use index.
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1449 *this = index (i.complement (n));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1450 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1451 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1452 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1453
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1454 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1455 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1456 Array<T, Alloc>::delete_elements (int dim, const octave::idx_vector& i)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1457 {
8302
f2e050b62199 fix dim check in Array<T>::delete_elements
Jaroslav Hajek <highegg@gmail.com>
parents: 8290
diff changeset
1458 if (dim < 0 || dim >= ndims ())
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
1459 (*current_liboctave_error_handler) ("invalid dimension in delete_elements");
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1460
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1461 octave_idx_type n = m_dimensions(dim);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1462 if (i.is_colon ())
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1463 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1464 *this = Array<T, Alloc> ();
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1465 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1466 else if (i.length (n) != 0)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1467 {
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
1468 if (i.extent (n) != n)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
1469 octave::err_del_index_out_of_range (false, i.extent (n), n);
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10352
diff changeset
1470
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1471 octave_idx_type l, u;
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1472
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1473 if (i.is_cont_range (n, l, u))
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1474 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1475 // Special case deleting a contiguous range.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1476 octave_idx_type nd = n + l - u;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1477 octave_idx_type dl = 1;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1478 octave_idx_type du = 1;
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1479 dim_vector rdv = m_dimensions;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1480 rdv(dim) = nd;
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1481 for (int k = 0; k < dim; k++) dl *= m_dimensions(k);
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1482 for (int k = dim + 1; k < ndims (); k++) du *= m_dimensions(k);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1483
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1484 // Special case deleting a contiguous range.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1485 Array<T, Alloc> tmp = Array<T, Alloc> (rdv);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1486 const T *src = data ();
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1487 T *dest = tmp.fortran_vec ();
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1488 l *= dl; u *= dl; n *= dl;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1489 for (octave_idx_type k = 0; k < du; k++)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1490 {
23420
1fee389bd04b Use std::copy_n rather than std::copy for clarity in liboctave/array.
Rik <rik@octave.org>
parents: 23386
diff changeset
1491 std::copy_n (src, l, dest);
9773
01f897d8a130 optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 9732
diff changeset
1492 dest += l;
19381
af41e41ad28e replace oct-mem.h inline indirections by standard function calls.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 18146
diff changeset
1493 std::copy (src + u, src + n, dest);
9773
01f897d8a130 optimize memory manipulation by arrays & indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 9732
diff changeset
1494 dest += n - u;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1495 src += n;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1496 }
4870
2cbc6f37b0c2 [project @ 2004-04-21 17:30:51 by jwe]
jwe
parents: 4834
diff changeset
1497
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1498 *this = tmp;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1499 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1500 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1501 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1502 // Use index.
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
1503 Array<octave::idx_vector> ia (dim_vector (ndims (), 1), octave::idx_vector::colon);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1504 ia (dim) = i.complement (n);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1505 *this = index (ia);
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1506 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1507 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1508 }
4747
3f28979bbe2c [project @ 2004-02-06 23:56:46 by jwe]
jwe
parents: 4746
diff changeset
1509
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1510 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1511 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1512 Array<T, Alloc>::delete_elements (const Array<octave::idx_vector>& ia)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1513 {
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
1514 int ial = ia.numel ();
17642
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1515
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1516 if (ial == 1)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1517 delete_elements (ia(0));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1518 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1519 {
17642
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1520 int k, dim = -1;
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1521 for (k = 0; k < ial; k++)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1522 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1523 if (! ia(k).is_colon ())
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1524 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1525 if (dim < 0)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1526 dim = k;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1527 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1528 break;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1529 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1530 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1531 if (dim < 0)
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1532 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1533 dim_vector dv = m_dimensions;
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1534 dv(0) = 0;
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1535 *this = Array<T, Alloc> (dv);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1536 }
17642
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1537 else if (k == ial)
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1538 {
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1539 delete_elements (dim, ia(dim));
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1540 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1541 else
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1542 {
17642
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1543 // Allow the null assignment to succeed if it won't change
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1544 // anything because the indices reference an empty slice,
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1545 // provided that there is at most one non-colon (or
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1546 // equivalent) index. So, we still have the requirement of
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1547 // deleting a slice, but it is OK if the slice is empty.
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1548
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1549 // For compatibility with Matlab, stop checking once we see
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1550 // more than one non-colon index or an empty index. Matlab
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1551 // considers "[]" to be an empty index but not "false". We
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1552 // accept both.
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1553
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1554 bool empty_assignment = false;
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1555
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1556 int num_non_colon_indices = 0;
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1557
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1558 int nd = ndims ();
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1559
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1560 for (int i = 0; i < ial; i++)
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1561 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1562 octave_idx_type dim_len = (i >= nd ? 1 : m_dimensions(i));
17642
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1563
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1564 if (ia(i).length (dim_len) == 0)
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1565 {
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1566 empty_assignment = true;
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1567 break;
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1568 }
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1569
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1570 if (! ia(i).is_colon_equiv (dim_len))
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1571 {
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1572 num_non_colon_indices++;
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1573
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1574 if (num_non_colon_indices == 2)
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1575 break;
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1576 }
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1577 }
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1578
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1579 if (! empty_assignment)
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1580 (*current_liboctave_error_handler)
7ed397c8ca68 improve compatibility of null assignment (bug #31287)
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
1581 ("a null assignment can only have one non-colon index");
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 8179
diff changeset
1582 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1583 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1584
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1585 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1586
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1587 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1588 Array<T, Alloc>&
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1589 Array<T, Alloc>::insert (const Array<T, Alloc>& a, octave_idx_type r, octave_idx_type c)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1590 {
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
1591 octave::idx_vector i (r, r + a.rows ());
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
1592 octave::idx_vector j (c, c + a.columns ());
4786
fc316bde0053 [project @ 2004-02-18 12:52:20 by jwe]
jwe
parents: 4765
diff changeset
1593 if (ndims () == 2 && a.ndims () == 2)
10115
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1594 assign (i, j, a);
4786
fc316bde0053 [project @ 2004-02-18 12:52:20 by jwe]
jwe
parents: 4765
diff changeset
1595 else
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1596 {
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
1597 Array<octave::idx_vector> idx (dim_vector (a.ndims (), 1));
10115
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1598 idx(0) = i;
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1599 idx(1) = j;
12884
73e75ff9c31b Fix incorrect loop (bug #32683)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12550
diff changeset
1600 for (int k = 2; k < a.ndims (); k++)
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1601 idx(k) = octave::idx_vector (0, a.m_dimensions(k));
10115
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1602 assign (idx, a);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1603 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1604
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1605 return *this;
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1606 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1607
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1608 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1609 Array<T, Alloc>&
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1610 Array<T, Alloc>::insert (const Array<T, Alloc>& a, const Array<octave_idx_type>& ra_idx)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1611 {
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
1612 octave_idx_type n = ra_idx.numel ();
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
1613 Array<octave::idx_vector> idx (dim_vector (n, 1));
10115
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1614 const dim_vector dva = a.dims ().redim (n);
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1615 for (octave_idx_type k = 0; k < n; k++)
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
1616 idx(k) = octave::idx_vector (ra_idx(k), ra_idx(k) + dva(k));
10115
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1617
ed49cef7e005 simplify Array::insert methods
Jaroslav Hajek <highegg@gmail.com>
parents: 10113
diff changeset
1618 assign (idx, a);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1619
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1620 return *this;
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1621 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1622
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1623 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1624 Array<T, Alloc>
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
1625 Array<T, Alloc>::transpose () const
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1626 {
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4545
diff changeset
1627 assert (ndims () == 2);
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4545
diff changeset
1628
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5265
diff changeset
1629 octave_idx_type nr = dim1 ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5265
diff changeset
1630 octave_idx_type nc = dim2 ();
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1631
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1632 if (nr >= 8 && nc >= 8)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1633 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1634 Array<T, Alloc> result (dim_vector (nc, nr));
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1635
9121
bb62bc406ea7 reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents: 9100
diff changeset
1636 // Reuse the implementation used for permuting.
bb62bc406ea7 reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents: 9100
diff changeset
1637
bb62bc406ea7 reuse fast blocked transpose implementation from rec_permute_helper in Array<T>::transpose
Jaroslav Hajek <highegg@gmail.com>
parents: 9100
diff changeset
1638 rec_permute_helper::blk_trans (data (), result.fortran_vec (), nr, nc);
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1639
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1640 return result;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1641 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1642 else if (nr > 1 && nc > 1)
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1643 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1644 Array<T, Alloc> result (dim_vector (nc, nr));
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1645
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5265
diff changeset
1646 for (octave_idx_type j = 0; j < nc; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1647 for (octave_idx_type i = 0; i < nr; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1648 result.xelem (j, i) = xelem (i, j);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1649
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1650 return result;
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1651 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1652 else
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1653 {
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8302
diff changeset
1654 // Fast transpose for vectors and empty matrices.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1655 return Array<T, Alloc> (*this, dim_vector (nc, nr));
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1656 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1657 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1658
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
1659 template <typename T>
8028
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1660 static T
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1661 no_op_fcn (const T& x)
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1662 {
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1663 return x;
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1664 }
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1665
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1666 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1667 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1668 Array<T, Alloc>::hermitian (T (*fcn) (const T&)) const
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1669 {
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1670 assert (ndims () == 2);
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1671
8028
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1672 if (! fcn)
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1673 fcn = no_op_fcn<T>;
f0fbf47c914c avoid null pointer call in Array<T>::hermitian
Jaroslav Hajek <highegg@gmail.com>
parents: 7796
diff changeset
1674
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1675 octave_idx_type nr = dim1 ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1676 octave_idx_type nc = dim2 ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1677
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1678 if (nr >= 8 && nc >= 8)
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1679 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1680 Array<T, Alloc> result (dim_vector (nc, nr));
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1681
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1682 // Blocked transpose to attempt to avoid cache misses.
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1683
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1684 T buf[64];
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1685
23777
cda0614beaec Replace static instances of OCTAVE_LOCAL_BUFFER with local variables.
Rik <rik@octave.org>
parents: 23734
diff changeset
1686 octave_idx_type jj;
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1687 for (jj = 0; jj < (nc - 8 + 1); jj += 8)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1688 {
23779
7becd026c8ba Array.cc: Move variable declaration to be just ahead of where it is used.
Rik <rik@octave.org>
parents: 23777
diff changeset
1689 octave_idx_type ii;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1690 for (ii = 0; ii < (nr - 8 + 1); ii += 8)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1691 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1692 // Copy to buffer
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1693 for (octave_idx_type j = jj, k = 0, idxj = jj * nr;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1694 j < jj + 8; j++, idxj += nr)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1695 for (octave_idx_type i = ii; i < ii + 8; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1696 buf[k++] = xelem (i + idxj);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1697
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1698 // Copy from buffer
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1699 for (octave_idx_type i = ii, idxi = ii * nc; i < ii + 8;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1700 i++, idxi += nc)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1701 for (octave_idx_type j = jj, k = i - ii; j < jj + 8;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1702 j++, k+=8)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1703 result.xelem (j + idxi) = fcn (buf[k]);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1704 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1705
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1706 if (ii < nr)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1707 for (octave_idx_type j = jj; j < jj + 8; j++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1708 for (octave_idx_type i = ii; i < nr; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1709 result.xelem (j, i) = fcn (xelem (i, j));
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1710 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1711
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1712 for (octave_idx_type j = jj; j < nc; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1713 for (octave_idx_type i = 0; i < nr; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1714 result.xelem (j, i) = fcn (xelem (i, j));
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1715
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1716 return result;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1717 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1718 else
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1719 {
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1720 Array<T, Alloc> result (dim_vector (nc, nr));
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1721
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1722 for (octave_idx_type j = 0; j < nc; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1723 for (octave_idx_type i = 0; i < nr; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1724 result.xelem (j, i) = fcn (xelem (i, j));
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1725
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1726 return result;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1727 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1728 }
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1729
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1730 /*
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1731
23779
7becd026c8ba Array.cc: Move variable declaration to be just ahead of where it is used.
Rik <rik@octave.org>
parents: 23777
diff changeset
1732 ## Transpose tests for matrices of the tile size and plus or minus a row
22737
7abc25e6206a maint: Clean up code base to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 22402
diff changeset
1733 ## and with four tiles.
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1734
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1735 %!shared m7, mt7, m8, mt8, m9, mt9
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1736 %! m7 = reshape (1 : 7*8, 8, 7);
7796
762801c50b21 Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
1737 %! mt7 = [1:8; 9:16; 17:24; 25:32; 33:40; 41:48; 49:56];
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1738 %! m8 = reshape (1 : 8*8, 8, 8);
7796
762801c50b21 Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
1739 %! mt8 = mt8 = [mt7; 57:64];
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1740 %! m9 = reshape (1 : 9*8, 8, 9);
7796
762801c50b21 Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
1741 %! mt9 = [mt8; 65:72];
762801c50b21 Fix tests for transpose in Array.cc
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
1742
14427
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1743 %!assert (m7', mt7)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1744 %!assert ((1i*m7).', 1i * mt7)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1745 %!assert ((1i*m7)', conj (1i * mt7))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1746 %!assert (m8', mt8)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1747 %!assert ((1i*m8).', 1i * mt8)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1748 %!assert ((1i*m8)', conj (1i * mt8))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1749 %!assert (m9', mt9)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1750 %!assert ((1i*m9).', 1i * mt9)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1751 %!assert ((1i*m9)', conj (1i * mt9))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1752 %!assert ([m7, m8; m7, m8]', [mt7, mt7; mt8, mt8])
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1753 %!assert ((1i*[m7, m8; m7, m8]).', 1i * [mt7, mt7; mt8, mt8])
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1754 %!assert ((1i*[m7, m8; m7, m8])', conj (1i * [mt7, mt7; mt8, mt8]))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1755 %!assert ([m8, m8; m8, m8]', [mt8, mt8; mt8, mt8])
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1756 %!assert ((1i*[m8, m8; m8, m8]).', 1i * [mt8, mt8; mt8, mt8])
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1757 %!assert ((1i*[m8, m8; m8, m8])', conj (1i * [mt8, mt8; mt8, mt8]))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1758 %!assert ([m9, m8; m9, m8]', [mt9, mt9; mt8, mt8])
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1759 %!assert ((1i*[m9, m8; m9, m8]).', 1i * [mt9, mt9; mt8, mt8])
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14158
diff changeset
1760 %!assert ((1i*[m9, m8; m9, m8])', conj (1i * [mt9, mt9; mt8, mt8]))
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1761
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1762 */
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7717
diff changeset
1763
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1764 template <typename T, typename Alloc>
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1765 T *
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
1766 Array<T, Alloc>::fortran_vec ()
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1767 {
6881
cd2c6a69a70d [project @ 2007-09-07 21:48:09 by jwe]
jwe
parents: 6867
diff changeset
1768 make_unique ();
cd2c6a69a70d [project @ 2007-09-07 21:48:09 by jwe]
jwe
parents: 6867
diff changeset
1769
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
1770 return m_slice_data;
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1771 }
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
1772
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1773 // Non-real types don't have NaNs.
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21136
diff changeset
1774 template <typename T>
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1775 inline bool
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1776 sort_isnan (typename ref_param<T>::type)
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1777 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1778 return false;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1779 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1780
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1781 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1782 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1783 Array<T, Alloc>::sort (int dim, sortmode mode) const
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1784 {
10703
5eb420d92307 fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
1785 if (dim < 0)
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
1786 (*current_liboctave_error_handler) ("sort: invalid dimension");
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1787
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1788 Array<T, Alloc> m (dims ());
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1789
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1790 dim_vector dv = m.dims ();
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1791
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
1792 if (m.numel () < 1)
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1793 return m;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1794
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
1795 if (dim >= dv.ndims ())
10703
5eb420d92307 fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
1796 dv.resize (dim+1, 1);
5eb420d92307 fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
1797
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1798 octave_idx_type ns = dv(dim);
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1799 octave_idx_type iter = dv.numel () / ns;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1800 octave_idx_type stride = 1;
8505
124dd27bedae Array.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 8410
diff changeset
1801
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1802 for (int i = 0; i < dim; i++)
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1803 stride *= dv(i);
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1804
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1805 T *v = m.fortran_vec ();
8651
ea8e65ca234f reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
1806 const T *ov = data ();
ea8e65ca234f reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
1807
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1808 octave_sort<T> lsort;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1809
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1810 if (mode != UNSORTED)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1811 lsort.set_compare (mode);
7463
2467639bd8c0 eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents: 7443
diff changeset
1812 else
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1813 return m;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1814
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1815 if (stride == 1)
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1816 {
29979
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1817 // Special case along first dimension avoids gather/scatter AND directly
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1818 // sorts into destination buffer for an 11% performance boost.
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1819 for (octave_idx_type j = 0; j < iter; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1820 {
29979
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1821 // Copy and partition out NaNs.
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1822 // No need to special case integer types <T> from floating point
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1823 // types <T> to avoid sort_isnan() test as it makes no discernible
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1824 // performance impact.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1825 octave_idx_type kl = 0;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1826 octave_idx_type ku = ns;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1827 for (octave_idx_type i = 0; i < ns; i++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1828 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1829 T tmp = ov[i];
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1830 if (sort_isnan<T> (tmp))
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1831 v[--ku] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1832 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1833 v[kl++] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1834 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1835
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1836 // sort.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1837 lsort.sort (v, kl);
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1838
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1839 if (ku < ns)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1840 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1841 // NaNs are in reverse order
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1842 std::reverse (v + ku, v + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1843 if (mode == DESCENDING)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1844 std::rotate (v, v + ku, v + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1845 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1846
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1847 v += ns;
8651
ea8e65ca234f reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
1848 ov += ns;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1849 }
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1850 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1851 else
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1852 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1853 OCTAVE_LOCAL_BUFFER (T, buf, ns);
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1854
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1855 for (octave_idx_type j = 0; j < iter; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1856 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1857 octave_idx_type offset = j;
29975
06bf5aca68bb Improve performance of sort along dimensions > 1 (bug #60928).
Rik <rik@octave.org>
parents: 29887
diff changeset
1858 octave_idx_type n_strides = j / stride;
06bf5aca68bb Improve performance of sort along dimensions > 1 (bug #60928).
Rik <rik@octave.org>
parents: 29887
diff changeset
1859 offset += n_strides * stride * (ns - 1);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1860
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1861 // gather and partition out NaNs.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1862 octave_idx_type kl = 0;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1863 octave_idx_type ku = ns;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1864 for (octave_idx_type i = 0; i < ns; i++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1865 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1866 T tmp = ov[i*stride + offset];
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1867 if (sort_isnan<T> (tmp))
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1868 buf[--ku] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1869 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1870 buf[kl++] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1871 }
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1872
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1873 // sort.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1874 lsort.sort (buf, kl);
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1875
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1876 if (ku < ns)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1877 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1878 // NaNs are in reverse order
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1879 std::reverse (buf + ku, buf + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1880 if (mode == DESCENDING)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1881 std::rotate (buf, buf + ku, buf + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1882 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1883
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1884 // scatter.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1885 for (octave_idx_type i = 0; i < ns; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1886 v[i*stride + offset] = buf[i];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1887 }
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1888 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1889
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1890 return m;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1891 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1892
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1893 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1894 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1895 Array<T, Alloc>::sort (Array<octave_idx_type>& sidx, int dim,
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1896 sortmode mode) const
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1897 {
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1898 if (dim < 0 || dim >= ndims ())
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
1899 (*current_liboctave_error_handler) ("sort: invalid dimension");
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1900
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
1901 Array<T, Alloc> m (dims ());
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1902
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1903 dim_vector dv = m.dims ();
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1904
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20228
diff changeset
1905 if (m.numel () < 1)
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1906 {
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1907 sidx = Array<octave_idx_type> (dv);
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1908 return m;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1909 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1910
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1911 octave_idx_type ns = dv(dim);
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1912 octave_idx_type iter = dv.numel () / ns;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1913 octave_idx_type stride = 1;
8505
124dd27bedae Array.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 8410
diff changeset
1914
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1915 for (int i = 0; i < dim; i++)
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1916 stride *= dv(i);
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1917
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1918 T *v = m.fortran_vec ();
8651
ea8e65ca234f reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
1919 const T *ov = data ();
ea8e65ca234f reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
1920
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1921 octave_sort<T> lsort;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1922
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1923 sidx = Array<octave_idx_type> (dv);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1924 octave_idx_type *vi = sidx.fortran_vec ();
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1925
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1926 if (mode != UNSORTED)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1927 lsort.set_compare (mode);
7463
2467639bd8c0 eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents: 7443
diff changeset
1928 else
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1929 return m;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1930
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1931 if (stride == 1)
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1932 {
29979
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1933 // Special case for dim 1 avoids gather/scatter for performance boost.
ac5e1b64f8c9 Resolve two FIXME notes in Array::sort routines.
Rik <rik@octave.org>
parents: 29975
diff changeset
1934 // See comments in Array::sort (dim, mode).
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1935 for (octave_idx_type j = 0; j < iter; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1936 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1937 // copy and partition out NaNs.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1938 octave_idx_type kl = 0;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1939 octave_idx_type ku = ns;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1940 for (octave_idx_type i = 0; i < ns; i++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1941 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1942 T tmp = ov[i];
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1943 if (sort_isnan<T> (tmp))
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1944 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1945 --ku;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1946 v[ku] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1947 vi[ku] = i;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1948 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1949 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1950 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1951 v[kl] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1952 vi[kl] = i;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1953 kl++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1954 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1955 }
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1956
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1957 // sort.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1958 lsort.sort (v, vi, kl);
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1959
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1960 if (ku < ns)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1961 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1962 // NaNs are in reverse order
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1963 std::reverse (v + ku, v + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1964 std::reverse (vi + ku, vi + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1965 if (mode == DESCENDING)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1966 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1967 std::rotate (v, v + ku, v + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1968 std::rotate (vi, vi + ku, vi + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1969 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1970 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1971
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1972 v += ns;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1973 vi += ns;
8651
ea8e65ca234f reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
1974 ov += ns;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1975 }
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1976 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1977 else
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
1978 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1979 OCTAVE_LOCAL_BUFFER (T, buf, ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1980 OCTAVE_LOCAL_BUFFER (octave_idx_type, bufi, ns);
8651
ea8e65ca234f reduce memory usage in sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8580
diff changeset
1981
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1982 for (octave_idx_type j = 0; j < iter; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1983 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
1984 octave_idx_type offset = j;
29975
06bf5aca68bb Improve performance of sort along dimensions > 1 (bug #60928).
Rik <rik@octave.org>
parents: 29887
diff changeset
1985 octave_idx_type n_strides = j / stride;
06bf5aca68bb Improve performance of sort along dimensions > 1 (bug #60928).
Rik <rik@octave.org>
parents: 29887
diff changeset
1986 offset += n_strides * stride * (ns - 1);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1987
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
1988 // gather and partition out NaNs.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1989 octave_idx_type kl = 0;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
1990 octave_idx_type ku = ns;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1991 for (octave_idx_type i = 0; i < ns; i++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1992 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1993 T tmp = ov[i*stride + offset];
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1994 if (sort_isnan<T> (tmp))
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1995 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1996 --ku;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1997 buf[ku] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1998 bufi[ku] = i;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
1999 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2000 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2001 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2002 buf[kl] = tmp;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2003 bufi[kl] = i;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2004 kl++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2005 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2006 }
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
2007
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2008 // sort.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2009 lsort.sort (buf, bufi, kl);
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2010
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2011 if (ku < ns)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2012 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2013 // NaNs are in reverse order
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2014 std::reverse (buf + ku, buf + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2015 std::reverse (bufi + ku, bufi + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2016 if (mode == DESCENDING)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2017 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2018 std::rotate (buf, buf + ku, buf + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2019 std::rotate (bufi, bufi + ku, bufi + ns);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2020 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2021 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2022
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8651
diff changeset
2023 // scatter.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2024 for (octave_idx_type i = 0; i < ns; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2025 v[i*stride + offset] = buf[i];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2026 for (octave_idx_type i = 0; i < ns; i++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2027 vi[i*stride + offset] = bufi[i];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2028 }
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
2029 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
2030
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
2031 return m;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
2032 }
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7321
diff changeset
2033
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2034 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2035 typename Array<T, Alloc>::compare_fcn_type
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2036 safe_comparator (sortmode mode, const Array<T, Alloc>& /* a */,
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2037 bool /* allow_chk */)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2038 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2039 if (mode == ASCENDING)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2040 return octave_sort<T>::ascending_compare;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2041 else if (mode == DESCENDING)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2042 return octave_sort<T>::descending_compare;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2043 else
23796
4f12819a634f Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23795
diff changeset
2044 return nullptr;
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2045 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2046
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2047 template <typename T, typename Alloc>
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2048 sortmode
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2049 Array<T, Alloc>::issorted (sortmode mode) const
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2050 {
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2051 octave_sort<T> lsort;
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2052
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2053 octave_idx_type n = numel ();
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2054
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2055 if (n <= 1)
26412
ccacd7162520 Array.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
2056 return (mode == UNSORTED) ? ASCENDING : mode;
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2057
11210
b79924abf776 Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents: 10716
diff changeset
2058 if (mode == UNSORTED)
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2059 {
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2060 // Auto-detect mode.
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2061 compare_fcn_type compare
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2062 = safe_comparator (ASCENDING, *this, false);
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2063
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2064 if (compare (elem (n-1), elem (0)))
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2065 mode = DESCENDING;
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2066 else
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2067 mode = ASCENDING;
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2068 }
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2069
26412
ccacd7162520 Array.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
2070 lsort.set_compare (safe_comparator (mode, *this, false));
ccacd7162520 Array.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
2071
ccacd7162520 Array.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
2072 if (! lsort.issorted (data (), n))
ccacd7162520 Array.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
2073 mode = UNSORTED;
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2074
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2075 return mode;
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2076
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2077 }
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2078
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2079 template <typename T, typename Alloc>
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2080 Array<octave_idx_type>
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2081 Array<T, Alloc>::sort_rows_idx (sortmode mode) const
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2082 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2083 Array<octave_idx_type> idx;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2084
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2085 octave_sort<T> lsort (safe_comparator (mode, *this, true));
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2086
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2087 octave_idx_type r = rows ();
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2088 octave_idx_type c = cols ();
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2089
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
2090 idx = Array<octave_idx_type> (dim_vector (r, 1));
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2091
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2092 lsort.sort_rows (data (), idx.fortran_vec (), r, c);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2093
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2094 return idx;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2095 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2096
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2097 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2098 sortmode
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2099 Array<T, Alloc>::is_sorted_rows (sortmode mode) const
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2100 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2101 octave_sort<T> lsort;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2102
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2103 octave_idx_type r = rows ();
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2104 octave_idx_type c = cols ();
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2105
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2106 if (r <= 1 || c == 0)
26412
ccacd7162520 Array.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
2107 return (mode == UNSORTED) ? ASCENDING : mode;
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2108
11210
b79924abf776 Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents: 10716
diff changeset
2109 if (mode == UNSORTED)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2110 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2111 // Auto-detect mode.
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
2112 compare_fcn_type compare
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2113 = safe_comparator (ASCENDING, *this, false);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2114
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2115 octave_idx_type i;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2116 for (i = 0; i < cols (); i++)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2117 {
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2118 T l = elem (0, i);
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2119 T u = elem (rows () - 1, i);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2120 if (compare (l, u))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2121 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2122 if (mode == DESCENDING)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2123 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2124 mode = UNSORTED;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2125 break;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2126 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2127 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2128 mode = ASCENDING;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2129 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2130 else if (compare (u, l))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2131 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2132 if (mode == ASCENDING)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2133 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2134 mode = UNSORTED;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2135 break;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2136 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2137 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2138 mode = DESCENDING;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2139 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2140 }
11210
b79924abf776 Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents: 10716
diff changeset
2141 if (mode == UNSORTED && i == cols ())
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2142 mode = ASCENDING;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2143 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2144
11210
b79924abf776 Array.cc: use comparisons to sortmode values instead of testing mode directly
John W. Eaton <jwe@octave.org>
parents: 10716
diff changeset
2145 if (mode != UNSORTED)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2146 {
9920
56fbe170d354 fix issorted with NaNs in middle
Jaroslav Hajek <highegg@gmail.com>
parents: 9886
diff changeset
2147 lsort.set_compare (safe_comparator (mode, *this, false));
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2148
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2149 if (! lsort.is_sorted_rows (data (), r, c))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2150 mode = UNSORTED;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2151 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2152
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2153 return mode;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2154
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2155 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2156
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2157 // Do a binary lookup in a sorted array.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2158 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2159 octave_idx_type
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2160 Array<T, Alloc>::lookup (const T& value, sortmode mode) const
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2161 {
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2162 octave_idx_type n = numel ();
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2163 octave_sort<T> lsort;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2164
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2165 if (mode == UNSORTED)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2166 {
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2167 // auto-detect mode
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2168 if (n > 1 && lsort.descending_compare (elem (0), elem (n-1)))
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2169 mode = DESCENDING;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2170 else
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2171 mode = ASCENDING;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2172 }
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2173
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2174 lsort.set_compare (mode);
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2175
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2176 return lsort.lookup (data (), n, value);
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2177 }
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2178
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2179 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2180 Array<octave_idx_type>
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2181 Array<T, Alloc>::lookup (const Array<T, Alloc>& values, sortmode mode) const
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2182 {
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2183 octave_idx_type n = numel ();
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2184 octave_idx_type nval = values.numel ();
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2185 octave_sort<T> lsort;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2186 Array<octave_idx_type> idx (values.dims ());
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2187
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2188 if (mode == UNSORTED)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2189 {
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2190 // auto-detect mode
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2191 if (n > 1 && lsort.descending_compare (elem (0), elem (n-1)))
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2192 mode = DESCENDING;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2193 else
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2194 mode = ASCENDING;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2195 }
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2196
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2197 lsort.set_compare (mode);
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2198
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
2199 // This determines the split ratio between the O(M*log2(N)) and O(M+N)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
2200 // algorithms.
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2201 static const double ratio = 1.0;
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2202 sortmode vmode = UNSORTED;
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2203
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2204 // Attempt the O(M+N) algorithm if M is large enough.
21782
2aef506f3fec use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
2205 if (nval > ratio * n / octave::math::log2 (n + 1.0))
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2206 {
23588
0549061d35b9 maint: Deprecate is_sorted and replace with issorted.
Rik <rik@octave.org>
parents: 23577
diff changeset
2207 vmode = values.issorted ();
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2208 // The table must not contain a NaN.
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2209 if ((vmode == ASCENDING && sort_isnan<T> (values(nval-1)))
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2210 || (vmode == DESCENDING && sort_isnan<T> (values(0))))
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2211 vmode = UNSORTED;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2212 }
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2213
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2214 if (vmode != UNSORTED)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2215 lsort.lookup_sorted (data (), n, values.data (), nval,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2216 idx.fortran_vec (), vmode != mode);
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2217 else
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9920
diff changeset
2218 lsort.lookup (data (), n, values.data (), nval, idx.fortran_vec ());
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2219
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2220 return idx;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2221 }
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8799
diff changeset
2222
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2223 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2224 octave_idx_type
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
2225 Array<T, Alloc>::nnz () const
9878
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2226 {
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2227 const T *src = data ();
20228
00cf2847355d Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20218
diff changeset
2228 octave_idx_type nel = numel ();
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2229 octave_idx_type retval = 0;
9878
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2230 const T zero = T ();
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2231 for (octave_idx_type i = 0; i < nel; i++)
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2232 if (src[i] != zero)
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2233 retval++;
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2234
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2235 return retval;
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2236 }
ead4f9c82a9a implement Array<T>::nnz
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
2237
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2238 template <typename T, typename Alloc>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2239 Array<octave_idx_type>
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2240 Array<T, Alloc>::find (octave_idx_type n, bool backward) const
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2241 {
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2242 Array<octave_idx_type> retval;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2243 const T *src = data ();
20228
00cf2847355d Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20218
diff changeset
2244 octave_idx_type nel = numel ();
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2245 const T zero = T ();
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9237
diff changeset
2246 if (n < 0 || n >= nel)
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2247 {
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2248 // We want all elements, which means we'll almost surely need
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 21724
diff changeset
2249 // to resize. So count first, then allocate array of exact size.
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2250 octave_idx_type cnt = 0;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2251 for (octave_idx_type i = 0; i < nel; i++)
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2252 cnt += src[i] != zero;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2253
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
2254 retval.clear (cnt, 1);
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2255 octave_idx_type *dest = retval.fortran_vec ();
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2256 for (octave_idx_type i = 0; i < nel; i++)
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2257 if (src[i] != zero) *dest++ = i;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2258 }
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2259 else
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2260 {
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 21724
diff changeset
2261 // We want a fixed max number of elements, usually small. So be
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2262 // optimistic, alloc the array in advance, and then resize if
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2263 // needed.
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10314
diff changeset
2264 retval.clear (n, 1);
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2265 if (backward)
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2266 {
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2267 // Do the search as a series of successive single-element searches.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2268 octave_idx_type k = 0;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2269 octave_idx_type l = nel - 1;
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2270 for (; k < n; k++)
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2271 {
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
2272 for (; l >= 0 && src[l] == zero; l--) ;
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2273 if (l >= 0)
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2274 retval(k) = l--;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2275 else
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2276 break;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2277 }
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2278 if (k < n)
11574
a83bad07f7e3 attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
2279 retval.resize2 (k, 1);
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9237
diff changeset
2280 octave_idx_type *rdata = retval.fortran_vec ();
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9237
diff changeset
2281 std::reverse (rdata, rdata + k);
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2282 }
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2283 else
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2284 {
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2285 // Do the search as a series of successive single-element searches.
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2286 octave_idx_type k = 0;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2287 octave_idx_type l = 0;
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2288 for (; k < n; k++)
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2289 {
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
2290 for (; l != nel && src[l] == zero; l++) ;
9029
2df28ad88b0e small fix in Array::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9025
diff changeset
2291 if (l != nel)
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2292 retval(k) = l++;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2293 else
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2294 break;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2295 }
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2296 if (k < n)
11574
a83bad07f7e3 attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
2297 retval.resize2 (k, 1);
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2298 }
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2299 }
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2300
9046
88bf56bbccca make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 9029
diff changeset
2301 // Fixup return dimensions, for Matlab compatibility.
15018
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14846
diff changeset
2302 // find (zeros (0,0)) -> zeros (0,0)
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14846
diff changeset
2303 // find (zeros (1,0)) -> zeros (1,0)
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14846
diff changeset
2304 // find (zeros (0,1)) -> zeros (0,1)
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14846
diff changeset
2305 // find (zeros (0,X)) -> zeros (0,1)
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14846
diff changeset
2306 // find (zeros (1,1)) -> zeros (0,0) !!!! WHY?
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14846
diff changeset
2307 // find (zeros (0,1,0)) -> zeros (0,0)
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14846
diff changeset
2308 // find (zeros (0,1,0,1)) -> zeros (0,0) etc
9046
88bf56bbccca make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 9029
diff changeset
2309
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23487
diff changeset
2310 if ((numel () == 1 && retval.isempty ())
9046
88bf56bbccca make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 9029
diff changeset
2311 || (rows () == 0 && dims ().numel (1) == 0))
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2312 retval.m_dimensions = dim_vector ();
9046
88bf56bbccca make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 9029
diff changeset
2313 else if (rows () == 1 && ndims () == 2)
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2314 retval.m_dimensions = dim_vector (1, retval.numel ());
9046
88bf56bbccca make Array::find already return Matlab-compatible dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 9029
diff changeset
2315
9025
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2316 return retval;
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2317 }
484756d558d6 add Array<T>::find
Jaroslav Hajek <highegg@gmail.com>
parents: 9012
diff changeset
2318
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2319 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2320 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2321 Array<T, Alloc>::nth_element (const octave::idx_vector& n, int dim) const
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2322 {
10703
5eb420d92307 fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
2323 if (dim < 0)
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2324 (*current_liboctave_error_handler) ("nth_element: invalid dimension");
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2325
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2326 dim_vector dv = dims ();
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
2327 if (dim >= dv.ndims ())
10703
5eb420d92307 fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
2328 dv.resize (dim+1, 1);
5eb420d92307 fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
2329
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2330 octave_idx_type ns = dv(dim);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2331
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2332 octave_idx_type nn = n.length (ns);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2333
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2334 dv(dim) = std::min (nn, ns);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2335 dv.chop_trailing_singletons ();
21827
2781d6efa88f Fix build error with --enable-64
Mike Miller <mtmiller@octave.org>
parents: 21785
diff changeset
2336 dim = std::min (dv.ndims (), static_cast<octave_idx_type> (dim));
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2337
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2338 Array<T, Alloc> m (dv);
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2339
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23487
diff changeset
2340 if (m.isempty ())
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2341 return m;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2342
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2343 sortmode mode = UNSORTED;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2344 octave_idx_type lo = 0;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2345
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2346 switch (n.idx_class ())
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2347 {
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
2348 case octave::idx_vector::class_scalar:
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2349 mode = ASCENDING;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2350 lo = n(0);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2351 break;
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
2352 case octave::idx_vector::class_range:
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2353 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2354 octave_idx_type inc = n.increment ();
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2355 if (inc == 1)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2356 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2357 mode = ASCENDING;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2358 lo = n(0);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2359 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2360 else if (inc == -1)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2361 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2362 mode = DESCENDING;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2363 lo = ns - 1 - n(0);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2364 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2365 }
23800
2fdc65c031b5 Add break or continue statements to prevent implicit fall-through in case statements.
Rik <rik@octave.org>
parents: 23796
diff changeset
2366 break;
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
2367 case octave::idx_vector::class_vector:
23734
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2368 // This case resolves bug #51329, a fallback to allow the given index
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2369 // to be a sequential vector instead of the typical scalar or range
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2370 if (n(1) - n(0) == 1)
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2371 {
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2372 mode = ASCENDING;
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2373 lo = n(0);
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2374 }
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2375 else if (n(1) - n(0) == -1)
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2376 {
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2377 mode = DESCENDING;
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2378 lo = ns - 1 - n(0);
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2379 }
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2380 // Ensure that the vector is actually an arithmetic contiguous sequence
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2381 for (octave_idx_type i = 2; i < n.length () && mode != UNSORTED; i++)
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2382 if ((mode == ASCENDING && n(i) - n(i-1) != 1)
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2383 || (mode == DESCENDING && n(i) - n(i-1) != -1))
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2384 mode = UNSORTED;
3e93a2a66cdf nth_element: allow sequence of indices to be passed as a vector (bug #51329)
Brad Kennedy <bk@co60.ca>
parents: 23592
diff changeset
2385 break;
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2386 default:
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2387 break;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2388 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2389
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2390 if (mode == UNSORTED)
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2391 (*current_liboctave_error_handler)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2392 ("nth_element: n must be a scalar or a contiguous range");
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2393
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2394 octave_idx_type up = lo + nn;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2395
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2396 if (lo < 0 || up > ns)
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2397 (*current_liboctave_error_handler) ("nth_element: invalid element index");
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2398
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2399 octave_idx_type iter = numel () / ns;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2400 octave_idx_type stride = 1;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2401
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2402 for (int i = 0; i < dim; i++)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2403 stride *= dv(i);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2404
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2405 T *v = m.fortran_vec ();
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2406 const T *ov = data ();
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2407
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2408 OCTAVE_LOCAL_BUFFER (T, buf, ns);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2409
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2410 octave_sort<T> lsort;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2411 lsort.set_compare (mode);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2412
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2413 for (octave_idx_type j = 0; j < iter; j++)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2414 {
18084
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2415 octave_idx_type kl = 0;
8e056300994b Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents: 17769
diff changeset
2416 octave_idx_type ku = ns;
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2417
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2418 if (stride == 1)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2419 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2420 // copy without NaNs.
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2421 for (octave_idx_type i = 0; i < ns; i++)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2422 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2423 T tmp = ov[i];
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2424 if (sort_isnan<T> (tmp))
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2425 buf[--ku] = tmp;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2426 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2427 buf[kl++] = tmp;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2428 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2429
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2430 ov += ns;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2431 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2432 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2433 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2434 octave_idx_type offset = j % stride;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2435 // copy without NaNs.
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2436 for (octave_idx_type i = 0; i < ns; i++)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2437 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2438 T tmp = ov[offset + i*stride];
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2439 if (sort_isnan<T> (tmp))
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2440 buf[--ku] = tmp;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2441 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2442 buf[kl++] = tmp;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2443 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2444
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2445 if (offset == stride-1)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2446 ov += ns*stride;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2447 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2448
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2449 if (ku == ns)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
2450 lsort.nth_element (buf, ns, lo, up);
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2451 else if (mode == ASCENDING)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2452 lsort.nth_element (buf, ku, lo, std::min (ku, up));
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2453 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2454 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2455 octave_idx_type nnan = ns - ku;
10258
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
2456 octave_idx_type zero = 0;
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
2457 lsort.nth_element (buf, ku, std::max (lo - nnan, zero),
e317791645c4 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
2458 std::max (up - nnan, zero));
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2459 std::rotate (buf, buf + ku, buf + ns);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2460 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2461
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2462 if (stride == 1)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2463 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2464 for (octave_idx_type i = 0; i < nn; i++)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2465 v[i] = buf[lo + i];
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2466
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2467 v += nn;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2468 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2469 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2470 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2471 octave_idx_type offset = j % stride;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2472 for (octave_idx_type i = 0; i < nn; i++)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2473 v[offset + stride * i] = buf[lo + i];
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2474 if (offset == stride-1)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2475 v += nn*stride;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2476 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2477 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2478
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2479 return m;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2480 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
2481
29722
9a8a6a99a0a7 Re-instantiate signature of macro that is part of the API (bug #60608).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29569
diff changeset
2482 #define NO_INSTANTIATE_ARRAY_SORT_API(T, API) \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2483 template <> API Array<T> \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2484 Array<T>::sort (int, sortmode) const \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2485 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2486 return *this; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2487 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2488 template <> API Array<T> \
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2489 Array<T>::sort (Array<octave_idx_type> &sidx, int, sortmode) const \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2490 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2491 sidx = Array<octave_idx_type> (); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2492 return *this; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2493 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2494 template <> API sortmode \
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2495 Array<T>::issorted (sortmode) const \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2496 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2497 return UNSORTED; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2498 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2499 API Array<T>::compare_fcn_type \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2500 safe_comparator (sortmode, const Array<T>&, bool) \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2501 { \
23796
4f12819a634f Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents: 23795
diff changeset
2502 return nullptr; \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2503 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2504 template <> API Array<octave_idx_type> \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2505 Array<T>::sort_rows_idx (sortmode) const \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2506 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2507 return Array<octave_idx_type> (); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2508 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2509 template <> API sortmode \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2510 Array<T>::is_sorted_rows (sortmode) const \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2511 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2512 return UNSORTED; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2513 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2514 template <> API octave_idx_type \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2515 Array<T>::lookup (T const &, sortmode) const \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2516 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2517 return 0; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2518 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2519 template <> API Array<octave_idx_type> \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2520 Array<T>::lookup (const Array<T>&, sortmode) const \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2521 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2522 return Array<octave_idx_type> (); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2523 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2524 template <> API octave_idx_type \
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31706
diff changeset
2525 Array<T>::nnz () const \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2526 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2527 return 0; \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2528 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2529 template <> API Array<octave_idx_type> \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2530 Array<T>::find (octave_idx_type, bool) const \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2531 { \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2532 return Array<octave_idx_type> (); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2533 } \
29246
1b2845593788 Export Array template specializations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29225
diff changeset
2534 template <> API Array<T> \
29722
9a8a6a99a0a7 Re-instantiate signature of macro that is part of the API (bug #60608).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29569
diff changeset
2535 Array<T>::nth_element (const octave::idx_vector&, int) const { \
22197
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2536 return Array<T> (); \
e43d83253e28 refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents: 22139
diff changeset
2537 }
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
2538
29722
9a8a6a99a0a7 Re-instantiate signature of macro that is part of the API (bug #60608).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29569
diff changeset
2539 #define NO_INSTANTIATE_ARRAY_SORT(T) NO_INSTANTIATE_ARRAY_SORT_API (T,)
9a8a6a99a0a7 Re-instantiate signature of macro that is part of the API (bug #60608).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29569
diff changeset
2540
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2541 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2542 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2543 Array<T, Alloc>::diag (octave_idx_type k) const
7620
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2544 {
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2545 dim_vector dv = dims ();
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
2546 octave_idx_type nd = dv.ndims ();
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2547 Array<T, Alloc> d;
7620
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2548
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2549 if (nd > 2)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2550 (*current_liboctave_error_handler) ("Matrix must be 2-dimensional");
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2551
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2552 octave_idx_type nnr = dv(0);
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2553 octave_idx_type nnc = dv(1);
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2554
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2555 if (nnr == 0 && nnc == 0)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2556 ; // do nothing for empty matrix
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2557 else if (nnr != 1 && nnc != 1)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2558 {
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2559 // Extract diag from matrix
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2560 if (k > 0)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2561 nnc -= k;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2562 else if (k < 0)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2563 nnr += k;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2564
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2565 if (nnr > 0 && nnc > 0)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2566 {
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2567 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2568
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2569 d.resize (dim_vector (ndiag, 1));
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2570
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2571 if (k > 0)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2572 {
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2573 for (octave_idx_type i = 0; i < ndiag; i++)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2574 d.xelem (i) = elem (i, i+k);
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2575 }
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2576 else if (k < 0)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2577 {
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2578 for (octave_idx_type i = 0; i < ndiag; i++)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2579 d.xelem (i) = elem (i-k, i);
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2580 }
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2581 else
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2582 {
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2583 for (octave_idx_type i = 0; i < ndiag; i++)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2584 d.xelem (i) = elem (i, i);
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2585 }
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2586 }
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2587 else // Matlab returns [] 0x1 for out-of-range diagonal
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2588 d.resize (dim_vector (0, 1));
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2589 }
7620
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2590 else
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2591 {
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2592 // Create diag matrix from vector
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2593 octave_idx_type roff = 0;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2594 octave_idx_type coff = 0;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2595 if (k > 0)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2596 {
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2597 roff = 0;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2598 coff = k;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2599 }
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2600 else if (k < 0)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2601 {
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2602 roff = -k;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2603 coff = 0;
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2604 }
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2605
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2606 if (nnr == 1)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2607 {
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2608 octave_idx_type n = nnc + std::abs (k);
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2609 d = Array<T, Alloc> (dim_vector (n, n), resize_fill_value ());
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2610
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2611 for (octave_idx_type i = 0; i < nnc; i++)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2612 d.xelem (i+roff, i+coff) = elem (0, i);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2613 }
12550
ea3a4bf39048 Treat empty vector (1x0 or 0x1) as valid input to diag().
Rik <octave@nomad.inbox5.com>
parents: 12153
diff changeset
2614 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2615 {
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2616 octave_idx_type n = nnr + std::abs (k);
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2617 d = Array<T, Alloc> (dim_vector (n, n), resize_fill_value ());
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2618
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2619 for (octave_idx_type i = 0; i < nnr; i++)
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2620 d.xelem (i+roff, i+coff) = elem (i, 0);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2621 }
7620
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2622 }
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2623
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2624 return d;
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2625 }
36594d5bbe13 Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents: 7605
diff changeset
2626
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2627 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2628 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2629 Array<T, Alloc>::diag (octave_idx_type m, octave_idx_type n) const
14557
e8e86ae3abbc make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents: 14446
diff changeset
2630 {
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2631 if (ndims () != 2 || (rows () != 1 && cols () != 1))
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2632 (*current_liboctave_error_handler) ("cat: invalid dimension");
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2633
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2634 Array<T, Alloc> retval (dim_vector (m, n), resize_fill_value ());
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2635
27323
1582a6140275 Stop segfault when calling 3-input form of diag with cell arrays (bug #56711).
Rik <rik@octave.org>
parents: 26412
diff changeset
2636 octave_idx_type nel = std::min (numel (), std::min (m, n));
1582a6140275 Stop segfault when calling 3-input form of diag with cell arrays (bug #56711).
Rik <rik@octave.org>
parents: 26412
diff changeset
2637 for (octave_idx_type i = 0; i < nel; i++)
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2638 retval.xelem (i, i) = xelem (i);
14557
e8e86ae3abbc make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents: 14446
diff changeset
2639
e8e86ae3abbc make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents: 14446
diff changeset
2640 return retval;
e8e86ae3abbc make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents: 14446
diff changeset
2641 }
e8e86ae3abbc make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents: 14446
diff changeset
2642
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2643 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2644 Array<T, Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2645 Array<T, Alloc>::cat (int dim, octave_idx_type n, const Array<T, Alloc> *array_list)
10531
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2646 {
10716
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2647 // Default concatenation.
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2648 bool (dim_vector::*concat_rule) (const dim_vector&, int) = &dim_vector::concat;
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2649
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2650 if (dim == -1 || dim == -2)
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2651 {
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2652 concat_rule = &dim_vector::hvcat;
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2653 dim = -dim - 1;
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2654 }
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2655 else if (dim < 0)
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2656 (*current_liboctave_error_handler) ("cat: invalid dimension");
10531
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2657
10535
3f973f6c841c improve sparse concatenation operator
Jaroslav Hajek <highegg@gmail.com>
parents: 10533
diff changeset
2658 if (n == 1)
3f973f6c841c improve sparse concatenation operator
Jaroslav Hajek <highegg@gmail.com>
parents: 10533
diff changeset
2659 return array_list[0];
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10674
diff changeset
2660 else if (n == 0)
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2661 return Array<T, Alloc> ();
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10674
diff changeset
2662
11241
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2663 // Special case:
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2664 //
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2665 // cat (dim, [], ..., [], A, ...)
11241
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2666 //
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2667 // with dim > 2, A not 0x0, and at least three arguments to
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2668 // concatenate is equivalent to
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2669 //
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2670 // cat (dim, A, ...)
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2671 //
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2672 // Note that this check must be performed here because for full-on
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2673 // braindead Matlab compatibility, we need to have things like
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2674 //
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2675 // cat (3, [], [], A)
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2676 //
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2677 // succeed, but to have things like
11241
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2678 //
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2679 // cat (3, cat (3, [], []), A)
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2680 // cat (3, zeros (0, 0, 2), A)
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2681 //
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2682 // fail. See also bug report #31615.
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2683
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2684 octave_idx_type istart = 0;
11241
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2685
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2686 if (n > 2 && dim > 1)
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2687 {
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2688 for (octave_idx_type i = 0; i < n; i++)
11241
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2689 {
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2690 dim_vector dv = array_list[i].dims ();
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2691
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2692 if (dv.zero_by_zero ())
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2693 istart++;
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2694 else
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2695 break;
11241
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2696 }
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2697
27956
2310164737b3 fix many spelling errors (bug #57613)
John W. Eaton <jwe@octave.org>
parents: 27323
diff changeset
2698 // Don't skip any initial arguments if they are all empty.
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2699 if (istart >= n)
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2700 istart = 0;
11241
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2701 }
80e01d79cf80 special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11210
diff changeset
2702
11242
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2703 dim_vector dv = array_list[istart++].dims ();
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2704
0090bb47d0b5 simplify special case for concatenation of empty matrices
John W. Eaton <jwe@octave.org>
parents: 11241
diff changeset
2705 for (octave_idx_type i = istart; i < n; i++)
10716
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2706 if (! (dv.*concat_rule) (array_list[i].dims (), dim))
21136
7cac4e7458f2 maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents: 21131
diff changeset
2707 (*current_liboctave_error_handler) ("cat: dimension mismatch");
10531
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2708
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2709 Array<T, Alloc> retval (dv);
10716
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2710
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23487
diff changeset
2711 if (retval.isempty ())
10716
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2712 return retval;
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2713
21827
2781d6efa88f Fix build error with --enable-64
Mike Miller <mtmiller@octave.org>
parents: 21785
diff changeset
2714 int nidx = std::max (dv.ndims (), static_cast<octave_idx_type> (dim + 1));
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
2715 Array<octave::idx_vector> idxa (dim_vector (nidx, 1), octave::idx_vector::colon);
10531
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2716 octave_idx_type l = 0;
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2717
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2718 for (octave_idx_type i = 0; i < n; i++)
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2719 {
10716
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2720 // NOTE: This takes some thinking, but no matter what the above rules
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2721 // are, an empty array can always be skipped at this point, because
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2722 // the result dimensions are already determined, and there is no way
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2723 // an empty array may contribute a nonzero piece along the dimension
f7f26094021b improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10703
diff changeset
2724 // at this point, unless an empty array can be promoted to a non-empty
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 21724
diff changeset
2725 // one (which makes no sense). I repeat, *no way*, think about it.
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23487
diff changeset
2726 if (array_list[i].isempty ())
10533
f094ac9bc93e reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents: 10531
diff changeset
2727 continue;
f094ac9bc93e reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents: 10531
diff changeset
2728
f094ac9bc93e reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents: 10531
diff changeset
2729 octave_quit ();
f094ac9bc93e reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents: 10531
diff changeset
2730
10531
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2731 octave_idx_type u;
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2732 if (dim < array_list[i].ndims ())
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2733 u = l + array_list[i].dims ()(dim);
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2734 else
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2735 u = l + 1;
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2736
29569
29a1f8fd8ee6 move idx_vector classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29419
diff changeset
2737 idxa(dim) = octave::idx_vector (l, u);
10531
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2738
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2739 retval.assign (idxa, array_list[i]);
10533
f094ac9bc93e reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents: 10531
diff changeset
2740
f094ac9bc93e reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents: 10531
diff changeset
2741 l = u;
10531
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2742 }
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2743
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2744 return retval;
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2745 }
2dd8ea8bfd71 basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents: 10512
diff changeset
2746
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2747 template <typename T, typename Alloc>
4517
b4449b1193ac [project @ 2003-09-20 02:06:06 by jwe]
jwe
parents: 4513
diff changeset
2748 void
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2749 Array<T, Alloc>::print_info (std::ostream& os, const std::string& prefix) const
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3657
diff changeset
2750 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2751 os << prefix << "m_rep address: " << m_rep << '\n'
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2752 << prefix << "m_rep->m_len: " << m_rep->m_len << '\n'
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2753 << prefix << "m_rep->m_data: " << static_cast<void *> (m_rep->m_data) << '\n'
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2754 << prefix << "m_rep->m_count: " << m_rep->m_count << '\n'
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2755 << prefix << "m_slice_data: " << static_cast<void *> (m_slice_data) << '\n'
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2756 << prefix << "m_slice_len: " << m_slice_len << '\n';
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
2757
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
2758 // 2D info:
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
2759 //
4657
a585a2dfca6c [project @ 2003-11-25 01:30:09 by jwe]
jwe
parents: 4656
diff changeset
2760 // << pefix << "rows: " << rows () << "\n"
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4192
diff changeset
2761 // << prefix << "cols: " << cols () << "\n";
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3657
diff changeset
2762 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3657
diff changeset
2763
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2764 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2765 bool Array<T, Alloc>::optimize_dimensions (const dim_vector& dv)
10674
e3064439d6b4 new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents: 10673
diff changeset
2766 {
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2767 bool retval = m_dimensions == dv;
10674
e3064439d6b4 new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents: 10673
diff changeset
2768 if (retval)
29887
be669d935fb6 use m_prefix for data members in array classes
John W. Eaton <jwe@octave.org>
parents: 29722
diff changeset
2769 m_dimensions = dv;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
2770
10674
e3064439d6b4 new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents: 10673
diff changeset
2771 return retval;
e3064439d6b4 new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents: 10673
diff changeset
2772 }
e3064439d6b4 new Array method for internal use
Jaroslav Hajek <highegg@gmail.com>
parents: 10673
diff changeset
2773
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2774 template <typename T, typename Alloc>
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2775 void Array<T, Alloc>::instantiation_guard ()
9201
472f0e22aa60 guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents: 9121
diff changeset
2776 {
472f0e22aa60 guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents: 9121
diff changeset
2777 // This guards against accidental implicit instantiations.
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2778 // Array<T, Alloc> instances should always be explicit and use INSTANTIATE_ARRAY.
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
2779 T::__xXxXx__ ();
9201
472f0e22aa60 guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents: 9121
diff changeset
2780 }
472f0e22aa60 guard against implicit instantiation
Jaroslav Hajek <highegg@gmail.com>
parents: 9121
diff changeset
2781
31400
e7fc6251b698 Use more specialized API flags for template classes (bug #61711).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31396
diff changeset
2782 #define INSTANTIATE_ARRAY(T, API) \
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2783 template <> API void \
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2784 Array<T>::instantiation_guard () { } \
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2785 \
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2786 template class API Array<T>
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
2787
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2788 // FIXME: is this used?
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2789
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2790 template <typename T, typename Alloc>
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2791 std::ostream&
30486
298995435071 allow custom allocators for the Array class
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
2792 operator << (std::ostream& os, const Array<T, Alloc>& a)
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2793 {
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2794 dim_vector a_dims = a.dims ();
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2795
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21574
diff changeset
2796 int n_dims = a_dims.ndims ();
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2797
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2798 os << n_dims << "-dimensional array";
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2799
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2800 if (n_dims)
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23800
diff changeset
2801 os << " (" << a_dims.str () << ')';
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2802
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2803 os <<"\n\n";
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2804
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2805 if (n_dims)
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2806 {
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2807 os << "data:";
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2808
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
2809 Array<octave_idx_type> ra_idx (dim_vector (n_dims, 1), 0);
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2810
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2811 // Number of times the first 2d-array is to be displayed.
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2812
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2813 octave_idx_type m = 1;
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2814 for (int i = 2; i < n_dims; i++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2815 m *= a_dims(i);
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2816
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2817 if (m == 1)
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2818 {
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2819 octave_idx_type rows = 0;
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2820 octave_idx_type cols = 0;
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2821
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2822 switch (n_dims)
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2823 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2824 case 2:
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2825 rows = a_dims(0);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2826 cols = a_dims(1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2827
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2828 for (octave_idx_type j = 0; j < rows; j++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2829 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2830 ra_idx(0) = j;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2831 for (octave_idx_type k = 0; k < cols; k++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2832 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2833 ra_idx(1) = k;
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23800
diff changeset
2834 os << ' ' << a.elem (ra_idx);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2835 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2836 os << "\n";
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2837 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2838 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2839
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2840 default:
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2841 rows = a_dims(0);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2842
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2843 for (octave_idx_type k = 0; k < rows; k++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2844 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2845 ra_idx(0) = k;
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23800
diff changeset
2846 os << ' ' << a.elem (ra_idx);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2847 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2848 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2849 }
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2850
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2851 os << "\n";
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2852 }
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2853 else
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2854 {
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2855 octave_idx_type rows = a_dims(0);
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2856 octave_idx_type cols = a_dims(1);
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2857
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2858 for (int i = 0; i < m; i++)
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2859 {
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2860 os << "\n(:,:,";
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2861
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2862 for (int j = 2; j < n_dims - 1; j++)
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23800
diff changeset
2863 os << ra_idx(j) + 1 << ',';
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2864
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2865 os << ra_idx(n_dims - 1) + 1 << ") = \n";
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2866
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2867 for (octave_idx_type j = 0; j < rows; j++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2868 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2869 ra_idx(0) = j;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2870
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2871 for (octave_idx_type k = 0; k < cols; k++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2872 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2873 ra_idx(1) = k;
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23800
diff changeset
2874 os << ' ' << a.elem (ra_idx);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2875 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2876
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2877 os << "\n";
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2878 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2879
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2880 os << "\n";
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2881
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2882 if (i != m - 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10270
diff changeset
2883 increment_index (ra_idx, a_dims, 2);
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2884 }
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2885 }
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2886 }
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2887
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2888 return os;
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
2889 }