@@ -60,46 +60,41 @@ func (c *smap) Range(f func(key string, value any) bool) {
6060
6161type (
6262 ConcurrentMap [K comparable , V any ] struct {
63- hasher maphash.Hasher [K ]
64- sharding uint64
65- buckets []* Map [K , V ]
63+ hasher maphash.Hasher [K ]
64+ num uint64
65+ shardings []* Map [K , V ]
6666 }
6767)
6868
6969// NewConcurrentMap create a new concurrency-safe map
7070// arg0 represents the number of shardings; arg1 represents the initialized capacity of a sharding.
7171func NewConcurrentMap [K comparable , V any ](size ... uint64 ) * ConcurrentMap [K , V ] {
72- sharding , capacity := uint64 (0 ), uint64 (0 )
73- if len (size ) >= 1 {
74- sharding = size [0 ]
75- }
76- if len (size ) >= 2 {
77- capacity = size [1 ]
78- }
79- sharding = internal .SelectValue (sharding <= 0 , 16 , sharding )
80- sharding = internal .ToBinaryNumber (sharding )
72+ size = append (size , 0 , 0 )
73+ num , capacity := size [0 ], size [1 ]
74+ num = internal .ToBinaryNumber (internal .SelectValue (num <= 0 , 16 , num ))
8175 var cm = & ConcurrentMap [K , V ]{
82- hasher : maphash .NewHasher [K ](),
83- sharding : sharding ,
84- buckets : make ([]* Map [K , V ], sharding ),
76+ hasher : maphash .NewHasher [K ](),
77+ num : num ,
78+ shardings : make ([]* Map [K , V ], num ),
8579 }
86- for i , _ := range cm .buckets {
87- cm .buckets [i ] = & Map [K , V ]{ m : make ( map [ K ] V , capacity )}
80+ for i , _ := range cm .shardings {
81+ cm .shardings [i ] = NewMap [K , V ]( int ( capacity ))
8882 }
8983 return cm
9084}
9185
9286// GetSharding returns a map sharding for a key
87+ // the operations inside the sharding is lockless, and need to be locked manually.
9388func (c * ConcurrentMap [K , V ]) GetSharding (key K ) * Map [K , V ] {
9489 var hashCode = c .hasher .Hash (key )
95- var index = hashCode & (c .sharding - 1 )
96- return c .buckets [index ]
90+ var index = hashCode & (c .num - 1 )
91+ return c .shardings [index ]
9792}
9893
9994// Len returns the number of elements in the map
10095func (c * ConcurrentMap [K , V ]) Len () int {
10196 var length = 0
102- for _ , b := range c .buckets {
97+ for _ , b := range c .shardings {
10398 b .Lock ()
10499 length += b .Len ()
105100 b .Unlock ()
@@ -142,8 +137,8 @@ func (c *ConcurrentMap[K, V]) Range(f func(key K, value V) bool) {
142137 next = f (k , v )
143138 return next
144139 }
145- for i := uint64 (0 ); i < c .sharding && next ; i ++ {
146- var b = c .buckets [i ]
140+ for i := uint64 (0 ); i < c .num && next ; i ++ {
141+ var b = c .shardings [i ]
147142 b .Lock ()
148143 b .Range (cb )
149144 b .Unlock ()
@@ -155,6 +150,16 @@ type Map[K comparable, V any] struct {
155150 m map [K ]V
156151}
157152
153+ func NewMap [K comparable , V any ](size ... int ) * Map [K , V ] {
154+ var capacity = 0
155+ if len (size ) > 0 {
156+ capacity = size [0 ]
157+ }
158+ c := new (Map [K , V ])
159+ c .m = make (map [K ]V , capacity )
160+ return c
161+ }
162+
158163func (c * Map [K , V ]) Len () int { return len (c .m ) }
159164
160165func (c * Map [K , V ]) Load (key K ) (value V , ok bool ) {
0 commit comments