1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
#ifndef _WDL_ASSOCARRAY_H_
#define _WDL_ASSOCARRAY_H_
#include "heapbuf.h"
#include "mergesort.h"
#include "wdlcstring.h"
template<class T> static int WDL_assocarray_cmp(const T *a, const T *b) { return *a > *b ? 1 : *a < *b ? -1 : 0; }
template<class T> static int WDL_assocarray_cmpmem(const T *a, const T *b) { return memcmp(a,b,sizeof(*a)); }
template<class T> static int WDL_assocarray_cmpstr(T * const *a, T * const *b) { return strcmp(*a,*b); }
template<class T> static int WDL_assocarray_cmpistr(T * const *a, T * const *b) { return stricmp(*a,*b); }
// on all of these, if valdispose is set, the array will dispose of values as needed.
// if keydup/keydispose are set, copies of (any) key data will be made/destroyed as necessary
// WDL_AssocArrayImpl can be used on its own, and can contain structs for keys or values
template <class KEY, class VAL> class WDL_AssocArrayImpl
{
WDL_AssocArrayImpl(const WDL_AssocArrayImpl &cp) { CopyContents(cp); }
WDL_AssocArrayImpl &operator=(const WDL_AssocArrayImpl &cp) { CopyContents(cp); return *this; }
public:
explicit WDL_AssocArrayImpl(int (*keycmp)(const KEY *k1, const KEY *k2),
KEY (*keydup)(KEY)=NULL,
void (*keydispose)(KEY)=NULL,
void (*valdispose)(VAL)=NULL)
{
m_keycmp = keycmp;
m_keydup = keydup;
m_keydispose = keydispose;
m_valdispose = valdispose;
}
~WDL_AssocArrayImpl()
{
DeleteAll();
}
void Prealloc(int sz) { m_data.Prealloc(sz*sizeof(KeyVal)); }
VAL* GetPtr(KEY key, KEY *keyPtrOut=NULL) const
{
bool ismatch = false;
int i = LowerBound(key, &ismatch);
if (ismatch)
{
KeyVal* kv = m_data.Get()+i;
if (keyPtrOut) *keyPtrOut = kv->key;
return &(kv->val);
}
return 0;
}
bool Exists(KEY key) const
{
bool ismatch = false;
LowerBound(key, &ismatch);
return ismatch;
}
int Insert(KEY key, VAL val)
{
bool ismatch = false;
int i = LowerBound(key, &ismatch);
if (ismatch)
{
KeyVal* kv = m_data.Get()+i;
if (m_valdispose) m_valdispose(kv->val);
kv->val = val;
}
else
{
KeyVal *kv = m_data.ResizeOK(m_data.GetSize()+1);
if (WDL_NORMALLY(kv != NULL))
{
memmove(kv+i+1, kv+i, (m_data.GetSize()-i-1)*sizeof(KeyVal));
if (m_keydup) key = m_keydup(key);
kv[i].key = key;
kv[i].val = val;
}
}
return i;
}
void Delete(KEY key)
{
bool ismatch = false;
int i = LowerBound(key, &ismatch);
if (ismatch)
{
KeyVal* kv = m_data.Get()+i;
if (m_keydispose) m_keydispose(kv->key);
if (m_valdispose) m_valdispose(kv->val);
m_data.Delete(i);
}
}
void DeleteByIndex(int idx)
{
if (idx >= 0 && idx < m_data.GetSize())
{
KeyVal* kv = m_data.Get()+idx;
if (m_keydispose) m_keydispose(kv->key);
if (m_valdispose) m_valdispose(kv->val);
m_data.Delete(idx);
}
}
void DeleteAll(bool resizedown=false)
{
if (m_keydispose || m_valdispose)
{
int i;
for (i = 0; i < m_data.GetSize(); ++i)
{
KeyVal* kv = m_data.Get()+i;
if (m_keydispose) m_keydispose(kv->key);
if (m_valdispose) m_valdispose(kv->val);
}
}
m_data.Resize(0, resizedown);
}
int GetSize() const
{
return m_data.GetSize();
}
VAL* EnumeratePtr(int i, KEY* key=NULL) const
{
if (i >= 0 && i < m_data.GetSize())
{
KeyVal* kv = m_data.Get()+i;
if (key) *key = kv->key;
return &(kv->val);
}
return 0;
}
KEY* ReverseLookupPtr(VAL val) const
{
int i;
for (i = 0; i < m_data.GetSize(); ++i)
{
KeyVal* kv = m_data.Get()+i;
if (kv->val == val) return &kv->key;
}
return 0;
}
void ChangeKey(KEY oldkey, KEY newkey)
{
bool ismatch=false;
int i=LowerBound(oldkey, &ismatch);
if (ismatch) ChangeKeyByIndex(i, newkey, true);
}
void ChangeKeyByIndex(int idx, KEY newkey, bool needsort)
{
if (idx >= 0 && idx < m_data.GetSize())
{
KeyVal* kv=m_data.Get()+idx;
if (!needsort)
{
if (m_keydispose) m_keydispose(kv->key);
if (m_keydup) newkey=m_keydup(newkey);
kv->key=newkey;
}
else
{
VAL val=kv->val;
m_data.Delete(idx);
Insert(newkey, val);
}
}
}
// fast add-block mode
void AddUnsorted(KEY key, VAL val)
{
int i=m_data.GetSize();
KeyVal *kv = m_data.ResizeOK(i+1);
if (WDL_NORMALLY(kv != NULL))
{
if (m_keydup) key = m_keydup(key);
kv[i].key = key;
kv[i].val = val;
}
}
void Resort(int (*new_keycmp)(const KEY *k1, const KEY *k2)=NULL)
{
if (new_keycmp) m_keycmp = new_keycmp;
if (m_data.GetSize() > 1 && m_keycmp)
{
qsort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
(int(*)(const void*, const void*))m_keycmp);
if (!new_keycmp)
RemoveDuplicateKeys();
}
}
void ResortStable()
{
if (m_data.GetSize() > 1 && m_keycmp)
{
char *tmp=(char*)malloc(m_data.GetSize()*sizeof(KeyVal));
if (WDL_NORMALLY(tmp))
{
WDL_mergesort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
(int(*)(const void*, const void*))m_keycmp, tmp);
free(tmp);
}
else
{
qsort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
(int(*)(const void*, const void*))m_keycmp);
}
RemoveDuplicateKeys();
}
}
int LowerBound(KEY key, bool* ismatch) const
{
int a = 0;
int c = m_data.GetSize();
while (a != c)
{
int b = (a+c)/2;
KeyVal* kv=m_data.Get()+b;
int cmp = m_keycmp(&key, &kv->key);
if (cmp > 0) a = b+1;
else if (cmp < 0) c = b;
else
{
*ismatch = true;
return b;
}
}
*ismatch = false;
return a;
}
int GetIdx(KEY key) const
{
bool ismatch=false;
int i = LowerBound(key, &ismatch);
if (ismatch) return i;
return -1;
}
void SetGranul(int gran)
{
m_data.SetGranul(gran);
}
void CopyContents(const WDL_AssocArrayImpl &cp)
{
m_data=cp.m_data;
m_keycmp = cp.m_keycmp;
m_keydup = cp.m_keydup;
m_keydispose = m_keydup ? cp.m_keydispose : NULL;
m_valdispose = NULL; // avoid disposing of values twice, since we don't have a valdup, we can't have a fully valid copy
if (m_keydup)
{
const int n=m_data.GetSize();
for (int x=0;x<n;x++)
{
KeyVal *kv=m_data.Get()+x;
kv->key = m_keydup(kv->key);
}
}
}
void CopyContentsAsReference(const WDL_AssocArrayImpl &cp)
{
DeleteAll(true);
m_keycmp = cp.m_keycmp;
m_keydup = NULL; // this no longer can own any data
m_keydispose = NULL;
m_valdispose = NULL;
m_data=cp.m_data;
}
// private data, but exposed in case the caller wants to manipulate at its own risk
struct KeyVal
{
KEY key;
VAL val;
};
WDL_TypedBuf<KeyVal> m_data;
// for (const auto &a : list) { a.key, a.val }
const KeyVal *begin() const { return m_data.begin(); }
const KeyVal *end() const { return m_data.end(); }
// should be careful if modifying keys, and Resort() after
KeyVal *begin() { return m_data.begin(); }
KeyVal *end() { return m_data.end(); }
protected:
int (*m_keycmp)(const KEY *k1, const KEY *k2);
KEY (*m_keydup)(KEY);
void (*m_keydispose)(KEY);
void (*m_valdispose)(VAL);
private:
void RemoveDuplicateKeys() // after resorting
{
const int sz = m_data.GetSize();
int cnt = 1;
KeyVal *rd = m_data.Get() + 1, *wr = rd;
for (int x = 1; x < sz; x ++)
{
if (m_keycmp(&rd->key, &wr[-1].key))
{
if (rd != wr) *wr=*rd;
wr++;
cnt++;
}
else
{
if (m_keydispose) m_keydispose(rd->key);
if (m_valdispose) m_valdispose(rd->val);
}
rd++;
}
if (cnt < sz) m_data.Resize(cnt,false);
}
};
// WDL_AssocArray adds useful functions but requires assignment operator for keys and values
template <class KEY, class VAL> class WDL_AssocArray : public WDL_AssocArrayImpl<KEY, VAL>
{
public:
explicit WDL_AssocArray(int (*keycmp)(const KEY *k1, const KEY *k2),
KEY (*keydup)(KEY)=NULL,
void (*keydispose)(KEY)=NULL, void (*valdispose)(VAL)=NULL)
: WDL_AssocArrayImpl<KEY, VAL>(keycmp, keydup, keydispose, valdispose)
{
}
VAL Get(KEY key, VAL notfound=0) const
{
VAL* p = this->GetPtr(key);
if (p) return *p;
return notfound;
}
VAL Enumerate(int i, KEY* key=NULL, VAL notfound=0) const
{
VAL* p = this->EnumeratePtr(i, key);
if (p) return *p;
return notfound;
}
KEY ReverseLookup(VAL val, KEY notfound=0) const
{
KEY* p=this->ReverseLookupPtr(val);
if (p) return *p;
return notfound;
}
};
template <class KEY, class VAL> class WDL_KeyedArray : public WDL_AssocArray<KEY, VAL>
{
public:
explicit WDL_KeyedArray(void (*valdispose)(VAL)=NULL)
: WDL_AssocArray<KEY, VAL>(WDL_assocarray_cmp<KEY>, NULL, NULL, valdispose)
{
}
};
template <class KEY, class VAL> class WDL_KeyedArrayImpl : public WDL_AssocArrayImpl<KEY, VAL>
{
public:
explicit WDL_KeyedArrayImpl(void (*valdispose)(VAL)=NULL)
: WDL_AssocArrayImpl<KEY, VAL>(WDL_assocarray_cmp<KEY>, NULL, NULL, valdispose)
{
}
};
template <class KEY, class VAL> class WDL_MemKeyedArray : public WDL_AssocArray<KEY, VAL>
{
public:
explicit WDL_MemKeyedArray(void (*valdispose)(VAL)=NULL)
: WDL_AssocArray<KEY, VAL>(WDL_assocarray_cmpmem<KEY>, NULL, NULL, valdispose)
{
}
};
template <class KEY, class VAL> class WDL_MemKeyedArrayImpl : public WDL_AssocArrayImpl<KEY, VAL>
{
public:
explicit WDL_MemKeyedArrayImpl(void (*valdispose)(VAL)=NULL)
: WDL_AssocArrayImpl<KEY, VAL>(WDL_assocarray_cmpmem<KEY>, NULL, NULL, valdispose)
{
}
};
template <class VAL> class WDL_IntKeyedArray : public WDL_KeyedArray<int, VAL>
{
public:
explicit WDL_IntKeyedArray(void (*valdispose)(VAL)=NULL) : WDL_KeyedArray<int, VAL>(valdispose) {}
};
template <class VAL> class WDL_IntKeyedArray2 : public WDL_KeyedArrayImpl<int, VAL>
{
public:
explicit WDL_IntKeyedArray2(void (*valdispose)(VAL)=NULL) : WDL_KeyedArrayImpl<int, VAL>(valdispose) {}
};
template <class VAL> class WDL_StringKeyedArray : public WDL_AssocArray<const char *, VAL>
{
public:
explicit WDL_StringKeyedArray(bool caseSensitive=true, void (*valdispose)(VAL)=NULL, bool copyKeys=true)
: WDL_AssocArray<const char*, VAL>(caseSensitive?WDL_assocarray_cmpstr<const char>:WDL_assocarray_cmpistr<const char>, copyKeys?dupstr:NULL, copyKeys?wdl_freefunc<const char *>:NULL, valdispose) {}
static const char *dupstr(const char *s) { return strdup(s); } // these might not be necessary but depending on the libc maybe...
static void freecharptr(char *p) { free(p); } // remove eventually (wdl_freefunc replaced)
};
template <class VAL> class WDL_StringKeyedArray2 : public WDL_AssocArrayImpl<const char *, VAL>
{
public:
explicit WDL_StringKeyedArray2(bool caseSensitive=true, void (*valdispose)(VAL)=NULL, bool copyKeys=true)
: WDL_AssocArrayImpl<const char*, VAL>(caseSensitive?WDL_assocarray_cmpstr<const char>:WDL_assocarray_cmpistr<const char>, copyKeys?dupstr:NULL, copyKeys?wdl_freefunc<const char *>:NULL, valdispose) {}
~WDL_StringKeyedArray2() { }
static const char *dupstr(const char *s) { return strdup(s); } // these might not be necessary but depending on the libc maybe...
static void freecharptr(char *p) { free(p); } // remove eventually (wdl_freefunc replaced)
};
// sorts text as text, sorts anything that looks like a number as a number
template <class VAL> class WDL_LogicalSortStringKeyedArray : public WDL_StringKeyedArray<VAL>
{
public:
explicit WDL_LogicalSortStringKeyedArray(bool caseSensitive=true, void (*valdispose)(VAL)=NULL, bool copyKeys=true)
: WDL_StringKeyedArray<VAL>(caseSensitive, valdispose, copyKeys)
{
WDL_StringKeyedArray<VAL>::m_keycmp = caseSensitive?cmpstr:cmpistr; // override
}
~WDL_LogicalSortStringKeyedArray() { }
static int cmpstr(const char * const *a, const char * const *b)
{
int r=WDL_strcmp_logical_ex(*a, *b, 1, WDL_STRCMP_LOGICAL_EX_FLAG_UTF8CONVERT);
return r?r:strcmp(*a,*b);
}
static int cmpistr(const char * const *a, const char * const *b)
{
int r=WDL_strcmp_logical_ex(*a, *b, 0, WDL_STRCMP_LOGICAL_EX_FLAG_UTF8CONVERT);
return r?r:stricmp(*a,*b);
}
};
template <class VAL> class WDL_PtrKeyedArray : public WDL_KeyedArray<INT_PTR, VAL>
{
public:
explicit WDL_PtrKeyedArray(void (*valdispose)(VAL)=NULL) : WDL_KeyedArray<INT_PTR, VAL>(valdispose) {}
};
template <class KEY, class VAL> class WDL_PointerKeyedArray : public WDL_KeyedArray<KEY, VAL>
{
public:
explicit WDL_PointerKeyedArray(void (*valdispose)(VAL)=NULL) : WDL_KeyedArray<KEY, VAL>(valdispose) {}
};
struct WDL_Set_DummyRec { };
template <class KEY> class WDL_Set : public WDL_AssocArrayImpl<KEY,WDL_Set_DummyRec>
{
public:
explicit WDL_Set(int (*keycmp)(const KEY *k1, const KEY *k2),
KEY (*keydup)(KEY)=NULL,
void (*keydispose)(KEY)=NULL
)
: WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>(keycmp,keydup,keydispose)
{
}
int Insert(KEY key)
{
WDL_Set_DummyRec r;
return WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::Insert(key,r);
}
void AddUnsorted(KEY key)
{
WDL_Set_DummyRec r;
WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::AddUnsorted(key,r);
}
bool Get(KEY key) const
{
return WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::Exists(key);
}
bool Enumerate(int i, KEY *key=NULL)
{
return WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::EnumeratePtr(i,key) != NULL;
}
};
template <class KEY> class WDL_PtrSet : public WDL_Set<KEY>
{
public:
explicit WDL_PtrSet() : WDL_Set<KEY>( WDL_assocarray_cmp<KEY> ) { }
};
#endif
|