| 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 33 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 20 times.
✓ Branch 6 taken 41 times.
✓ Branch 7 taken 25 times.
✓ Branch 8 taken 36 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 62 times.
✓ Branch 11 taken 15 times.
|
354 |
return ptr; |
| 12 |
|
|
} |
| 13 |
|
|
|
| 14 |
|
|
autoptr& operator=(T ptr_) |
| 15 |
|
|
{ |
| 16 |
|
84 |
ptr = ptr_; |
| 17 |
|
69 |
return *this; |
| 18 |
|
|
} |
| 19 |
|
|
|
| 20 |
|
123 |
autoptr() |
| 21 |
|
|
{ |
| 22 |
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 39 times.
|
123 |
ptr = nullptr; |
| 23 |
|
|
} |
| 24 |
|
|
|
| 25 |
|
988279 |
autoptr(T ptr_) |
| 26 |
|
|
{ |
| 27 |
11/16
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2964 times.
✓ Branch 2 taken 436227 times.
✓ Branch 3 taken 1140 times.
✓ Branch 4 taken 2490 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1404 times.
✓ Branch 7 taken 333 times.
✓ Branch 8 taken 27 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 12 times.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 57 times.
|
985429 |
ptr = ptr_; |
| 28 |
|
|
} |
| 29 |
|
|
|
| 30 |
|
|
autoptr(const autoptr<T>& ptr_) |
| 31 |
|
|
{ |
| 32 |
|
|
ptr = ptr_.ptr; |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
993208 |
~autoptr() |
| 36 |
|
|
{ |
| 37 |
2/2
✓ Branch 0 taken 988303 times.
✓ Branch 1 taken 144 times.
|
993208 |
if (ptr) free((void*)ptr); |
| 38 |
|
993208 |
} |
| 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 |
|
1927989 |
T& get(int id) |
| 60 |
|
|
{ |
| 61 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 964002 times.
|
1927989 |
if (id < 0) return dummy; |
| 62 |
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 963992 times.
|
1927989 |
if (id >= bufferlen - 4) |
| 63 |
|
|
{ |
| 64 |
|
20 |
resize(id); |
| 65 |
|
|
} |
| 66 |
2/2
✓ Branch 0 taken 2771 times.
✓ Branch 1 taken 961231 times.
|
1927989 |
if (id >= count) |
| 67 |
|
|
{ |
| 68 |
3/3
✓ Branch 0 taken 1944 times.
✓ Branch 1 taken 2771 times.
✓ Branch 2 taken 863 times.
|
11138 |
for (int i = count;i <= id;i++) new(ptr + i) T(); |
| 69 |
|
5533 |
count = id + 1; |
| 70 |
|
|
} |
| 71 |
|
1927989 |
return ptr[id]; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
20 |
void resize(int size) |
| 75 |
|
|
{ |
| 76 |
|
20 |
int oldlen = count; |
| 77 |
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
40 |
while (bufferlen <= size + 4) bufferlen *= 2; |
| 78 |
|
20 |
T *old = ptr; |
| 79 |
|
20 |
ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen); |
| 80 |
2/2
✓ Branch 0 taken 1768 times.
✓ Branch 1 taken 10 times.
|
3556 |
for(int i = 0; i < oldlen; i++){ |
| 81 |
|
3536 |
new(ptr + i) T(); |
| 82 |
|
3536 |
ptr[i] = static_cast<T &&>(old[i]); |
| 83 |
|
|
} |
| 84 |
|
20 |
free(old); |
| 85 |
|
20 |
memset(ptr + oldlen, 0, (size_t)(bufferlen - oldlen) * sizeof(T)); |
| 86 |
|
20 |
} |
| 87 |
|
|
|
| 88 |
|
|
public: |
| 89 |
|
|
|
| 90 |
|
6670 |
void reset(int keep = 0) |
| 91 |
|
|
{ |
| 92 |
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 3045 times.
|
6670 |
if (keep >= count) return; |
| 93 |
2/2
✓ Branch 0 taken 439 times.
✓ Branch 1 taken 314 times.
|
1506 |
for (int i = keep;i < count;i++) ptr[i].~T(); |
| 94 |
|
675 |
memset(ptr + keep, 0, (size_t)(count - keep) * sizeof(T)); |
| 95 |
1/2
✓ Branch 0 taken 339 times.
✗ Branch 1 not taken.
|
675 |
if (keep < bufferlen / 2) |
| 96 |
|
|
{ |
| 97 |
3/4
✓ Branch 0 taken 924 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 585 times.
✓ Branch 3 taken 339 times.
|
1841 |
while (keep < bufferlen / 2 && bufferlen>8) bufferlen /= 2; |
| 98 |
|
675 |
T *old = ptr; |
| 99 |
|
675 |
ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen); |
| 100 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 339 times.
|
681 |
for(int i = 0; i < keep; i++){ |
| 101 |
|
6 |
new(ptr + i) T(); |
| 102 |
|
6 |
ptr[i] = static_cast<T &&>(old[i]); |
| 103 |
|
|
} |
| 104 |
|
675 |
free(old); |
| 105 |
|
|
|
| 106 |
|
|
} |
| 107 |
|
675 |
count = keep; |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
T& operator[](int id) |
| 111 |
|
|
{ |
| 112 |
|
961062 |
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 |
|
2510 |
T& append(const T& item) |
| 131 |
|
|
{ |
| 132 |
|
2510 |
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 |
|
48 |
void remove(int id) |
| 164 |
|
|
{ |
| 165 |
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
|
48 |
if (id < 0 || id >= count) return; |
| 166 |
|
48 |
count--; |
| 167 |
|
48 |
ptr[id].~T(); |
| 168 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
48 |
for(int i = id; i < count; i++){ |
| 169 |
|
✗ |
ptr[i] = static_cast<T &&>(ptr[i+1]); |
| 170 |
|
|
} |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
4295 |
autoarray() |
| 174 |
|
1797 |
{ |
| 175 |
|
4295 |
ptr = (T*)malloc(sizeof(T) * default_size); |
| 176 |
|
4295 |
memset(ptr, 0, default_size*sizeof(T)); |
| 177 |
|
4295 |
bufferlen = default_size; |
| 178 |
|
4295 |
count = 0; |
| 179 |
|
4295 |
} |
| 180 |
|
|
|
| 181 |
|
4310 |
~autoarray() |
| 182 |
|
|
{ |
| 183 |
2/2
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 1001 times.
|
2814 |
for (int i = 0;i < count;i++) ptr[i].~T(); |
| 184 |
|
4310 |
free(ptr); |
| 185 |
|
4310 |
} |
| 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 |
|
|
|