github.com/mhmtszr/concurrent-swiss-map@v1.0.8/example/base/base.go (about)

     1  package main
     2  
     3  import (
     4  	"hash/fnv"
     5  
     6  	csmap "github.com/mhmtszr/concurrent-swiss-map"
     7  )
     8  
     9  func main() {
    10  	myMap := csmap.Create[string, int](
    11  		// set the number of map shards. the default value is 32.
    12  		csmap.WithShardCount[string, int](32),
    13  
    14  		// if don't set custom hasher, use the built-in maphash.
    15  		csmap.WithCustomHasher[string, int](func(key string) uint64 {
    16  			hash := fnv.New64a()
    17  			hash.Write([]byte(key))
    18  			return hash.Sum64()
    19  		}),
    20  
    21  		// set the total capacity, every shard map has total capacity/shard count capacity. the default value is 0.
    22  		csmap.WithSize[string, int](1000),
    23  	)
    24  
    25  	key := "swiss-map"
    26  	myMap.Store(key, 10)
    27  
    28  	val, ok := myMap.Load(key)
    29  	println("load val:", val, "exists:", ok)
    30  
    31  	deleted := myMap.Delete(key)
    32  	println("deleted:", deleted)
    33  
    34  	ok = myMap.Has(key)
    35  	println("has:", ok)
    36  
    37  	empty := myMap.IsEmpty()
    38  	println("empty:", empty)
    39  
    40  	myMap.SetIfAbsent(key, 11)
    41  
    42  	myMap.Range(func(key string, value int) (stop bool) {
    43  		println("range:", key, value)
    44  		return true
    45  	})
    46  
    47  	count := myMap.Count()
    48  	println("count:", count)
    49  
    50  	// Output:
    51  	// load val: 10 exists: true
    52  	// deleted: true
    53  	// has: false
    54  	// empty: true
    55  	// range: swiss-map 11
    56  	// count: 1
    57  }