| Line |
Branch |
Exec |
Source |
| 1 |
|
|
//API for assocarr<mytype> myarr: |
| 2 |
|
|
//myarr.exists("index") |
| 3 |
|
|
// Returns a boolean telling whether the entry exists. |
| 4 |
|
|
// Complexity: O(log n). |
| 5 |
|
|
//myarr.find("index") |
| 6 |
|
|
// Returns a mytype& corresponding to the relevant entry. If it doesn't exist, behaviour is undefined. |
| 7 |
|
|
// Complexity: O(log n). |
| 8 |
|
|
//myarr.create("index") |
| 9 |
|
|
// Returns a mytype& corresponding to the relevant entry. If it doesn't exist, it's created. |
| 10 |
|
|
// Complexity: Worst case O(n), reduced to amortized O(log n) if the element should be at the end (strict weak ordering), or O(log n) if the element |
| 11 |
|
|
// exists. |
| 12 |
|
|
//myarr.remove("index") |
| 13 |
|
|
// Removes the element corresponding to the relevant entry. If it doesn't exist, this structure is left untouched. |
| 14 |
|
|
// Complexity: Worst case O(n), reduced to amortized O(log n) if the element is at the end. |
| 15 |
|
|
//myarr.reset() |
| 16 |
|
|
// Clears the data structure, calling all destructors. It is safe to call this function on an empty structure. |
| 17 |
|
|
// Complexity: O(n). |
| 18 |
|
|
//myarr.move("from", "to") |
| 19 |
|
|
// Moves an element from index "from" to "to". If the source doesn't exist, behaviour is undefined. If the target exists, no action is performed. |
| 20 |
|
|
// Complexity: O(n). |
| 21 |
|
|
//myarr["index"] |
| 22 |
|
|
// Similar to myarr.create("index"), but if the returned entry isn't changed by the next call to any function in the same structure, it's removed. |
| 23 |
|
|
// if (myarr["index"]) is safe if type can cast to a bool, and myarr["index"]=4 is also valid if the assignment is valid for a type&. However, it is |
| 24 |
|
|
// not safe to use assocarr<assocarr<int> > myarr; if (myarr["3"]["4"]), since the inner assocarr won't know that it's supposed to be garbage collected, |
| 25 |
|
|
// and the outer one sees that the inner one changed, so you get pollution and memory waste. However, if (myarr["3"].exists("4")) is safe. |
| 26 |
|
|
// Complexity: Same as myarr.create(). |
| 27 |
|
|
//myarr.each(func) |
| 28 |
|
|
// Calls func() for each entry in the structure. func must match the prototype void func(const char * index, mytype& val), but can be a lambda. No |
| 29 |
|
|
// non-const function may be called on this structure from inside func(), but it is safe to call const functions of the structure. The function |
| 30 |
|
|
// calls are in the same order as the indexes, in a strict weak ordering. |
| 31 |
|
|
// Complexity: O(n). |
| 32 |
|
|
//Space usage: O(n). |
| 33 |
|
|
//C++ version: C++98 or C++03, not sure which. |
| 34 |
|
|
//Serializer support: Yes, if mytype is serializable. |
| 35 |
|
|
//"Undefined behaviour" means "segfault" in most cases. |
| 36 |
|
|
|
| 37 |
|
|
#pragma once |
| 38 |
|
|
|
| 39 |
|
|
#include <initializer_list> |
| 40 |
|
|
#include "std-includes.h" |
| 41 |
|
|
#include "libmisc.h"//bitround |
| 42 |
|
|
|
| 43 |
|
|
//just a helper for initializer list |
| 44 |
|
|
template<typename T1, typename T2> struct pair{ |
| 45 |
|
|
T1 first; |
| 46 |
|
|
T2 second; |
| 47 |
|
|
}; |
| 48 |
|
|
|
| 49 |
|
|
template<typename right> class assocarr { |
| 50 |
|
|
public: |
| 51 |
|
|
int num; |
| 52 |
|
|
|
| 53 |
|
|
private: |
| 54 |
|
|
|
| 55 |
|
|
const char ** indexes; |
| 56 |
|
|
right ** ptr; |
| 57 |
|
|
int bufferlen; |
| 58 |
|
|
|
| 59 |
|
|
char lastone[sizeof(right)]; |
| 60 |
|
|
int lastid; |
| 61 |
|
|
|
| 62 |
|
406500 |
void collectgarbage(const char * ifnotmatch=nullptr) |
| 63 |
|
|
{ |
| 64 |
2/2
✓ Branch 0 taken 226840 times.
✓ Branch 1 taken 88152 times.
|
406500 |
if (lastid==-1) return; |
| 65 |
4/4
✓ Branch 0 taken 87952 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 87898 times.
|
176304 |
if (ifnotmatch && !strcmp(indexes[lastid], ifnotmatch)) return; |
| 66 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88098 times.
|
176196 |
if (!memcmp(ptr[lastid], lastone, sizeof(right))) |
| 67 |
|
|
{ |
| 68 |
|
✗ |
int tmpid=lastid; |
| 69 |
|
✗ |
lastid=-1; |
| 70 |
|
✗ |
remove(indexes[tmpid]); |
| 71 |
|
|
} |
| 72 |
|
176196 |
lastid=-1; |
| 73 |
|
|
} |
| 74 |
|
|
|
| 75 |
|
403032 |
right& rawadd(const char * index, bool collect) |
| 76 |
|
|
{ |
| 77 |
|
403032 |
collectgarbage(index); |
| 78 |
|
403032 |
int loc=0; |
| 79 |
|
403032 |
int skip= (int)bitround((unsigned int)num); |
| 80 |
2/2
✓ Branch 0 taken 1666810 times.
✓ Branch 1 taken 98810 times.
|
2464464 |
while (skip) |
| 81 |
|
|
{ |
| 82 |
|
|
int dir; |
| 83 |
2/2
✓ Branch 0 taken 86487 times.
✓ Branch 1 taken 1580323 times.
|
2277018 |
if (loc>=num) dir=1; |
| 84 |
|
2111508 |
else dir=strcmp(indexes[loc], index); |
| 85 |
2/2
✓ Branch 0 taken 212852 times.
✓ Branch 1 taken 1453958 times.
|
2277018 |
if (!dir) return ptr[loc][0]; |
| 86 |
|
2062730 |
skip/=2; |
| 87 |
2/2
✓ Branch 0 taken 631672 times.
✓ Branch 1 taken 822286 times.
|
2062730 |
if (dir>0) loc-=skip; |
| 88 |
|
1199790 |
else loc+=skip; |
| 89 |
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 1453284 times.
|
2062730 |
if (loc<0) |
| 90 |
|
|
{ |
| 91 |
|
1298 |
loc=0; |
| 92 |
|
1298 |
break; |
| 93 |
|
|
} |
| 94 |
|
|
} |
| 95 |
4/4
✓ Branch 0 taken 92935 times.
✓ Branch 1 taken 6549 times.
✓ Branch 2 taken 65935 times.
✓ Branch 3 taken 27000 times.
|
188744 |
if (loc<num && strcmp(indexes[loc], index)<0) loc++; |
| 96 |
2/2
✓ Branch 0 taken 11880 times.
✓ Branch 1 taken 87604 times.
|
188744 |
if (num==bufferlen) |
| 97 |
|
|
{ |
| 98 |
2/2
✓ Branch 0 taken 2338 times.
✓ Branch 1 taken 9542 times.
|
18546 |
if (!num) bufferlen=1; |
| 99 |
|
15084 |
else bufferlen*=2; |
| 100 |
|
18546 |
ptr=(right**)realloc(ptr, sizeof(right*)*(size_t)bufferlen); |
| 101 |
|
18546 |
indexes=(const char**)realloc(indexes, sizeof(const char *)*(size_t)bufferlen); |
| 102 |
|
|
} |
| 103 |
|
188744 |
num++; |
| 104 |
|
188744 |
memmove(indexes+loc+1, indexes+loc, sizeof(const char *)*(size_t)(num-loc-1)); |
| 105 |
|
188744 |
memmove(ptr+loc+1, ptr+loc, sizeof(right*)*(size_t)(num-loc-1)); |
| 106 |
|
188744 |
indexes[loc]= duplicate_string(index); |
| 107 |
|
188744 |
ptr[loc]=(right*)malloc(sizeof(right)); |
| 108 |
|
188744 |
memset(ptr[loc], 0, sizeof(right)); |
| 109 |
3/6
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10566 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
188744 |
new(ptr[loc]) right; |
| 110 |
2/2
✓ Branch 0 taken 88636 times.
✓ Branch 1 taken 10848 times.
|
188744 |
if (collect) |
| 111 |
|
|
{ |
| 112 |
|
177272 |
lastid=loc; |
| 113 |
|
177272 |
memcpy(lastone, ptr[loc], sizeof(right)); |
| 114 |
|
|
} |
| 115 |
|
188744 |
return ptr[loc][0]; |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
1089066 |
int find_i(const char * index) const |
| 119 |
|
|
{ |
| 120 |
|
1089066 |
int loc=0; |
| 121 |
|
1089066 |
int skip=(int)bitround((unsigned int)num); |
| 122 |
2/2
✓ Branch 0 taken 4598770 times.
✓ Branch 1 taken 222948 times.
|
4849288 |
while (skip) |
| 123 |
|
|
{ |
| 124 |
|
|
int dir; |
| 125 |
2/2
✓ Branch 0 taken 7827 times.
✓ Branch 1 taken 4590943 times.
|
4625444 |
if (loc>=num) dir=1; |
| 126 |
|
4617448 |
else dir=strcmp(indexes[loc], index); |
| 127 |
2/2
✓ Branch 0 taken 856634 times.
✓ Branch 1 taken 3742136 times.
|
4625444 |
if (!dir) return loc; |
| 128 |
|
3760524 |
skip/=2; |
| 129 |
2/2
✓ Branch 0 taken 1588046 times.
✓ Branch 1 taken 2154090 times.
|
3760524 |
if (dir>0) loc-=skip; |
| 130 |
|
2165920 |
else loc+=skip; |
| 131 |
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 3741916 times.
|
3760524 |
if (loc<0) return -1; |
| 132 |
|
|
} |
| 133 |
|
223844 |
return -1; |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
public: |
| 137 |
|
|
|
| 138 |
|
657708 |
bool exists(const char * index) const |
| 139 |
|
|
{ |
| 140 |
|
657708 |
return find_i(index)>=0; |
| 141 |
|
|
} |
| 142 |
|
|
|
| 143 |
|
855468 |
right& find(const char * index) const |
| 144 |
|
|
{ |
| 145 |
|
855468 |
return ptr[find_i(index)][0]; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
223792 |
right& create(const char * index) |
| 149 |
|
|
{ |
| 150 |
|
223792 |
return rawadd(index, false); |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
60 |
void remove(const char * index) |
| 154 |
|
|
{ |
| 155 |
|
60 |
collectgarbage(); |
| 156 |
|
60 |
int loc=0; |
| 157 |
|
60 |
int skip= (int)bitround((unsigned int)num); |
| 158 |
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
|
240 |
while (skip) |
| 159 |
|
|
{ |
| 160 |
|
|
int dir; |
| 161 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 234 times.
|
240 |
if (loc>=num) dir=1; |
| 162 |
|
234 |
else dir=strcmp(indexes[loc], index); |
| 163 |
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 180 times.
|
240 |
if (!dir) |
| 164 |
|
|
{ |
| 165 |
|
60 |
free((void*)indexes[loc]); |
| 166 |
|
60 |
ptr[loc]->~right(); |
| 167 |
|
60 |
free(ptr[loc]); |
| 168 |
|
60 |
memmove(indexes+loc, indexes+loc+1, sizeof(const char *)*(size_t)(num-loc-1)); |
| 169 |
|
60 |
memmove(ptr+loc, ptr+loc+1, sizeof(right*)*(size_t)(num-loc-1)); |
| 170 |
|
60 |
num--; |
| 171 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 |
if (num==bufferlen/2) |
| 172 |
|
|
{ |
| 173 |
|
✗ |
bufferlen/=2; |
| 174 |
|
✗ |
ptr=(right**)realloc(ptr, sizeof(right*)*(size_t)bufferlen); |
| 175 |
|
✗ |
indexes=(const char**)realloc(indexes, sizeof(const char *)*(size_t)bufferlen); |
| 176 |
|
|
} |
| 177 |
|
60 |
return; |
| 178 |
|
|
} |
| 179 |
|
180 |
skip/=2; |
| 180 |
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 120 times.
|
180 |
if (dir>0) loc-=skip; |
| 181 |
|
120 |
else loc+=skip; |
| 182 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
|
180 |
if (loc<0) return; |
| 183 |
|
|
} |
| 184 |
|
|
} |
| 185 |
|
|
|
| 186 |
|
|
void move(const char * from, const char * to) |
| 187 |
|
|
{ |
| 188 |
|
|
collectgarbage(); |
| 189 |
|
|
int frompos=find_i(from); |
| 190 |
|
|
int topos=0; |
| 191 |
|
|
int skip=bitround(num); |
| 192 |
|
|
while (skip) |
| 193 |
|
|
{ |
| 194 |
|
|
int dir; |
| 195 |
|
|
if (topos>=num) dir=1; |
| 196 |
|
|
else dir=strcmp(indexes[topos], to); |
| 197 |
|
|
if (!dir) return; |
| 198 |
|
|
skip/=2; |
| 199 |
|
|
if (dir>0) topos-=skip; |
| 200 |
|
|
else topos+=skip; |
| 201 |
|
|
if (topos<0) |
| 202 |
|
|
{ |
| 203 |
|
|
topos=0; |
| 204 |
|
|
break; |
| 205 |
|
|
} |
| 206 |
|
|
} |
| 207 |
|
|
if (topos<num && strcmp(indexes[topos], to)<0) topos++; |
| 208 |
|
|
right * tmp=ptr[frompos]; |
| 209 |
|
|
if (topos==frompos || topos==frompos+1) |
| 210 |
|
|
{ |
| 211 |
|
|
free((void*)indexes[frompos]); |
| 212 |
|
|
indexes[frompos]= duplicate_string(to); |
| 213 |
|
|
} |
| 214 |
|
|
else if (topos>frompos) |
| 215 |
|
|
{ |
| 216 |
|
|
free((void*)indexes[frompos]); |
| 217 |
|
|
memmove(indexes+frompos, indexes+frompos+1, sizeof(const char *)*(topos-frompos-1)); |
| 218 |
|
|
memmove(ptr+frompos, ptr+frompos+1, sizeof(right*)*(topos-frompos-1)); |
| 219 |
|
|
ptr[topos-1]=tmp; |
| 220 |
|
|
indexes[topos-1]= duplicate_string(to);//I wonder what the fuck I'm doing. |
| 221 |
|
|
} |
| 222 |
|
|
else |
| 223 |
|
|
{ |
| 224 |
|
|
free((void*)indexes[frompos]); |
| 225 |
|
|
memmove(indexes+topos+1, indexes+topos, sizeof(const char *)*(frompos-topos)); |
| 226 |
|
|
memmove(ptr+topos+1, ptr+topos, sizeof(right*)*(frompos-topos)); |
| 227 |
|
|
ptr[topos]=tmp; |
| 228 |
|
|
indexes[topos]= duplicate_string(to); |
| 229 |
|
|
} |
| 230 |
|
|
} |
| 231 |
|
|
|
| 232 |
|
7652 |
void reset() |
| 233 |
|
|
{ |
| 234 |
2/2
✓ Branch 0 taken 78214 times.
✓ Branch 1 taken 4176 times.
|
164028 |
for (int i=0;i<num;i++) |
| 235 |
|
|
{ |
| 236 |
|
156376 |
free((void*)indexes[i]); |
| 237 |
|
156376 |
ptr[i]->~right(); |
| 238 |
|
156376 |
free(ptr[i]); |
| 239 |
|
|
} |
| 240 |
|
7652 |
free(indexes); |
| 241 |
|
7652 |
free(ptr); |
| 242 |
|
7652 |
indexes=nullptr; |
| 243 |
|
7652 |
ptr= nullptr; |
| 244 |
|
7652 |
num=0; |
| 245 |
|
7652 |
bufferlen=0; |
| 246 |
|
7652 |
lastid=-1; |
| 247 |
|
7652 |
} |
| 248 |
|
|
|
| 249 |
|
3600 |
assocarr() |
| 250 |
|
|
{ |
| 251 |
|
3600 |
indexes= nullptr; |
| 252 |
|
3600 |
ptr= nullptr; |
| 253 |
|
3600 |
num=0; |
| 254 |
|
3600 |
bufferlen=0; |
| 255 |
|
3600 |
lastid=-1; |
| 256 |
|
3600 |
} |
| 257 |
|
|
|
| 258 |
|
200 |
assocarr(std::initializer_list<pair<const char *, right>> list) |
| 259 |
|
|
{ |
| 260 |
|
200 |
indexes= nullptr; |
| 261 |
|
200 |
ptr= nullptr; |
| 262 |
|
200 |
num=0; |
| 263 |
|
200 |
bufferlen=0; |
| 264 |
|
200 |
lastid=-1; |
| 265 |
|
|
|
| 266 |
2/2
✓ Branch 0 taken 12800 times.
✓ Branch 1 taken 200 times.
|
13000 |
for(auto &item : list){ |
| 267 |
|
12800 |
rawadd(item.first, true) = item.second; |
| 268 |
|
|
} |
| 269 |
|
200 |
} |
| 270 |
|
|
|
| 271 |
|
2100 |
~assocarr() |
| 272 |
|
|
{ |
| 273 |
|
2100 |
reset(); |
| 274 |
|
2100 |
} |
| 275 |
|
|
|
| 276 |
|
153640 |
right& operator[](const char * index) |
| 277 |
|
|
{ |
| 278 |
|
153640 |
return rawadd(index, true); |
| 279 |
|
|
} |
| 280 |
|
|
|
| 281 |
|
|
//void(*func)(const char * key, right& value) |
| 282 |
|
3608 |
template<typename t> void each(t func) |
| 283 |
|
|
{ |
| 284 |
|
3608 |
collectgarbage(); |
| 285 |
2/2
✓ Branch 0 taken 46150 times.
✓ Branch 1 taken 2596 times.
|
87988 |
for (int i=0;i<num;i++) |
| 286 |
|
|
{ |
| 287 |
2/4
✓ Branch 0 taken 8202 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8202 times.
✗ Branch 3 not taken.
|
84380 |
func(indexes[i], ptr[i][0]); |
| 288 |
|
|
} |
| 289 |
|
3608 |
} |
| 290 |
|
|
|
| 291 |
|
|
//void debug(){puts("");for(int i=0;i<num;i++)puts(indexes[i]);} |
| 292 |
|
|
|
| 293 |
|
|
#ifdef SERIALIZER |
| 294 |
|
|
void serialize(serializer & s) |
| 295 |
|
|
{ |
| 296 |
|
|
collectgarbage(); |
| 297 |
|
|
if (s.serializing) |
| 298 |
|
|
{ |
| 299 |
|
|
s(num); |
| 300 |
|
|
s(bufferlen); |
| 301 |
|
|
for (int i=0;i<num;i++) |
| 302 |
|
|
{ |
| 303 |
|
|
s(ptr[i][0]); |
| 304 |
|
|
s(indexes[i]); |
| 305 |
|
|
} |
| 306 |
|
|
} |
| 307 |
|
|
else |
| 308 |
|
|
{ |
| 309 |
|
|
reset(); |
| 310 |
|
|
s(num); |
| 311 |
|
|
s(bufferlen); |
| 312 |
|
|
ptr=(right**)malloc(sizeof(right*)*bufferlen); |
| 313 |
|
|
indexes=(const char**)malloc(sizeof(const char *)*bufferlen); |
| 314 |
|
|
for (int i=0;i<num;i++) |
| 315 |
|
|
{ |
| 316 |
|
|
ptr[i]=(right*)malloc(sizeof(right)); |
| 317 |
|
|
memset(ptr[i], 0, sizeof(right)); |
| 318 |
|
|
new(ptr[i]) right; |
| 319 |
|
|
s(ptr[i][0]); |
| 320 |
|
|
s(indexes[i]); |
| 321 |
|
|
} |
| 322 |
|
|
} |
| 323 |
|
|
} |
| 324 |
|
|
#endif |
| 325 |
|
|
#define SERIALIZER_BANNED |
| 326 |
|
|
}; |
| 327 |
|
|
|