| 1 |
// { dg-do compile } |
| 2 |
// { dg-options "-O3 -floop-nest-optimize -Wno-conversion-null -Wno-return-type" } |
| 3 |
|
| 4 |
void __throw_bad_alloc (); |
| 5 |
|
| 6 |
template <typename _Tp> void |
| 7 |
swap (_Tp & __a, _Tp __b) |
| 8 |
{ |
| 9 |
__a = __b; |
| 10 |
} |
| 11 |
|
| 12 |
template <typename _Category> struct iterator |
| 13 |
{ |
| 14 |
typedef _Category iterator_category; |
| 15 |
}; |
| 16 |
|
| 17 |
template <typename _Tp> struct allocator |
| 18 |
{ |
| 19 |
typedef __SIZE_TYPE__ size_type; |
| 20 |
typedef _Tp pointer; |
| 21 |
pointer allocate (size_type) |
| 22 |
{ |
| 23 |
__throw_bad_alloc (); |
| 24 |
return __null; |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
template <class T, class = allocator <T> >class unbounded_array; |
| 29 |
template <class T, class = unbounded_array <T> >class vector; |
| 30 |
template <class = int> class scalar_vector; |
| 31 |
template <class IC> struct random_access_iterator_base : public iterator <IC> |
| 32 |
{ |
| 33 |
}; |
| 34 |
|
| 35 |
template <class X, class> struct promote_traits |
| 36 |
{ |
| 37 |
typedef __typeof__ ((X ())) promote_type; |
| 38 |
}; |
| 39 |
|
| 40 |
template <class T> struct scalar_traits |
| 41 |
{ |
| 42 |
typedef T const_reference; |
| 43 |
typedef T reference; |
| 44 |
}; |
| 45 |
|
| 46 |
template <class T> struct type_traits : scalar_traits <T> |
| 47 |
{ |
| 48 |
}; |
| 49 |
|
| 50 |
struct dense_proxy_tag |
| 51 |
{ |
| 52 |
}; |
| 53 |
|
| 54 |
template <class> struct iterator_base_traits; |
| 55 |
|
| 56 |
template <> struct iterator_base_traits <dense_proxy_tag> |
| 57 |
{ |
| 58 |
template <class, class> struct iterator_base |
| 59 |
{ |
| 60 |
typedef random_access_iterator_base <dense_proxy_tag> type; |
| 61 |
}; |
| 62 |
}; |
| 63 |
|
| 64 |
template <class I1, class> struct iterator_restrict_traits |
| 65 |
{ |
| 66 |
typedef I1 iterator_category; |
| 67 |
}; |
| 68 |
|
| 69 |
template <class> class storage_array |
| 70 |
{ |
| 71 |
}; |
| 72 |
|
| 73 |
template <class T, class ALLOC> struct unbounded_array : public storage_array <unbounded_array <ALLOC> > |
| 74 |
{ |
| 75 |
typedef typename ALLOC::size_type size_type; |
| 76 |
typedef T & reference; |
| 77 |
typedef T *pointer; |
| 78 |
unbounded_array (size_type size, ALLOC = ALLOC ()) : alloc_ (), size_ (size) |
| 79 |
{ |
| 80 |
alloc_.allocate (size_); |
| 81 |
} |
| 82 |
~unbounded_array () |
| 83 |
{ |
| 84 |
if (size_) |
| 85 |
for (;;); |
| 86 |
} |
| 87 |
size_type |
| 88 |
size () const |
| 89 |
{ |
| 90 |
return size_; |
| 91 |
} |
| 92 |
reference |
| 93 |
operator[] (size_type i) |
| 94 |
{ |
| 95 |
return data_[i]; |
| 96 |
} |
| 97 |
void |
| 98 |
swap (unbounded_array & a) |
| 99 |
{ |
| 100 |
::swap (size_, a.size_); |
| 101 |
} |
| 102 |
ALLOC alloc_; |
| 103 |
size_type size_; |
| 104 |
pointer data_; |
| 105 |
}; |
| 106 |
|
| 107 |
template <class T1, class T2> struct scalar_binary_functor |
| 108 |
{ |
| 109 |
typedef typename promote_traits <T1, T2>::promote_type result_type; |
| 110 |
}; |
| 111 |
|
| 112 |
template <class T1, class T2> struct scalar_plus : public scalar_binary_functor <T1, T2> |
| 113 |
{ |
| 114 |
}; |
| 115 |
|
| 116 |
template <class T1, class T2> struct scalar_multiplies : public scalar_binary_functor <T1, T2> |
| 117 |
{ |
| 118 |
}; |
| 119 |
|
| 120 |
template <class T1, class T2> struct scalar_binary_assign_functor |
| 121 |
{ |
| 122 |
typedef typename type_traits <T1>::reference argument1_type; |
| 123 |
typedef typename type_traits <T2>::const_reference argument2_type; |
| 124 |
}; |
| 125 |
|
| 126 |
template <class T1, class T2> struct scalar_assign : public scalar_binary_assign_functor <T1, T2> |
| 127 |
{ |
| 128 |
typedef typename scalar_binary_assign_functor <T1, T2>::argument1_type argument1_type; |
| 129 |
typedef typename scalar_binary_assign_functor <T1, T2>::argument2_type argument2_type; |
| 130 |
static const bool computed = false; |
| 131 |
static void |
| 132 |
apply (argument1_type t1, argument2_type t2) |
| 133 |
{ |
| 134 |
t1 = t2; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
template <class E> struct vector_expression |
| 139 |
{ |
| 140 |
typedef E expression_type; |
| 141 |
const expression_type & |
| 142 |
operator () () const |
| 143 |
{ |
| 144 |
return *static_cast <const expression_type *>(this); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
template <class C> class vector_container : public vector_expression <C> |
| 149 |
{ |
| 150 |
}; |
| 151 |
|
| 152 |
template <class E> struct vector_reference : public vector_expression <vector_reference <E> > |
| 153 |
{ |
| 154 |
typedef typename E::size_type size_type; |
| 155 |
typename E::const_reference const_reference; |
| 156 |
typedef E referred_type; |
| 157 |
vector_reference (referred_type & e) : e_ (e) |
| 158 |
{ |
| 159 |
} |
| 160 |
size_type |
| 161 |
size () const |
| 162 |
{ |
| 163 |
return expression ().size (); |
| 164 |
} |
| 165 |
referred_type & |
| 166 |
expression () const |
| 167 |
{ |
| 168 |
return e_; |
| 169 |
} |
| 170 |
referred_type &e_; |
| 171 |
}; |
| 172 |
|
| 173 |
template <class E1, class E2, class F> struct vector_binary : public vector_expression <vector_binary <E1, E2, F> > |
| 174 |
{ |
| 175 |
typedef E1 expression1_type; |
| 176 |
typedef E2 expression2_type; |
| 177 |
typedef typename E1::const_closure_type expression1_closure_type; |
| 178 |
typedef typename E2::const_closure_type expression2_closure_type; |
| 179 |
typedef typename promote_traits <typename E1::size_type, typename E2::size_type>::promote_type size_type; |
| 180 |
typedef typename F::result_type value_type; |
| 181 |
|
| 182 |
vector_binary (const expression1_type & e1, expression2_type e2) : e1_ (e1), e2_ (e2) |
| 183 |
{ |
| 184 |
} |
| 185 |
|
| 186 |
size_type |
| 187 |
size () const |
| 188 |
{ |
| 189 |
return e1_.size (); |
| 190 |
} |
| 191 |
|
| 192 |
class const_iterator : public iterator_base_traits <typename iterator_restrict_traits <typename E1::const_iterator::iterator_category, const_iterator>::iterator_category>::template iterator_base <const_iterator, value_type>::type |
| 193 |
{ |
| 194 |
}; |
| 195 |
expression1_closure_type e1_; |
| 196 |
expression2_closure_type e2_; |
| 197 |
}; |
| 198 |
|
| 199 |
template <class E1, class E2, class F> struct vector_binary_traits |
| 200 |
{ |
| 201 |
typedef vector_binary <E1, E2, F> expression_type; |
| 202 |
typedef expression_type result_type; |
| 203 |
}; |
| 204 |
|
| 205 |
template <class E1, class E2> typename vector_binary_traits <E1, E2, scalar_plus <typename E1::value_type, typename E2::value_type> >::result_type |
| 206 |
operator + (vector_expression <E1> &e1, const vector_expression <E2> &e2) |
| 207 |
{ |
| 208 |
typedef typename vector_binary_traits <E1, E2, scalar_plus <typename E1::value_type, typename E2::value_type> >::expression_type expression_type; |
| 209 |
return expression_type (e1 (), e2 ()); |
| 210 |
} |
| 211 |
|
| 212 |
template <class E1, class E2, class F> struct vector_binary_scalar2 : public vector_expression <vector_binary_scalar2 <E1, E2, F> > |
| 213 |
{ |
| 214 |
typedef vector_binary_scalar2 <E1, E2, F> self_type; |
| 215 |
typedef typename E1::size_type size_type; |
| 216 |
typedef typename F::result_type value_type; |
| 217 |
typedef self_type const_closure_type; |
| 218 |
}; |
| 219 |
|
| 220 |
template <class E1, class E2, class F> struct vector_binary_scalar2_traits |
| 221 |
{ |
| 222 |
typedef vector_binary_scalar2 <E1, E2, F> result_type; |
| 223 |
}; |
| 224 |
|
| 225 |
template <class E1, class T2> |
| 226 |
typename vector_binary_scalar2_traits <E1, T2, scalar_multiplies <typename E1::value_type, T2> >::result_type |
| 227 |
operator * (vector_expression <E1>, T2) |
| 228 |
{ |
| 229 |
} |
| 230 |
|
| 231 |
template <class SC> struct vector_assign_traits |
| 232 |
{ |
| 233 |
typedef SC storage_category; |
| 234 |
}; |
| 235 |
|
| 236 |
template <template <class, class> class F, class V, class E> void |
| 237 |
indexing_vector_assign (V & v, vector_expression <E>) |
| 238 |
{ |
| 239 |
typedef F <typename V::reference, typename E::value_type> functor_type; |
| 240 |
typedef typename V::size_type size_type; |
| 241 |
size_type size (v.size ()); |
| 242 |
for (size_type i; i <size; ++i) |
| 243 |
functor_type::apply (v (i), (i)); |
| 244 |
} |
| 245 |
|
| 246 |
template <template <class, class> class F, class V, class E> void |
| 247 |
vector_assign (V & v, const vector_expression <E> &e, dense_proxy_tag) |
| 248 |
{ |
| 249 |
indexing_vector_assign <F> (v, e); |
| 250 |
} |
| 251 |
|
| 252 |
template <template <class, class> class F, class V, class E> void |
| 253 |
vector_assign (V & v, const vector_expression <E> &e) |
| 254 |
{ |
| 255 |
typedef typename vector_assign_traits <typename V::storage_category>::storage_category storage_category; |
| 256 |
vector_assign <F> (v, e, storage_category ()); |
| 257 |
} |
| 258 |
|
| 259 |
template <class T, class A> struct vector : public vector_container <vector <T> > |
| 260 |
{ |
| 261 |
typedef vector <T> self_type; |
| 262 |
typedef typename A::size_type size_type; |
| 263 |
typedef T value_type; |
| 264 |
typedef typename type_traits <T>::const_reference const_reference; |
| 265 |
typedef T &reference; |
| 266 |
typedef A array_type; |
| 267 |
typedef vector_reference <const self_type> const_closure_type; |
| 268 |
typedef dense_proxy_tag storage_category; |
| 269 |
vector (size_type size):vector_container <self_type> (), data_ (size) |
| 270 |
{ |
| 271 |
} |
| 272 |
vector (size_type size, value_type):vector_container <self_type> (), data_ (size) |
| 273 |
{ |
| 274 |
} |
| 275 |
template <class AE> vector (const vector_expression <AE> &ae) : vector_container <self_type> (), data_ (ae ().size ()) |
| 276 |
{ |
| 277 |
vector_assign <scalar_assign> (*this, ae); |
| 278 |
} |
| 279 |
size_type |
| 280 |
size () const |
| 281 |
{ |
| 282 |
return data_.size (); |
| 283 |
} |
| 284 |
array_type & |
| 285 |
data () |
| 286 |
{ |
| 287 |
return data_; |
| 288 |
} |
| 289 |
reference |
| 290 |
operator () (size_type i) |
| 291 |
{ |
| 292 |
return data ()[i]; |
| 293 |
} |
| 294 |
template <class AE> vector operator += (const vector_expression <AE> &ae) |
| 295 |
{ |
| 296 |
self_type temporary (*this + ae); |
| 297 |
data_.swap (temporary.data ()); |
| 298 |
return *this; |
| 299 |
} |
| 300 |
class const_iterator : public random_access_iterator_base <dense_proxy_tag> |
| 301 |
{ |
| 302 |
}; |
| 303 |
array_type data_; |
| 304 |
}; |
| 305 |
|
| 306 |
template <class T> struct scalar_vector : public vector_container <scalar_vector <> > |
| 307 |
{ |
| 308 |
typedef scalar_vector self_type; |
| 309 |
typedef __SIZE_TYPE__ size_type; |
| 310 |
typedef T value_type; |
| 311 |
typedef T const_reference; |
| 312 |
typedef vector_reference <self_type> const_closure_type; |
| 313 |
}; |
| 314 |
|
| 315 |
void |
| 316 |
bar (vector <double>) |
| 317 |
{ |
| 318 |
} |
| 319 |
|
| 320 |
void |
| 321 |
foo (int n_samp) |
| 322 |
{ |
| 323 |
vector <double> xi (n_samp, 0); |
| 324 |
for (int n = 0; n <n_samp; ++n) |
| 325 |
{ |
| 326 |
vector <double> cos_w_n (n_samp); |
| 327 |
xi += cos_w_n * 6.0; |
| 328 |
} |
| 329 |
vector <double> cos_wd (n_samp); |
| 330 |
xi += cos_wd; |
| 331 |
bar (xi + scalar_vector <> ()); |
| 332 |
} |