少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
 * ghashtable.c: Hashtable implementation
 *
 * Author:
 *   Miguel de Icaza (miguel@novell.com)
 *
 * (C) 2006 Novell, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <stdio.h>
#include <math.h>
#include <glib.h>
 
typedef struct _Slot Slot;
 
struct _Slot {
    gpointer key;
    gpointer value;
    Slot    *next;
};
 
static gpointer KEYMARKER_REMOVED = &KEYMARKER_REMOVED;
 
struct _GHashTable {
    GHashFunc      hash_func;
    GEqualFunc     key_equal_func;
 
    Slot **table;
    int   table_size;
    int   in_use;
    int   threshold;
    int   last_rehash;
    GDestroyNotify value_destroy_func, key_destroy_func;
};
 
typedef struct {
    GHashTable *ht;
    int slot_index;
    Slot *slot;
} Iter;
 
static const guint prime_tbl[] = {
    11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
    1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
    47431, 71143, 106721, 160073, 240101, 360163,
    540217, 810343, 1215497, 1823231, 2734867, 4102283,
    6153409, 9230113, 13845163
};
 
static gboolean
test_prime (int x)
{
    if ((x & 1) != 0) {
        int n;
        for (n = 3; n< (int)sqrt (x); n += 2) {
            if ((x % n) == 0)
                return FALSE;
        }
        return TRUE;
    }
    // There is only one even prime - 2.
    return (x == 2);
}
 
static int
calc_prime (int x)
{
    int i;
    
    for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
        if (test_prime (i))
            return i;
    }
    return x;
}
 
guint
g_spaced_primes_closest (guint x)
{
    int i;
    
    for (i = 0; i < G_N_ELEMENTS (prime_tbl); i++) {
        if (x <= prime_tbl [i])
            return prime_tbl [i];
    }
    return calc_prime (x);
}
 
GHashTable *
g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
{
    GHashTable *hash;
 
    if (hash_func == NULL)
        hash_func = g_direct_hash;
    if (key_equal_func == NULL)
        key_equal_func = g_direct_equal;
    hash = g_new0 (GHashTable, 1);
 
    hash->hash_func = hash_func;
    hash->key_equal_func = key_equal_func;
 
    hash->table_size = g_spaced_primes_closest (1);
    hash->table = g_new0 (Slot *, hash->table_size);
    hash->last_rehash = hash->table_size;
    
    return hash;
}
 
GHashTable *
g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
               GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
{
    GHashTable *hash = g_hash_table_new (hash_func, key_equal_func);
    if (hash == NULL)
        return NULL;
    
    hash->key_destroy_func = key_destroy_func;
    hash->value_destroy_func = value_destroy_func;
    
    return hash;
}
 
#if 0
static void
dump_hash_table (GHashTable *hash)
{
    int i;
 
    for (i = 0; i < hash->table_size; i++) {
        Slot *s;
 
        for (s = hash->table [i]; s != NULL; s = s->next){
            guint hashcode = (*hash->hash_func) (s->key);
            guint slot = (hashcode) % hash->table_size;
            printf ("key %p hash %x on slot %d correct slot %d tb size %d\n", s->key, hashcode, i, slot, hash->table_size);
        }
    }
}
#endif
 
#ifdef SANITY_CHECK
static void
sanity_check (GHashTable *hash)
{
    int i;
 
    for (i = 0; i < hash->table_size; i++) {
        Slot *s;
 
        for (s = hash->table [i]; s != NULL; s = s->next){
            guint hashcode = (*hash->hash_func) (s->key);
            guint slot = (hashcode) % hash->table_size;
            if (slot != i) {
                dump_hashcode_func = 1;
                hashcode = (*hash->hash_func) (s->key);
                dump_hashcode_func = 0;
                g_error ("Key %p (bucket %d) on invalid bucket %d (hashcode %x) (tb size %d)", s->key, slot, i, hashcode, hash->table_size);
            }
        }
    }
}
#else
 
#define sanity_check(HASH) do {}while(0)
 
#endif
 
