Changeset 1345
 Timestamp:
 May 6, 2012, 10:52:40 PM (9 years ago)
 Location:
 trunk
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/array.h
r1331 r1345 32 32 */ 33 33 34 template<typename T > class ArrayBase34 template<typename T, typename ARRAY> class ArrayBase 35 35 { 36 36 public: … … 88 88 } 89 89 90 ArrayBase& operator+=(ARRAY const &that) 91 { 92 int todo = that.m_count; 93 Reserve(m_count + that.m_count); 94 for (int i = 0; i < todo; i++) 95 *this << that[i]; 96 return *this; 97 } 98 99 ARRAY operator+(ARRAY const &that) const 100 { 101 /* FIXME: upon return, this makes a copy of the temporary object; 102 * use either C++11 move semantics, or add a special flag to the 103 * object indicating we're a temporary about to be destroyed */ 104 ARRAY ret; 105 ret.Reserve(m_count + that.m_count); 106 for (int i = 0; i < m_count; i++) 107 ret << (*this)[i]; 108 for (int i = 0; i < that.m_count; i++) 109 ret << that[i]; 110 return ret; 111 } 112 90 113 inline Element& operator[](int n) 91 114 { … … 108 131 } 109 132 110 inline ArrayBase <T> const& operator<<(T const &x)133 inline ArrayBase& operator<<(T const &x) 111 134 { 112 135 if (m_count >= m_reserved) … … 237 260 typename T4 = void, typename T5 = void, typename T6 = void, 238 261 typename T7 = void, typename T8 = void> 239 class Array : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, T8> > 262 class Array : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, T8>, 263 Array<T1, T2, T3, T4, T5, T6, T7, T8> > 240 264 { 241 265 public: … … 275 299 typename T6, typename T7> 276 300 class Array<T1, T2, T3, T4, T5, T6, T7, void> 277 : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, void> > 301 : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>, 302 Array<T1, T2, T3, T4, T5, T6, T7> > 278 303 { 279 304 public: … … 311 336 typename T6> 312 337 class Array<T1, T2, T3, T4, T5, T6, void, void> 313 : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, void, void> > 338 : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, void, void>, 339 Array<T1, T2, T3, T4, T5, T6> > 314 340 { 315 341 public: … … 344 370 template<typename T1, typename T2, typename T3, typename T4, typename T5> 345 371 class Array<T1, T2, T3, T4, T5, void, void, void> 346 : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, void, void, void> > 372 : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, void, void, void>, 373 Array<T1, T2, T3, T4, T5> > 347 374 { 348 375 public: … … 375 402 template<typename T1, typename T2, typename T3, typename T4> 376 403 class Array<T1, T2, T3, T4, void, void, void, void> 377 : public ArrayBase<ArrayElement<T1, T2, T3, T4, void, void, void, void> > 404 : public ArrayBase<ArrayElement<T1, T2, T3, T4, void, void, void, void>, 405 Array<T1, T2, T3, T4> > 378 406 { 379 407 public: … … 402 430 template<typename T1, typename T2, typename T3> 403 431 class Array<T1, T2, T3, void, void, void, void, void> 404 : public ArrayBase<ArrayElement<T1, T2, T3, void, void, void, void, void> > 432 : public ArrayBase<ArrayElement<T1, T2, T3, void, void, void, void, void>, 433 Array<T1, T2, T3> > 405 434 { 406 435 public: … … 427 456 template<typename T1, typename T2> 428 457 class Array<T1, T2, void, void, void, void, void, void> 429 : public ArrayBase<ArrayElement<T1, T2, void, void, void, void, void, void> > 458 : public ArrayBase<ArrayElement<T1, T2, void, void, void, void, void, void>, 459 Array<T1, T2> > 430 460 { 431 461 public: … … 449 479 450 480 template<typename T> 451 class Array<T, void, void, void, void, void, void, void> : public ArrayBase<T> 481 class Array<T, void, void, void, void, void, void, void> 482 : public ArrayBase<T, 483 Array<T> > 452 484 { 453 485 }; 
trunk/test/unit/array.cpp
r1328 r1345 25 25 void TearDown() {} 26 26 27 LOLUNIT_TEST(Array Fill)27 LOLUNIT_TEST(ArrayPush) 28 28 { 29 29 Array<int> a; … … 39 39 } 40 40 41 LOLUNIT_TEST(ArrayPushWithShift) 42 { 43 Array<int> a; 44 a << 0 << 1 << 2 << 3; 45 46 LOLUNIT_ASSERT_EQUAL(a[0], 0); 47 LOLUNIT_ASSERT_EQUAL(a[1], 1); 48 LOLUNIT_ASSERT_EQUAL(a[2], 2); 49 LOLUNIT_ASSERT_EQUAL(a[3], 3); 50 } 51 41 52 LOLUNIT_TEST(ArrayCopy) 42 53 { 43 54 Array<int> a; 44 a.Push(0); 45 a.Push(1); 46 a.Push(2); 47 a.Push(3); 55 a << 0 << 1 << 2 << 3; 48 56 49 57 Array<int> b = a; … … 58 66 { 59 67 Array<int> a; 60 a.Push(0); 61 a.Push(1); 62 a.Push(2); 63 a.Push(3); 68 a << 0 << 1 << 2 << 3; 64 69 a.Remove(1); 65 70 … … 83 88 LOLUNIT_ASSERT_EQUAL(a[0].m8, 0); 84 89 } 90 91 LOLUNIT_TEST(ArrayConcat) 92 { 93 Array<int> a, b; 94 a << 0 << 1; 95 b << 2 << 3; 96 97 Array<int> c = a + b; 98 LOLUNIT_ASSERT_EQUAL(c[0], 0); 99 LOLUNIT_ASSERT_EQUAL(c[1], 1); 100 LOLUNIT_ASSERT_EQUAL(c[2], 2); 101 LOLUNIT_ASSERT_EQUAL(c[3], 3); 102 } 103 104 LOLUNIT_TEST(ArrayAppend) 105 { 106 Array<int> a, b; 107 a << 0 << 1; 108 b << 2 << 3; 109 110 a += b; 111 LOLUNIT_ASSERT_EQUAL(a[0], 0); 112 LOLUNIT_ASSERT_EQUAL(a[1], 1); 113 LOLUNIT_ASSERT_EQUAL(a[2], 2); 114 LOLUNIT_ASSERT_EQUAL(a[3], 3); 115 116 b += b; 117 LOLUNIT_ASSERT_EQUAL(b[0], 2); 118 LOLUNIT_ASSERT_EQUAL(b[1], 3); 119 LOLUNIT_ASSERT_EQUAL(b[2], 2); 120 LOLUNIT_ASSERT_EQUAL(b[3], 3); 121 } 85 122 }; 86 123
Note: See TracChangeset
for help on using the changeset viewer.