-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTable.hpp
917 lines (864 loc) · 31.1 KB
/
Table.hpp
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
// Copyright Kabuki Starship™ <kabukistarship.com>.
#pragma once
#include <_Config.h>
#if SEAM >= SCRIPT2_TABLE
#ifndef SCRIPTT2_TABLE_TEMPLATES
#define SCRIPTT2_TABLE_TEMPLATES
#include "Array.hpp"
#include "Binary.hpp"
#include "Hash.hpp"
#if SEAM == SCRIPT2_TABLE
#include "_Debug.hxx"
#else
#include "_Release.hxx"
#endif
#define TBL_A \
typename CHT = CHR, typename ISZ = ISR, typename ISY = ISQ, typename HSH = IUN
#define TBL_P CHT, ISZ, ISY, HSH
namespace _ {
/* A dense key-index map.
@see ASCII Data Type Specification for DRY documentation.
@link ./Spec/Data/MapTypes/Table.md
@code
Table Memory Layout
+--------------------------------------+
|_ISY_ pile_size (negative) |
|_____ | Collision 0 ISB |
|_____ v Collision N pile |
|_____ Collision N+ 1 |
|_____ pie_end |
+--------------------------------------+
|_____ Boofer Stop |
|_____ Boofer |
|_____ Boofer Start |
+--------------------------------------+
|_____ ^ Key N CHC |
|_____ | Key 0 Keys |
+--------------------------------------+
|_____ Boofer ISY |
|_____ ^ Unsorted Index N sort_map |
| | Unsorted Index 0 |
+--------------------------------------+
|_____ Boofer ISY |
|_____ ^ Collision Index N pile_map |
| | Collision Index 0 |
+--------------------------------------+
|_____ Boofer ISZ |
|_____ ^ Key Offset N keys_map |
| | Key Offset 0 |
+--------------------------------------+
|_____ Boofer HSH |
|_____ ^ Sorted Hash N hash_map |
| | Sorted Hash 0 |
+--------------------------------------+ ^ Up in addresses.
| TTable Header | |
+--------------------------------------+ 0x0
@endcode
*/
template<TBL_A>
struct TTable {
ISZ bytes, //< Size of this object in bytes.
stop; //< Keys boofer stop offset or start if count == total.
TStack<SCK_P> map; //< A Stack of offset mappings to strings.
};
#define TBL TTable<CHT, ISZ, ISY, HSH>
enum {
TableSizeMin = 64, //< Min size of a Table
};
template<TBL_A>
constexpr ISZ CTableEntryOverhead() {
return sizeof(HSH) + sizeof(ISZ) + sizeof(ISY) + sizeof(ISY);
}
template<TBL_A>
inline HSH* TTableHashes(TBL* table) {
return TPtr<HSH>(table, sizeof(TBL));
}
template<TBL_A>
inline const HSH* TTableHashes(const TBL* table) {
return CPtr<HSH>(TTableHashes<TBL_P>(CPtr<TBL>(table)));
}
template<TBL_A>
inline BOL TTableHashesVerify(const TBL* table) {
const HSH* hash_map = TTableHashes<TBL_P>(table);
return THashFindFirstUnsorted<ISY, HSH>(hash_map, hash_map + table->map.count) < 0;
}
template<TBL_A>
inline ISY* TTableCollisionMap(TBL* table, ISY total) {
return TPtr<ISZ>(&TTableHashes<TBL_P>(table)[total]);
}
template<TBL_A>
inline const ISY* TTableCollisionMap(const TBL* table, ISY total) {
return CPtr<ISY>(TTableCollisionMap<TBL_P>(CPtr<TBL>(table),
total));
}
// Map from sorted indexes to unsorted key index.
template<TBL_A>
inline ISY* TTableSortMap(TBL* table, ISY total) {
return TPtr<ISY>(TTableHashes<TBL_P>(table),
total * (sizeof(HSH) + sizeof(ISZ) + sizeof(ISY)));
}
template<TBL_A>
inline const ISY* TTableSortMap(const TBL* table, ISY total) {
return CPtr<ISY>(TTableSortMap<TBL_P>(CPtr<TBL>(table), total));
}
template<TBL_A>
inline ISY* TTablePile(TBL* table, ISZ bytes) {
BOL fml = TAlignIs<ISY, ISZ>(bytes);
D_ASSERT(fml); //< @todo For some reason this is compile erroring?
return TPtrDown<ISY>(table, bytes) - 1;
}
template<TBL_A>
inline const ISY* TTablePile(const TBL* table, ISZ bytes) {
return CPtr<ISY>(TTablePile<TBL_P>(CPtr<TBL>(table), bytes));
}
template<TBL_A>
inline ISY* TTablePile(TBL* table) {
return TTablePile<TBL_P>(table, table->bytes);
}
template<TBL_A>
inline const ISY* TTablePile(const TBL* table) {
return TTablePile<TBL_P>(table, table->bytes);
}
/* TTablePointers<TBL_P>(table, bytes, total, keys_map, hash_map, pile_map,
sort_map);
*/
template<TBL_A>
inline void TTablePointers(TBL* table, ISZ bytes, ISY total, ISZ*& keys_map,
HSH*& hash_map, ISY*& pile_map, ISY*& sort_map) {
hash_map = TPtr<HSH>(table, sizeof(TBL));
keys_map = TPtr<ISZ>(hash_map + total);
pile_map = TPtr<ISY>(keys_map + total);
sort_map = pile_map + total;
}
template<TBL_A>
inline void TTablePointers(const TBL* table, ISZ bytes, ISY total,
const ISZ*& keys_map, const HSH*& hash_map, const ISY*& pile_map,
const ISY*& sort_map) {
hash_map = TPtr<HSH>(table, sizeof(TBL));
keys_map = TPtr<ISZ>(hash_map + total);
pile_map = TPtr<ISY>(keys_map + total);
sort_map = pile_map + total;
}
template<TBL_A, ISZ Total_ = 32, ISZ AverageStringLength_ = 16>
constexpr ISZ CTableSize() {
return sizeof(TBL) +
2 * Total_ * (sizeof(ISZ) + sizeof(ISY)) +
Total_ * (AverageStringLength_ + 1);
}
template<TBL_A>
constexpr ISZ CTableSize(ISY total, ISZ average_string_length = 24) {
return sizeof(TBL) +
2 * total * (sizeof(ISZ) + sizeof(ISY)) +
total * (average_string_length + 1);
}
template<TBL_A>
void* TTableEnd(TBL* table) {
return TPtr<void>(table, table->bytes);
}
template<TBL_A>
const void* TTableEnd(const TBL* table) {
return CPtr<void>(TTableEnd<TBL_P>(CPtr<TBL>(table)));
}
// The offset from the keys_map to the keys boofer start.
template <TBL_A>
inline ISZ TTableKeysMapOffset(ISY total) {
return (sizeof(HSH) + sizeof(ISZ)) * ISZ(total) +
sizeof(TBL);
}
/* Points to the string offsets array. */
template <TBL_A>
ISZ* TTableKeysMap(TBL* table, ISY total) {
return TPtr<ISZ>(table, sizeof(HSH) * total + sizeof(TBL));
}
template <TBL_A>
const ISZ* TTableKeysMap(const TBL* table, ISY total) {
return CPtr<ISZ>(TTableKeysMap<TBL_P>(CPtr<TBL>(table), total));
}
template <TBL_A>
ISZ* TTableKeysMap(TBL* table) {
return TTableKeysMap<TBL_P>(table, ISY(table->map.total));
}
template <TBL_A>
const ISZ* TTableKeysMap(const TBL* table) {
return TTableKeysMap<TBL_P>(table, ISY(table->map.total));
}
template<TBL_A>
inline CHT* TTableKey(ISZ* keys_map, ISZ offset) {
return TPtr<CHT>(keys_map, offset);
}
template<TBL_A>
inline const CHT* TTableKey(const ISZ* keys_map, ISZ offset) {
return CPtr<CHT>(TTableKey<TBL_P>(CPtr<ISZ>(keys_map), offset));
}
template <TBL_A>
CHT* TTableKey(TBL* table, ISY total, ISY count) {
return TTableKey<TBL_P>(table, table)[count];
}
template <TBL_A>
const ISZ* TTableKey(const TBL* table, ISY total, ISY count) {
return CPtr<ISZ>(TTableKey<TBL_P>(CPtr<TBL>(table), total, count));
}
template<TBL_A>
inline CHT* TTableKeyAt(ISZ* keys_map, ISY index) {
return TPtr<CHT>(keys_map, keys_map[index]);
}
template<TBL_A>
inline const CHT* TTableKeyAt(const ISZ* keys_map, ISY index) {
return CPtr<CHT>(TTableKeyAt<TBL_P>(CPtr<ISZ>(keys_map), index));
}
template<TBL_A>
inline CHT* TTableKeysStop(TBL* table, ISZ bytes) {
ISY* pile = TTablePile<TBL_P>(table, bytes);
ISY pile_size = *pile;
D_ASSERT(pile_size < 0);
return TPtrDown<CHT>(TPtr<ISA>(pile + pile_size) - sizeof(CHT));
}
template<TBL_A>
inline const CHT* TTableKeysStop(const TBL* table, ISZ bytes) {
return CPtr<CHT>(TTableKeysStop<TBL_P>(CPtr<TBL>(table), bytes));
}
template<TBL_A>
inline ISZ TTableKeysMapStartOffset(ISY total) {
return ISZ(sizeof(HSH) + sizeof(ISZ) + sizeof(ISY) + sizeof(ISY)) * ISZ(total);
}
template<TBL_A>
inline ISZ TTableKeysStopOffset(const TBL* table, ISZ bytes, const ISZ* keys_map) {
return TDelta<ISZ>(keys_map, TTableKeysStop<TBL_P>(table, bytes));
}
// The address of the next collision index.
template<TBL_A>
inline CHT* TTablePileBotton(TBL* table, ISZ bytes) {
ISY* pile = TTablePile<TBL_P>(table, bytes);
return pile + *TPtr<ISY>(pile);
}
template<TBL_A>
inline const CHT* TTablePileBotton(const TBL* table, ISZ bytes) {
return CPtr<CHT>(TTablePileBotton<TBL_P>(CPtr<TBL>(table), bytes));
}
/* Free space left in the keys boofer. */
template<TBL_A>
inline ISZ TTableFreeSpace(const TBL* table, ISZ bytes, ISZ stop,
ISY total, ISY count) {
const ISZ* keys_map = TTableKeysMap<TBL_P>(table, total);
if (count >= total) // start is stored as table->stop.
return TDelta<ISZ>(keys_map, TTableKeysStop<TBL_P>(table, bytes)) - stop;
// stop is the stop and start is at keys_map[count].
return stop - keys_map[count];
}
template<TBL_A>
inline ISZ TTableFreeSpace(const TBL* table) {
return TTableFreeSpace<TBL_P>(table, table->bytes, table->stop,
ISY(table->map.total), ISY(table->map.count));
}
/* Prints to the Printer. */
template<typename Printer, TBL_A>
Printer& TTablePrint(Printer& o, const TBL* table) {
if (AIsError(table)) return o;
enum {
HashSize = (sizeof(HSH) * 2) < 10 ? 10 : (sizeof(HSH) * 2),
};
ISZ bytes = table->bytes,
stop = table->stop;
ISY count = ISY(table->map.count),
total = ISY(table->map.total);
const ISZ* keys_map = nullptr;
const HSH* hash_map = nullptr;
const ISY* pile_map = nullptr,
* sort_map = nullptr;
TTablePointers<TBL_P>(table, bytes, total, keys_map, hash_map, pile_map,
sort_map);
ISN total_length = STRLength(total) + 1;
const ISY* pile = TTablePile<TBL_P>(table, bytes);
D_ASSERT((TDelta<ISW>(pile) & ISW(sizeof(ISY) - 1)) == 0);
ISY pile_size = *pile;
D_ASSERT(pile_size < 0);
total_length = (total_length < 2 ? 2 : total_length);
#if D_THIS
ISZ start = 0;
if (count < total) {
start = keys_map[count];
} else {
start = stop;
stop = TDelta<ISZ>(keys_map, TTableKeysStop<TBL_P>(table, bytes));
}
#endif
o << Linef("\n+---\n| Table<CH") << CSizeCodef<CHT>()
<< ",IS" << CSizeCodef<ISZ>()
<< ",IS" << CSizeCodef<ISY>()
<< ",IU" << CSizeCodef<HSH>() << ">:0x"
<< Hexf(table) << " bytes:" << bytes << " count:" << count
<< " total:" << total
#if D_THIS
<< "\n| pile_size:" << pile_size << " start:" << start << " stop:" << stop
#endif
<< Linef("\n+---\n| ") << Rightf("i", total_length) << ' '
#if D_THIS
<< Centerf("hash_e", HashSize - 2) << ' '
<< Centerf("hash_u", HashSize - 2) << ' '
#endif
<< Centerf("hash_s", HashSize - 2) << ' '
<< Centerf("index_s", HashSize) << ' '
<< Centerf("offset", 8) << Linef(" \"Key\":{Collissions}\n+---");
for (ISY i = 0; i < count; ++i) {
ISZ offset = keys_map[i];
ISY index_s = sort_map[i];
HSH hash_u = hash_map[index_s],
hash_s = hash_map[i];
const CHT* key = TTableKey<TBL_P>(keys_map, offset);
o << "\n| " << Rightf(i, total_length) << ' '
#if D_THIS
<< Centerf().Hex(THashPrime<HSH, CHT>(key), HashSize) << ' '
<< Centerf().Hex(hash_u, HashSize) << ' '
#endif
<< Centerf().Hex(hash_s, HashSize) << ' '
<< Centerf(index_s, HashSize) << ' '
<< Centerf(offset, 8) << ' '
<< " \"" << key << "\":{";
ISY pile_index = pile_map[i];
if (pile_index > 0 || pile_index <= pile_size) {
o << "\nOut of bounds pile_index at pile_map[" << i << "]: " << pile_index
<< " pile_size:" << pile_size;
D_ASSERT(pile_index > pile_size);
return o;
}
if (pile_index < 0) {
const ISY* pile_cursor = pile + pile_index,
* pile_end = pile + pile_size;
ISY collision = *pile_cursor--;
while (collision >= 0) {
D_ASSERT(collision < count);
o << collision;
collision = *pile_cursor--;
if (collision >= 0) o << ", ";
if (pile_cursor < pile_end)
return o << " Invalid collision" << collision << "}";
}
}
o << '}';
}
#if D_THIS
return o << Linef("\n+---") << Charsf(table, table->bytes) << '\n';
#endif
return o << Linef("\n+---\n");
#endif
}
/* Inits a hash table. */
template<TBL_A>
TBL* TTableInit(TBL* table, ISY total) {
D_CHECK_PTR_RETURN(table);
ISZ bytes = table->bytes;
bytes = TAlignDown<ISZ>(bytes);
D_OBJ_WIPE(table);
ISZ* keys_map = TTableKeysMap<TBL_P>(table, total);
ISY* pile = TTablePile<TBL_P>(table, bytes);
*pile-- = CAInvalidIndex<ISY>();
ISZ start = TTableKeysMapStartOffset<TBL_P>(total);
*keys_map = start;
CHT* stop_ptr = TPtrDown<CHT>(pile, -ISW(sizeof(CHT)));
D_COUT("\nkeys_stop delta:" << TDelta<>(table, stop_ptr));
ISZ stop = TDelta<ISZ>(keys_map, stop_ptr);
D_COUT("\nCreating a TTable<> with bytes:" << bytes << " total:" << total <<
" start:" << start << " stop:" << stop);
if (start >= stop)
D_RETURN_TPTR_ERROR(TBL, ErrorKeysBooferOverflow);
table->stop = stop;
table->map.total = total;
table->map.count = 0;
D_COUT_TABLE(table);
return table;
}
template<TBL_A>
inline TBL* TTableInit(IUW* socket, ISY total) {
return TTableInit<TBL_P>(TPtr<TBL>(socket), total);
}
/* Adds the key to the table.
@return An invalid index upon failure or valid index upon success.
@todo Check if pile has enough room to add another. */
template<TBL_A>
ISY TTableAppend(TBL* table, const CHT* key) {
D_CHECK_PTR_TRETURN(ISY, table);
D_CHECK_PTR_TRETURN(ISY, key);
ISZ bytes = table->bytes,
stop = table->stop;
ISY total = ISY(table->map.total),
count = ISY(table->map.count);
if (count >= total)
D_RETURNT(ISY, -ErrorBooferOverflow);
HSH* hash_map = nullptr;
ISZ* keys_map = nullptr;
ISY* pile_map = nullptr,
* sort_map = nullptr;
TTablePointers<TBL_P>(table, bytes, total, keys_map, hash_map, pile_map,
sort_map);
ISY count_new = count + 1;
ISZ start = keys_map[count];
CHT* stop_ptr = TPtr<CHT>(keys_map, stop);
D_ASSERT(count >= 0 && count < total);
if (count_new == total) {
start = stop;
D_COUT("\nstop:" << stop);
stop_ptr = TTableKeysStop<TBL_P>(table, bytes);
stop = TDelta<ISZ>(keys_map, stop_ptr);
}
CHT* start_ptr = TPtr<CHT>(keys_map, start);
auto start_stop_delta = stop - start;
auto start_stop_ptr_delta = stop_ptr - start_ptr;
D_ASSERT(start_stop_delta == start_stop_ptr_delta);
HSH hash = 0;
ISZ length = 0;
CHT* start_next_ptr = THashPrimePrint_NC<HSH, ISZ, CHT, CHT>(start_ptr,
stop_ptr, key, hash, length);
D_CHECK_PTR_TRETURN(ISY, start_next_ptr);
if (++start_next_ptr >= stop_ptr) D_RETURNT(ISY, -ErrorKeysBooferOverflow);
ISZ start_next = TDelta<ISZ>(keys_map, start_next_ptr);
if (count_new < total) keys_map[count_new] = start_next;
D_COUT(Linef("\n\n---\nAdding key: \"") << key << "\":" << length << ":0x" << Hexf(hash) <<
" count:" << count << " total:" << total);
D_COUT("\n\n Finding insert location... ");
if (count == 0) {
*hash_map = hash;
*pile_map = 0;
*sort_map = count;
table->map.count = count_new;
D_COUT_TABLE(table);
return count;
}
ISY low = 0,
mid = 0,
high = count;
HSH current_hash = 0;
while (low <= high) {
mid = (low + high) >> 1; //< Shift >> 1 to / 2
current_hash = hash_map[mid];
D_COUT("\n low:" << low << " mid:" << mid << " high:" << high <<
" hash:0x" << Hexf(current_hash));
if (hash < current_hash) {
high = mid - 1;
} else if (hash > current_hash) {
low = mid + 1;
} else { // Hash collision
hash_map[count] = ~HSH(0); //< high init value must be the max value.
ISY sort_index = sort_map[mid],
pile_index = pile_map[sort_index];
D_COUT("\n Found hash at sort_index:" << sort_index <<
" pile_index:" << pile_index);
if (pile_index < 0) {
D_COUT("\n Found collision. Checking the pile...");
ISY* pile = TTablePile<TBL_P>(table, bytes);
ISY* pile_cursor = pile + pile_index;
ISY pile_size = *pile,
stack_index = *pile_cursor--;
ISY* pile_end = pile + pile_size;
while (stack_index >= 0) {
if (pile_cursor <= pile_end)
D_RETURNT(ISY, -ErrorBooferOverflow);
ISZ key_offset = keys_map[stack_index];
D_COUT("\n Comparing to \"" <<
TTableKey<TBL_P>(keys_map, key_offset) <<
"\":" << stack_index <<
" table_to_pile_cursor_delta:" << TDelta<>(table, pile_cursor));
if (TSTREquals<CHT>(key, TTableKey<TBL_P>(keys_map, key_offset))) {
D_COUT(" Found key at stack_index:" << stack_index);
return stack_index;
}
stack_index = *pile_cursor--;
}
D_COUT("\n New collision detected, inserting into pile");
if (TPtrDown<CHT>(pile - 1) < start_ptr)
D_RETURNT(ISY, -ErrorBooferOverflow);
sort_map[count] = count;
pile_map[count] = pile_index;
for (ISY i = pile_index; i > pile_index - 5; --i)
D_COUT("\n pile[" << i << "]: " << *(pile + i));
TArrayInsertDown_NC<ISY>(pile + pile_index, pile + pile_size, count);
*pile = --pile_size;
D_COUT("\n Inserted at pile_index:" << pile_index);
for (ISY i = pile_index; i > pile_index - 5; --i)
D_COUT("\n pile[" << i << "]: " << *(pile + i));
table->map.count = count_new;
if (count_new == total - 1)
table->stop = start_next;
D_COUT_TABLE(table);
return count;
}
ISZ key_offset = keys_map[sort_index];
CHT* other_key = TTableKey<TBL_P>(keys_map, key_offset);
D_COUT("\n No collisions exist." <<
"\n Comparing to " << sort_index << ":\"" << other_key <<
"\" at offset:" << key_offset);
if (!TSTREquals<CHT>(key, other_key)) {
ISY* pile = TTablePile<TBL_P>(table, bytes);
ISY pile_size = *pile;
D_COUT("\n New collision with sort_index:" <<
sort_index << " count:" << count << " pile_size:" << pile_size <<
"\n Adding to end of pile:");
for (ISY i = pile_size; i < 0; ++i)
D_COUT("\n pile[" << i << "]: " << *(pile + i));
ISY* pile_cursor = pile + pile_size;
if (TPtr<>(pile_cursor - 3) < TPtr<>(stop))
D_RETURNT(ISY, -ErrorKeysBooferOverflow);
sort_map[count ] = count;
pile_map[count ] = pile_size;
pile_map[sort_index] = pile_size;
*pile = pile_size - 3;
*pile_cursor-- = count;
*pile_cursor-- = sort_index;
*pile_cursor = -1;
table->map.count = count_new;
if (count_new == total - 1) table->stop = start_next;
D_COUT_TABLE(table);
return count;
}
D_COUT("\nTable already contains the key.");
return sort_index;
}
}
D_COUT("\n Hash not found. Inserting into mid:" << mid <<
" before hash:0x" << Hexf(hash_map[mid]) << '\n');
if (hash > current_hash) ++mid;
TArrayInsert_NC<HSH>(hash_map + mid, hash_map + count, hash);
TArrayInsert_NC<ISY>(sort_map + mid, sort_map + count, count);
pile_map[count] = 0;
table->map.count = count_new;
if (count_new == total - 1) table->stop = start_next;
D_COUT_TABLE(table);
return count;
}
template<TBL_A>
ISY TTableAppend(TBL* table, TBL* items) {
ISY count = ISY(items->map.count),
total = ISY(items->map.total);
D_ASSERT(count >= 0 && count <= total);
ISZ* keys_map = TTableKeysMap<TBL_P>(items, total);
ISY result = 0;
for (ISY i = 0; i < count; ++i) {
result = TTableAppend<TBL_P>(table, TPtr<CHT>(keys_map, keys_map[i]));
if (result < 0) return result;
}
return result;
}
/* Doubles the size and total of the table.
@return Returns nil if the size is greater than the amount of memory that
can fit in type ISW, the unaltered socket pointer if the Stack has grown to the
size upper bounds, or a new dynamically allocated socket upon failure. */
template<TBL_A>
BOL TTableGrow(Autoject& obj) {
D_COUT("\n\nGrowing Table...");
/* Grow Algoirhm.
1. Check if we can grow and if so, creatze a new block of memory.
2. Copy the stacks. */
auto origin = obj.origin;
D_ASSERT(origin);
auto source = TPtr<TBL>(origin);
auto size = source->bytes;
auto stop = source->stop;
ISY total = ISY(source->map.total),
count = ISY(source->map.count);
D_COUT("\nsize:" << size << " stop:" << stop << " total:" << total <<
" count:" << count);
auto size_new = ATypeGrow(size);
if (!ATypeCanGrow(size, size_new)) {
D_COUT("\n\nError: keys_size cannot grow! keys_size:" << size <<
" keys_size_new:" << size_new);
return false;
}
auto total_new = ATypeGrow(total);
if (!ATypeCanGrow(total, total_new)) {
D_COUT("\n\nError: total cannot grow! count:" << total <<
" total_new:" << total_new);
return false;
}
IUW* origin_new = obj.ram(nullptr, size_new);
D_COUT("\n\n*TPtr<ISZ>(origin_new):" << *TPtr<ISZ>(origin_new) <<
" size_new:" << size_new);
auto destination = TPtr<TBL>(origin_new);
TTableInit<TBL_P>(destination, total_new);
TTableAppend<TBL_P>(destination, source);
obj.origin = origin_new;
D_COUT("\n\nFinished growing. :-)\n\n");
D_COUT_TABLE(TPtr<TBL>(obj.origin));
return true;
}
/* Adds a string to the end of the Table, auto-growing if neccissary.
@return The index upon success or -1 if the obj can't grow anymore.
@todo Verify copmile size of this function isolated and in the AArray class. */
template<TBL_A>
ISY TTableAppend(Autoject& obj, const CHT* key) {
D_CHECK_PTR_TRETURN(ISY, key);
IUW* origin = obj.origin;
ISY result = TTableAppend<TBL_P>(TPtr<TBL>(origin), key);
while (result < 0) {
if (!TTableGrow<TBL_P>(obj))
D_RETURNT(ISY, -ErrorBooferOverflow);
TBL* table = TPtr<TBL>(obj.origin);
result = TTableAppend<TBL_P>(table, key);
if (result < 0) {
D_COUT("\n\n\nFailed to append to table:" << result << ' ' <<
CrabsErrorSTR(result));
D_COUT_TABLE(table);
}
D_COUT("\ndez nutz baby!!!\n");
}
return result;
}
template<TBL_A>
ISY TTableAppend(Autoject& obj, TBL* items) {
TBL* table = TPtr<TBL>(obj.origin);
ISY count = ISY(table->map.count),
total = ISY(table->map.total);
D_ASSERT(count >= 0 && count <= total);
ISZ* keys_map = TTableKeysMap<TBL_P>(table, total);
ISY result = 0;
for (ISY i = 0; i < count; ++i) {
result = TTableAppend<TBL_P>(obj, TPtr<CHT>(keys_map, keys_map[i]));
if (result < 0) {
D_ASSERT(result >= 0);
return result;
}
}
D_COUT("\nFinished appending with result:" << result);
return result;
}
/* Attempts to find the given key.
@return Returns TTableInvalidIndex<ISY>() upon failure, and valid index upon
success. */
template<TBL_A>
ISY TTableFind(const TBL* table, const CHT* key) {
D_CHECK_PTR_TRETURN(ISY, table);
D_CHECK_PTR_TRETURN(ISY, key);
ISY count = ISY(table->map.count),
total = ISY(table->map.total);
if (count == 0) D_RETURNT(ISY, -ErrorInvalidBounds);
ISZ bytes = table->bytes;
const ISZ* keys_map = nullptr;
const HSH* hash_map = nullptr;
const ISY* pile_map = nullptr,
* sort_map = nullptr;
TTablePointers<TBL_P>(table, bytes, total, keys_map, hash_map, pile_map,
sort_map);
HSH hash = THashPrime<HSH, CHT>(key);
D_COUT("\n\nSearching for key:\"" << key << "\"\n hash:0x" <<
Hexf(hash) << "...");
if (count == 1) {
ISZ offset = keys_map[0];
D_COUT("\nComparing to only key \"" <<
TTableKey<TBL_P>(keys_map, offset) << "\":" << offset << "...");
if (TSTREquals<CHT>(key, TTableKey<TBL_P>(keys_map, offset))) {
D_COUT("hit at offset:" << offset);
return 0;
}
if (!TSTREquals<CHT>(key, TTableKey<TBL_P>(keys_map, offset))) {
D_COUT("key not found.");
return CAInvalidIndex<ISY>();
}
D_COUT("Found key:\"" << key << '\"' << Linef('-'));
return 0;
}
ISY low = 0, mid, high = count - 1;
while (low <= high) { // Binary search through hashes.
mid = (low + high) >> 1; //< >> 1 to /2
HSH current_hash = hash_map[mid];
D_COUT("\n low:" << low << " mid:" << mid << " high:" << high <<
" hashes[mid]:" << Hexf(hash_map[mid]));
if (current_hash > hash) {
high = mid - 1;
} else if (current_hash < hash) {
low = mid + 1;
} else {
ISY sorted_index = sort_map[mid],
pile_index = pile_map[sorted_index];
D_COUT("\n Found hash[" << mid << "]:0x" << Hexf(hash_map[mid]) <<
" sort_index:" << sorted_index << " pile_index:" << pile_index);
if (pile_index < 0) {
const ISY* pile = TTablePile<TBL_P>(table, bytes);
ISY pile_offset = pile_map[sorted_index];
const ISY* pile_cursor = pile + pile_offset;
D_COUT("\n Collision detected at pile_index:" << pile_index <<
".\n Checking the pile_map[" << sorted_index << "]:" << pile_offset);
ISY stack_index = *pile_cursor--;
// @todo I'm not sure if we care if the collision list is empty.
//if (stack_index < 0) return D_RETURNT(ISY, -ErrorInvalidInput);
D_COUT(" pile_cursor:(" << stack_index << ", " << *(pile_cursor - 1) << ')');
while (stack_index >= 0) {
if (stack_index >= count)
D_RETURNT(ISY, -ErrorInvalidInput);
ISZ offset = keys_map[stack_index];
const CHT* other = TTableKey<TBL_P>(keys_map, offset);
D_COUT("\n Comparing to:\"" << other << "\" at offset:" <<
offset << " at " << stack_index);
if (TSTREquals<CHT>(key, other)) {
D_COUT("\n Hit at index:" << stack_index << " offset:" << offset);
return stack_index;
}
stack_index = *pile_cursor--;
}
D_COUT("\n pile does not contain the key.");
D_RETURNT(ISY, -ErrorUnavailable);
}
ISZ offset = keys_map[sorted_index];
const CHT* other = TTableKeyAt<TBL_P>(keys_map, sorted_index);
D_COUT("\n Comparing to key:\"" << other << '"');
if (!TSTREquals<CHT>(key, other)) {
D_COUT("\n Table does not contain key.");
return CAInvalidIndex<ISY>();
}
D_COUT("\n Found key at sort_index:" << sorted_index);
return sorted_index;
}
}
D_COUT(Linef("\nTable does not contain the hash.\n---"));
return CAInvalidIndex<ISY>();
}
/* Gets the given key by index without bounds checking. */
template<TBL_A>
inline CHT* TTableGet_NC(TBL* table, ISY total, ISY index) {
return TPtr<CHT>(table, TTableKeysMap<TBL_P>(table, total)[index]);
}
template<TBL_A>
inline const CHT* TTableGet_NC(const TBL* table, ISY total, ISY index) {
return CPtr<CHT>(TTableGet_NC<TBL_P>(CPtr<TBL>(table), index));
}
/* Gets the given key by index without bounds checking. */
template<TBL_A>
inline CHT* TTableGet_NC(TBL* table, ISY index) {
return TTableGet_NC<TBL_P>(table, ISY(table->map.total), index);
}
template<TBL_A>
inline const CHT* TTableGet_NC(const TBL* table, ISY index) {
return TTableGet_NC<TBL_P>(table, ISY(table->map.total), index);
}
/* Gets the given key by unsorted index. */
template<TBL_A>
inline CHT* TTableGet(TBL* table, ISY total, ISY index) {
D_CHECK_TPTR_RETURN(CHT, table);
ISY count = ISY(table->map.count);
if (index < 0 || index >= count) return nullptr;
ISZ* keys_map = TTableKeysMap<TBL_P>(table, total);
return TPtr<CHT>(keys_map, keys_map[index]);
}
template<TBL_A>
inline const CHT* TTableGet(const TBL* table, ISY total, ISY index) {
return CPtr<CHT>(TTableGet<TBL_P>(CPtr<TBL>(table), total, index));
}
/* Gets the given key by unsorted index. */
template<TBL_A>
inline CHT* TTableGet(TBL* table, ISY index) {
return TTableGet<TBL_P>(table, ISY(table->map.total), index);
}
template<TBL_A>
inline const CHT* TTableGet(const TBL* table, ISY index) {
return TTableGet<TBL_P>(table, ISY(table->map.total), index);
}
/* Removes the string at the given sort_map index. */
template<TBL_A>
ISY TTableRemove(TBL* table, ISY index, BOL pack = false) {
D_CHECK_PTR_TRETURN(ISY, table);
D_COUT("\nBefore:");
D_COUT_TABLE(table);
ISY count = ISY(table->map.count);
ISZ bytes = table->bytes,
stop = table->stop;
ISY total = ISY(table->map.total);
if (index < 0 || index >= count) D_RETURNT(ISY, -ErrorInvalidIndex);
if (index == count - 1) pack = false;
ISZ* keys_map = nullptr;
HSH* hash_map = nullptr;
ISY* pile_map = nullptr,
* sort_map = nullptr;
TTablePointers<TBL_P>(table, bytes, total, keys_map, hash_map, pile_map,
sort_map);
CHT* key_start = 0,
* key_end = 0;
ISY sorted_index = sort_map[index];
TStackRemove<HSH, ISY>(hash_map, total, sorted_index);
TStackRemove<ISZ, ISY>(keys_map, count, index);
ISY pile_offset = pile_map[index];
if (pile_offset > 0) {
ISY* pile = TTablePile<TBL_P>(table, bytes);
ISY pile_size = *pile;
ISY* cursor = pile + pile_offset;
ISY stack_index = *cursor++;
while (stack_index >= 0) {
if (stack_index == index) {
ISY pile_end = *pile;
ISY* pile_end_ptr = pile + pile_end;
while (cursor > pile_end_ptr) *cursor-- = *cursor;
}
stack_index = *cursor++;
}
}
TStackRemove<ISY, ISY>(pile_map, count, index);
if (pack) {
D_ASSERT(false);
key_start = TPtr<CHT>(keys_map, sort_map[index]);
key_end = (index == count) ? TPtr<CHT>(keys_map, stop)
: TPtr<CHT>(keys_map, sort_map[index + 1]);
}
TStackRemove<ISY, ISY>(sort_map, count, index);
ISY* end = sort_map + count;
sort_map += index;
while (sort_map < end) *sort_map = *sort_map - 1;
if (count-- == total)
table->stop = TDelta<ISZ>(keys_map, TTableKeysStop<TBL_P>(table, bytes));
table->map.count = count;
return count;
}
/* Removes the given key from the table. */
template<TBL_A>
ISY TTableRemove(TBL* table, const CHT* key) {
ISY index = 0;// @todo fix me!
return TTableRemove<TBL_P>(table, index);
}
/* An ASCII Table Autoject. */
template<TBL_A, ISY Total = 32,
typename BUF = TBUF<CTableSize<TBL_P, Total>(), ISA, ISZ, TBL>>
class ATable {
AArray<CHT, ISZ, BUF> obj_; //< Auto-Array of CHT(s).
public:
/* Constructs a Table. */
ATable() {
D_OBJ_WIPE(obj_.Origin());
TTableInit<TBL_P>(obj_.Origin(), Total);
}
/* Constructs a Table. */
ATable(ISY total) {
D_OBJ_WIPE(obj_.Origin());
TTableInit<TBL_P>(obj_.Origin(), total);
}
inline ISZ Bytes() { return This()->bytes; }
/* Deep copies the given string into the Table.
@return The index of the string in the Table. */
inline ISY Append(const CHT* string) {
return TTableAppend<TBL_P>(obj_.AJT(), string);
}
/* Removes the string at the given index from the Table. */
inline ISY Remove(ISY index) {
return TTableRemove<TBL_P>(This(), index);
}
/* Removes the string at the given index from the Table. */
inline ISY Remove(const CHT* key) {
return TTableRemove<TBL_P>(This(), key);
}
/* Gets the string at the given index. */
inline CHT* Get(ISY index) {
return TTableGet<TBL_P>(This(), index);
}
inline ISY Find(const CHT* string) {
return TTableFind<TBL_P>(This(), string);
}
/* Gets the ASCII Object. */
inline TBL* This() { return obj_.OriginAs<TBL*>(); }
/* Gets the Autoject. */
inline Autoject AJT() { return obj_.AJT(); }
/* Gets the Auto-Array.
inline AArray<IUA, ISZ, BUF>& AJT_ARY() { return obj_; } */
#if USING_STR
template<typename Printer>
inline Printer& PrintTo(Printer& o) {
return TTablePrint<Printer, TBL_P>(o, This());
}
inline void CPrint() { PrintTo<_::COut>(_::StdOut()); }
#endif
};
} //< namespace _
#endif