Changeset 16 for cppstreams/stream.h
- Timestamp:
- Dec 5, 2012, 10:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cppstreams/stream.h
r3 r16 3 3 * All rights reserved. 4 4 * 5 * This software was developed by Attila Gobi and Zalan Szugyi. 5 * This software was developed by Attila Gobi and Zalan Szugyi. 6 6 * The project was supported by the European Union and co-financed by the 7 7 * European Social Fund (grant agreement no. TAMOP 4.2.1./B-09/1/KMR-2010-0003) … … 9 9 * Redistribution and use in source and binary forms, with or without 10 10 * modification, are permitted provided that the following conditions are met: 11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright 12 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright 14 14 * notice, this list of conditions and the following disclaimer in the 15 15 * documentation and/or other materials provided with the distribution. … … 61 61 stream(const stream<T> &o) 62 62 : impl_(o.impl_->clone()) 63 { 63 { 64 64 } 65 65 … … 69 69 std::swap(impl_, o.impl_); 70 70 } 71 71 72 72 stream<T> & operator = (stream<T> &&o) 73 73 { 74 75 76 77 78 } 79 74 if(this != &o) { 75 std::swap(impl_, o.impl_); 76 return *this; 77 } 78 } 79 80 80 ~stream() 81 81 { … … 84 84 85 85 struct iterator { 86 iterator& operator ++() 86 iterator& operator ++() 87 87 { 88 88 (*impl_)->next(*this); … … 128 128 struct addimpl: public impl { 129 129 addimpl(const T &a, ST &&s) 130 : a_(a), s_(std::forward<ST>(s)) 130 : a_(a), s_(std::forward<ST>(s)) 131 131 { } 132 132 133 const T &get(const iterator &) 133 const T &get(const iterator &) 134 134 { 135 135 return a_; … … 141 141 } 142 142 143 impl *clone() 143 impl *clone() 144 144 { 145 145 return new addimpl<ST>(a_, std::forward<ST>(s_)); … … 160 160 161 161 const T &get(const iterator &it) 162 { 162 { 163 163 *(it.impl_)=new addimpl<stream<T>&&>(op_(*it1), stream<T>(this)); 164 164 ++it1; … … 166 166 } 167 167 168 void next(iterator &it) 168 void next(iterator &it) 169 169 { 170 170 *(it.impl_)=new addimpl<stream<T>&&>(op_(*it1), stream<T>(this)); … … 173 173 } 174 174 175 impl *clone() 175 impl *clone() 176 176 { 177 177 return new mapimpl2<Op, ST>(op_, std::forward<ST>(s_)); … … 181 181 iterator it1; 182 182 Op op_; 183 183 }; 184 184 185 185 template<typename Op, typename ST> … … 189 189 { } 190 190 191 const T &get(const iterator &it) 191 const T &get(const iterator &it) 192 192 { 193 193 impl *x = new mapimpl2<Op, ST>(op_, std::forward<ST>(s_)); 194 194 *(it.impl_) = x; 195 195 196 196 delete this; 197 197 return *it; … … 206 206 } 207 207 208 impl *clone() 208 impl *clone() 209 209 { 210 210 return new mapimpl<Op, ST>(op_, std::forward<ST>(s_)); … … 213 213 private: 214 214 typename storage_type<ST>::type s_; 215 215 Op op_; 216 216 }; 217 217 … … 223 223 { } 224 224 225 const T &get(const iterator &it) 226 { 225 const T &get(const iterator &it) 226 { 227 227 impl *x = new zipimpl2<Op, ST1, ST2>(op_, std::forward<ST1>(s1_), std::forward<ST2>(s2_)); 228 228 *(it.impl_) = x; … … 231 231 } 232 232 233 void next(iterator &it) 233 void next(iterator &it) 234 234 { 235 235 impl *x = new zipimpl2<Op, ST1, ST2>(op_, std::forward<ST1>(s1_), std::forward<ST2>(s2_)); … … 239 239 } 240 240 241 impl *clone() 241 impl *clone() 242 242 { 243 243 return new zipimpl<Op, ST1, ST2>(op_, std::forward<ST1>(s1_), std::forward<ST2>(s2_)); … … 259 259 260 260 const T &get(const iterator &it) 261 { 261 { 262 262 *(it.impl_)=new addimpl<stream<T>&&>(op_(*it1, *it2), stream<T>(this)); 263 263 ++it1; … … 266 266 } 267 267 268 void next(iterator &it) 268 void next(iterator &it) 269 269 { 270 270 *(it.impl_)=new addimpl<stream<T>&&>(op_(*it1, *it2), stream<T>(this)); … … 274 274 } 275 275 276 impl *clone() 276 impl *clone() 277 277 { 278 278 return new zipimpl2<Op, ST1, ST2>(op_, std::forward<ST1>(s1_), std::forward<ST2>(s2_)); … … 286 286 287 287 public: 288 289 290 288 template <typename S, typename U> 289 friend stream<U> operator <<= (const U& a, S && s); 290 291 291 template <typename Op, typename ST1, typename ST2> 292 292 static stream<T> zipwith(Op op, ST1 &&s1, ST2 &&s2) … … 294 294 return stream(new zipimpl<Op, decltype(s1), decltype(s2)>(op, std::forward<ST1>(s1), std::forward<ST2>(s2))); 295 295 } 296 297 296 297 template <typename ST1, typename Op> 298 298 static stream<T> map(Op op, ST1 &&s1) 299 299 { … … 301 301 } 302 302 303 304 305 306 307 303 static stream<T> pure(const T& v) 304 { 305 stream<T> s = v<<=s; 306 return s; 307 } 308 308 }; 309 309 … … 311 311 struct stream_value_type 312 312 { 313 313 typedef typename std::remove_reference<ST>::type::value_type type; 314 314 }; 315 315 … … 317 317 stream<U> operator <<= (const U& a, S && s) 318 318 { 319 320 } 321 319 return stream<U>(new typename stream<U>::template addimpl<decltype(s)>(a, std::forward<S>(s))); 320 } 321 322 322 template<typename ST1, typename ST2, typename T=typename stream_value_type<ST1>::type> 323 323 stream<T> operator +(ST1 &&s1, ST2 &&s2) 324 324 { 325 325 return stream<T>::zipwith(std::plus<T>(), std::forward<ST1>(s1), std::forward<ST2>(s2)); 326 326 } 327 327 … … 329 329 stream<T> operator -(ST1 &&s1, ST2 &&s2) 330 330 { 331 331 return stream<T>::zipwith(std::minus<T>(), std::forward<ST1>(s1), std::forward<ST2>(s2)); 332 332 } 333 333 … … 335 335 stream<T> operator *(ST1 &&s1, ST2 &&s2) 336 336 { 337 337 return stream<T>::zipwith(std::multiplies<T>(), std::forward<ST1>(s1), std::forward<ST2>(s2)); 338 338 } 339 339 … … 341 341 stream<T> operator /(ST1 &&s1, ST2 &&s2) 342 342 { 343 343 return stream<T>::zipwith(std::divides<T>(), std::forward<ST1>(s1), std::forward<ST2>(s2)); 344 344 } 345 345 … … 347 347 stream<T> operator %(ST1 &&s1, ST2 &&s2) 348 348 { 349 349 return stream<T>::zipwith(std::modulus<T>(), std::forward<ST1>(s1), std::forward<ST2>(s2)); 350 350 } 351 351 … … 353 353 stream<T> operator -(ST &&s) 354 354 { 355 355 return stream<T>::map(std::negate<T>(), std::forward<ST>(s)); 356 356 } 357 357 … … 360 360 struct stream_proxy 361 361 { 362 363 364 365 366 367 368 362 stream_proxy(unsigned long long i): x(i) {} 363 template <typename T> 364 operator stream<T> () { 365 stream<T> a = static_cast<T>(x)<<=a; 366 return a; 367 } 368 unsigned long long x; 369 369 }; 370 370 371 371 stream_proxy operator "" _s (unsigned long long i) 372 372 { 373 373 return stream_proxy(i); 374 374 } 375 375
Note: See TracChangeset
for help on using the changeset viewer.