]> 127.0.0.1 Git - part/.git/commitdiff
106 v0.4.2
authorqydysky <qydysky@foxmail.com>
Wed, 17 Feb 2021 16:37:23 +0000 (00:37 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 17 Feb 2021 16:37:23 +0000 (00:37 +0800)
map/Map.go
map/Map_test.go

index 16d9c817a6c8a3b8691992359de32d50d358caf8..97dafdb0e37ffedd4b7d68ddbbc169c1d607e0f3 100644 (file)
@@ -37,7 +37,7 @@ func (t *Map) Store(k,v interface{}) {
        t.lock.Unlock()
 }
 
-func (t *Map) Load(k interface{}) (v interface{}) {
+func (t *Map) Load(k interface{}) (interface{},bool) {
 
        m,_ := t.readOnly.Load().(map[interface{}]*ptr)
        p,ok := m[k]
@@ -60,12 +60,17 @@ func (t *Map) Load(k interface{}) (v interface{}) {
        }
 
        if !ok{
-               return nil
+               return nil,false
        }
 
        return p.tryLoad()
 }
 
+func (t *Map) LoadV(k interface{}) (v interface{}) {
+       v,_ = t.Load(k)
+       return
+}
+
 func (t *Map) Delete(k interface{}) {
        m,_ := t.readOnly.Load().(map[interface{}]*ptr)
        
@@ -103,12 +108,12 @@ func (t *ptr) tryStore(v *interface{}) {
        // atomic.StorePointer(&t.p, unsafe.Pointer(v))
 }
 
-func (t *ptr) tryLoad() (interface{}) {
+func (t *ptr) tryLoad() (interface{},bool) {
        // p := atomic.LoadPointer(&t.p)
        if t.p == nil{
-               return nil
+               return nil,false
        }
-       return *(*interface{})(t.p)
+       return *(*interface{})(t.p),true
 }
 
 type pLock struct{
index d5aa198997571aa1c93c973c0868e4164d1819ca..9e6d9a5cfb2c83adc1cdb96ab452ca6448212cf4 100644 (file)
@@ -9,13 +9,13 @@ func Test_customMap(t *testing.T) {
        var c Map
        //set
        c.Store(0, 3)
-       if c.Load(0) != 3{t.Error(`1`)}
+       if v,ok := c.Load(0);ok && v != 3{t.Error(`1`)}
        //change
        c.Store(0, 1)
-       if c.Load(0) != 1{t.Error(`2`)}
+       if v,ok := c.Load(0);ok && v != 1{t.Error(`2`)}
        //del
        c.Store(0, nil)
-       if c.Load(0) != nil{t.Error(`3`)}
+       if v,ok := c.Load(0);ok && v != nil{t.Error(`3`)}
 }
 
 func Benchmark_customMap_Set(b *testing.B) {
@@ -79,7 +79,7 @@ func Benchmark_customMap_Get(b *testing.B) {
        b.ResetTimer()
        for i := 0; i < t; i++ {
                go func(index int) {
-                       if c.Load(index).(int) != index {
+                       if c.LoadV(index).(int) != index {
                                b.Error("q")
                        }
                        w.Done()
@@ -113,7 +113,7 @@ func Benchmark_customMap_SetGet(b *testing.B) {
                        w.Done()
                }(i)
                go func(index int) {
-                       if t,ok := c.Load(index).(int);!ok || t != index && t != index+1{
+                       if t,ok := c.LoadV(index).(int);!ok || t != index && t != index+1{
                                b.Error(`E`, index, t)
                        }
                        w.Done()