Mercurial > hg > octave-nkf
annotate liboctave/Range.h @ 10396:a0b51ac0f88a
optimize accumdim with summation
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 05 Mar 2010 12:31:30 +0100 |
parents | cbc402e64d83 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
3 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2002, 2004, 2005, |
8920 | 4 2006, 2007, 2008, 2009 John W. Eaton |
3 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3 | 21 |
22 */ | |
23 | |
382 | 24 #if !defined (octave_Range_h) |
25 #define octave_Range_h 1 | |
26 | |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
27 #include <iosfwd> |
7458 | 28 |
4810 | 29 #include "dMatrix.h" |
7458 | 30 #include "oct-sort.h" |
3 | 31 |
1860 | 32 class |
6108 | 33 OCTAVE_API |
1860 | 34 Range |
3 | 35 { |
36 public: | |
1860 | 37 |
1528 | 38 Range (void) |
5347 | 39 : rng_base (0), rng_limit (0), rng_inc (0), rng_nelem (0), cache (1, 0) { } |
1528 | 40 |
41 Range (const Range& r) | |
1860 | 42 : rng_base (r.rng_base), rng_limit (r.rng_limit), rng_inc (r.rng_inc), |
8971 | 43 rng_nelem (r.rng_nelem), cache (r.cache) { } |
3 | 44 |
1528 | 45 Range (double b, double l) |
1860 | 46 : rng_base (b), rng_limit (l), rng_inc (1), |
4811 | 47 rng_nelem (nelem_internal ()), cache () { } |
1528 | 48 |
49 Range (double b, double l, double i) | |
1860 | 50 : rng_base (b), rng_limit (l), rng_inc (i), |
4811 | 51 rng_nelem (nelem_internal ()), cache () { } |
1528 | 52 |
8589
0131fa223dbc
make length invariant in range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8553
diff
changeset
|
53 // For operators' usage (to preserve element count). |
8971 | 54 Range (double b, double i, octave_idx_type n); |
8589
0131fa223dbc
make length invariant in range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8553
diff
changeset
|
55 |
4811 | 56 double base (void) const { return rng_base; } |
1528 | 57 double limit (void) const { return rng_limit; } |
4811 | 58 double inc (void) const { return rng_inc; } |
5275 | 59 octave_idx_type nelem (void) const { return rng_nelem; } |
3 | 60 |
2383 | 61 bool all_elements_are_ints (void) const; |
62 | |
645 | 63 Matrix matrix_value (void) const; |
64 | |
3 | 65 double min (void) const; |
66 double max (void) const; | |
67 | |
7458 | 68 void sort_internal (bool ascending = true); |
69 void sort_internal (Array<octave_idx_type>& sidx, bool ascending = true); | |
70 | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
71 Matrix diag (octave_idx_type k = 0) const; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
72 |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
73 Range sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; |
7458 | 74 |
75 Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0, | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
76 sortmode mode = ASCENDING) const; |
208 | 77 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
78 sortmode is_sorted (sortmode mode = ASCENDING) const; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
79 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
80 // Support for single-index subscripting, without generating matrix cache. |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
81 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
82 double checkelem (octave_idx_type i) const; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
83 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
84 double elem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
85 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
86 #if defined (BOUNDS_CHECKING) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
87 return checkelem (i); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
88 #else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
89 return rng_base + rng_inc * i; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
90 #endif |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
91 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
92 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
93 Array<double> index (const idx_vector& i) const; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
94 |
4811 | 95 void set_base (double b) |
96 { | |
97 if (rng_base != b) | |
98 { | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
99 rng_base = b; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
100 clear_cache (); |
4811 | 101 } |
102 } | |
103 | |
104 void set_limit (double l) | |
105 { | |
106 if (rng_limit != l) | |
107 { | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
108 rng_limit = l; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
109 clear_cache (); |
4811 | 110 } |
111 } | |
112 | |
113 void set_inc (double i) | |
114 { | |
115 if (rng_inc != i) | |
116 { | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
117 rng_inc = i; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
118 clear_cache (); |
4811 | 119 } |
120 } | |
1528 | 121 |
6108 | 122 friend OCTAVE_API std::ostream& operator << (std::ostream& os, const Range& r); |
123 friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r); | |
3 | 124 |
8971 | 125 friend OCTAVE_API Range operator - (const Range& r); |
126 friend OCTAVE_API Range operator + (double x, const Range& r); | |
127 friend OCTAVE_API Range operator + (const Range& r, double x); | |
128 friend OCTAVE_API Range operator - (double x, const Range& r); | |
129 friend OCTAVE_API Range operator - (const Range& r, double x); | |
130 friend OCTAVE_API Range operator * (double x, const Range& r); | |
131 friend OCTAVE_API Range operator * (const Range& r, double x); | |
132 | |
3 | 133 void print_range (void); |
134 | |
135 private: | |
1860 | 136 |
208 | 137 double rng_base; |
138 double rng_limit; | |
139 double rng_inc; | |
1860 | 140 |
5275 | 141 octave_idx_type rng_nelem; |
3 | 142 |
4811 | 143 mutable Matrix cache; |
144 | |
5275 | 145 octave_idx_type nelem_internal (void) const; |
4811 | 146 |
147 void clear_cache (void) const { cache.resize (0, 0); } | |
8971 | 148 |
3 | 149 }; |
150 | |
6108 | 151 extern OCTAVE_API Range operator - (const Range& r); |
2599 | 152 |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
153 extern OCTAVE_API Range operator + (double x, const Range& r); |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
154 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
155 extern OCTAVE_API Range operator + (const Range& r, double x); |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
156 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
157 extern OCTAVE_API Range operator - (double x, const Range& r); |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
158 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
159 extern OCTAVE_API Range operator - (const Range& r, double x); |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
160 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
161 extern OCTAVE_API Range operator * (double x, const Range& r); |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
162 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
163 extern OCTAVE_API Range operator * (const Range& r, double x); |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
164 |
3 | 165 #endif |