asar coverage - build #100


src/asar/
File: src/asar/autoarray.h
Date: 2024-01-19 20:26:51
Lines:
84/84
100.0%
Functions:
138/145
95.2%
Branches:
40/55
72.7%

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 713105 operator T() const
10 {
11 713105 return ptr;
12 }
13
14 2070 autoptr& operator=(T ptr_)
15 {
16 2070 ptr = ptr_;
17 2070 return *this;
18 }
19
20 2430 autoptr()
21 {
22 2430 ptr = nullptr;
23 2430 }
24
25 388755 autoptr(T ptr_)
26 {
27 388755 ptr = ptr_;
28 388755 }
29
30 autoptr(const autoptr<T>& ptr_)
31 {
32 ptr = ptr_.ptr;
33 }
34
35 391035 ~autoptr()
36 {
37
2/2
✓ Branch 0 taken 361587 times.
✓ Branch 1 taken 990 times.
391035 if (ptr) free((void*)ptr);
38 391035 }
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 42576 const T& getconst(int id) const
53 {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21288 times.
42576 if (id < 0) return dummy;
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21288 times.
42576 if (id >= count) return dummy;
56 42576 return ptr[id];
57 }
58
59 7889685 T& get(int id)
60 {
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6456732 times.
7889685 if (id < 0) return dummy;
62
2/2
✓ Branch 0 taken 817 times.
✓ Branch 1 taken 6455915 times.
7889685 if (id >= bufferlen - 4)
63 {
64 1634 resize(id);
65 }
66
2/2
✓ Branch 0 taken 677835 times.
✓ Branch 1 taken 5778897 times.
7889685 if (id >= count)
67 {
68
5/7
✓ Branch 0 taken 22716 times.
✓ Branch 1 taken 677911 times.
✓ Branch 2 taken 655299 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2711660 for (int i = count;i <= id;i++) new(ptr + i) T();
69 1355556 count = id + 1;
70 }
71 7889685 return ptr[id];
72 }
73
74 1685 void resize(int size)
75 {
76 1685 int oldlen = count;
77
2/2
✓ Branch 0 taken 868 times.
✓ Branch 1 taken 868 times.
3370 while (bufferlen <= size + 4) bufferlen *= 2;
78 1685 T *old = ptr;
79 1685 ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen);
80
2/2
✓ Branch 0 taken 144416 times.
✓ Branch 1 taken 868 times.
289929 for(int i = 0; i < oldlen; i++){
81
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
288244 new(ptr + i) T();
82 288244 ptr[i] = static_cast<T &&>(old[i]);
83 }
84 1685 free(old);
85 1685 memset(ptr + oldlen, 0, (size_t)(bufferlen - oldlen) * sizeof(T));
86 1685 }
87
88 public:
89
90 64405 void reset(int keep = 0)
91 {
92
2/2
✓ Branch 0 taken 28323 times.
✓ Branch 1 taken 4598 times.
64405 if (keep >= count) return;
93
3/3
✓ Branch 0 taken 16251 times.
✓ Branch 1 taken 6565 times.
✓ Branch 2 taken 2554 times.
48093 for (int i = keep;i < count;i++) ptr[i].~T();
94 8495 memset(ptr + keep, 0, (size_t)(count - keep) * sizeof(T));
95
1/2
✓ Branch 0 taken 4598 times.
✗ Branch 1 not taken.
8495 if (keep < bufferlen / 2)
96 {
97
3/4
✓ Branch 0 taken 9029 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4431 times.
✓ Branch 3 taken 4598 times.
16278 while (keep < bufferlen / 2 && bufferlen>8) bufferlen /= 2;
98 8495 T *old = ptr;
99 8495 ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen);
100
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 4598 times.
8603 for(int i = 0; i < keep; i++){
101 108 new(ptr + i) T();
102 108 ptr[i] = static_cast<T &&>(old[i]);
103 }
104 8495 free(old);
105
106 }
107 8495 count = keep;
108 }
109
110 6545058 T& operator[](int id)
111 {
112 6545058 return get(id);
113 }
114
115 42576 const T& operator[](int id) const
116 {
117 42576 return getconst(id);
118 }
119
120 3990 operator T*()
121 {
122 3990 return ptr;
123 }
124
125 operator const T*() const
126 {
127 return ptr;
128 }
129
130 1312199 T& append(const T& item)
131 {
132 1312199 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 476986 void insert(int pos, const T& item)
150 {
151
2/4
✓ Branch 0 taken 476986 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 476986 times.
476986 if (pos<0 || pos>count) return;
152
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 476935 times.
476986 if (count >= bufferlen - 4)
153 {
154 51 resize(count);
155 }
156 476986 memmove(ptr + pos + 1, ptr + pos, sizeof(T)*(size_t)(count - pos));
157 476986 memset(ptr + pos, 0, sizeof(T));
158 476986 new(ptr + pos) T();
159 476986 ptr[pos] = item;
160 476986 count++;
161 }
162
163 1104950 void remove(int id)
164 {
165
2/4
✓ Branch 0 taken 1103276 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1103276 times.
1104950 if (id < 0 || id >= count) return;
166 1104950 count--;
167 1104950 ptr[id].~T();
168
2/2
✓ Branch 0 taken 582915 times.
✓ Branch 1 taken 1103276 times.
1687865 for(int i = id; i < count; i++){
169 582915 ptr[i] = static_cast<T &&>(ptr[i+1]);
170 }
171 }
172
173 25829 autoarray()
174 13882 {
175 25829 ptr = (T*)malloc(sizeof(T) * default_size);
176 25829 memset(ptr, 0, default_size*sizeof(T));
177 25829 bufferlen = default_size;
178 25829 count = 0;
179 25829 }
180
181 21127 ~autoarray()
182 {
183
3/3
✓ Branch 0 taken 5494 times.
✓ Branch 1 taken 27395 times.
✓ Branch 2 taken 5620 times.
76655 for (int i = 0;i < count;i++) ptr[i].~T();
184 21127 free(ptr);
185 21127 }
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