static void
do_rehash (GHashTable *hash)
{
    int current_size, i;
    Slot **table;
 
    /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
    hash->last_rehash = hash->table_size;
    current_size = hash->table_size;
    hash->table_size = g_spaced_primes_closest (hash->in_use);
    /* printf ("New size: %d\n", hash->table_size); */
    table = hash->table;
    hash->table = g_new0 (Slot *, hash->table_size);
    
    for (i = 0; i < current_size; i++){
        Slot *s, *next;
 
        for (s = table [i]; s != NULL; s = next){
            guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
            next = s->next;
 
            s->next = hash->table [hashcode];
            hash->table [hashcode] = s;
        }
    }
    g_free (table);
}
 
static void
rehash (GHashTable *hash)
{
    int diff = ABS (hash->last_rehash - hash->in_use);
 
    /* These are the factors to play with to change the rehashing strategy */
    /* I played with them with a large range, and could not really get */
    /* something that was too good, maybe the tests are not that great */
    if (!(diff * 0.75 > hash->table_size * 2))
        return;
    do_rehash (hash);
    sanity_check (hash);
}
 
void
g_hash_table_insert_replace (GHashTable *hash, gpointer key, gpointer value, gboolean replace)
{
    guint hashcode;
    Slot *s;
    GEqualFunc equal;
    
    g_return_if_fail (hash != NULL);
    sanity_check (hash);
 
    equal = hash->key_equal_func;
    if (hash->in_use >= hash->threshold)
        rehash (hash);
 
    hashcode = ((*hash->hash_func) (key)) % hash->table_size;
    for (s = hash->table [hashcode]; s != NULL; s = s->next){
        if ((*equal) (s->key, key)){
            if (replace){
                if (hash->key_destroy_func != NULL)
                    (*hash->key_destroy_func)(s->key);
                s->key = key;
            }
            if (hash->value_destroy_func != NULL)
                (*hash->value_destroy_func) (s->value);
            s->value = value;
            sanity_check (hash);
            return;
        }
    }
    s = g_new (Slot, 1);
    s->key = key;
    s->value = value;
    s->next = hash->table [hashcode];
    hash->table [hashcode] = s;
    hash->in_use++;
    sanity_check (hash);
}
 
GList*
g_hash_table_get_keys (GHashTable *hash)
{
    GHashTableIter iter;
    GList *rv = NULL;
    gpointer key;
 
    g_hash_table_iter_init (&iter, hash);
 
    while (g_hash_table_iter_next (&iter, &key, NULL))
        rv = g_list_prepend (rv, key);
 
    return g_list_reverse (rv);
}
 
GList*
g_hash_table_get_values (GHashTable *hash)
{
    GHashTableIter iter;
    GList *rv = NULL;
    gpointer value;
 
    g_hash_table_iter_init (&iter, hash);
 
    while (g_hash_table_iter_next (&iter, NULL, &value))
        rv = g_list_prepend (rv, value);
 
    return g_list_reverse (rv);
}
 
 
guint
g_hash_table_size (GHashTable *hash)
{
    g_return_val_if_fail (hash != NULL, 0);
    
    return hash->in_use;
}
 
gpointer
g_hash_table_lookup (GHashTable *hash, gconstpointer key)
{
    gpointer orig_key, value;
    
    if (g_hash_table_lookup_extended (hash, key, &orig_key, &value))
        return value;
    else
        return NULL;
}
 
gboolean
g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
{
    GEqualFunc equal;
    Slot *s;
    guint hashcode;
    
    g_return_val_if_fail (hash != NULL, FALSE);
    sanity_check (hash);
    equal = hash->key_equal_func;
 
    hashcode = ((*hash->hash_func) (key)) % hash->table_size;
    
    for (s = hash->table [hashcode]; s != NULL; s = s->next){
        if ((*equal)(s->key, key)){
            if (orig_key)
                *orig_key = s->key;
            if (value)
                *value = s->value;
            return TRUE;
        }
    }
    return FALSE;
}
 
