Line |
Branch |
Exec |
Source |
1 |
|
|
#pragma once |
2 |
|
|
|
3 |
|
|
#include "std-includes.h" |
4 |
|
|
|
5 |
|
|
//Note: T must be a pointer type, or stuff will screw up. To make a pointer last longer than this object, assign nullptr to it and it won't free the old one. |
6 |
|
|
template<typename T> class autoptr { |
7 |
|
|
T ptr; |
8 |
|
|
public: |
9 |
|
|
operator T() const |
10 |
|
|
{ |
11 |
12/12
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 40 times.
✓ Branch 6 taken 82 times.
✓ Branch 7 taken 50 times.
✓ Branch 8 taken 72 times.
✓ Branch 9 taken 8 times.
✓ Branch 10 taken 124 times.
✓ Branch 11 taken 30 times.
|
708 |
return ptr; |
12 |
|
|
} |
13 |
|
|
|
14 |
|
|
autoptr& operator=(T ptr_) |
15 |
|
|
{ |
16 |
|
168 |
ptr = ptr_; |
17 |
|
138 |
return *this; |
18 |
|
|
} |
19 |
|
|
|
20 |
|
246 |
autoptr() |
21 |
|
|
{ |
22 |
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 78 times.
|
246 |
ptr = nullptr; |
23 |
|
|
} |
24 |
|
|
|
25 |
|
1976558 |
autoptr(T ptr_) |
26 |
|
|
{ |
27 |
11/16
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5928 times.
✓ Branch 2 taken 872454 times.
✓ Branch 3 taken 2280 times.
✓ Branch 4 taken 4980 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2808 times.
✓ Branch 7 taken 651 times.
✓ Branch 8 taken 54 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 24 times.
✓ Branch 11 taken 24 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 114 times.
|
1970858 |
ptr = ptr_; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
autoptr(const autoptr<T>& ptr_) |
31 |
|
|
{ |
32 |
|
|
ptr = ptr_.ptr; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
4946916 |
~autoptr() |
36 |
|
|
{ |
37 |
2/2
✓ Branch 0 taken 1976586 times.
✓ Branch 1 taken 288 times.
|
2970062 |
if (ptr) free((void*)ptr); |
38 |
|
4946916 |
} |
39 |
|
|
}; |
40 |
|
|
|
41 |
|
|
template<typename T> class autoarray { |
42 |
|
|
public: |
43 |
|
|
int count; |
44 |
|
|
|
45 |
|
|
private: |
46 |
|
|
T* ptr; |
47 |
|
|
int bufferlen; |
48 |
|
|
|
49 |
|
|
T dummy; |
50 |
|
|
static const int default_size = 128; |
51 |
|
|
|
52 |
|
✗ |
const T& getconst(int id) const |
53 |
|
|
{ |
54 |
|
✗ |
if (id < 0) return dummy; |
55 |
|
✗ |
if (id >= count) return dummy; |
56 |
|
✗ |
return ptr[id]; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
3855978 |
T& get(int id) |
60 |
|
|
{ |
61 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1928004 times.
|
3855978 |
if (id < 0) return dummy; |
62 |
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1927984 times.
|
3855978 |
if (id >= bufferlen - 4) |
63 |
|
|
{ |
64 |
|
40 |
resize(id); |
65 |
|
|
} |
66 |
2/2
✓ Branch 0 taken 5542 times.
✓ Branch 1 taken 1922462 times.
|
3855978 |
if (id >= count) |
67 |
|
|
{ |
68 |
3/3
✓ Branch 0 taken 3888 times.
✓ Branch 1 taken 5542 times.
✓ Branch 2 taken 1726 times.
|
22276 |
for (int i = count;i <= id;i++) new(ptr + i) T(); |
69 |
|
11066 |
count = id + 1; |
70 |
|
|
} |
71 |
|
3855978 |
return ptr[id]; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
40 |
void resize(int size) |
75 |
|
|
{ |
76 |
|
40 |
int oldlen = count; |
77 |
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
|
80 |
while (bufferlen <= size + 4) bufferlen *= 2; |
78 |
|
40 |
T *old = ptr; |
79 |
|
40 |
ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen); |
80 |
2/2
✓ Branch 0 taken 3536 times.
✓ Branch 1 taken 20 times.
|
7112 |
for(int i = 0; i < oldlen; i++){ |
81 |
|
7072 |
new(ptr + i) T(); |
82 |
|
7072 |
ptr[i] = static_cast<T &&>(old[i]); |
83 |
|
|
} |
84 |
|
40 |
free(old); |
85 |
|
40 |
memset(ptr + oldlen, 0, (size_t)(bufferlen - oldlen) * sizeof(T)); |
86 |
|
40 |
} |
87 |
|
|
|
88 |
|
|
public: |
89 |
|
|
|
90 |
|
13340 |
void reset(int keep = 0) |
91 |
|
|
{ |
92 |
2/2
✓ Branch 0 taken 678 times.
✓ Branch 1 taken 6090 times.
|
13340 |
if (keep >= count) return; |
93 |
2/2
✓ Branch 0 taken 878 times.
✓ Branch 1 taken 628 times.
|
3012 |
for (int i = keep;i < count;i++) ptr[i].~T(); |
94 |
|
1350 |
memset(ptr + keep, 0, (size_t)(count - keep) * sizeof(T)); |
95 |
1/2
✓ Branch 0 taken 678 times.
✗ Branch 1 not taken.
|
1350 |
if (keep < bufferlen / 2) |
96 |
|
|
{ |
97 |
3/4
✓ Branch 0 taken 1848 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1170 times.
✓ Branch 3 taken 678 times.
|
3682 |
while (keep < bufferlen / 2 && bufferlen>8) bufferlen /= 2; |
98 |
|
1350 |
T *old = ptr; |
99 |
|
1350 |
ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen); |
100 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 678 times.
|
1362 |
for(int i = 0; i < keep; i++){ |
101 |
|
12 |
new(ptr + i) T(); |
102 |
|
12 |
ptr[i] = static_cast<T &&>(old[i]); |
103 |
|
|
} |
104 |
|
1350 |
free(old); |
105 |
|
|
|
106 |
|
|
} |
107 |
|
1350 |
count = keep; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
T& operator[](int id) |
111 |
|
|
{ |
112 |
|
1922124 |
return get(id); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
const T& operator[](int id) const |
116 |
|
|
{ |
117 |
|
✗ |
return getconst(id); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
operator T*() |
121 |
|
|
{ |
122 |
|
|
return ptr; |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
operator const T*() const |
126 |
|
|
{ |
127 |
|
|
return ptr; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
5020 |
T& append(const T& item) |
131 |
|
|
{ |
132 |
|
5020 |
return (get(count) = item); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
//insert is not safe for non pod types!!! |
136 |
|
|
void insert(int pos) |
137 |
|
|
{ |
138 |
|
|
if (pos<0 || pos>count) return; |
139 |
|
|
if (count >= bufferlen - 4) |
140 |
|
|
{ |
141 |
|
|
resize(count); |
142 |
|
|
} |
143 |
|
|
memmove(ptr + pos + 1, ptr + pos, sizeof(T)*(count - pos)); |
144 |
|
|
memset(ptr + pos, 0, sizeof(T)); |
145 |
|
|
new(ptr + pos) T(); |
146 |
|
|
count++; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
void insert(int pos, const T& item) |
150 |
|
|
{ |
151 |
|
|
if (pos<0 || pos>count) return; |
152 |
|
|
if (count >= bufferlen - 4) |
153 |
|
|
{ |
154 |
|
|
resize(count); |
155 |
|
|
} |
156 |
|
|
memmove(ptr + pos + 1, ptr + pos, sizeof(T)*(size_t)(count - pos)); |
157 |
|
|
memset(ptr + pos, 0, sizeof(T)); |
158 |
|
|
new(ptr + pos) T(); |
159 |
|
|
ptr[pos] = item; |
160 |
|
|
count++; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
96 |
void remove(int id) |
164 |
|
|
{ |
165 |
2/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
|
96 |
if (id < 0 || id >= count) return; |
166 |
|
96 |
count--; |
167 |
|
96 |
ptr[id].~T(); |
168 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
96 |
for(int i = id; i < count; i++){ |
169 |
|
✗ |
ptr[i] = static_cast<T &&>(ptr[i+1]); |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
|
173 |
|
8590 |
autoarray() |
174 |
|
3594 |
{ |
175 |
|
8590 |
ptr = (T*)malloc(sizeof(T) * default_size); |
176 |
|
8590 |
memset(ptr, 0, default_size*sizeof(T)); |
177 |
|
8590 |
bufferlen = default_size; |
178 |
|
8590 |
count = 0; |
179 |
|
8813 |
} |
180 |
|
|
|
181 |
|
6248 |
~autoarray() |
182 |
|
|
{ |
183 |
2/2
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 1336 times.
|
4062 |
for (int i = 0;i < count;i++) ptr[i].~T(); |
184 |
|
5578 |
free(ptr); |
185 |
|
6248 |
} |
186 |
|
|
|
187 |
|
|
#ifdef SERIALIZER |
188 |
|
|
void serialize(serializer& s) |
189 |
|
|
{ |
190 |
|
|
if (s.serializing) s(count); |
191 |
|
|
else |
192 |
|
|
{ |
193 |
|
|
int i; |
194 |
|
|
s(i); |
195 |
|
|
get(i - 1); |
196 |
|
|
} |
197 |
|
|
for (int i = 0;i < count;i++) s(ptr[i]); |
198 |
|
|
} |
199 |
|
|
#endif |
200 |
|
|
#define SERIALIZER_BANNED |
201 |
|
|
}; |
202 |
|
|
|