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