void
g_hash_table_foreach (GHashTable *hash, GHFunc func, gpointer user_data)
{
    int i;
    
    g_return_if_fail (hash != NULL);
    g_return_if_fail (func != NULL);
 
    for (i = 0; i < hash->table_size; i++){
        Slot *s;
 
        for (s = hash->table [i]; s != NULL; s = s->next)
            (*func)(s->key, s->value, user_data);
    }
}
 
gpointer
g_hash_table_find (GHashTable *hash, GHRFunc predicate, gpointer user_data)
{
    int i;
    
    g_return_val_if_fail (hash != NULL, NULL);
    g_return_val_if_fail (predicate != NULL, NULL);
 
    for (i = 0; i < hash->table_size; i++){
        Slot *s;
 
        for (s = hash->table [i]; s != NULL; s = s->next)
            if ((*predicate)(s->key, s->value, user_data))
                return s->value;
    }
    return NULL;
}
 
void
g_hash_table_remove_all (GHashTable *hash)
{
    int i;
    
    g_return_if_fail (hash != NULL);
 
    for (i = 0; i < hash->table_size; i++){
        Slot *s;
 
        while (hash->table [i]) {
            s = hash->table [i];
            g_hash_table_remove (hash, s->key);
        }
    }
}
 
gboolean
g_hash_table_remove (GHashTable *hash, gconstpointer key)
{
    GEqualFunc equal;
    Slot *s, *last;
    guint hashcode;
    
    g_return_val_if_fail (hash != NULL, FALSE);
    sanity_check (hash);
    equal = hash->key_equal_func;
 
    hashcode = ((*hash->hash_func)(key)) % hash->table_size;
    last = NULL;
    for (s = hash->table [hashcode]; s != NULL; s = s->next){
        if ((*equal)(s->key, key)){
            if (hash->key_destroy_func != NULL)
                (*hash->key_destroy_func)(s->key);
            if (hash->value_destroy_func != NULL)
                (*hash->value_destroy_func)(s->value);
            if (last == NULL)
                hash->table [hashcode] = s->next;
            else
                last->next = s->next;
            g_free (s);
            hash->in_use--;
            sanity_check (hash);
            return TRUE;
        }
        last = s;
    }
    sanity_check (hash);
    return FALSE;
}
 
guint
g_hash_table_foreach_remove (GHashTable *hash, GHRFunc func, gpointer user_data)
{
    int i;
    int count = 0;
    
    g_return_val_if_fail (hash != NULL, 0);
    g_return_val_if_fail (func != NULL, 0);
 
    sanity_check (hash);
    for (i = 0; i < hash->table_size; i++){
        Slot *s, *last;
 
        last = NULL;
        for (s = hash->table [i]; s != NULL; ){
            if ((*func)(s->key, s->value, user_data)){
                Slot *n;
 
                if (hash->key_destroy_func != NULL)
                    (*hash->key_destroy_func)(s->key);
                if (hash->value_destroy_func != NULL)
                    (*hash->value_destroy_func)(s->value);
                if (last == NULL){
                    hash->table [i] = s->next;
                    n = s->next;
                } else  {
                    last->next = s->next;
                    n = last->next;
                }
                g_free (s);
                hash->in_use--;
                count++;
                s = n;
            } else {
                last = s;
                s = s->next;
            }
        }
    }
    sanity_check (hash);
    if (count > 0)
        rehash (hash);
    return count;
}
 
gboolean
g_hash_table_steal (GHashTable *hash, gconstpointer key)
{
    GEqualFunc equal;
    Slot *s, *last;
    guint hashcode;
    
    g_return_val_if_fail (hash != NULL, FALSE);
    sanity_check (hash);
    equal = hash->key_equal_func;
    
    hashcode = ((*hash->hash_func)(key)) % hash->table_size;
    last = NULL;
    for (s = hash->table [hashcode]; s != NULL; s = s->next){
        if ((*equal)(s->key, key)) {
            if (last == NULL)
                hash->table [hashcode] = s->next;
            else
                last->next = s->next;
            g_free (s);
            hash->in_use--;
            sanity_check (hash);
            return TRUE;
        }
        last = s;
    }
    sanity_check (hash);
    return FALSE;
    
}
 
