1// { dg-do run } 2// GROUPS passed code-generation 3// code-gen file 4// From: "David" <norman@pi14.arc.umn.edu> 5// Date: Mon, 15 Nov 1993 20:59:14 -0600 (CST) 6// Subject: An error! 7// Message-ID: <9311160259.AA03353@pi14.arc.umn.edu> 8 9#include <cstdlib> 10#include <cstdio> 11#include <cassert> 12#include <fstream> 13#include <iostream> 14#include <cmath> 15 16#define ANSI_C 17 18typedef double VEC ; 19 20class Vector; 21 22class VectorInt 23{ 24 public: 25 26 /* Nothing public!! Only Vector can use this class */ 27 28 private: 29 30 VectorInt( int ); 31 VectorInt( int, double *, int = 0 ); 32 VectorInt( const VectorInt & ); 33 ~VectorInt(); 34 35 VectorInt *refer(); 36 void unrefer(); 37 int count; /* Number of Vector's referring to me */ 38 39 VEC *vec; 40 41 friend class Vector; 42 friend class VecElem; 43}; 44 45class VecElem 46{ 47 public: 48 49 operator double(); 50 double operator=( double ); 51 52 private: 53 54 VecElem( Vector &, int ); 55 VecElem( const VecElem & ); 56 57 Vector &v; 58 int row; /* Row element refers to */ 59 60 friend class Vector; 61}; 62 63class Vector 64{ 65 public: 66 67 Vector(); // Must be assigned to before used 68 Vector( VectorInt * ); 69 Vector( int ); 70 Vector( int, double *, int beg = 0 ); 71 Vector( const Vector & ); 72 Vector &operator=( const Vector & ); 73 ~Vector() { if(r) r->unrefer(); } 74 75 int row() const { return 19; } 76 int dim() const { return 10; } 77 78 double operator()( int ) const; 79 VecElem operator()( int ); 80 81 double assign( int, double ); 82 83 friend std::ostream& operator<<(std::ostream&, const Vector& m ); 84 85 private: 86 87 VectorInt *r; /* Reference to real data */ 88 89 friend class VecElem; 90 friend class LUDecom; 91 friend class SVD; 92}; 93 94 95Vector:: 96Vector() 97 : r(0) 98{} 99 100Vector:: 101Vector( VectorInt *vi ) 102 : r(vi) 103{ 104 r->refer(); 105} 106 107Vector:: 108Vector( int row ) 109{ 110 assert( row > 0 ); 111 112 r = new VectorInt( row ); 113 114 r->refer(); 115} 116 117Vector:: 118Vector( int row, double *d, int beg ) 119{ 120 assert( row > 0 ); 121 122 r = new VectorInt( row, d, beg ); 123 124 r->refer(); 125} 126 127Vector:: 128Vector( const Vector &A ) 129 : r( A.r->refer() ) 130{} 131 132Vector& Vector:: 133operator=( const Vector &A ) 134{ 135 if( r ) 136 r->unrefer(); 137 138 r = A.r->refer(); 139 140 return *this; 141} 142 143double Vector:: 144operator()( int row ) const 145{ 146 assert(r != 0); 147 148 return *r->vec; 149} 150 151VecElem Vector:: 152operator()( int r ) 153{ 154 assert(r != 0); 155 156 return VecElem( *this, r ); 157} 158 159 /* assign changes the matrix, it does not create a new one! */ 160double Vector:: 161assign( int rownum, double d ) 162{ 163 assert(r != 0); 164 165 if( rownum > row() || rownum <= 0 ) { 166 std::cerr << "Warning: trying to assign out of bounds" << std::endl; 167 std::cerr << "row " << rownum << std::endl; 168 std::cerr << "Vector size " << row() << std::endl; 169 std::abort(); 170 } 171 172 if( r->count == 1 ) { 173 /* Don't need to create a new matrix, since we are the only */ 174 /* one pointing to ours */ 175 } 176 else { 177 VectorInt *vi = new VectorInt( *r ); 178 r->unrefer(); 179 r = vi->refer(); 180 } 181 182 return d; 183} 184 185 186VectorInt:: 187VectorInt( int sx ) 188 : vec( new double[sx] ), count(0) 189{ } 190 191VectorInt:: 192VectorInt( int sx, double *, int ) 193 : vec( new double[sx] ), count(0) 194{ 195} 196 197VectorInt:: 198VectorInt( const VectorInt & ) 199 : vec( new double[10] ), count(0) 200{ 201} 202 203VectorInt * VectorInt:: 204refer() 205{ 206 count ++; 207 return this; 208 209 // cout << "Refering vec" << endl; 210} 211 212void VectorInt:: 213unrefer() 214{ 215 count--; 216 217 if( count == 0 ) { 218 delete this; 219 } 220 221 // cout << "Unrefering vec" << endl; 222} 223 224VectorInt:: 225~VectorInt() 226{ 227 delete vec; 228 vec = 0; 229} 230 231VecElem:: 232VecElem( Vector &vec, int r ) 233 : v(vec), row(r) 234{ 235 if( r < 1 || r > vec.row() ) { 236 std::cerr << "Trying to access vector element out of bounds"; 237 std::cerr << std::endl; 238 std::abort(); 239 } 240} 241 242VecElem:: 243VecElem( const VecElem &elem ) 244 : v(elem.v), row(elem.row) 245{} 246 247VecElem:: 248operator double() 249{ 250 assert( v.r->vec != 0 ); 251 return *v.r->vec; 252} 253 254double VecElem:: 255operator=( double d ) 256{ 257 return v.assign( row, d ); 258} 259 260 261 262 263 264int makeforms( Vector cen, Vector **a, Vector **b ); 265 266int main() 267{ 268 Vector *a[8], *b[8], disp(3); 269 Vector cen(3), cen2(3); 270 int i, j; 271 272 if (makeforms (cen,a,b) != 10) 273 { std::printf ("FAIL\n"); return 1; } 274 else 275 std::printf ("PASS\n"); 276 277 278} 279 280int 281makeforms( Vector cen, Vector **a, Vector **b) 282{ 283 return 10; 284} 285 286 287 288