annotate liboctave/dim-vector.h @ 13985:43cc49c7abd1

Use thread-safe atomic reference counting (GCC and MSVC). * configure.ac: New --enable-atomic-refcount argument. (octave_allocator): Fix USE_OCTAVE_ALLOCATOR variable assignment. (OCTAVE_CONFIG_INCLUDED): New macro in config.h. * oct-refcount.h (OCTREFCOUNT_ATOMIC_INCREMENT, OCTREFCOUNT_ATOMIC_INCREMENT_POST, OCTREFCOUNT_ATOMIC_DECREMENT, OCTREFCOUNT_ATOMIC_DECREMENT_POST): New macro, defined for MSVC and GCC when USE_ATOMIC_REFCOUNT is defined. (octave_refcount:operator++, octave_refcount::operator--): Use them. (octave_refcount::operator count_type): Cast returned value to volatile. (octave_refcount::direct): Remove unsafe member. * Array.h (Array::make_unique): Delete rep if refcount reaches 0. * Sparse.h (Sparse::make_unique): Delete rep if refcount reaches 0. * Array.h (Array:~Array, Array::operator=): Delete rep only when refcount is excatly 0. * Array.cc (Array::clear): Likewise. * Sparse.cc (Sparse::~Sparse, Sparse::operator=): Likewise. * SparseCmplxQR.h (SparseCmplxQR::~SparseCmplxQR, SparseCmplxQR::operator=): Likewise. * SparseQR.h (SparseQR::~SparseQR, SparseQR::operator=): Likewise. * sparse-base-chol.h (sparse_base_chol::~sparse_base_chol, sparse_base_chol::operator): Likewise. * dim-vector.h (oct-refcount.h): New included header. (dim_vector::make_unique, dim_vector::resize): Use OCTREFCOUNT_ATOMIC_DECREMENT macro and delete rep when refcount reaches 0. (dim_vector::dim_vector): Use OCTREFCOUNT_ATOMIC_INCREMENT. (dim_vector::operator=): Use OCTREFCOUNT_ATOMIC_INCREMENT and OCTREFCOUNT_ATOMIC_DECREMENT. (dim_vector::~dim_vector): Use OCTREFCOUNT_ATOMIC_DECREMENT. * oct-mutex.h (oct-refcount.h): New included header. (octave_base_mutex::count): Use octave_refcount class. * gl-render.cc (oct-refcount.h): New included header. * graphics.h.in (oct-refcount.h): Likewise. (base_property::count, base_graphics_toolkit::count, base_graphics_object::count, base_graphics_event::count): Use octave_refcount. (property::~property, property::operator=): Delete rep only when refcountn is excatly 0. * oct-map.h (octave_fields::make_unique): Delete rep when refcount reaches 0. * oct-stream.h (oct-refcount.h): New included header. (octave_base_stream::count): Use octave_refcount class. * ov.h (octave_value::make_unique): Delete rep when refcount reaches 0. * symtab.h (oct-refcount.h): New included header. (symbol_record_rep::count, fcn_info_rep::count): Use octave_refcount class. * DLD-FUNCTIONS/urlwrite.cc (oct-refcount.h): New included header. (curl_handle_rep::count): Use octave_refcount class.
author Michael Goffioul <michael.goffioul@gmail.com>
date Sat, 03 Dec 2011 15:19:42 +0000
parents 12df7854fa7c
children 72c96de7a403
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
1 /*
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
2
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11173
diff changeset
3 Copyright (C) 2003-2011 John W. Eaton
10521
4d1fc073fbb7 add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents: 10498
diff changeset
4 Copyirght (C) 2009, 2010 VZLU Prague
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
5
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
7
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6216
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6216
diff changeset
11 option) any later version.
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
12
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
16 for more details.
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
17
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6216
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6216
diff changeset
20 <http://www.gnu.org/licenses/>.
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
21
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
22 */
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
23
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
24 #if !defined (octave_dim_vector_h)
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
25 #define octave_dim_vector_h 1
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
26
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
27 #include <cassert>
9840
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
28 #include <limits>
8950
d865363208d6 include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents: 8920
diff changeset
29
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5307
diff changeset
30 #include <sstream>
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
31 #include <string>
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
32
6216
74ca56e4414c [project @ 2006-12-22 17:45:47 by jwe]
jwe
parents: 5765
diff changeset
33 #include "lo-error.h"
10419
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
34 #include "lo-macros.h"
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
35 #include "oct-refcount.h"
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
36
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
37 // Rationale: This implementation is more tricky than Array, but the
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
38 // big plus is that dim_vector requires only one allocation instead of
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
39 // two. It is (slightly) patterned after GCC's basic_string
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
40 // implementation. rep is a pointer to an array of memory, comprising
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
41 // count, length, and the data:
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
42 //
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
43 // <count>
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
44 // <ndims>
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
45 // rep --> <dims[0]>
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
46 // <dims[1]>
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
47 // ...
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
48 //
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
49 // The inlines count(), ndims() recover this data from the rep. Note
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
50 // that rep points to the beginning of dims to grant faster access
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
51 // (reinterpret_cast is assumed to be an inexpensive operation).
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
52
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
53 class
11173
298a75c128ad Additional exported symbols [MSVC]
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10830
diff changeset
54 OCTAVE_API
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
55 dim_vector
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
56 {
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
57 private:
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
58
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
59 octave_idx_type *rep;
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
60
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
61 octave_idx_type& ndims (void) const { return rep[-1]; }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
62
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
63 octave_idx_type& count (void) const { return rep[-2]; }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
64
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
65 // Construct a new rep with count = 1 and ndims given.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
66
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
67 static octave_idx_type *newrep (int ndims)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
68 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
69 octave_idx_type *r = new octave_idx_type[ndims + 2];
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
70
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
71 *r++ = 1;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
72 *r++ = ndims;
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
73
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
74 return r;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
75 }
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
76
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
77 // Clone this->rep.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
78
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
79 octave_idx_type *clonerep (void)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
80 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
81 int l = ndims ();
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
82
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
83 octave_idx_type *r = new octave_idx_type[l + 2];
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
84
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
85 *r++ = 1;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
86 *r++ = l;
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
87
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
88 for (int i = 0; i < l; i++)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
89 r[i] = rep[i];
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
90
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
91 return r;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
92 }
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
93
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
94 // Clone and resize this->rep to length n, filling by given value.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
95
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
96 octave_idx_type *resizerep (int n, octave_idx_type fill_value)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
97 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
98 int l = ndims ();
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
99
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
100 if (n < 2)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
101 n = 2;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
102
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
103 octave_idx_type *r = new octave_idx_type[n + 2];
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
104
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
105 *r++ = 1;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
106 *r++ = n;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
107
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
108 if (l > n)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
109 l = n;
4673
6639c2574023 [project @ 2003-12-10 16:11:54 by jwe]
jwe
parents: 4655
diff changeset
110
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
111 int j;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
112 for (j = 0; j < l; j++)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
113 r[j] = rep[j];
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
114 for (; j < n; j++)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
115 r[j] = fill_value;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
116
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
117 return r;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
118 }
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
119
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
120 // Free the rep.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
121
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
122 void freerep (void)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
123 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
124 assert (count () == 0);
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
125 delete [] (rep - 2);
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
126 }
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
127
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
128 void make_unique (void)
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
129 {
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
130 if (count () > 1)
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
131 {
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
132 octave_idx_type *new_rep = clonerep ();
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
133
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
134 if (OCTREFCOUNT_ATOMIC_DECREMENT(&(count())) == 0)
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
135 freerep ();
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
136
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
137 rep = new_rep;
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
138 }
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
139 }
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
140
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
141 public:
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
142
10420
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
143 // The constructor
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
144 //
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
145 // dim_vector (n)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
146 //
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
147 // creates an dimension vector with N rows and 1 column. It is
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
148 // deprecated because of the potentiol for confusion that it causes.
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
149 // Additional constructors of the form
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
150 //
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
151 // dim_vector (r, c)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
152 // dim_vector (r, c, p)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
153 // dim_vector (d1, d2, d3, d4, ...)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
154 //
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
155 // are available for up to 7 dimensions.
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
156
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
157 explicit dim_vector (octave_idx_type n) GCC_ATTR_DEPRECATED
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
158 : rep (newrep (2))
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
159 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
160 rep[0] = n;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
161 rep[1] = 1;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
162 }
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
163
10419
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
164 #define ASSIGN_REP(i) rep[i] = d ## i;
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
165 #define DIM_VECTOR_CTOR(N) \
10420
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
166 dim_vector (OCT_MAKE_DECL_LIST (octave_idx_type, d, N)) \
10419
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
167 : rep (newrep (N)) \
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
168 { \
10420
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
169 OCT_ITERATE_MACRO (ASSIGN_REP, N) \
10418
6c19d6fcd7e5 up to 7th-order dim_vector constructors
Jaroslav Hajek <highegg@gmail.com>
parents: 10403
diff changeset
170 }
10419
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
171
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
172 // Add more if needed.
10420
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
173 DIM_VECTOR_CTOR (2)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
174 DIM_VECTOR_CTOR (3)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
175 DIM_VECTOR_CTOR (4)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
176 DIM_VECTOR_CTOR (5)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
177 DIM_VECTOR_CTOR (6)
3373fdc0b14a use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents: 10419
diff changeset
178 DIM_VECTOR_CTOR (7)
10419
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
179
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
180 #undef ASSIGN_REP
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
181 #undef DIM_VECTOR_CTOR
afe44ee90cbd implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents: 10418
diff changeset
182
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
183 octave_idx_type& elem (int i)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
184 {
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
185 #ifdef BOUNDS_CHECKING
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
186 assert (i >= 0 && i < ndims ());
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
187 #endif
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
188 make_unique ();
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
189 return rep[i];
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
190 }
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
191
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
192 octave_idx_type elem (int i) const
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
193 {
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
194 #ifdef BOUNDS_CHECKING
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
195 assert (i >= 0 && i < ndims ());
10366
e5ae13b8b2c2 improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents: 10350
diff changeset
196 #endif
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
197 return rep[i];
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
198 }
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
199
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
200 void chop_trailing_singletons (void)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
201 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
202 int l = ndims ();
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
203 if (l > 2 && rep[l-1] == 1)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
204 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
205 make_unique ();
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
206 do
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
207 l--;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
208 while (l > 2 && rep[l-1] == 1);
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
209 ndims () = l;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
210 }
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
211 }
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
212
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10645
diff changeset
213 void chop_all_singletons (void);
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
214
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
215 private:
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
216
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
217 static octave_idx_type *nil_rep (void)
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
218 {
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
219 static dim_vector zv (0, 0);
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
220 return zv.rep;
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
221 }
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
222
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
223 explicit dim_vector (octave_idx_type *r)
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
224 : rep (r) { }
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
225
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
226 public:
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
227
10830
b4ebfd675321 avoid static initialization disaster in dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 10810
diff changeset
228 static octave_idx_type dim_max (void);
10810
6683f0c9d742 make the maximum extent externally accessible
Jaroslav Hajek <highegg@gmail.com>
parents: 10715
diff changeset
229
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
230 explicit dim_vector (void) : rep (nil_rep ())
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
231 { OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); }
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
232
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
233 dim_vector (const dim_vector& dv) : rep (dv.rep)
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
234 { OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); }
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
235
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
236 static dim_vector alloc (int n)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
237 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
238 return dim_vector (newrep (n < 2 ? 2 : n));
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
239 }
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
240
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
241 dim_vector& operator = (const dim_vector& dv)
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
242 {
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
243 if (&dv != this)
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
244 {
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
245 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0)
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
246 freerep ();
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
247
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
248 rep = dv.rep;
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
249 OCTREFCOUNT_ATOMIC_INCREMENT (&(count()));
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
250 }
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
251
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
252 return *this;
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
253 }
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
254
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
255 ~dim_vector (void)
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
256 {
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
257 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0)
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
258 freerep ();
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
259 }
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
260
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
261 int length (void) const { return ndims (); }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
262
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
263 octave_idx_type& operator () (int i) { return elem (i); }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
264
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
265 octave_idx_type operator () (int i) const { return elem (i); }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
266
4887
bbddd4339cf2 [project @ 2004-04-30 17:08:55 by jwe]
jwe
parents: 4735
diff changeset
267 void resize (int n, int fill_value = 0)
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
268 {
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
269 int len = length ();
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
270
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
271 if (n != len)
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
272 {
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
273 octave_idx_type *r = resizerep (n, fill_value);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
274
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 11586
diff changeset
275 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0)
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
276 freerep ();
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
277
9507
b096d11237be dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents: 9420
diff changeset
278 rep = r;
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
279 }
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
280 }
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
281
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10645
diff changeset
282 std::string str (char sep = 'x') const;
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
283
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
284 bool all_zero (void) const
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
285 {
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
286 bool retval = true;
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
287
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
288 for (int i = 0; i < length (); i++)
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
289 {
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
290 if (elem (i) != 0)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
291 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
292 retval = false;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
293 break;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
294 }
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
295 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
296
4548
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
297 return retval;
820323598f4f [project @ 2003-10-26 03:16:42 by jwe]
jwe
parents: 4543
diff changeset
298 }
4559
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
299
10715
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
300 bool empty_2d (void) const
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
301 {
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
302 return length () == 2 && (elem (0) == 0 || elem (1) == 0);
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
303 }
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
304
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
305
9886
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
306 bool zero_by_zero (void) const
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
307 {
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
308 return length () == 2 && elem (0) == 0 && elem (1) == 0;
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
309 }
cddd5c3d5f04 fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 9840
diff changeset
310
4559
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
311 bool any_zero (void) const
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
312 {
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
313 bool retval = false;
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
314
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
315 for (int i = 0; i < length (); i++)
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
316 {
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
317 if (elem (i) == 0)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
318 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
319 retval = true;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
320 break;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
321 }
4559
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
322 }
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
323
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
324 return retval;
802818bfac91 [project @ 2003-10-28 19:13:43 by jwe]
jwe
parents: 4548
diff changeset
325 }
4567
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
326
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10645
diff changeset
327 int num_ones (void) const;
4635
f2cd320cbf6e [project @ 2003-11-20 01:48:23 by jwe]
jwe
parents: 4567
diff changeset
328
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
329 bool all_ones (void) const
4655
c8829691db47 [project @ 2003-11-24 21:24:37 by jwe]
jwe
parents: 4635
diff changeset
330 {
c8829691db47 [project @ 2003-11-24 21:24:37 by jwe]
jwe
parents: 4635
diff changeset
331 return (num_ones () == length ());
c8829691db47 [project @ 2003-11-24 21:24:37 by jwe]
jwe
parents: 4635
diff changeset
332 }
c8829691db47 [project @ 2003-11-24 21:24:37 by jwe]
jwe
parents: 4635
diff changeset
333
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
334 // Return the number of elements that a matrix with this dimension
4567
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
335 // vector would have, NOT the number of dimensions (elements in the
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
336 // dimension vector).
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
337
9027
9a46ba093db4 generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents: 8950
diff changeset
338 octave_idx_type numel (int n = 0) const
4567
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
339 {
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
340 int n_dims = length ();
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
341
9027
9a46ba093db4 generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents: 8950
diff changeset
342 octave_idx_type retval = 1;
4567
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
343
9027
9a46ba093db4 generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents: 8950
diff changeset
344 for (int i = n; i < n_dims; i++)
4567
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
345 retval *= elem (i);
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
346
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
347 return retval;
fc30061d01da [project @ 2003-10-29 20:11:15 by jwe]
jwe
parents: 4565
diff changeset
348 }
4673
6639c2574023 [project @ 2003-12-10 16:11:54 by jwe]
jwe
parents: 4655
diff changeset
349
9840
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
350 // The following function will throw a std::bad_alloc ()
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
351 // exception if the requested size is larger than can be indexed by
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
352 // octave_idx_type. This may be smaller than the actual amount of
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
353 // memory that can be safely allocated on a system. However, if we
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
354 // don't fail here, we can end up with a mysterious crash inside a
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
355 // function that is iterating over an array using octave_idx_type
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
356 // indices.
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
357
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10645
diff changeset
358 octave_idx_type safe_numel (void) const;
9840
c0b54271904b improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9743
diff changeset
359
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
360 bool any_neg (void) const
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
361 {
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
362 int n_dims = length ();
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
363 int i;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
364
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
365 for (i = 0; i < n_dims; i++)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
366 if (elem (i) < 0)
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
367 break;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
368
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
369 return i < n_dims;
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
370 }
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
371
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10645
diff changeset
372 dim_vector squeeze (void) const;
10498
8615b55b5caf fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents: 10420
diff changeset
373
10715
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
374 // This corresponds to cat().
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
375 bool concat (const dim_vector& dvb, int dim);
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
376
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
377 // This corresponds to [,] (horzcat, dim = 0) and [;] (vertcat, dim = 1).
10715
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
378 // The rules are more relaxed here.
53253f796351 make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10681
diff changeset
379 bool hvcat (const dim_vector& dvb, int dim);
8290
7cbe01c21986 improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
380
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
381 // Force certain dimensionality, preserving numel (). Missing
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
382 // dimensions are set to 1, redundant are folded into the trailing
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
383 // one. If n = 1, the result is 2d and the second dim is 1
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
384 // (dim_vectors are always at least 2D). If the original dimensions
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
385 // were all zero, the padding value is zero.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
386
10681
0ba9bd294421 make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10645
diff changeset
387 dim_vector redim (int n) const;
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8290
diff changeset
388
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
389 dim_vector as_column (void) const
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
390 {
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
391 if (length () == 2 && elem (1) == 1)
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
392 return *this;
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
393 else
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
394 return dim_vector (numel (), 1);
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
395 }
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
396
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
397 dim_vector as_row (void) const
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
398 {
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
399 if (length () == 2 && elem (0) == 1)
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
400 return *this;
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
401 else
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
402 return dim_vector (1, numel ());
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
403 }
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10150
diff changeset
404
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8290
diff changeset
405 bool is_vector (void) const
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8290
diff changeset
406 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8290
diff changeset
407 return (length () == 2 && (elem (0) == 1 || elem (1) == 1));
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8290
diff changeset
408 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8290
diff changeset
409
9511
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
410 int first_non_singleton (int def = 0) const
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
411 {
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
412 for (int i = 0; i < length (); i++)
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
413 {
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
414 if (elem (i) != 1)
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
415 return i;
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
416 }
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
417
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
418 return def;
9511
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
419 }
cc1fd3084cb2 implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents: 9507
diff changeset
420
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
421 // Compute a linear index from an index tuple.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
422
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
423 octave_idx_type compute_index (const octave_idx_type *idx) const
9739
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
424 {
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
425 octave_idx_type k = 0;
9743
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9739
diff changeset
426 for (int i = length () - 1; i >= 0; i--)
9739
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
427 k = k * rep[i] + idx[i];
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
428
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
429 return k;
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
430 }
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
431
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
432 // Ditto, but the tuple may be incomplete (nidx < length ()).
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
433
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
434 octave_idx_type compute_index (const octave_idx_type *idx, int nidx) const
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
435 {
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
436 octave_idx_type k = 0;
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
437 for (int i = nidx - 1; i >= 0; i--)
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
438 k = k * rep[i] + idx[i];
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
439
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
440 return k;
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
441 }
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
442
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
443 // Increment a multi-dimensional index tuple, optionally starting
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
444 // from an offset position and return the index of the last index
9739
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
445 // position that was changed, or length () if just cycled over.
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
446
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
447 int increment_index (octave_idx_type *idx, int start = 0) const
9739
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
448 {
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
449 int i;
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
450 for (i = start; i < length (); i++)
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
451 {
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
452 if (++(*idx) == rep[i])
9743
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9739
diff changeset
453 *idx++ = 0;
9739
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
454 else
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
455 break;
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
456 }
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
457 return i;
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
458 }
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
459
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
460 // Return cumulative dimensions.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
461
9739
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
462 dim_vector cumulative (void) const
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
463 {
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
464 int nd = length ();
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
465 dim_vector retval = alloc (nd);
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
466
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
467 octave_idx_type k = 1;
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
468 for (int i = 0; i < nd; i++)
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
469 retval.rep[i] = k *= rep[i];
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
470
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
471 return retval;
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
472 }
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
473
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
474 // Compute a linear index from an index tuple. Dimensions are
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
475 // required to be cumulative.
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
476
10645
8645b7087859 abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents: 10586
diff changeset
477 octave_idx_type cum_compute_index (const octave_idx_type *idx) const
9739
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
478 {
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
479 octave_idx_type k = idx[0];
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
480
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
481 for (int i = 1; i < length (); i++)
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
482 k += rep[i-1] * idx[i];
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
483
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
484 return k;
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
485 }
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
486
13b57eec9440 a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents: 9694
diff changeset
487
9694
50db3c5175b5 allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents: 9666
diff changeset
488 friend bool operator == (const dim_vector& a, const dim_vector& b);
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
489 };
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
490
9694
50db3c5175b5 allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents: 9666
diff changeset
491 inline bool
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
492 operator == (const dim_vector& a, const dim_vector& b)
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
493 {
9694
50db3c5175b5 allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents: 9666
diff changeset
494 // Fast case.
50db3c5175b5 allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents: 9666
diff changeset
495 if (a.rep == b.rep)
50db3c5175b5 allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents: 9666
diff changeset
496 return true;
50db3c5175b5 allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents: 9666
diff changeset
497
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
498 bool retval = true;
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
499
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
500 int a_len = a.length ();
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
501 int b_len = b.length ();
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
502
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
503 if (a_len != b_len)
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
504 retval = false;
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
505 else
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
506 {
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
507 for (int i = 0; i < a_len; i++)
10150
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
508 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
509 if (a(i) != b(i))
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
510 {
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
511 retval = false;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
512 break;
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
513 }
3778ea95adea style fixes
John W. Eaton <jwe@octave.org>
parents: 10095
diff changeset
514 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
515 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
516
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
517 return retval;
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
518 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
519
9694
50db3c5175b5 allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents: 9666
diff changeset
520 inline bool
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
521 operator != (const dim_vector& a, const dim_vector& b)
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
522 {
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
523 return ! operator == (a, b);
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
524 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4532
diff changeset
525
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents:
diff changeset
526 #endif