guint
g_hash_table_foreach_steal (GHashTable *hash, GHRFunc func, gpointer user_data)
{
    int i;
    int count = 0;
    
    g_return_val_if_fail (hash != NULL, 0);
    g_return_val_if_fail (func != NULL, 0);
 
    sanity_check (hash);
    for (i = 0; i < hash->table_size; i++){
        Slot *s, *last;
 
        last = NULL;
        for (s = hash->table [i]; s != NULL; ){
            if ((*func)(s->key, s->value, user_data)){
                Slot *n;
 
                if (last == NULL){
                    hash->table [i] = s->next;
                    n = s->next;
                } else  {
                    last->next = s->next;
                    n = last->next;
                }
                g_free (s);
                hash->in_use--;
                count++;
                s = n;
            } else {
                last = s;
                s = s->next;
            }
        }
    }
    sanity_check (hash);
    if (count > 0)
        rehash (hash);
    return count;
}
 
void
g_hash_table_destroy (GHashTable *hash)
{
    int i;
    
    g_return_if_fail (hash != NULL);
 
    for (i = 0; i < hash->table_size; i++){
        Slot *s, *next;
 
        for (s = hash->table [i]; s != NULL; s = next){
            next = s->next;
            
            if (hash->key_destroy_func != NULL)
                (*hash->key_destroy_func)(s->key);
            if (hash->value_destroy_func != NULL)
                (*hash->value_destroy_func)(s->value);
            g_free (s);
        }
    }
    g_free (hash->table);
    
    g_free (hash);
}
 
void
g_hash_table_print_stats (GHashTable *table)
{
    int i, max_chain_index, chain_size, max_chain_size;
    Slot *node;
 
    max_chain_size = 0;
    max_chain_index = -1;
    for (i = 0; i < table->table_size; i++) {
        chain_size = 0;
        for (node = table->table [i]; node; node = node->next)
            chain_size ++;
        if (chain_size > max_chain_size) {
            max_chain_size = chain_size;
            max_chain_index = i;
        }
    }
 
    printf ("Size: %d Table Size: %d Max Chain Length: %d at %d\n", table->in_use, table->table_size, max_chain_size, max_chain_index);
}
 
void
g_hash_table_iter_init (GHashTableIter *it, GHashTable *hash_table)
{
    Iter *iter = (Iter*)it;
 
    memset (iter, 0, sizeof (Iter));
    iter->ht = hash_table;
    iter->slot_index = -1;
}
 
gboolean g_hash_table_iter_next (GHashTableIter *it, gpointer *key, gpointer *value)
{
    Iter *iter = (Iter*)it;
 
    GHashTable *hash = iter->ht;
 
    g_assert (iter->slot_index != -2);
    g_assert (sizeof (Iter) <= sizeof (GHashTableIter));
 
    if (!iter->slot) {
        while (TRUE) {
            iter->slot_index ++;
            if (iter->slot_index >= hash->table_size) {
                iter->slot_index = -2;
                return FALSE;
            }
            if (hash->table [iter->slot_index])
                break;
        }
        iter->slot = hash->table [iter->slot_index];
    }
 
    if (key)
        *key = iter->slot->key;
    if (value)
        *value = iter->slot->value;
    iter->slot = iter->slot->next;
 
    return TRUE;
}
 
gboolean
g_direct_equal (gconstpointer v1, gconstpointer v2)
{
    return v1 == v2;
}
 
guint
g_direct_hash (gconstpointer v1)
{
    return GPOINTER_TO_UINT (v1);
}
 
gboolean
g_int_equal (gconstpointer v1, gconstpointer v2)
{
    return *(gint *)v1 == *(gint *)v2;
}
 
guint
g_int_hash (gconstpointer v1)
{
    return *(guint *)v1;
}
 
gboolean
g_str_equal (gconstpointer v1, gconstpointer v2)
{
    return strcmp (v1, v2) == 0;
}
 
guint
g_str_hash (gconstpointer v1)
{
    guint hash = 0;
    char *p = (char *) v1;
 
    while (*p++)
        hash = (hash << 5) - (hash + *p);
 
    return hash;
}