asar coverage - build #291


src/asar/
File: src/asar/autoarray.h
Date: 2025-03-10 01:11:53
Lines:
66/73
90.4%
Functions:
80/94
85.1%
Branches:
31/41
75.6%

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 4194878 operator T() const
10 {
11 4194878 return ptr;
12 }
13
14 378 autoptr& operator=(T ptr_)
15 {
16 378 ptr = ptr_;
17 378 return *this;
18 }
19
20 456 autoptr()
21 {
22 456 ptr = nullptr;
23 456 }
24
25 1988838 autoptr(T ptr_)
26 {
27 1988838 ptr = ptr_;
28 1988838 }
29
30 autoptr(const autoptr<T>& ptr_)
31 {
32 ptr = ptr_.ptr;
33 }
34
35 1989229 ~autoptr()
36 {
37
2/2
✓ Branch 0 taken 1979464 times.
✓ Branch 1 taken 288 times.
1989229 if (ptr) free((void*)ptr);
38 1989229 }
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 4144230 T& get(int id)
60 {
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2072130 times.
4144230 if (id < 0) return dummy;
62
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 2072086 times.
4144230 if (id >= bufferlen - 4)
63 {
64 88 resize(id);
65 }
66
2/2
✓ Branch 0 taken 77584 times.
✓ Branch 1 taken 1994546 times.
4144230 if (id >= count)
67 {
68
3/3
✓ Branch 0 taken 75720 times.
✓ Branch 1 taken 77584 times.
✓ Branch 2 taken 1936 times.
310444 for (int i = count;i <= id;i++) new(ptr + i) T();
69 155150 count = id + 1;
70 }
71 4144230 return ptr[id];
72 }
73
74 88 void resize(int size)
75 {
76 88 int oldlen = count;
77
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
176 while (bufferlen <= size + 4) bufferlen *= 2;
78 88 T *old = ptr;
79 88 ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen);
80
2/2
✓ Branch 0 taken 135280 times.
✓ Branch 1 taken 44 times.
270648 for(int i = 0; i < oldlen; i++){
81 270560 new(ptr + i) T();
82 270560 ptr[i] = static_cast<T &&>(old[i]);
83 }
84 88 free(old);
85 88 memset(ptr + oldlen, 0, (size_t)(bufferlen - oldlen) * sizeof(T));
86 88 }
87
88 public:
89
90 14098 void reset(int keep = 0)
91 {
92
2/2
✓ Branch 0 taken 6424 times.
✓ Branch 1 taken 728 times.
14098 if (keep >= count) return;
93
3/3
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 988 times.
✓ Branch 2 taken 682 times.
3460 for (int i = keep;i < count;i++) ptr[i].~T();
94 1450 memset(ptr + keep, 0, (size_t)(count - keep) * sizeof(T));
95
1/2
✓ Branch 0 taken 728 times.
✗ Branch 1 not taken.
1450 if (keep < bufferlen / 2)
96 {
97
3/4
✓ Branch 0 taken 1970 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
✓ Branch 3 taken 728 times.
3926 while (keep < bufferlen / 2 && bufferlen>8) bufferlen /= 2;
98 1450 T *old = ptr;
99 1450 ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen);
100
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 728 times.
1462 for(int i = 0; i < keep; i++){
101 12 new(ptr + i) T();
102 12 ptr[i] = static_cast<T &&>(old[i]);
103 }
104 1450 free(old);
105
106 }
107 1450 count = keep;
108 }
109
110 3916892 T& operator[](int id)
111 {
112 3916892 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 77210 T& append(const T& item)
131 {
132 77210 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 not taken.
✓ Branch 3 taken 54 times.
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 9000 autoarray()
174 4184 {
175 9000 ptr = (T*)malloc(sizeof(T) * default_size);
176 9000 memset(ptr, 0, default_size*sizeof(T));
177 9000 bufferlen = default_size;
178 9000 count = 0;
179 9000 }
180
181 5798 ~autoarray()
182 {
183
3/3
✓ Branch 0 taken 38030 times.
✓ Branch 1 taken 2386 times.
✓ Branch 2 taken 1391 times.
83414 for (int i = 0;i < count;i++) ptr[i].~T();
184 5798 free(ptr);
185 5798 }
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