Mercurial > hg > octave-nkf
annotate liboctave/idx-vector.h @ 7904:1fddd9b8e862
Fix nil_rep reference counting in gl-render.cc::vertex_data internal class
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 09 Jul 2008 10:59:29 -0400 |
parents | 755bf7ecc29b |
children | 7cbe01c21986 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2003, |
4 2004, 2005, 2006, 2007 John W. Eaton | |
1 | 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. | |
1 | 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/>. | |
1 | 21 |
22 */ | |
23 | |
383 | 24 #if !defined (octave_idx_vector_h) |
25 #define octave_idx_vector_h 1 | |
1 | 26 |
3503 | 27 #include <iostream> |
28 | |
4653 | 29 #include "dim-vector.h" |
4938 | 30 #include "oct-inttypes.h" |
31 #include "intNDArray.h" | |
4504 | 32 |
1560 | 33 class ColumnVector; |
4650 | 34 class boolNDArray; |
35 class NDArray; | |
164 | 36 class Range; |
37 | |
453 | 38 class |
6108 | 39 OCTAVE_API |
453 | 40 idx_vector |
1 | 41 { |
1560 | 42 private: |
43 | |
1879 | 44 class |
6108 | 45 OCTAVE_API |
1560 | 46 idx_vector_rep |
1879 | 47 { |
48 public: | |
49 | |
2802 | 50 idx_vector_rep (void) |
7422 | 51 : data (0), len (0), num_zeros (0), num_ones (0), |
52 range_base (0), range_step (0), max_val (0), | |
4653 | 53 min_val (0), count (1), frozen_at_z_len (0), frozen_len (0), |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7422
diff
changeset
|
54 colon (0), range(0), initialized (0), frozen (0), |
4653 | 55 colon_equiv_checked (0), colon_equiv (0), orig_dims () { } |
1560 | 56 |
1879 | 57 idx_vector_rep (const ColumnVector& v); |
1560 | 58 |
4650 | 59 idx_vector_rep (const NDArray& nda); |
1879 | 60 |
4938 | 61 template <class U> |
62 idx_vector_rep (const intNDArray<U>& inda) | |
63 : data (0), len (inda.length ()), num_zeros (0), num_ones (0), | |
7422 | 64 range_base (0), range_step (0), max_val (0), min_val (0), |
65 count (1), frozen_at_z_len (0), frozen_len (0), colon (0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7422
diff
changeset
|
66 range(0), initialized (0), frozen (0), |
7422 | 67 colon_equiv_checked (0), colon_equiv (0), orig_dims (inda.dims ()) |
4938 | 68 { |
69 if (len == 0) | |
70 { | |
71 initialized = 1; | |
72 return; | |
73 } | |
74 else | |
75 { | |
5275 | 76 data = new octave_idx_type [len]; |
4938 | 77 |
5275 | 78 for (octave_idx_type i = 0; i < len; i++) |
7198 | 79 data[i] = tree_to_mat_idx (inda.elem (i)); |
4938 | 80 } |
81 | |
82 init_state (); | |
83 } | |
84 | |
1879 | 85 idx_vector_rep (const Range& r); |
1560 | 86 |
2386 | 87 idx_vector_rep (double d); |
88 | |
5275 | 89 idx_vector_rep (octave_idx_type i); |
3928 | 90 |
1879 | 91 idx_vector_rep (char c); |
1560 | 92 |
2828 | 93 idx_vector_rep (bool b); |
94 | |
4938 | 95 template <class U> |
96 idx_vector_rep (const octave_int<U>& i) | |
97 : data (0), len (1), num_zeros (0), num_ones (0), | |
7422 | 98 range_base (0), range_step (0), max_val (0), min_val (0), |
99 count (1), frozen_at_z_len (0), frozen_len (0), colon (0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7422
diff
changeset
|
100 range(0), initialized (0), frozen (0), |
7422 | 101 colon_equiv_checked (0), colon_equiv (0), orig_dims (1, 1) |
4938 | 102 { |
5275 | 103 data = new octave_idx_type [len]; |
4938 | 104 |
105 data[0] = tree_to_mat_idx (i); | |
106 | |
107 init_state (); | |
108 } | |
109 | |
4650 | 110 idx_vector_rep (const boolNDArray& bnda); |
2828 | 111 |
1879 | 112 idx_vector_rep (const idx_vector_rep& a); |
1560 | 113 |
2802 | 114 ~idx_vector_rep (void) { delete [] data; } |
1560 | 115 |
1879 | 116 idx_vector_rep& operator = (const idx_vector_rep& a); |
1560 | 117 |
1879 | 118 int ok (void) { return initialized; } |
1560 | 119 |
5275 | 120 octave_idx_type capacity (void) const { return len; } |
121 octave_idx_type length (octave_idx_type colon_len) const { return colon ? colon_len : len; } | |
1560 | 122 |
7422 | 123 octave_idx_type elem (octave_idx_type n) const |
124 { | |
125 return colon ? n : (range ? range_base + range_step*n : data[n]); | |
126 } | |
1560 | 127 |
5275 | 128 octave_idx_type checkelem (octave_idx_type n) const; |
129 octave_idx_type operator () (octave_idx_type n) const { return checkelem (n); } | |
1560 | 130 |
5275 | 131 octave_idx_type max (void) const { return max_val; } |
132 octave_idx_type min (void) const { return min_val; } | |
1879 | 133 |
5275 | 134 octave_idx_type zeros_count (void) const { return num_zeros; } |
135 octave_idx_type ones_count (void) const { return num_ones; } | |
1879 | 136 |
137 int is_colon (void) const { return colon; } | |
5275 | 138 int is_colon_equiv (octave_idx_type n, int sort_uniq); |
1560 | 139 |
3079 | 140 void sort (bool uniq); |
141 | |
5275 | 142 octave_idx_type orig_rows (void) const { return orig_dims(0); } |
143 octave_idx_type orig_columns (void) const { return orig_dims(1); } | |
1560 | 144 |
4653 | 145 dim_vector orig_dimensions (void) const { return orig_dims; } |
4504 | 146 |
1879 | 147 // other stuff |
1560 | 148 |
5275 | 149 void shorten (octave_idx_type n); // Unsafe. Avoid at all cost. |
1560 | 150 |
5781 | 151 octave_idx_type freeze (octave_idx_type z_len, const char *tag, bool resize_ok); |
1560 | 152 |
1879 | 153 // i/o |
1560 | 154 |
3504 | 155 std::ostream& print (std::ostream& os) const; |
1560 | 156 |
5275 | 157 octave_idx_type *data; |
158 octave_idx_type len; | |
159 octave_idx_type num_zeros; | |
160 octave_idx_type num_ones; | |
7422 | 161 octave_idx_type range_base; |
162 octave_idx_type range_step; | |
5275 | 163 octave_idx_type max_val; |
164 octave_idx_type min_val; | |
4504 | 165 |
1879 | 166 int count; |
5275 | 167 |
168 octave_idx_type frozen_at_z_len; | |
169 octave_idx_type frozen_len; | |
4653 | 170 |
1879 | 171 unsigned int colon : 1; |
7422 | 172 unsigned int range : 1; |
1879 | 173 unsigned int initialized : 1; |
174 unsigned int frozen : 1; | |
175 unsigned int colon_equiv_checked : 1; | |
176 unsigned int colon_equiv : 1; | |
1560 | 177 |
4653 | 178 dim_vector orig_dims; |
179 | |
1879 | 180 void init_state (void); |
1560 | 181 |
5275 | 182 octave_idx_type tree_to_mat_idx (double x, bool& conversion_error); |
4938 | 183 |
5275 | 184 octave_idx_type tree_to_mat_idx (octave_idx_type i) { return i - 1; } |
4938 | 185 |
5275 | 186 template <class U> octave_idx_type tree_to_mat_idx (const octave_int<U>& i) |
4938 | 187 { return i.value () - 1; } |
1879 | 188 }; |
1560 | 189 |
1 | 190 public: |
1551 | 191 |
4979 | 192 idx_vector (void) : rep (new idx_vector_rep ()) { } |
1560 | 193 |
4979 | 194 idx_vector (const ColumnVector& v) : rep (new idx_vector_rep (v)) { } |
1560 | 195 |
4979 | 196 idx_vector (const NDArray& nda) : rep (new idx_vector_rep (nda)) { } |
1560 | 197 |
4938 | 198 template <class U> |
4979 | 199 idx_vector (const intNDArray<U>& inda) : rep (new idx_vector_rep (inda)) { } |
4938 | 200 |
4979 | 201 idx_vector (const Range& r) : rep (new idx_vector_rep (r)) { } |
1560 | 202 |
4979 | 203 idx_vector (double d) : rep (new idx_vector_rep (d)) { } |
2386 | 204 |
5275 | 205 idx_vector (octave_idx_type i) : rep (new idx_vector_rep (i)) { } |
3928 | 206 |
4979 | 207 idx_vector (char c) : rep (new idx_vector_rep (c)) { } |
1560 | 208 |
4979 | 209 idx_vector (bool b) : rep (new idx_vector_rep (b)) { } |
2828 | 210 |
4938 | 211 template <class U> |
4979 | 212 idx_vector (const octave_int<U>& i) : rep (new idx_vector_rep (i)) { } |
4938 | 213 |
4979 | 214 idx_vector (const boolNDArray& bnda) : rep (new idx_vector_rep (bnda)) { } |
2828 | 215 |
4979 | 216 idx_vector (const idx_vector& a) : rep (a.rep) { rep->count++; } |
1551 | 217 |
2802 | 218 ~idx_vector (void) |
1560 | 219 { |
220 if (--rep->count <= 0) | |
221 delete rep; | |
222 } | |
1 | 223 |
1560 | 224 idx_vector& operator = (const idx_vector& a) |
225 { | |
226 if (this != &a) | |
227 { | |
228 if (--rep->count <= 0) | |
229 delete rep; | |
1 | 230 |
1560 | 231 rep = a.rep; |
232 rep->count++; | |
233 } | |
234 return *this; | |
1551 | 235 } |
236 | |
3145 | 237 operator bool () const { return rep->ok (); } |
1560 | 238 |
5275 | 239 octave_idx_type capacity (void) const { return rep->capacity (); } |
240 octave_idx_type length (octave_idx_type cl) const { return rep->length (cl); } | |
191 | 241 |
5275 | 242 octave_idx_type elem (octave_idx_type n) const { return rep->elem (n); } |
243 octave_idx_type checkelem (octave_idx_type n) const { return rep->checkelem (n); } | |
244 octave_idx_type operator () (octave_idx_type n) const { return rep->operator () (n); } | |
1 | 245 |
5275 | 246 octave_idx_type max (void) const { return rep->max (); } |
247 octave_idx_type min (void) const { return rep->min (); } | |
1551 | 248 |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7422
diff
changeset
|
249 int one_zero_only (void) const { return 0; } |
5275 | 250 octave_idx_type zeros_count (void) const { return rep->zeros_count (); } |
251 octave_idx_type ones_count (void) const { return rep->ones_count (); } | |
1 | 252 |
1560 | 253 int is_colon (void) const { return rep->is_colon (); } |
5275 | 254 int is_colon_equiv (octave_idx_type n, int sort_uniq = 0) const |
2356 | 255 { return rep->is_colon_equiv (n, sort_uniq); } |
1 | 256 |
3079 | 257 void sort (bool uniq = false) { rep->sort (uniq); } |
258 | |
5275 | 259 octave_idx_type orig_rows (void) const { return rep->orig_rows (); } |
260 octave_idx_type orig_columns (void) const { return rep->orig_columns (); } | |
208 | 261 |
4653 | 262 dim_vector orig_dimensions (void) const { return rep->orig_dimensions (); } |
4504 | 263 |
2663 | 264 int orig_empty (void) const |
4653 | 265 { return (! is_colon () && orig_dimensions().any_zero ()); } |
2663 | 266 |
4653 | 267 // Unsafe. Avoid at all cost. |
5275 | 268 void shorten (octave_idx_type n) { rep->shorten (n); } |
434 | 269 |
4653 | 270 // i/o |
1 | 271 |
5781 | 272 octave_idx_type freeze (octave_idx_type z_len, const char *tag, bool resize_ok = false) |
273 { return rep->freeze (z_len, tag, resize_ok); } | |
1560 | 274 |
3504 | 275 std::ostream& print (std::ostream& os) const { return rep->print (os); } |
1560 | 276 |
3504 | 277 friend std::ostream& operator << (std::ostream& os, const idx_vector& a) |
1560 | 278 { return a.print (os); } |
279 | |
1 | 280 private: |
281 | |
1560 | 282 idx_vector_rep *rep; |
1 | 283 |
1560 | 284 void init_state (void) { rep->init_state (); } |
1 | 285 }; |
286 | |
287 #endif | |
288 | |
289 /* | |
290 ;;; Local Variables: *** | |
291 ;;; mode: C++ *** | |
292 ;;; End: *** | |
293 */ |