-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage.html
2091 lines (1845 loc) · 79.3 KB
/
coverage.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hashmap: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/billowdev/fastmap/hashmap/appendable.go (6.9%)</option>
<option value="file1">github.com/billowdev/fastmap/hashmap/appendable_threadsafe.go (8.3%)</option>
<option value="file2">github.com/billowdev/fastmap/hashmap/combination.go (92.4%)</option>
<option value="file3">github.com/billowdev/fastmap/hashmap/config_operation_threadsafe.go (100.0%)</option>
<option value="file4">github.com/billowdev/fastmap/hashmap/config_operations.go (100.0%)</option>
<option value="file5">github.com/billowdev/fastmap/hashmap/conversion.go (100.0%)</option>
<option value="file6">github.com/billowdev/fastmap/hashmap/core.go (100.0%)</option>
<option value="file7">github.com/billowdev/fastmap/hashmap/core_threadsafe.go (100.0%)</option>
<option value="file8">github.com/billowdev/fastmap/hashmap/functional.go (100.0%)</option>
<option value="file9">github.com/billowdev/fastmap/hashmap/functional_threadsafe.go (100.0%)</option>
<option value="file10">github.com/billowdev/fastmap/hashmap/multiple_appendable.go (86.0%)</option>
<option value="file11">github.com/billowdev/fastmap/hashmap/multiple_key_threadsafe.go (89.3%)</option>
<option value="file12">github.com/billowdev/fastmap/hashmap/mutiple_key.go (92.3%)</option>
<option value="file13">github.com/billowdev/fastmap/hashmap/operation_threadsafe.go (100.0%)</option>
<option value="file14">github.com/billowdev/fastmap/hashmap/operations.go (95.5%)</option>
<option value="file15">github.com/billowdev/fastmap/robinhood/robinhood.go (90.5%)</option>
<option value="file16">github.com/billowdev/fastmap/sample/main.go (0.0%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package fastmap
import "sort"
// AppendableHashMap extends HashMap to provide specialized functionality for handling slice operations.
// It maintains type safety through generics while providing convenient methods for appending values
// to existing slices within the map.
//
// Example:
//
// layoutMap := NewAppendableHashMap[string, Component]()
// layoutMap.AppendValues("section1", component1, component2)
// layoutMap.AppendValues("section1", component3) // Appends to existing slice
type AppendableHashMap[K comparable, V any] struct {
*HashMap[K, []V]
}
// NewAppendableHashMap creates a new instance of AppendableHashMap that safely manages
// slices of values associated with keys. It initializes an underlying HashMap to store
// the key-value pairs where values are slices.
//
// Example:
//
// map := NewAppendableHashMap[string, int]()
// map.AppendValues("key1", 1, 2, 3)
func NewAppendableHashMap[K comparable, V any]() *AppendableHashMap[K, V] <span class="cov8" title="1">{
return &AppendableHashMap[K, V]{
HashMap: NewHashMap[K, []V](),
}
}</span>
// AppendValues appends multiple values to an existing slice for a given key.
// If the key doesn't exist, it creates a new slice with the provided values.
// This method provides a safe way to handle the spread operator equivalent in Go.
//
// Parameters:
// - key: The key to associate the values with
// - values: Variadic parameter of values to append
//
// Example:
//
// map.AppendValues("components", component1, component2)
// map.AppendValues("components", component3) // Appends to existing slice
func (h *AppendableHashMap[K, V]) AppendValues(key K, values ...V) <span class="cov8" title="1">{
if existing, exists := h.Get(key); exists </span><span class="cov8" title="1">{
h.Put(key, append(existing, values...))
}</span> else<span class="cov8" title="1"> {
h.Put(key, values)
}</span>
}
// SortValues sorts the slice stored at the given key using a custom comparison function.
// Returns true if the key exists and sorting was performed, false otherwise.
// The less function should return true if a comes before b in the desired order.
//
// Parameters:
// - key: The key whose values should be sorted
// - less: Comparison function that defines the sort order
//
// Example:
//
// map.AppendValues("scores", 75, 82, 90, 65)
// map.SortValues("scores", func(a, b int) bool { return a < b })
// // Result: [65, 75, 82, 90]
func (h *AppendableHashMap[K, V]) SortValues(key K, less func(a, b V) bool) bool <span class="cov0" title="0">{
if values, exists := h.Get(key); exists </span><span class="cov0" title="0">{
sort.Slice(values, func(i, j int) bool </span><span class="cov0" title="0">{
return less(values[i], values[j])
}</span>)
<span class="cov0" title="0">h.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// SortValuesByField sorts values using a field extractor function that retrieves
// comparable values (int, string, float64) from the slice elements.
// Returns true if sorting was performed, false if key doesn't exist.
//
// Example:
//
// type User struct {
// Name string
// Age int
// }
// map.AppendValues("users", User{"Bob", 30}, User{"Alice", 25})
// map.SortValuesByField("users", func(u User) any { return u.Name })
// // Result: [{Alice 25} {Bob 30}]
func (h *AppendableHashMap[K, V]) SortValuesByField(key K, extractor func(V) any) bool <span class="cov0" title="0">{
if values, exists := h.Get(key); exists </span><span class="cov0" title="0">{
sort.Slice(values, func(i, j int) bool </span><span class="cov0" title="0">{
a := extractor(values[i])
b := extractor(values[j])
switch v := a.(type) </span>{
case int:<span class="cov0" title="0">
return v < b.(int)</span>
case string:<span class="cov0" title="0">
return v < b.(string)</span>
case float64:<span class="cov0" title="0">
return v < b.(float64)</span>
default:<span class="cov0" title="0">
return false</span>
}
})
<span class="cov0" title="0">h.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// ReverseValues reverses the order of values stored at the given key.
// Returns true if reversal was performed, false if key doesn't exist.
//
// Example:
//
// map.AppendValues("items", "a", "b", "c")
// map.ReverseValues("items")
// // Result: ["c", "b", "a"]
func (h *AppendableHashMap[K, V]) ReverseValues(key K) bool <span class="cov0" title="0">{
if values, exists := h.Get(key); exists </span><span class="cov0" title="0">{
for i, j := 0, len(values)-1; i < j; i, j = i+1, j-1 </span><span class="cov0" title="0">{
values[i], values[j] = values[j], values[i]
}</span>
<span class="cov0" title="0">h.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// FilterValues applies a predicate function to filter values at the given key.
// Keeps only values where predicate returns true.
// Returns true if filtering was performed, false if key doesn't exist.
//
// Example:
//
// map.AppendValues("numbers", 1, 2, 3, 4, 5)
// map.FilterValues("numbers", func(n int) bool { return n%2 == 0 })
// // Result: [2, 4]
func (h *AppendableHashMap[K, V]) FilterValues(key K, predicate func(V) bool) bool <span class="cov0" title="0">{
if values, exists := h.Get(key); exists </span><span class="cov0" title="0">{
filtered := make([]V, 0, len(values))
for _, v := range values </span><span class="cov0" title="0">{
if predicate(v) </span><span class="cov0" title="0">{
filtered = append(filtered, v)
}</span>
}
<span class="cov0" title="0">h.Put(key, filtered)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// TransformValues applies a transformation function to all values at the given key.
// The transform function maps each value to a new value of the same type.
// Returns true if transformation was performed, false if key doesn't exist.
//
// Example:
//
// map.AppendValues("numbers", 1, 2, 3)
// map.TransformValues("numbers", func(n int) int { return n * 2 })
// // Result: [2, 4, 6]
func (h *AppendableHashMap[K, V]) TransformValues(key K, transform func(V) V) bool <span class="cov0" title="0">{
if values, exists := h.Get(key); exists </span><span class="cov0" title="0">{
for i, v := range values </span><span class="cov0" title="0">{
values[i] = transform(v)
}</span>
<span class="cov0" title="0">h.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// RemoveDuplicates removes duplicate values at the given key while maintaining order.
// Uses the provided equals function to determine value equality.
// Returns true if deduplication was performed, false if key doesn't exist.
//
// Example:
//
// map.AppendValues("tags", "go", "java", "go", "python", "java")
// map.RemoveDuplicates("tags", func(a, b string) bool { return a == b })
// // Result: ["go", "java", "python"]
func (h *AppendableHashMap[K, V]) RemoveDuplicates(key K, equals func(a, b V) bool) bool <span class="cov0" title="0">{
if values, exists := h.Get(key); exists </span><span class="cov0" title="0">{
if len(values) <= 1 </span><span class="cov0" title="0">{
return true
}</span>
<span class="cov0" title="0">unique := make([]V, 0, len(values))
unique = append(unique, values[0])
for i := 1; i < len(values); i++ </span><span class="cov0" title="0">{
isDuplicate := false
for j := 0; j < len(unique); j++ </span><span class="cov0" title="0">{
if equals(values[i], unique[j]) </span><span class="cov0" title="0">{
isDuplicate = true
break</span>
}
}
<span class="cov0" title="0">if !isDuplicate </span><span class="cov0" title="0">{
unique = append(unique, values[i])
}</span>
}
<span class="cov0" title="0">h.Put(key, unique)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package fastmap
import "sort"
// ThreadSafeAppendableHashMap provides thread-safe operations for handling slice values
// in a concurrent environment. It uses mutex locks to ensure safe access and modification
// of the underlying data structure.
//
// Example:
//
// safeMap := NewThreadSafeAppendableHashMap[string, Component]()
// // Safe for concurrent access
// go func() { safeMap.AppendValues("section1", component1) }()
// go func() { safeMap.AppendValues("section1", component2) }()
type ThreadSafeAppendableHashMap[K comparable, V any] struct {
*ThreadSafeHashMap[K, []V]
}
// NewThreadSafeAppendableHashMap creates a new instance of ThreadSafeAppendableHashMap
// that provides synchronized access to slice operations. It's suitable for concurrent
// environments where multiple goroutines might append values simultaneously.
//
// Example:
//
// safeMap := NewThreadSafeAppendableHashMap[string, int]()
// // Safe for concurrent operations
// go func() { safeMap.AppendValues("key1", 1, 2) }()
func NewThreadSafeAppendableHashMap[K comparable, V any]() *ThreadSafeAppendableHashMap[K, V] <span class="cov8" title="1">{
return &ThreadSafeAppendableHashMap[K, V]{
ThreadSafeHashMap: NewThreadSafeHashMap[K, []V](),
}
}</span>
// AppendValues safely appends multiple values to an existing slice for a given key
// in a thread-safe manner. It uses mutex locks to ensure concurrent safety during
// the append operation.
//
// Parameters:
// - key: The key to associate the values with
// - values: Variadic parameter of values to append
//
// Example:
//
// safeMap.AppendValues("users", user1, user2)
// // Concurrent access is safe
// go safeMap.AppendValues("users", user3)
func (t *ThreadSafeAppendableHashMap[K, V]) AppendValues(key K, values ...V) <span class="cov8" title="1">{
t.mutex.Lock()
defer t.mutex.Unlock()
if existing, exists := t.data.Get(key); exists </span><span class="cov8" title="1">{
t.data.Put(key, append(existing, values...))
}</span> else<span class="cov8" title="1"> {
t.data.Put(key, values)
}</span>
}
// SortValues sorts the slice in a thread-safe manner using the custom comparison function.
// Returns true if sorting was performed, false if key doesn't exist.
//
// Example:
//
// safeMap.AppendValues("scores", 75, 82, 90, 65)
// safeMap.SortValues("scores", func(a, b int) bool { return a < b })
// // Result: [65, 75, 82, 90]
func (t *ThreadSafeAppendableHashMap[K, V]) SortValues(key K, less func(a, b V) bool) bool <span class="cov0" title="0">{
t.mutex.Lock()
defer t.mutex.Unlock()
if values, exists := t.Get(key); exists </span><span class="cov0" title="0">{
sort.Slice(values, func(i, j int) bool </span><span class="cov0" title="0">{
return less(values[i], values[j])
}</span>)
<span class="cov0" title="0">t.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// SortValuesByField sorts values in a thread-safe manner using a field extractor.
// Returns true if sorting was performed, false if key doesn't exist.
//
// Example:
//
// type User struct {
// Name string
// Age int
// }
// safeMap.AppendValues("users", User{"Bob", 30}, User{"Alice", 25})
// safeMap.SortValuesByField("users", func(u User) any { return u.Name })
// // Result: [{Alice 25} {Bob 30}]
func (t *ThreadSafeAppendableHashMap[K, V]) SortValuesByField(key K, extractor func(V) any) bool <span class="cov0" title="0">{
t.mutex.Lock()
defer t.mutex.Unlock()
if values, exists := t.Get(key); exists </span><span class="cov0" title="0">{
sort.Slice(values, func(i, j int) bool </span><span class="cov0" title="0">{
a := extractor(values[i])
b := extractor(values[j])
switch v := a.(type) </span>{
case int:<span class="cov0" title="0">
return v < b.(int)</span>
case string:<span class="cov0" title="0">
return v < b.(string)</span>
case float64:<span class="cov0" title="0">
return v < b.(float64)</span>
default:<span class="cov0" title="0">
return false</span>
}
})
<span class="cov0" title="0">t.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// ReverseValues reverses values order in a thread-safe manner.
// Returns true if reversal was performed, false if key doesn't exist.
//
// Example:
//
// safeMap.AppendValues("items", "a", "b", "c")
// safeMap.ReverseValues("items")
// // Result: ["c", "b", "a"]
func (t *ThreadSafeAppendableHashMap[K, V]) ReverseValues(key K) bool <span class="cov0" title="0">{
t.mutex.Lock()
defer t.mutex.Unlock()
if values, exists := t.Get(key); exists </span><span class="cov0" title="0">{
for i, j := 0, len(values)-1; i < j; i, j = i+1, j-1 </span><span class="cov0" title="0">{
values[i], values[j] = values[j], values[i]
}</span>
<span class="cov0" title="0">t.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// FilterValues filters values in a thread-safe manner using a predicate.
// Returns true if filtering was performed, false if key doesn't exist.
//
// Example:
//
// safeMap.AppendValues("numbers", 1, 2, 3, 4, 5)
// safeMap.FilterValues("numbers", func(n int) bool { return n%2 == 0 })
// // Result: [2, 4]
func (t *ThreadSafeAppendableHashMap[K, V]) FilterValues(key K, predicate func(V) bool) bool <span class="cov0" title="0">{
t.mutex.Lock()
defer t.mutex.Unlock()
if values, exists := t.Get(key); exists </span><span class="cov0" title="0">{
filtered := make([]V, 0, len(values))
for _, v := range values </span><span class="cov0" title="0">{
if predicate(v) </span><span class="cov0" title="0">{
filtered = append(filtered, v)
}</span>
}
<span class="cov0" title="0">t.Put(key, filtered)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// TransformValues transforms values in a thread-safe manner.
// Returns true if transformation was performed, false if key doesn't exist.
//
// Example:
//
// safeMap.AppendValues("numbers", 1, 2, 3)
// safeMap.TransformValues("numbers", func(n int) int { return n * 2 })
// // Result: [2, 4, 6]
func (t *ThreadSafeAppendableHashMap[K, V]) TransformValues(key K, transform func(V) V) bool <span class="cov0" title="0">{
t.mutex.Lock()
defer t.mutex.Unlock()
if values, exists := t.Get(key); exists </span><span class="cov0" title="0">{
for i, v := range values </span><span class="cov0" title="0">{
values[i] = transform(v)
}</span>
<span class="cov0" title="0">t.Put(key, values)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
// RemoveDuplicates removes duplicates in a thread-safe manner.
// Returns true if deduplication was performed, false if key doesn't exist.
//
// Example:
//
// safeMap.AppendValues("tags", "go", "java", "go", "python", "java")
// safeMap.RemoveDuplicates("tags", func(a, b string) bool { return a == b })
// // Result: ["go", "java", "python"]
func (t *ThreadSafeAppendableHashMap[K, V]) RemoveDuplicates(key K, equals func(a, b V) bool) bool <span class="cov0" title="0">{
t.mutex.Lock()
defer t.mutex.Unlock()
if values, exists := t.Get(key); exists </span><span class="cov0" title="0">{
if len(values) <= 1 </span><span class="cov0" title="0">{
return true
}</span>
<span class="cov0" title="0">unique := make([]V, 0, len(values))
unique = append(unique, values[0])
for i := 1; i < len(values); i++ </span><span class="cov0" title="0">{
isDuplicate := false
for j := 0; j < len(unique); j++ </span><span class="cov0" title="0">{
if equals(values[i], unique[j]) </span><span class="cov0" title="0">{
isDuplicate = true
break</span>
}
}
<span class="cov0" title="0">if !isDuplicate </span><span class="cov0" title="0">{
unique = append(unique, values[i])
}</span>
}
<span class="cov0" title="0">t.Put(key, unique)
return true</span>
}
<span class="cov0" title="0">return false</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package fastmap
import (
"fmt"
)
// ComboKeyHashMap maps combinations of keys to a single value
type ComboKeyHashMap[K comparable, V any] struct {
data map[string]V // stores values
keyGroups map[K][]KeyGroup[K] // maps individual keys to their key groups
}
// NewComboKeyHashMap creates a new ComboKeyHashMap
func NewComboKeyHashMap[K comparable, V any]() *ComboKeyHashMap[K, V] <span class="cov8" title="1">{
return &ComboKeyHashMap[K, V]{
data: make(map[string]V),
keyGroups: make(map[K][]KeyGroup[K]),
}
}</span>
// generateKeyString creates a unique string for a key group
func (m *ComboKeyHashMap[K, V]) generateKeyString(keys []K) string <span class="cov8" title="1">{
sorted := make([]K, len(keys))
copy(sorted, keys)
sortSlice(sorted)
result := ""
for i, key := range sorted </span><span class="cov8" title="1">{
if i > 0 </span><span class="cov8" title="1">{
result += ","
}</span>
<span class="cov8" title="1">result += fmt.Sprintf("%v", key)</span>
}
<span class="cov8" title="1">return result</span>
}
// Put associates a value with a combination of keys
func (m *ComboKeyHashMap[K, V]) Put(keys []K, value V) <span class="cov8" title="1">{
if len(keys) == 0 </span><span class="cov8" title="1">{
return
}</span>
<span class="cov8" title="1">keyString := m.generateKeyString(keys)
m.data[keyString] = value
// Store key group reference for each key
keyGroup := make(KeyGroup[K], len(keys))
copy(keyGroup, keys)
for _, key := range keys </span><span class="cov8" title="1">{
m.keyGroups[key] = append(m.keyGroups[key], keyGroup)
}</span>
}
// Get retrieves a value by its exact key combination
func (m *ComboKeyHashMap[K, V]) Get(keys []K) (V, bool) <span class="cov8" title="1">{
keyString := m.generateKeyString(keys)
val, exists := m.data[keyString]
return val, exists
}</span>
// GetByKeys retrieves a value where provided keys are part of the key group
func (m *ComboKeyHashMap[K, V]) GetByKeys(keys []K) (V, bool) <span class="cov8" title="1">{
if len(keys) == 0 </span><span class="cov8" title="1">{
var zero V
return zero, false
}</span>
// Check first key's groups
<span class="cov8" title="1">firstKey := keys[0]
groups := m.keyGroups[firstKey]
for _, group := range groups </span><span class="cov8" title="1">{
// Check if all provided keys are in this group
allKeysFound := true
for _, searchKey := range keys </span><span class="cov8" title="1">{
if !contains(group, searchKey) </span><span class="cov0" title="0">{
allKeysFound = false
break</span>
}
}
<span class="cov8" title="1">if allKeysFound </span><span class="cov8" title="1">{
keyString := m.generateKeyString(group)
if val, exists := m.data[keyString]; exists </span><span class="cov8" title="1">{
return val, true
}</span>
}
}
<span class="cov8" title="1">var zero V
return zero, false</span>
}
// Helper functions
func contains[K comparable](slice []K, item K) bool <span class="cov8" title="1">{
for _, v := range slice </span><span class="cov8" title="1">{
if v == item </span><span class="cov8" title="1">{
return true
}</span>
}
<span class="cov0" title="0">return false</span>
}
func sortSlice[K comparable](slice []K) <span class="cov8" title="1">{
n := len(slice)
for i := 0; i < n-1; i++ </span><span class="cov8" title="1">{
for j := 0; j < n-i-1; j++ </span><span class="cov8" title="1">{
if fmt.Sprintf("%v", slice[j]) > fmt.Sprintf("%v", slice[j+1]) </span><span class="cov8" title="1">{
slice[j], slice[j+1] = slice[j+1], slice[j]
}</span>
}
}
}
type KeyGroup[K comparable] []K
// GetByExactKeys retrieves a value where provided keys exactly match a key group
func (m *ComboKeyHashMap[K, V]) GetByExactKeys(keys []K) (V, bool) <span class="cov8" title="1">{
if len(keys) == 0 </span><span class="cov8" title="1">{
var zero V
return zero, false
}</span>
// Check first key's groups
<span class="cov8" title="1">firstKey := keys[0]
groups := m.keyGroups[firstKey]
for _, group := range groups </span><span class="cov8" title="1">{
// First check if lengths match - all keys must be present
if len(group) != len(keys) </span><span class="cov8" title="1">{
continue</span>
}
// Check if all provided keys are in this group
<span class="cov8" title="1">allKeysFound := true
for _, searchKey := range keys </span><span class="cov8" title="1">{
if !contains(group, searchKey) </span><span class="cov0" title="0">{
allKeysFound = false
break</span>
}
}
<span class="cov8" title="1">if allKeysFound </span><span class="cov8" title="1">{
keyString := m.generateKeyString(group)
if val, exists := m.data[keyString]; exists </span><span class="cov8" title="1">{
return val, true
}</span>
}
}
<span class="cov8" title="1">var zero V
return zero, false</span>
}
</pre>
<pre class="file" id="file3" style="display: none">package fastmap
// HandleFieldConfigs processes data using field configurations and returns results
// Example:
//
// configs := map[string]FieldConfig[int]{
// "field1": {
// Handler: func(data map[string]interface{}) *int {
// if val, ok := data["value"].(int); ok {
// return &val
// }
// return nil
// },
// },
// }
// results := safeMap.HandleFieldConfigs(data, configs, "field1")
func (t *ThreadSafeHashMap[K, V]) HandleFieldConfigs(
data []map[string]interface{},
configs map[K]FieldConfig[V],
fieldKey K,
) []V <span class="cov8" title="1">{
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.data.HandleFieldConfigs(data, configs, fieldKey)
}</span>
// ApplyFieldConfig applies a single field configuration to data
// Example:
//
// config := FieldConfig[int]{
// Handler: func(data map[string]interface{}) *int {
// if val, ok := data["value"].(int); ok {
// return &val
// }
// return nil
// },
// }
// success := safeMap.ApplyFieldConfig("field1", config, data)
func (t *ThreadSafeHashMap[K, V]) ApplyFieldConfig(
key K,
config FieldConfig[V],
data map[string]interface{},
) bool <span class="cov8" title="1">{
t.mutex.Lock()
defer t.mutex.Unlock()
return t.data.ApplyFieldConfig(key, config, data)
}</span>
// ProcessFieldConfigs processes data using field configurations with a callback
// Example:
//
// configs := map[string]FieldConfig[int]{
// "field1": {
// Handler: func(data map[string]interface{}) *int {
// if val, ok := data["value"].(int); ok {
// return &val
// }
// return nil
// },
// },
// }
// safeMap.ProcessFieldConfigs(configs, data, func(key string, value int, index int) {
// fmt.Printf("Processed: %s = %d at index %d\n", key, value, index)
// })
func (t *ThreadSafeHashMap[K, V]) ProcessFieldConfigs(
configs map[K]FieldConfig[V],
data []map[string]interface{},
processor func(key K, value V, index int),
) <span class="cov8" title="1">{
t.mutex.Lock()
defer t.mutex.Unlock()
t.data.ProcessFieldConfigs(configs, data, processor)
}</span>
</pre>
<pre class="file" id="file4" style="display: none">package fastmap
type FieldConfig[T any] struct {
RowIndex *int
Handler func(data map[string]interface{}) *T
}
func (h *HashMap[K, V]) HandleFieldConfigs(
data []map[string]interface{},
configs map[K]FieldConfig[V],
fieldKey K,
) []V <span class="cov8" title="1">{
result := make([]V, 0, len(data)*len(configs))
for index, temp := range data </span><span class="cov8" title="1">{
if _, exists := h.Get(fieldKey); exists </span><span class="cov8" title="1">{
if handler := configs[fieldKey].Handler; handler != nil </span><span class="cov8" title="1">{
if configs[fieldKey].RowIndex != nil </span><span class="cov8" title="1">{
*configs[fieldKey].RowIndex = index
}</span>
<span class="cov8" title="1">if value := handler(temp); value != nil </span><span class="cov8" title="1">{
result = append(result, *value)
}</span>
}
}
}
<span class="cov8" title="1">return result</span>
}
func (h *HashMap[K, V]) ApplyFieldConfig(
key K,
config FieldConfig[V],
data map[string]interface{},
) bool <span class="cov8" title="1">{
if value := config.Handler(data); value != nil </span><span class="cov8" title="1">{
return h.UpdateValue(key, *value)
}</span>
<span class="cov8" title="1">return false</span>
}
func (h *HashMap[K, V]) ProcessFieldConfigs(
configs map[K]FieldConfig[V],
data []map[string]interface{},
processor func(key K, value V, index int),
) <span class="cov8" title="1">{
for index, temp := range data </span><span class="cov8" title="1">{
for key, config := range configs </span><span class="cov8" title="1">{
if h.Contains(key) </span><span class="cov8" title="1">{
if config.RowIndex != nil </span><span class="cov8" title="1">{
*config.RowIndex = index
}</span>
<span class="cov8" title="1">if value := config.Handler(temp); value != nil </span><span class="cov8" title="1">{
if h.UpdateValue(key, *value) </span><span class="cov8" title="1">{
if processor != nil </span><span class="cov8" title="1">{
if val, exists := h.Get(key); exists </span><span class="cov8" title="1">{
processor(key, val, index)
}</span>
}
}
}
}
}
}
}
</pre>
<pre class="file" id="file5" style="display: none">package fastmap
// ToMap returns the underlying map
// Example:
//
// standardMap := hashMap.ToMap()
// for k, v := range standardMap {
// fmt.Printf("%v: %v\n", k, v)
// }
func (h *HashMap[K, V]) ToMap() map[K]V <span class="cov8" title="1">{
result := make(map[K]V, len(h.data))
for k, v := range h.data </span><span class="cov8" title="1">{
result[k] = v
}</span>
<span class="cov8" title="1">return result</span>
}
// FromMap creates a new HashMap from a regular map
// Example:
//
// regularMap := map[string]int{"one": 1, "two": 2}
// hashMap := FromMap(regularMap)
func FromMap[K comparable, V any](m map[K]V) *HashMap[K, V] <span class="cov8" title="1">{
h := NewHashMap[K, V]()
for k, v := range m </span><span class="cov8" title="1">{
h.Put(k, v)
}</span>
<span class="cov8" title="1">return h</span>
}
</pre>
<pre class="file" id="file6" style="display: none">package fastmap
type ValueCallback[K comparable, V any] func(key K, value V)
// HashMap is a generic key-value store supporting comparable keys and any value type
// Example:
//
// hashMap := NewHashMap[string, int]()
type HashMap[K comparable, V any] struct {
data map[K]V
}
// NewHashMap creates a new empty HashMap
// Example:
//
// hashMap := NewHashMap[string, User]()
func NewHashMap[K comparable, V any]() *HashMap[K, V] <span class="cov8" title="1">{
return &HashMap[K, V]{
data: make(map[K]V),
}
}</span>
// Put adds or updates a key-value pair in the HashMap
// Example:
//
// hashMap.Put("user123", User{Name: "John"})
func (h *HashMap[K, V]) Put(key K, value V) <span class="cov8" title="1">{
h.data[key] = value
}</span>
// Get retrieves a value by key and returns whether it exists
// Example:
//
// if user, exists := hashMap.Get("user123"); exists {
// fmt.Printf("Found user: %v\n", user)
// }
func (h *HashMap[K, V]) Get(key K) (V, bool) <span class="cov8" title="1">{
value, exists := h.data[key]
return value, exists
}</span>
// Remove deletes a key-value pair from the HashMap
// Example:
//
// hashMap.Remove("user123")
func (h *HashMap[K, V]) Remove(key K) <span class="cov8" title="1">{
delete(h.data, key)
}</span>
// Size returns the number of elements in the HashMap
// Example:
//
// count := hashMap.Size()
// fmt.Printf("HashMap contains %d elements\n", count)
func (h *HashMap[K, V]) Size() int <span class="cov8" title="1">{
return len(h.data)
}</span>
</pre>
<pre class="file" id="file7" style="display: none">package fastmap
import (
"sync"
)
// ThreadSafeHashMap provides thread-safe operations for HashMap through mutex synchronization
// Example:
//
// safeMap := NewThreadSafeHashMap[string, User]()
// safeMap.Put("user1", User{Name: "John"})
type ThreadSafeHashMap[K comparable, V any] struct {
mutex sync.RWMutex
data *HashMap[K, V]
}
// NewThreadSafeHashMap creates a new thread-safe HashMap
// Example:
//
// safeMap := NewThreadSafeHashMap[string, User]()
func NewThreadSafeHashMap[K comparable, V any]() *ThreadSafeHashMap[K, V] <span class="cov8" title="1">{
return &ThreadSafeHashMap[K, V]{
data: NewHashMap[K, V](),
}
}</span>
// Put adds or updates a key-value pair in the ThreadSafeHashMap with write lock
// Example:
//
// safeMap.Put("user123", User{Name: "John"})
func (t *ThreadSafeHashMap[K, V]) Put(key K, value V) <span class="cov8" title="1">{
t.mutex.Lock()
defer t.mutex.Unlock()
t.data.Put(key, value)
}</span>
// Get retrieves a value by key and returns whether it exists with read lock
// Example:
//
// if user, exists := safeMap.Get("user123"); exists {
// fmt.Printf("Found user: %v\n", user)
// }
func (t *ThreadSafeHashMap[K, V]) Get(key K) (V, bool) <span class="cov8" title="1">{
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.data.Get(key)
}</span>
// Remove deletes a key-value pair from the ThreadSafeHashMap with write lock
// Example:
//
// safeMap.Remove("user123")
func (t *ThreadSafeHashMap[K, V]) Remove(key K) <span class="cov8" title="1">{
t.mutex.Lock()
defer t.mutex.Unlock()
t.data.Remove(key)
}</span>
// Clear removes all elements from the ThreadSafeHashMap with write lock
// Example:
//
// safeMap.Clear()
// fmt.Printf("Size after clear: %d\n", safeMap.Size())
func (t *ThreadSafeHashMap[K, V]) Clear() <span class="cov8" title="1">{
t.mutex.Lock()
defer t.mutex.Unlock()
t.data.Clear()
}</span>
// Size returns the number of elements in the ThreadSafeHashMap with read lock
// Example:
//
// count := safeMap.Size()
// fmt.Printf("HashMap contains %d elements\n", count)
func (t *ThreadSafeHashMap[K, V]) Size() int <span class="cov8" title="1">{