github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/reflectmap/special.go (about) 1 package reflectmap 2 3 type SpecialMap struct { 4 bucketCount int 5 buckets []specialBucket 6 } 7 8 type specialBucket struct { 9 hash hashBucket 10 key [bucketSize]int 11 value [bucketSize]int 12 } 13 14 func NewSpecialMap() *SpecialMap { 15 bucketCount := 128 16 return &SpecialMap{ 17 bucketCount: bucketCount, 18 buckets: make([]specialBucket, bucketCount), 19 } 20 } 21 22 func (m *SpecialMap) Add(key, value int) { 23 keyi := key 24 25 bucketi := keyi % m.bucketCount 26 tophash := byte(keyi) 27 if tophash == 0 { 28 tophash = 1 29 } 30 31 bucket := &m.buckets[bucketi] 32 hashes := &bucket.hash 33 keys := &bucket.key 34 values := &bucket.value 35 36 for i, v := range *hashes { 37 if v == 0 { 38 (*hashes)[i] = tophash 39 (*keys)[i] = key 40 (*values)[i] = value 41 return 42 } 43 } 44 45 (*hashes)[0] = tophash 46 (*keys)[0] = key 47 (*values)[0] = value 48 }