| 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 |
13/13
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 5208 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 35 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 49 times.
✓ Branch 7 taken 52 times.
✓ Branch 8 taken 37 times.
✓ Branch 9 taken 20 times.
✓ Branch 10 taken 44 times.
✓ Branch 11 taken 53 times.
✓ Branch 12 taken 11 times.
|
6958 |
return ptr; |
| 12 |
|
|
} |
| 13 |
|
|
|
| 14 |
|
|
autoptr& operator=(T ptr_) |
| 15 |
|
|
{ |
| 16 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 402 times.
|
438 |
ptr = ptr_; |
| 17 |
|
|
return *this; |
| 18 |
|
|
} |
| 19 |
|
|
|
| 20 |
|
558 |
autoptr() |
| 21 |
|
|
{ |
| 22 |
2/2
✓ Branch 0 taken 438 times.
✓ Branch 1 taken 120 times.
|
558 |
ptr = nullptr; |
| 23 |
|
|
} |
| 24 |
|
|
|
| 25 |
|
120164 |
autoptr(T ptr_) |
| 26 |
|
|
{ |
| 27 |
14/19
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5922 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 744 times.
✓ Branch 4 taken 4980 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2808 times.
✓ Branch 7 taken 5244 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 24 times.
✓ Branch 10 taken 18 times.
✓ Branch 11 taken 33 times.
✓ Branch 12 taken 12 times.
✓ Branch 13 taken 165 times.
✓ Branch 14 taken 12 times.
✓ Branch 15 taken 216 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 60 times.
✗ Branch 18 not taken.
|
114470 |
ptr = ptr_; |
| 28 |
|
|
} |
| 29 |
|
|
|
| 30 |
|
|
autoptr(const autoptr<T>& ptr_) |
| 31 |
|
|
{ |
| 32 |
|
|
ptr = ptr_.ptr; |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
320246 |
~autoptr() |
| 36 |
|
|
{ |
| 37 |
2/2
✓ Branch 0 taken 120476 times.
✓ Branch 1 taken 330 times.
|
190464 |
if (ptr) free((void*)ptr); |
| 38 |
|
320246 |
} |
| 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 |
|
2505356 |
T& get(int id) |
| 60 |
|
|
{ |
| 61 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2091794 times.
|
2505356 |
if (id < 0) return dummy; |
| 62 |
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2091752 times.
|
2505356 |
if (id >= bufferlen - 4) |
| 63 |
|
|
{ |
| 64 |
|
84 |
resize(id); |
| 65 |
|
|
} |
| 66 |
2/2
✓ Branch 0 taken 220840 times.
✓ Branch 1 taken 1870954 times.
|
2505356 |
if (id >= count) |
| 67 |
|
|
{ |
| 68 |
3/3
✓ Branch 0 taken 7374 times.
✓ Branch 1 taken 220864 times.
✓ Branch 2 taken 213554 times.
|
883508 |
for (int i = count;i <= id;i++) new(ptr + i) T(); |
| 69 |
|
441642 |
count = id + 1; |
| 70 |
|
|
} |
| 71 |
|
2505356 |
return ptr[id]; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
84 |
void resize(int size) |
| 75 |
|
|
{ |
| 76 |
|
84 |
int oldlen = count; |
| 77 |
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
168 |
while (bufferlen <= size + 4) bufferlen *= 2; |
| 78 |
|
84 |
T *old = ptr; |
| 79 |
|
84 |
ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen); |
| 80 |
2/2
✓ Branch 0 taken 39800 times.
✓ Branch 1 taken 42 times.
|
79684 |
for(int i = 0; i < oldlen; i++){ |
| 81 |
|
79600 |
new(ptr + i) T(); |
| 82 |
|
79600 |
ptr[i] = static_cast<T &&>(old[i]); |
| 83 |
|
|
} |
| 84 |
|
84 |
free(old); |
| 85 |
|
84 |
memset(ptr + oldlen, 0, (size_t)(bufferlen - oldlen) * sizeof(T)); |
| 86 |
|
84 |
} |
| 87 |
|
|
|
| 88 |
|
|
public: |
| 89 |
|
|
|
| 90 |
|
18788 |
void reset(int keep = 0) |
| 91 |
|
|
{ |
| 92 |
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 8604 times.
|
18788 |
if (keep >= count) return; |
| 93 |
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 728 times.
|
3520 |
for (int i = keep;i < count;i++) ptr[i].~T(); |
| 94 |
|
1800 |
memset(ptr + keep, 0, (size_t)(count - keep) * sizeof(T)); |
| 95 |
1/2
✓ Branch 0 taken 1024 times.
✗ Branch 1 not taken.
|
1800 |
if (keep < bufferlen / 2) |
| 96 |
|
|
{ |
| 97 |
3/4
✓ Branch 0 taken 3410 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2386 times.
✓ Branch 3 taken 1024 times.
|
5596 |
while (keep < bufferlen / 2 && bufferlen>8) bufferlen /= 2; |
| 98 |
|
1800 |
T *old = ptr; |
| 99 |
|
1800 |
ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen); |
| 100 |
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1024 times.
|
1836 |
for(int i = 0; i < keep; i++){ |
| 101 |
|
36 |
new(ptr + i) T(); |
| 102 |
|
36 |
ptr[i] = static_cast<T &&>(old[i]); |
| 103 |
|
|
} |
| 104 |
|
1800 |
free(old); |
| 105 |
|
|
|
| 106 |
|
|
} |
| 107 |
|
1800 |
count = keep; |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
T& operator[](int id) |
| 111 |
|
|
{ |
| 112 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
1239562 |
return get(id); |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
const T& operator[](int id) const |
| 116 |
|
|
{ |
| 117 |
|
✗ |
return getconst(id); |
| 118 |
|
|
} |
| 119 |
|
|
|
| 120 |
|
|
operator T*() |
| 121 |
|
|
{ |
| 122 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
|
118 |
return ptr; |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
operator const T*() const |
| 126 |
|
|
{ |
| 127 |
|
|
return ptr; |
| 128 |
|
|
} |
| 129 |
|
|
|
| 130 |
|
425422 |
T& append(const T& item) |
| 131 |
|
|
{ |
| 132 |
|
425422 |
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 |
|
159436 |
void insert(int pos, const T& item) |
| 150 |
|
|
{ |
| 151 |
2/4
✓ Branch 0 taken 159436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 159436 times.
✗ Branch 3 not taken.
|
159436 |
if (pos<0 || pos>count) return; |
| 152 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159436 times.
|
159436 |
if (count >= bufferlen - 4) |
| 153 |
|
|
{ |
| 154 |
|
✗ |
resize(count); |
| 155 |
|
|
} |
| 156 |
|
159436 |
memmove(ptr + pos + 1, ptr + pos, sizeof(T)*(size_t)(count - pos)); |
| 157 |
|
159436 |
memset(ptr + pos, 0, sizeof(T)); |
| 158 |
|
159436 |
new(ptr + pos) T(); |
| 159 |
|
159436 |
ptr[pos] = item; |
| 160 |
|
159436 |
count++; |
| 161 |
|
|
} |
| 162 |
|
|
|
| 163 |
|
368378 |
void remove(int id) |
| 164 |
|
|
{ |
| 165 |
2/4
✓ Branch 0 taken 367820 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 367820 times.
✗ Branch 3 not taken.
|
368378 |
if (id < 0 || id >= count) return; |
| 166 |
|
368378 |
count--; |
| 167 |
|
368378 |
ptr[id].~T(); |
| 168 |
2/2
✓ Branch 0 taken 194998 times.
✓ Branch 1 taken 367820 times.
|
563376 |
for(int i = id; i < count; i++){ |
| 169 |
|
194998 |
ptr[i] = static_cast<T &&>(ptr[i+1]); |
| 170 |
|
|
} |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
14466 |
autoarray() |
| 174 |
|
6762 |
{ |
| 175 |
|
14466 |
ptr = (T*)malloc(sizeof(T) * default_size); |
| 176 |
|
14466 |
memset(ptr, 0, default_size*sizeof(T)); |
| 177 |
|
14466 |
bufferlen = default_size; |
| 178 |
|
14466 |
count = 0; |
| 179 |
|
15027 |
} |
| 180 |
|
|
|
| 181 |
|
11268 |
~autoarray() |
| 182 |
|
|
{ |
| 183 |
2/2
✓ Branch 0 taken 2999 times.
✓ Branch 1 taken 2565 times.
|
11128 |
for (int i = 0;i < count;i++) ptr[i].~T(); |
| 184 |
|
9884 |
free(ptr); |
| 185 |
|
11268 |
} |
| 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 |
|
|
|