-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoso.c
1505 lines (1285 loc) · 44.3 KB
/
soso.c
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "soso.h"
//--- Internals ---//
void soso_random_seed(uint64_t seed);
uint64_t soso_random_get(void);
uint64_t soso_internal_random_get_in(uint64_t min, uint64_t max);
soso_int_t soso_internal_make_card(soso_int_t suit, soso_int_t value);
soso_int_t soso_internal_get_waste_card(const soso_game_t *game);
soso_int_t soso_internal_cvalue(soso_int_t card);
soso_int_t soso_internal_csuit(soso_int_t card);
soso_int_t soso_internal_ccolor(soso_int_t card);
bool soso_internal_valid_card(soso_int_t card);
bool soso_internal_foundation_valid(soso_int_t card, const soso_int_t *foundation_top);
bool soso_internal_tableau_valid(soso_int_t from, soso_int_t to);
int soso_internal_max_draws(const soso_ctx_t *ctx, const soso_game_t *game);
bool soso_internal_game_solved(const soso_game_t *game);
void soso_internal_revert_to_last_move(soso_ctx_t *ctx, soso_game_t *game);
uint32_t soso_internal_state_hash(const soso_game_t *game, const soso_move_t *move);
void soso_internal_add_move(soso_ctx_t *ctx, const soso_move_t m, bool is_auto);
void soso_internal_update_waste_moves(soso_ctx_t *ctx, const soso_game_t *game);
void soso_internal_update_tableau_moves(soso_ctx_t *ctx, const soso_game_t *game);
void soso_internal_update_stock_moves(soso_ctx_t *ctx, const soso_game_t *game);
void soso_internal_update_foundation_moves(soso_ctx_t *ctx, const soso_game_t *game);
bool soso_internal_update_available_moves(soso_ctx_t *ctx, const soso_game_t *game, bool no_stock);
bool soso_internal_draw(soso_game_t *game, int draw_count);
bool soso_internal_undo_draw(soso_game_t *game, int draw_count);
void soso_internal_make_move(soso_ctx_t *ctx, soso_game_t *game, soso_move_t m);
void soso_internal_undo_move(soso_ctx_t *ctx, soso_game_t *game);
//-----------------------------------------------------------------------------
// MurmurHash3 was written by Austin Appleby, and is placed in the
// public domain. The author hereby disclaims copyright to this source
// code.
#ifndef _MURMURHASH3_H_
#define _MURMURHASH3_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
//-----------------------------------------------------------------------------
void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out);
void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out);
void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out);
//-----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
#endif // _MURMURHASH3_H_
//-----------------------------------------------------------------------------
// simplehashtable was written by Tiziano Bettio, and is placed in the
// public domain. The author hereby disclaims copyright to this source
// code.
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct sht sht_t;
/**
* @brief Initialize a new hashtable with custom allocator.
*
* @param item_size The size in bytes of an item
* @param reserve Number of slots to reserve upon initialization (will be
* rounded up to the next power of two)
* @param seed Seed used by the hash function
* @param custom_alloc Pointer to the allocation function
* @param custom_free Pointer to the function for freeing allocated memory
* @return sht_t*
*/
sht_t *sht_init_alloc(uint32_t item_size, uint32_t reserve, uint32_t seed,
void *(*custom_alloc)(size_t),
void (*custom_free)(void *));
/**
* @brief Initialize a new hashtable using the memory allocator from `stdlib.h`
*
* @param item_size The size in bytes of an item
* @param reserve Number of slots to reserve upon initialization (will be
* rounded up to the next power of two)
* @param seed Seed used by the hash function
* @return sht_t*
*/
sht_t *sht_init(uint32_t item_size, uint32_t reserve, uint32_t seed);
void sht_destroy(sht_t *sht);
/**
* @brief Add or overwrite an item in the hashtable.
*
* @param sht Pointer to the hashtable
* @param key Pointer to the key to be used
* @param len The length in bytes of the passed key
* @param item Pointer to the item
* @return Hash value of the entry
*/
uint32_t sht_set(sht_t *sht, const void *key, int len, const void *item);
/**
* @brief Add or overwrite an item in the hashtable with a specific hash value.
*
* @param sht Pointer to the hashtable
* @param hash The hash value of the item
* @param item Pointer to the item
*/
void sht_set_by_hash(sht_t *sht, uint32_t hash, const void *item);
/**
* @brief Get an item if present.
*
* @param sht Pointer to the hashtable
* @param key Pointer to the key to be used
* @param len The length in bytes of the passed key
* @return void* of the requested item or NULL if not found
*/
void *sht_get(sht_t *sht, const void *key, int len);
/**
* @brief Get an item by its hash value if present.
*
* @param sht Pointer to the hashtable
* @param hash Hash value of the item
* @return void* of the requested item or NULL if not found
*/
void *sht_get_by_hash(sht_t *sht, uint32_t hash);
/**
* @brief Delete an item if present
*
* @param sht Pointer to the hashtable
* @param key Pointer to the key to be used
* @param len The length in bytes of the passed key
*/
void sht_del(sht_t *sht, const void *key, int len);
/**
* @brief Number of items stored in the hashtable
*
* @param sht Pointer to the hashtable
* @return uint32_t
*/
uint32_t sht_size(sht_t *sht);
typedef struct sht_it sht_it_t;
/**
* @brief Get a new iterator for the hashtable
*
* @param sht Pointer to the hashtable
* @return sht_it_t*
*/
sht_it_t *sht_iter(sht_t *sht);
/**
* @brief Get next item of the iterator.
*
* @param it Pointer to the iterator
* @return void* of the next item or `NULL`
*/
void *sht_iter_next(sht_it_t *it);
void sht_iter_destroy(sht_it_t *it);
/**
* @brief foreach macro to iterate over a hashtable that expects a pointer to
* the hashtable, a non initialized pointer to an iterator and a void * (or
* pointer to the item type).
*
*/
#define sht_foreach(sht, it, item) \
for ((it) = sht_iter((sht)), (item) = sht_iter_next((it)); (item) != NULL; \
(item) = sht_iter_next((it)))
#ifdef __cplusplus
}
#endif
//-----------------------------------------------------------------------------
// MurmurHash3 was written by Austin Appleby, and is placed in the public
// domain. The author hereby disclaims copyright to this source code.
// Note - The x86 and x64 versions do _not_ produce the same results, as the
// algorithms are optimized for their respective platforms. You can still
// compile and run any of them on any platform, but your performance with the
// non-native version will be less than optimal.
//-----------------------------------------------------------------------------
// Platform-specific functions and macros
#ifdef __GNUC__
#define FORCE_INLINE __attribute__((always_inline)) inline
#else
#define FORCE_INLINE inline
#endif
static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r )
{
return (x << r) | (x >> (32 - r));
}
static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r )
{
return (x << r) | (x >> (64 - r));
}
#define ROTL32(x,y) rotl32(x,y)
#define ROTL64(x,y) rotl64(x,y)
#define BIG_CONSTANT(x) (x##LLU)
//-----------------------------------------------------------------------------
// Block read - if your platform needs to do endian-swapping or can only
// handle aligned reads, do the conversion here
#define getblock(p, i) (p[i])
//-----------------------------------------------------------------------------
// Finalization mix - force all bits of a hash block to avalanche
static FORCE_INLINE uint32_t fmix32 ( uint32_t h )
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
//----------
static FORCE_INLINE uint64_t fmix64 ( uint64_t k )
{
k ^= k >> 33;
k *= BIG_CONSTANT(0xff51afd7ed558ccd);
k ^= k >> 33;
k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
k ^= k >> 33;
return k;
}
//-----------------------------------------------------------------------------
void MurmurHash3_x86_32 ( const void * key, int len,
uint32_t seed, void * out )
{
const uint8_t * data = (const uint8_t*)key;
const int nblocks = len / 4;
int i;
uint32_t h1 = seed;
uint32_t c1 = 0xcc9e2d51;
uint32_t c2 = 0x1b873593;
//----------
// body
const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
for(i = -nblocks; i; i++)
{
uint32_t k1 = getblock(blocks,i);
k1 *= c1;
k1 = ROTL32(k1,15);
k1 *= c2;
h1 ^= k1;
h1 = ROTL32(h1,13);
h1 = h1*5+0xe6546b64;
}
//----------
// tail
const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
uint32_t k1 = 0;
switch(len & 3)
{
case 3: k1 ^= tail[2] << 16;
case 2: k1 ^= tail[1] << 8;
case 1: k1 ^= tail[0];
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len;
h1 = fmix32(h1);
*(uint32_t*)out = h1;
}
//-----------------------------------------------------------------------------
void MurmurHash3_x86_128 ( const void * key, const int len,
uint32_t seed, void * out )
{
const uint8_t * data = (const uint8_t*)key;
const int nblocks = len / 16;
int i;
uint32_t h1 = seed;
uint32_t h2 = seed;
uint32_t h3 = seed;
uint32_t h4 = seed;
uint32_t c1 = 0x239b961b;
uint32_t c2 = 0xab0e9789;
uint32_t c3 = 0x38b34ae5;
uint32_t c4 = 0xa1e38b93;
//----------
// body
const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
for(i = -nblocks; i; i++)
{
uint32_t k1 = getblock(blocks,i*4+0);
uint32_t k2 = getblock(blocks,i*4+1);
uint32_t k3 = getblock(blocks,i*4+2);
uint32_t k4 = getblock(blocks,i*4+3);
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
}
//----------
// tail
const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
uint32_t k1 = 0;
uint32_t k2 = 0;
uint32_t k3 = 0;
uint32_t k4 = 0;
switch(len & 15)
{
case 15: k4 ^= tail[14] << 16;
case 14: k4 ^= tail[13] << 8;
case 13: k4 ^= tail[12] << 0;
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
case 12: k3 ^= tail[11] << 24;
case 11: k3 ^= tail[10] << 16;
case 10: k3 ^= tail[ 9] << 8;
case 9: k3 ^= tail[ 8] << 0;
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
case 8: k2 ^= tail[ 7] << 24;
case 7: k2 ^= tail[ 6] << 16;
case 6: k2 ^= tail[ 5] << 8;
case 5: k2 ^= tail[ 4] << 0;
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
case 4: k1 ^= tail[ 3] << 24;
case 3: k1 ^= tail[ 2] << 16;
case 2: k1 ^= tail[ 1] << 8;
case 1: k1 ^= tail[ 0] << 0;
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
h1 += h2; h1 += h3; h1 += h4;
h2 += h1; h3 += h1; h4 += h1;
h1 = fmix32(h1);
h2 = fmix32(h2);
h3 = fmix32(h3);
h4 = fmix32(h4);
h1 += h2; h1 += h3; h1 += h4;
h2 += h1; h3 += h1; h4 += h1;
((uint32_t*)out)[0] = h1;
((uint32_t*)out)[1] = h2;
((uint32_t*)out)[2] = h3;
((uint32_t*)out)[3] = h4;
}
//-----------------------------------------------------------------------------
void MurmurHash3_x64_128 ( const void * key, const int len,
const uint32_t seed, void * out )
{
const uint8_t * data = (const uint8_t*)key;
const int nblocks = len / 16;
int i;
uint64_t h1 = seed;
uint64_t h2 = seed;
uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
//----------
// body
const uint64_t * blocks = (const uint64_t *)(data);
for(i = 0; i < nblocks; i++)
{
uint64_t k1 = getblock(blocks,i*2+0);
uint64_t k2 = getblock(blocks,i*2+1);
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
}
//----------
// tail
const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
uint64_t k1 = 0;
uint64_t k2 = 0;
switch(len & 15)
{
case 15: k2 ^= (uint64_t)(tail[14]) << 48;
case 14: k2 ^= (uint64_t)(tail[13]) << 40;
case 13: k2 ^= (uint64_t)(tail[12]) << 32;
case 12: k2 ^= (uint64_t)(tail[11]) << 24;
case 11: k2 ^= (uint64_t)(tail[10]) << 16;
case 10: k2 ^= (uint64_t)(tail[ 9]) << 8;
case 9: k2 ^= (uint64_t)(tail[ 8]) << 0;
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
case 8: k1 ^= (uint64_t)(tail[ 7]) << 56;
case 7: k1 ^= (uint64_t)(tail[ 6]) << 48;
case 6: k1 ^= (uint64_t)(tail[ 5]) << 40;
case 5: k1 ^= (uint64_t)(tail[ 4]) << 32;
case 4: k1 ^= (uint64_t)(tail[ 3]) << 24;
case 3: k1 ^= (uint64_t)(tail[ 2]) << 16;
case 2: k1 ^= (uint64_t)(tail[ 1]) << 8;
case 1: k1 ^= (uint64_t)(tail[ 0]) << 0;
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len; h2 ^= len;
h1 += h2;
h2 += h1;
h1 = fmix64(h1);
h2 = fmix64(h2);
h1 += h2;
h2 += h1;
((uint64_t*)out)[0] = h1;
((uint64_t*)out)[1] = h2;
}
//-----------------------------------------------------------------------------
#include <assert.h>
#include <string.h>
struct sht {
uint8_t *table;
uint32_t item_size;
uint32_t size;
uint32_t capacity;
uint32_t seed;
void *(*myalloc)(size_t);
void (*myfree)(void *);
};
static uint32_t next_power_of_two(uint32_t v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
sht_t *sht_init_alloc(uint32_t item_size, uint32_t reserve, uint32_t seed,
void *(*custom_alloc)(size_t),
void (*custom_free)(void *)) {
sht_t *sht = (sht_t *)custom_alloc(sizeof(sht_t));
assert(sht != NULL);
sht->myalloc = custom_alloc;
sht->myfree = custom_free;
// ensure 4 byte alignment and reserve additional space for hash value
sht->item_size = item_size + (item_size % 4) + sizeof(uint32_t);
sht->seed = seed;
sht->size = 0;
sht->capacity = next_power_of_two(reserve);
if (sht->capacity < 2) sht->capacity = 2;
sht->table = (uint8_t *)custom_alloc(sht->capacity * sht->item_size);
assert(sht->table != NULL);
memset(sht->table, 0, sht->capacity * sht->item_size);
return sht;
}
sht_t *sht_init(uint32_t item_size, uint32_t reserve, uint32_t seed) {
return sht_init_alloc(item_size, reserve, seed, malloc, free);
}
void sht_destroy(sht_t *sht) {
assert(sht != NULL);
if (sht->table != NULL) sht->myfree(sht->table);
sht->myfree(sht);
}
static uint32_t insert(uint8_t *table, uint32_t k, uint32_t cap, const void *el,
uint32_t size) {
uint32_t id = k & (cap - 1);
for (;;) {
uint32_t cmp = *(uint32_t *)&table[id * size];
if (cmp > 0 && cmp != k) {
++id;
id = id % cap;
continue;
}
memcpy(&table[id * size], &k, sizeof(uint32_t));
memcpy(&table[id * size + sizeof(uint32_t)], el,
size - sizeof(uint32_t));
if (cmp == 0) return 1;
return 0;
}
}
static void grow(sht_t *sht) {
if ((sht->size << 1) < sht->capacity) return;
sht_t tmp;
memcpy(&tmp, sht, sizeof(sht_t));
tmp.capacity = sht->capacity << 1;
tmp.table = (uint8_t *)sht->myalloc(tmp.capacity * sht->item_size);
assert(tmp.table != NULL);
memset(tmp.table, 0, tmp.capacity * tmp.item_size);
for (uint32_t i = 0; i < sht->capacity; ++i) {
uint32_t *k = (uint32_t *)&sht->table[i * sht->item_size];
if (*k == 0) continue;
insert(tmp.table, *k, tmp.capacity,
&sht->table[i * tmp.item_size + sizeof(uint32_t)],
tmp.item_size);
}
sht->myfree(sht->table);
memcpy(sht, &tmp, sizeof(sht_t));
}
static uint32_t comp_key(const void *key, int len, uint32_t seed) {
uint32_t k;
MurmurHash3_x86_32(key, len, seed, &k);
if (k == 0) k = 1;
return k;
}
void sht_set_by_hash(sht_t *sht, uint32_t hash, const void *item) {
if (insert(sht->table, hash, sht->capacity, item, sht->item_size) > 0)
++(sht->size);
}
uint32_t sht_set(sht_t *sht, const void *key, int len, const void *element) {
assert(sht != NULL);
grow(sht);
uint32_t hash = comp_key(key, len, sht->seed);
sht_set_by_hash(sht, hash, element);
return hash;
}
void *sht_get(sht_t *sht, const void *key, int len) {
assert(sht != NULL);
uint32_t k = comp_key(key, len, sht->seed);
return sht_get_by_hash(sht, k);
}
void *sht_get_by_hash(sht_t *sht, uint32_t hash) {
assert(sht != NULL);
uint32_t id = hash & (sht->capacity - 1);
for (;;) {
uint32_t cmp = *(uint32_t *)&sht->table[id * sht->item_size];
if (cmp == 0) return NULL;
if (cmp == hash) break;
++id;
id = id % sht->capacity;
}
return (void *)&sht->table[id * sht->item_size + sizeof(uint32_t)];
}
static void move_left(sht_t *sht, uint32_t start) {
uint32_t k = *(uint32_t *)&sht->table[start * sht->item_size];
if (k == 0) return;
uint32_t id = k & (sht->capacity - 1);
if (id == start) return;
uint32_t dest = (start == 0) ? sht->capacity - 1 : start - 1;
memcpy(&sht->table[dest * sht->item_size],
&sht->table[start * sht->item_size], sht->item_size);
memset(&sht->table[start * sht->item_size], 0, sht->item_size);
move_left(sht, (start + 1) % sht->capacity);
}
void sht_del(sht_t *sht, const void *key, int len) {
assert(sht != NULL);
uint32_t k = comp_key(key, len, sht->seed);
uint32_t id = k & (sht->capacity - 1);
for (;;) {
uint32_t cmp = *(uint32_t *)&sht->table[id * sht->item_size];
if (cmp == 0) return;
if (cmp == k) break;
++id;
id = id % sht->capacity;
}
--(sht->size);
memset(&sht->table[id * sht->item_size], 0, sht->item_size);
move_left(sht, (id + 1) % sht->capacity);
}
uint32_t sht_size(sht_t *sht) {
assert(sht != NULL);
return sht->size;
}
struct sht_it {
sht_t *sht;
uint32_t cur;
int valid;
};
sht_it_t *sht_iter(sht_t *sht) {
assert(sht != NULL);
sht_it_t *it = (sht_it_t *)sht->myalloc(sizeof(sht_it_t));
assert(it != NULL);
it->sht = sht;
it->cur = 0;
it->valid = 1;
return it;
}
void *sht_iter_next(sht_it_t *it) {
assert(it != NULL && it->valid > 0);
sht_t *sht = it->sht;
for (; it->cur < sht->capacity;) {
uint32_t cmp = *(uint32_t *)&sht->table[it->cur * sht->item_size];
++(it->cur);
if (cmp != 0)
return (void *)&sht
->table[(it->cur - 1) * sht->item_size + sizeof(uint32_t)];
}
it->valid = 0;
return NULL;
}
void sht_iter_destroy(sht_it_t *it) {
assert(it != NULL);
it->sht->myfree(it);
}
// xoshiro256** 1.0
static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}
static uint64_t s[4] = {1, 2, 3, 4};
static uint64_t next(void) {
const uint64_t result = rotl(s[1] * 5, 7) * 9;
const uint64_t t = s[1] << 17;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = rotl(s[3], 45);
return result;
}
void soso_random_seed(uint64_t seed) {
s[1] = 1;
s[2] = 2;
s[3] = 3;
s[0] = seed;
s[1] = next();
s[2] = next();
s[3] = next();
}
uint64_t soso_random_get(void) {
return next();
}
#include <string.h>
#define SOSO_NUM_SHUFFLES 12
#define SOSO_HASH_SEED 0xdeadc0de
const char *soso_suits = "CDSH";
const char *soso_values = "A23456789TJQK";
void soso_shuffle(soso_deck_t *deck, uint64_t seed) {
soso_random_seed(seed);
for (soso_int_t i = 0; i < 4; ++i)
for (soso_int_t j = 0; j < 13; ++j) {
deck->cards[i * 13 + j] = soso_internal_make_card(i, j);
}
for (int s = 0; s < SOSO_NUM_SHUFFLES; ++s)
for (int i = 0; i < 51; ++i) {
int j = soso_internal_random_get_in(i, 51);
if (i == j) continue;
soso_int_t t = deck->cards[j];
deck->cards[j] = deck->cards[i];
deck->cards[i] = t;
}
}
void soso_deal(soso_game_t *game, soso_deck_t *deck) {
memset(game, SOSO_EMPTY_CARD, sizeof(soso_game_t));
game->stock_cur = 24;
game->stock_count = 24;
for (int i = 0; i < 7; ++i) {
game->tableau_top[i] = i + 1;
game->tableau_up[i] = i;
if (i < 4) game->foundation_top[i] = 0;
}
int next = 51;
for (int i = 0; i < 7; ++i)
for (int j = i; j < 7; ++j) game->tableau[j][i] = deck->cards[next--];
int stock_id = 23;
while (next > -1) game->stock[stock_id--] = deck->cards[next--];
}
void soso_export(const soso_game_t *game, char *buffer) {
int cur = 0;
for (int i = 0; i < SOSO_TOTAL_PILES; ++i) {
switch (i) {
// Treat the empty pile as waste
case SOSO_EMPTY_PILE: {
buffer[cur++] = ' ';
buffer[cur++] = 'W';
buffer[cur++] = ':';
buffer[cur++] = ' ';
if (game->stock_cur < game->stock_count)
for (int j = game->stock_cur; j < game->stock_count; ++j) {
buffer[cur++] = soso_values[soso_internal_cvalue(game->stock[j])];
buffer[cur++] = soso_suits[soso_internal_csuit(game->stock[j])];
buffer[cur++] = ' ';
}
} break;
case SOSO_STOCK_WASTE: {
buffer[cur++] = ' ';
buffer[cur++] = 'S';
buffer[cur++] = ':';
buffer[cur++] = ' ';
if (game->stock_cur > 0)
for (int j = game->stock_cur - 1; j >= 0; --j) {
buffer[cur++] = soso_values[soso_internal_cvalue(game->stock[j])];
buffer[cur++] = soso_suits[soso_internal_csuit(game->stock[j])];
buffer[cur++] = ' ';
}
} break;
case SOSO_TABLEAU1:
case SOSO_TABLEAU2:
case SOSO_TABLEAU3:
case SOSO_TABLEAU4:
case SOSO_TABLEAU5:
case SOSO_TABLEAU6:
case SOSO_TABLEAU7: {
int t = i - SOSO_TABLEAU1;
buffer[cur++] = 'T';
buffer[cur++] = 49 + t;
buffer[cur++] = ':';
buffer[cur++] = ' ';
if (game->tableau_top[t] > 0) {
for (int j = game->tableau_top[t] - 1; j >= game->tableau_up[t]; --j) {
buffer[cur++] = soso_values[soso_internal_cvalue(game->tableau[t][j])];
buffer[cur++] = soso_suits[soso_internal_csuit(game->tableau[t][j])];
buffer[cur++] = ' ';
}
buffer[cur++] = '|';
buffer[cur++] = ' ';
for (int j = game->tableau_up[t] - 1; j >= 0; --j) {
buffer[cur++] = soso_values[soso_internal_cvalue(game->tableau[t][j])];
buffer[cur++] = soso_suits[soso_internal_csuit(game->tableau[t][j])];
buffer[cur++] = ' ';
}
}
} break;
case SOSO_FOUNDATION1C:
case SOSO_FOUNDATION2D:
case SOSO_FOUNDATION3S:
case SOSO_FOUNDATION4H: {
int f = i - SOSO_FOUNDATION1C;
buffer[cur++] = 'F';
buffer[cur++] = soso_suits[f];
buffer[cur++] = ':';
buffer[cur++] = ' ';
for (int j = 0; j < game->foundation_top[f]; ++j) {
buffer[cur++] = soso_suits[soso_internal_csuit(f)];
buffer[cur++] = soso_values[soso_internal_cvalue(j)];
buffer[cur++] = ' ';
}
} break;
}
buffer[cur++] = '\n';
}
buffer[cur++] = '\0';
}
void soso_ctx_init(soso_ctx_t *ctx, int draw_count, int max_visited, void *(*custom_alloc)(size_t),
void *(*custom_realloc)(void *, size_t), void (*custom_free)(void *)) {
ctx->draw_count = draw_count;
ctx->max_visited = max_visited;
ctx->visited = NULL;
ctx->moves = NULL;
ctx->moves_cap = 0;
ctx->moves_top = 0;
ctx->moves_available_top = 0;
ctx->automoves_count = 0;
ctx->moves_total = 0;
if (custom_alloc != NULL) {
ctx->alloc = custom_alloc;
ctx->realloc = custom_realloc;
ctx->free = custom_free;
}
else {
ctx->alloc = malloc;
ctx->realloc = realloc;
ctx->free = free;
}
}
void soso_ctx_destroy(soso_ctx_t *ctx) {
if (ctx->moves != NULL) ctx->free(ctx->moves);
if (ctx->visited != NULL) sht_destroy(ctx->visited);
}
static void grow_moves(soso_ctx_t *ctx) {
if (ctx->moves_top >= ctx->moves_cap) {
if (ctx->moves_cap == 0) {
ctx->moves = (soso_move_t *)ctx->alloc(sizeof(soso_move_t) * 2);
ctx->moves_cap = 2;
}
else {
ctx->moves =
(soso_move_t *)ctx->realloc(ctx->moves, sizeof(soso_move_t) * ctx->moves_cap * 2);
ctx->moves_cap *= 2;
}
assert(ctx->moves != NULL);
}
}
void soso_internal_add_move(soso_ctx_t *ctx, const soso_move_t m, bool is_auto) {
grow_moves(ctx);
ctx->moves[ctx->moves_top++] = m;
++ctx->moves_total;
if (is_auto) ++ctx->automoves_count;
}
void soso_internal_update_waste_moves(soso_ctx_t *ctx, const soso_game_t *game) {
soso_int_t card_from = soso_internal_get_waste_card(game);
if (card_from == -1) return;
// Waste->Foundation
if (soso_internal_foundation_valid(card_from, game->foundation_top)) {
ctx->moves_available[ctx->moves_available_top].from = SOSO_STOCK_WASTE;
ctx->moves_available[ctx->moves_available_top].to =
SOSO_FOUNDATION1C + soso_internal_csuit(card_from);
ctx->moves_available[ctx->moves_available_top].count = 1;
ctx->moves_available[ctx->moves_available_top++].extra = SOSO_NOEXTRA;
}
// Waste->Tableau
if (soso_internal_cvalue(card_from) < 1) return; // No need to place Ace
bool king_placed = false;
for (int i = 0; i < 7; ++i) {
if (!king_placed && game->tableau_top[i] == 0 &&
soso_internal_cvalue(card_from) == SOSO_KING) {
king_placed = true;
ctx->moves_available[ctx->moves_available_top].from = SOSO_STOCK_WASTE;
ctx->moves_available[ctx->moves_available_top].to = SOSO_TABLEAU1 + i;
ctx->moves_available[ctx->moves_available_top].count = 1;
ctx->moves_available[ctx->moves_available_top++].extra = SOSO_NOEXTRA;
continue;
}
if (game->tableau_top[i] == 0) continue;
soso_int_t card_to = game->tableau[i][game->tableau_top[i] - 1];
if (soso_internal_tableau_valid(card_from, card_to)) {
ctx->moves_available[ctx->moves_available_top].from = SOSO_STOCK_WASTE;
ctx->moves_available[ctx->moves_available_top].to = SOSO_TABLEAU1 + i;
ctx->moves_available[ctx->moves_available_top].count = 1;
ctx->moves_available[ctx->moves_available_top++].extra = SOSO_NOEXTRA;
}
}
}
void soso_internal_update_tableau_moves(soso_ctx_t *ctx, const soso_game_t *game) {
soso_int_t card_from, card_to;
for (int i = 0; i < 7; ++i) {
if (game->tableau_top[i] < 1) continue;
card_from = game->tableau[i][game->tableau_top[i] - 1];
// Tableau->Foundation
if (game->tableau_top[i] > 0 &&
soso_internal_foundation_valid(card_from, game->foundation_top)) {
ctx->moves_available[ctx->moves_available_top].from = SOSO_TABLEAU1 + i;
ctx->moves_available[ctx->moves_available_top].to =
SOSO_FOUNDATION1C + soso_internal_csuit(card_from);
ctx->moves_available[ctx->moves_available_top].count = 1;
ctx->moves_available[ctx->moves_available_top++].extra = SOSO_NOEXTRA;
}
// Tableau->Tableau top card
if (game->tableau_top[i] - game->tableau_up[i] != 1) continue;
bool king_placed = false;
for (int j = 0; j < 7; ++j) {
if (i == j) continue;
if (!king_placed && game->tableau_top[j] == 0 &&
soso_internal_cvalue(card_from) == SOSO_KING) {
king_placed = true;
ctx->moves_available[ctx->moves_available_top].from = SOSO_TABLEAU1 + i;
ctx->moves_available[ctx->moves_available_top].to = SOSO_TABLEAU1 + j;
ctx->moves_available[ctx->moves_available_top].count = 1;
ctx->moves_available[ctx->moves_available_top++].extra = SOSO_NOEXTRA;
continue;
}
card_to = game->tableau[j][game->tableau_top[j] - 1];
if (soso_internal_tableau_valid(card_from, card_to)) {
ctx->moves_available[ctx->moves_available_top].from = SOSO_TABLEAU1 + i;
ctx->moves_available[ctx->moves_available_top].to = SOSO_TABLEAU1 + j;