Douglas Gregor | 8ab892a | 2009-05-26 21:27:04 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -verify %s |
| 2 | #include <stdlib.h> |
| 3 | #include <assert.h> |
| 4 | |
| 5 | template<typename T> |
| 6 | class dynarray { |
| 7 | dynarray() { Start = Last = End = 0; } |
| 8 | |
| 9 | dynarray(const dynarray &other) { |
| 10 | Start = (T*)malloc(sizeof(T) * other.size()); |
| 11 | Last = End = Start + other.size(); |
| 12 | |
| 13 | // FIXME: Use placement new, below |
| 14 | for (unsigned I = 0, N = other.size(); I != N; ++I) |
| 15 | Start[I] = other[I]; |
| 16 | // new (Start + I) T(other[I]); |
| 17 | } |
| 18 | |
| 19 | ~dynarray() { |
| 20 | free(Start); |
| 21 | } |
| 22 | |
| 23 | dynarray &operator=(const dynarray &other) { |
| 24 | T* NewStart = (T*)malloc(sizeof(T) * other.size()); |
| 25 | |
| 26 | // FIXME: Use placement new, below |
| 27 | for (unsigned I = 0, N = other.size(); I != N; ++I) |
| 28 | NewStart[I] = other[I]; |
| 29 | // new (Start + I) T(other[I]); |
| 30 | |
| 31 | // FIXME: destroy everything in Start |
| 32 | free(Start); |
| 33 | Start = NewStart; |
| 34 | Last = End = NewStart + other.size(); |
| 35 | return *this; |
| 36 | } |
| 37 | |
| 38 | unsigned size() const { return Last - Start; } |
| 39 | unsigned capacity() const { return End - Start; } |
| 40 | |
| 41 | void push_back(const T& value) { |
| 42 | if (Last == End) { |
| 43 | unsigned NewCapacity = capacity() * 2; |
| 44 | if (NewCapacity == 0) |
| 45 | NewCapacity = 4; |
| 46 | |
| 47 | T* NewStart = (T*)malloc(sizeof(T) * NewCapacity); |
| 48 | |
| 49 | unsigned Size = size(); |
| 50 | for (unsigned I = 0; I != Size; ++I) |
| 51 | // FIXME: new (NewStart + I) T(Start[I]) |
| 52 | NewStart[I] = Start[I]; |
| 53 | |
| 54 | // FIXME: destruct old values |
| 55 | free(Start); |
| 56 | |
| 57 | Start = NewStart; |
| 58 | Last = Start + Size; |
| 59 | End = Start + NewCapacity; |
| 60 | } |
| 61 | |
| 62 | // FIXME: new (Last) T(value); |
| 63 | *Last = value; |
| 64 | ++Last; |
| 65 | } |
| 66 | |
| 67 | void pop_back() { |
| 68 | // FIXME: destruct old value |
| 69 | --Last; |
| 70 | } |
| 71 | |
| 72 | T& operator[](unsigned Idx) { |
| 73 | return Start[Idx]; |
| 74 | } |
| 75 | |
| 76 | const T& operator[](unsigned Idx) const { |
| 77 | return Start[Idx]; |
| 78 | } |
| 79 | |
Douglas Gregor | 8ab892a | 2009-05-26 21:27:04 +0000 | [diff] [blame] | 80 | typedef T* iterator; |
| 81 | typedef const T* const_iterator; |
| 82 | |
Douglas Gregor | f97986d | 2009-05-27 05:35:12 +0000 | [diff] [blame^] | 83 | iterator begin() { return Start; } |
| 84 | const_iterator begin() const { return Start; } |
Douglas Gregor | 8ab892a | 2009-05-26 21:27:04 +0000 | [diff] [blame] | 85 | |
Douglas Gregor | f97986d | 2009-05-27 05:35:12 +0000 | [diff] [blame^] | 86 | iterator end() { return Last; } |
| 87 | const_iterator end() const { return Last; } |
Douglas Gregor | 8ab892a | 2009-05-26 21:27:04 +0000 | [diff] [blame] | 88 | |
| 89 | public: |
| 90 | T* Start, *Last, *End; |
| 91 | }; |
| 92 | |
| 93 | struct Point { |
| 94 | Point() { x = y = z = 0.0; } |
| 95 | Point(const Point& other) : x(other.x), y(other.y), z(other.z) { } |
| 96 | |
| 97 | float x, y, z; |
| 98 | }; |
| 99 | |
| 100 | // FIXME: remove these when we have implicit instantiation for member |
| 101 | // functions of class templates. |
| 102 | template struct dynarray<int>; |
| 103 | template struct dynarray<Point>; |
| 104 | |
| 105 | int main() { |
| 106 | dynarray<int> di; |
| 107 | di.push_back(0); |
| 108 | di.push_back(1); |
| 109 | di.push_back(2); |
| 110 | di.push_back(3); |
| 111 | di.push_back(4); |
| 112 | assert(di.size() == 5); |
| 113 | for (dynarray<int>::iterator I = di.begin(), IEnd = di.end(); I != IEnd; ++I) |
| 114 | assert(*I == I - di.begin()); |
| 115 | |
Douglas Gregor | f97986d | 2009-05-27 05:35:12 +0000 | [diff] [blame^] | 116 | for (int I = 0, N = di.size(); I != N; ++I) |
| 117 | assert(di[I] == I); |
| 118 | |
Douglas Gregor | 8ab892a | 2009-05-26 21:27:04 +0000 | [diff] [blame] | 119 | di.pop_back(); |
| 120 | assert(di.size() == 4); |
| 121 | di.push_back(4); |
| 122 | |
| 123 | #if 0 |
| 124 | // FIXME: Copy construction via copy initialization |
| 125 | dynarray<int> di2 = di; |
| 126 | assert(di2.size() == 5); |
| 127 | assert(di.begin() != di2.begin()); |
| 128 | for (dynarray<int>::iterator I = di2.begin(), IEnd = di2.end(); |
| 129 | I != IEnd; ++I) |
| 130 | assert(*I == I - di2.begin()); |
| 131 | |
| 132 | // FIXME: Copy construction via direct initialization |
| 133 | dynarray<int> di3(di); |
| 134 | assert(di3.size() == 5); |
| 135 | assert(di.begin() != di3.begin()); |
| 136 | for (dynarray<int>::iterator I = di3.begin(), IEnd = di3.end(); |
| 137 | I != IEnd; ++I) |
| 138 | assert(*I == I - di3.begin()); |
| 139 | |
| 140 | // FIXME: assignment operator |
| 141 | dynarray<int> di4; |
| 142 | assert(di4.size() == 0); |
| 143 | di4 = di; |
| 144 | assert(di4.size() == 5); |
| 145 | assert(di.begin() != di4.begin()); |
| 146 | for (dynarray<int>::iterator I = di4.begin(), IEnd = di4.end(); |
| 147 | I != IEnd; ++I) |
| 148 | assert(*I == I - di4.begin()); |
| 149 | #endif |
| 150 | |
| 151 | return 0; |
| 152 | } |