diff --git a/g/container/gmap/gmap_int_string_map.go b/g/container/gmap/gmap_int_string_map.go index b2bb701c7..f6290f23d 100644 --- a/g/container/gmap/gmap_int_string_map.go +++ b/g/container/gmap/gmap_int_string_map.go @@ -4,12 +4,11 @@ // If a copy of the MIT was not distributed with gm file, // You can obtain one at https://github.com/gogf/gf. - package gmap import ( - "github.com/gogf/gf/g/internal/rwmutex" - "github.com/gogf/gf/g/util/gconv" + "github.com/gogf/gf/g/internal/rwmutex" + "github.com/gogf/gf/g/util/gconv" ) type IntStringMap struct { @@ -20,21 +19,21 @@ type IntStringMap struct { // NewIntStringMap returns an empty IntStringMap object. // The param used to specify whether using map with un-concurrent-safety, // which is false in default, means concurrent-safe. -func NewIntStringMap(unsafe...bool) *IntStringMap { +func NewIntStringMap(unsafe ...bool) *IntStringMap { return &IntStringMap{ - m : make(map[int]string), - mu : rwmutex.New(unsafe...), - } + m: make(map[int]string), + mu: rwmutex.New(unsafe...), + } } // NewIntStringMapFrom returns an IntStringMap object from given map . // Notice that, the param map is a type of pointer, // there might be some concurrent-safe issues when changing the map outside. -func NewIntStringMapFrom(m map[int]string, unsafe...bool) *IntStringMap { - return &IntStringMap{ - m : m, - mu : rwmutex.New(unsafe...), - } +func NewIntStringMapFrom(m map[int]string, unsafe ...bool) *IntStringMap { + return &IntStringMap{ + m: m, + mu: rwmutex.New(unsafe...), + } } // NewIntStringMapFromArray returns an IntStringMap object from given array. @@ -43,37 +42,37 @@ func NewIntStringMapFrom(m map[int]string, unsafe...bool) *IntStringMap { // // If length of is greater than that of , // the corresponding overflow map values will be the default value of its type. -func NewIntStringMapFromArray(keys []int, values []string, unsafe...bool) *IntStringMap { - m := make(map[int]string) - l := len(values) - for i, k := range keys { - if i < l { - m[k] = values[i] - } else { - m[k] = "" - } - } - return &IntStringMap{ - m : m, - mu : rwmutex.New(unsafe...), - } +func NewIntStringMapFromArray(keys []int, values []string, unsafe ...bool) *IntStringMap { + m := make(map[int]string) + l := len(values) + for i, k := range keys { + if i < l { + m[k] = values[i] + } else { + m[k] = "" + } + } + return &IntStringMap{ + m: m, + mu: rwmutex.New(unsafe...), + } } // Iterator iterates the hash map with custom callback function . // If f returns true, then continue iterating; or false to stop. -func (gm *IntStringMap) Iterator(f func (k int, v string) bool) { - gm.mu.RLock() - defer gm.mu.RUnlock() - for k, v := range gm.m { - if !f(k, v) { - break - } - } +func (gm *IntStringMap) Iterator(f func(k int, v string) bool) { + gm.mu.RLock() + defer gm.mu.RUnlock() + for k, v := range gm.m { + if !f(k, v) { + break + } + } } // Clone returns a new hash map with copy of current map data. func (gm *IntStringMap) Clone() *IntStringMap { - return NewIntStringMapFrom(gm.Map(), !gm.mu.IsSafe()) + return NewIntStringMapFrom(gm.Map(), !gm.mu.IsSafe()) } // Map returns a copy of the data of the hash map. @@ -83,7 +82,7 @@ func (gm *IntStringMap) Map() map[int]string { for k, v := range gm.m { m[k] = v } - gm.mu.RUnlock() + gm.mu.RUnlock() return m } @@ -117,40 +116,40 @@ func (gm *IntStringMap) Get(key int) string { // // It returns value with given . func (gm *IntStringMap) doSetWithLockCheck(key int, value string) string { - gm.mu.Lock() - if v, ok := gm.m[key]; ok { - gm.mu.Unlock() - return v - } - gm.m[key] = value - gm.mu.Unlock() - return value + gm.mu.Lock() + if v, ok := gm.m[key]; ok { + gm.mu.Unlock() + return v + } + gm.m[key] = value + gm.mu.Unlock() + return value } // GetOrSet returns the value by key, // or set value with given if not exist and returns this value. func (gm *IntStringMap) GetOrSet(key int, value string) string { - gm.mu.RLock() - v, ok := gm.m[key] - gm.mu.RUnlock() - if !ok { - return gm.doSetWithLockCheck(key, value) - } else { - return v - } + gm.mu.RLock() + v, ok := gm.m[key] + gm.mu.RUnlock() + if !ok { + return gm.doSetWithLockCheck(key, value) + } else { + return v + } } // GetOrSetFunc returns the value by key, // or sets value with return value of callback function if not exist and returns this value. func (gm *IntStringMap) GetOrSetFunc(key int, f func() string) string { - gm.mu.RLock() - v, ok := gm.m[key] - gm.mu.RUnlock() - if !ok { - return gm.doSetWithLockCheck(key, f()) - } else { - return v - } + gm.mu.RLock() + v, ok := gm.m[key] + gm.mu.RUnlock() + if !ok { + return gm.doSetWithLockCheck(key, f()) + } else { + return v + } } // GetOrSetFuncLock returns the value by key, @@ -159,31 +158,31 @@ func (gm *IntStringMap) GetOrSetFunc(key int, f func() string) string { // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function // with mutex.Lock of the hash map. func (gm *IntStringMap) GetOrSetFuncLock(key int, f func() string) string { - gm.mu.RLock() - val, ok := gm.m[key] - gm.mu.RUnlock() - if !ok { - gm.mu.Lock() - defer gm.mu.Unlock() - if v, ok := gm.m[key]; ok { - return v - } - val = f() - gm.m[key] = val - return val - } else { - return val - } + gm.mu.RLock() + val, ok := gm.m[key] + gm.mu.RUnlock() + if !ok { + gm.mu.Lock() + defer gm.mu.Unlock() + if v, ok := gm.m[key]; ok { + return v + } + val = f() + gm.m[key] = val + return val + } else { + return val + } } // SetIfNotExist sets to the map if the does not exist, then return true. // It returns false if exists, and would be ignored. func (gm *IntStringMap) SetIfNotExist(key int, value string) bool { - if !gm.Contains(key) { - gm.doSetWithLockCheck(key, value) - return true - } - return false + if !gm.Contains(key) { + gm.doSetWithLockCheck(key, value) + return true + } + return false } // SetIfNotExistFunc sets value with return value of callback function , then return true. @@ -213,117 +212,116 @@ func (gm *IntStringMap) SetIfNotExistFuncLock(key int, f func() string) bool { return false } - // BatchRemove batch deletes values of the map by keys. func (gm *IntStringMap) BatchRemove(keys []int) { - gm.mu.Lock() - for _, key := range keys { - delete(gm.m, key) - } - gm.mu.Unlock() + gm.mu.Lock() + for _, key := range keys { + delete(gm.m, key) + } + gm.mu.Unlock() } // Remove deletes value from map by given , and return this deleted value. func (gm *IntStringMap) Remove(key int) string { - gm.mu.Lock() - val, exists := gm.m[key] - if exists { - delete(gm.m, key) - } - gm.mu.Unlock() - return val + gm.mu.Lock() + val, exists := gm.m[key] + if exists { + delete(gm.m, key) + } + gm.mu.Unlock() + return val } // Keys returns all keys of the map as a slice. func (gm *IntStringMap) Keys() []int { - gm.mu.RLock() - keys := make([]int, 0) - for key, _ := range gm.m { - keys = append(keys, key) - } - gm.mu.RUnlock() - return keys + gm.mu.RLock() + keys := make([]int, 0) + for key, _ := range gm.m { + keys = append(keys, key) + } + gm.mu.RUnlock() + return keys } // Values returns all values of the map as a slice. func (gm *IntStringMap) Values() []string { - gm.mu.RLock() - vals := make([]string, 0) - for _, val := range gm.m { - vals = append(vals, val) - } - gm.mu.RUnlock() - return vals + gm.mu.RLock() + vals := make([]string, 0) + for _, val := range gm.m { + vals = append(vals, val) + } + gm.mu.RUnlock() + return vals } // Contains checks whether a key exists. // It returns true if the exists, or else false. func (gm *IntStringMap) Contains(key int) bool { - gm.mu.RLock() - _, exists := gm.m[key] - gm.mu.RUnlock() - return exists + gm.mu.RLock() + _, exists := gm.m[key] + gm.mu.RUnlock() + return exists } // Size returns the size of the map. func (gm *IntStringMap) Size() int { - gm.mu.RLock() - length := len(gm.m) - gm.mu.RUnlock() - return length + gm.mu.RLock() + length := len(gm.m) + gm.mu.RUnlock() + return length } // IsEmpty checks whether the map is empty. // It returns true if map is empty, or else false. func (gm *IntStringMap) IsEmpty() bool { - gm.mu.RLock() - empty := len(gm.m) == 0 - gm.mu.RUnlock() - return empty + gm.mu.RLock() + empty := len(gm.m) == 0 + gm.mu.RUnlock() + return empty } // Clear deletes all data of the map, it will remake a new underlying map data map. func (gm *IntStringMap) Clear() { - gm.mu.Lock() - gm.m = make(map[int]string) - gm.mu.Unlock() + gm.mu.Lock() + gm.m = make(map[int]string) + gm.mu.Unlock() } // LockFunc locks writing with given callback function and mutex.Lock. func (gm *IntStringMap) LockFunc(f func(m map[int]string)) { - gm.mu.Lock() - defer gm.mu.Unlock() - f(gm.m) + gm.mu.Lock() + defer gm.mu.Unlock() + f(gm.m) } // RLockFunc locks reading with given callback function and mutex.RLock. func (gm *IntStringMap) RLockFunc(f func(m map[int]string)) { - gm.mu.RLock() - defer gm.mu.RUnlock() - f(gm.m) + gm.mu.RLock() + defer gm.mu.RUnlock() + f(gm.m) } // Flip exchanges key-value of the map, it will change key-value to value-key. func (gm *IntStringMap) Flip() { - gm.mu.Lock() - defer gm.mu.Unlock() - n := make(map[int]string, len(gm.m)) - for k, v := range gm.m { - n[gconv.Int(v)] = gconv.String(k) - } - gm.m = n + gm.mu.Lock() + defer gm.mu.Unlock() + n := make(map[int]string, len(gm.m)) + for k, v := range gm.m { + n[gconv.Int(v)] = gconv.String(k) + } + gm.m = n } // Merge merges two hash maps. // The map will be merged into the map . func (gm *IntStringMap) Merge(other *IntStringMap) { - gm.mu.Lock() - defer gm.mu.Unlock() - if other != gm { - other.mu.RLock() - defer other.mu.RUnlock() - } - for k, v := range other.m { - gm.m[k] = v - } -} \ No newline at end of file + gm.mu.Lock() + defer gm.mu.Unlock() + if other != gm { + other.mu.RLock() + defer other.mu.RUnlock() + } + for k, v := range other.m { + gm.m[k] = v + } +} diff --git a/g/container/gmap/gmap_string_int_map.go b/g/container/gmap/gmap_string_int_map.go index 052d956e3..028f82c74 100644 --- a/g/container/gmap/gmap_string_int_map.go +++ b/g/container/gmap/gmap_string_int_map.go @@ -8,8 +8,8 @@ package gmap import ( - "github.com/gogf/gf/g/internal/rwmutex" - "github.com/gogf/gf/g/util/gconv" + "github.com/gogf/gf/g/internal/rwmutex" + "github.com/gogf/gf/g/util/gconv" ) type StringIntMap struct { @@ -20,21 +20,21 @@ type StringIntMap struct { // NewStringIntMap returns an empty StringIntMap object. // The param used to specify whether using map with un-concurrent-safety, // which is false in default, means concurrent-safe. -func NewStringIntMap(unsafe...bool) *StringIntMap { +func NewStringIntMap(unsafe ...bool) *StringIntMap { return &StringIntMap{ - m : make(map[string]int), - mu : rwmutex.New(unsafe...), - } + m: make(map[string]int), + mu: rwmutex.New(unsafe...), + } } // NewStringIntMapFrom returns an StringIntMap object from given map . // Notice that, the param map is a type of pointer, // there might be some concurrent-safe issues when changing the map outside. -func NewStringIntMapFrom(m map[string]int, unsafe...bool) *StringIntMap { - return &StringIntMap{ - m : m, - mu : rwmutex.New(unsafe...), - } +func NewStringIntMapFrom(m map[string]int, unsafe ...bool) *StringIntMap { + return &StringIntMap{ + m: m, + mu: rwmutex.New(unsafe...), + } } // NewStringIntMapFromArray returns an StringIntMap object from given array. @@ -43,48 +43,48 @@ func NewStringIntMapFrom(m map[string]int, unsafe...bool) *StringIntMap { // // If length of is greater than that of , // the corresponding overflow map values will be the default value of its type. -func NewStringIntMapFromArray(keys []string, values []int, unsafe...bool) *StringIntMap { - m := make(map[string]int) - l := len(values) - for i, k := range keys { - if i < l { - m[k] = values[i] - } else { - m[k] = 0 - } - } - return &StringIntMap{ - m : m, - mu : rwmutex.New(unsafe...), - } +func NewStringIntMapFromArray(keys []string, values []int, unsafe ...bool) *StringIntMap { + m := make(map[string]int) + l := len(values) + for i, k := range keys { + if i < l { + m[k] = values[i] + } else { + m[k] = 0 + } + } + return &StringIntMap{ + m: m, + mu: rwmutex.New(unsafe...), + } } // Iterator iterates the hash map with custom callback function . // If f returns true, then continue iterating; or false to stop. -func (gm *StringIntMap) Iterator(f func (k string, v int) bool) { - gm.mu.RLock() - defer gm.mu.RUnlock() - for k, v := range gm.m { - if !f(k, v) { - break - } - } +func (gm *StringIntMap) Iterator(f func(k string, v int) bool) { + gm.mu.RLock() + defer gm.mu.RUnlock() + for k, v := range gm.m { + if !f(k, v) { + break + } + } } // Clone returns a new hash map with copy of current map data. func (gm *StringIntMap) Clone() *StringIntMap { - return NewStringIntMapFrom(gm.Map(), !gm.mu.IsSafe()) + return NewStringIntMapFrom(gm.Map(), !gm.mu.IsSafe()) } // Map returns a copy of the data of the hash map. func (gm *StringIntMap) Map() map[string]int { - m := make(map[string]int) - gm.mu.RLock() - for k, v := range gm.m { - m[k] = v - } - gm.mu.RUnlock() - return m + m := make(map[string]int) + gm.mu.RLock() + for k, v := range gm.m { + m[k] = v + } + gm.mu.RUnlock() + return m } // Set sets key-value to the hash map. @@ -107,7 +107,7 @@ func (gm *StringIntMap) BatchSet(m map[string]int) { func (gm *StringIntMap) Get(key string) int { gm.mu.RLock() val, _ := gm.m[key] - gm.mu.RUnlock() + gm.mu.RUnlock() return val } @@ -117,41 +117,41 @@ func (gm *StringIntMap) Get(key string) int { // // It returns value with given . func (gm *StringIntMap) doSetWithLockCheck(key string, value int) int { - gm.mu.Lock() - if v, ok := gm.m[key]; ok { - gm.mu.Unlock() - return v - } - gm.m[key] = value - gm.mu.Unlock() - return value + gm.mu.Lock() + if v, ok := gm.m[key]; ok { + gm.mu.Unlock() + return v + } + gm.m[key] = value + gm.mu.Unlock() + return value } // GetOrSet returns the value by key, // or set value with given if not exist and returns this value. func (gm *StringIntMap) GetOrSet(key string, value int) int { - gm.mu.RLock() - v, ok := gm.m[key] - gm.mu.RUnlock() - if !ok { - return gm.doSetWithLockCheck(key, value) - } else { - return v - } + gm.mu.RLock() + v, ok := gm.m[key] + gm.mu.RUnlock() + if !ok { + return gm.doSetWithLockCheck(key, value) + } else { + return v + } } // GetOrSetFunc returns the value by key, // or sets value with return value of callback function if not exist // and returns this value. func (gm *StringIntMap) GetOrSetFunc(key string, f func() int) int { - gm.mu.RLock() - v, ok := gm.m[key] - gm.mu.RUnlock() - if !ok { - return gm.doSetWithLockCheck(key, f()) - } else { - return v - } + gm.mu.RLock() + v, ok := gm.m[key] + gm.mu.RUnlock() + if !ok { + return gm.doSetWithLockCheck(key, f()) + } else { + return v + } } // GetOrSetFuncLock returns the value by key, @@ -161,31 +161,31 @@ func (gm *StringIntMap) GetOrSetFunc(key string, f func() int) int { // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function // with mutex.Lock of the hash map. func (gm *StringIntMap) GetOrSetFuncLock(key string, f func() int) int { - gm.mu.RLock() - val, ok := gm.m[key] - gm.mu.RUnlock() - if !ok { - gm.mu.Lock() - defer gm.mu.Unlock() - if v, ok := gm.m[key]; ok { - return v - } - val = f() - gm.m[key] = val - return val - } else { - return val - } + gm.mu.RLock() + val, ok := gm.m[key] + gm.mu.RUnlock() + if !ok { + gm.mu.Lock() + defer gm.mu.Unlock() + if v, ok := gm.m[key]; ok { + return v + } + val = f() + gm.m[key] = val + return val + } else { + return val + } } // SetIfNotExist sets to the map if the does not exist, then return true. // It returns false if exists, and would be ignored. func (gm *StringIntMap) SetIfNotExist(key string, value int) bool { - if !gm.Contains(key) { - gm.doSetWithLockCheck(key, value) - return true - } - return false + if !gm.Contains(key) { + gm.doSetWithLockCheck(key, value) + return true + } + return false } // SetIfNotExistFunc sets value with return value of callback function , then return true. @@ -217,114 +217,114 @@ func (gm *StringIntMap) SetIfNotExistFuncLock(key string, f func() int) bool { // BatchRemove batch deletes values of the map by keys. func (gm *StringIntMap) BatchRemove(keys []string) { - gm.mu.Lock() - for _, key := range keys { - delete(gm.m, key) - } - gm.mu.Unlock() + gm.mu.Lock() + for _, key := range keys { + delete(gm.m, key) + } + gm.mu.Unlock() } // Remove deletes value from map by given , and return this deleted value. func (gm *StringIntMap) Remove(key string) int { - gm.mu.Lock() - val, exists := gm.m[key] - if exists { - delete(gm.m, key) - } - gm.mu.Unlock() - return val + gm.mu.Lock() + val, exists := gm.m[key] + if exists { + delete(gm.m, key) + } + gm.mu.Unlock() + return val } // Keys returns all keys of the map as a slice. func (gm *StringIntMap) Keys() []string { - gm.mu.RLock() - keys := make([]string, 0) - for key, _ := range gm.m { - keys = append(keys, key) - } - gm.mu.RUnlock() - return keys + gm.mu.RLock() + keys := make([]string, 0) + for key, _ := range gm.m { + keys = append(keys, key) + } + gm.mu.RUnlock() + return keys } // Values returns all values of the map as a slice. func (gm *StringIntMap) Values() []int { - gm.mu.RLock() - vals := make([]int, 0) - for _, val := range gm.m { - vals = append(vals, val) - } - gm.mu.RUnlock() - return vals + gm.mu.RLock() + vals := make([]int, 0) + for _, val := range gm.m { + vals = append(vals, val) + } + gm.mu.RUnlock() + return vals } // Contains checks whether a key exists. // It returns true if the exists, or else false. func (gm *StringIntMap) Contains(key string) bool { - gm.mu.RLock() - _, exists := gm.m[key] - gm.mu.RUnlock() - return exists + gm.mu.RLock() + _, exists := gm.m[key] + gm.mu.RUnlock() + return exists } // Size returns the size of the map. func (gm *StringIntMap) Size() int { - gm.mu.RLock() - length := len(gm.m) - gm.mu.RUnlock() - return length + gm.mu.RLock() + length := len(gm.m) + gm.mu.RUnlock() + return length } // IsEmpty checks whether the map is empty. // It returns true if map is empty, or else false. func (gm *StringIntMap) IsEmpty() bool { - gm.mu.RLock() - empty := len(gm.m) == 0 - gm.mu.RUnlock() - return empty + gm.mu.RLock() + empty := len(gm.m) == 0 + gm.mu.RUnlock() + return empty } // Clear deletes all data of the map, it will remake a new underlying map data map. func (gm *StringIntMap) Clear() { - gm.mu.Lock() - gm.m = make(map[string]int) - gm.mu.Unlock() + gm.mu.Lock() + gm.m = make(map[string]int) + gm.mu.Unlock() } // LockFunc locks writing with given callback function and mutex.Lock. func (gm *StringIntMap) LockFunc(f func(m map[string]int)) { - gm.mu.Lock() - defer gm.mu.Unlock() - f(gm.m) + gm.mu.Lock() + defer gm.mu.Unlock() + f(gm.m) } // RLockFunc locks reading with given callback function and mutex.RLock. func (gm *StringIntMap) RLockFunc(f func(m map[string]int)) { - gm.mu.RLock() - defer gm.mu.RUnlock() - f(gm.m) + gm.mu.RLock() + defer gm.mu.RUnlock() + f(gm.m) } // Flip exchanges key-value of the map, it will change key-value to value-key. func (gm *StringIntMap) Flip() { - gm.mu.Lock() - defer gm.mu.Unlock() - n := make(map[string]int, len(gm.m)) - for k, v := range gm.m { - n[gconv.String(v)] = gconv.Int(k) - } - gm.m = n + gm.mu.Lock() + defer gm.mu.Unlock() + n := make(map[string]int, len(gm.m)) + for k, v := range gm.m { + n[gconv.String(v)] = gconv.Int(k) + } + gm.m = n } // Merge merges two hash maps. // The map will be merged into the map . func (gm *StringIntMap) Merge(other *StringIntMap) { - gm.mu.Lock() - defer gm.mu.Unlock() - if other != gm { - other.mu.RLock() - defer other.mu.RUnlock() - } - for k, v := range other.m { - gm.m[k] = v - } -} \ No newline at end of file + gm.mu.Lock() + defer gm.mu.Unlock() + if other != gm { + other.mu.RLock() + defer other.mu.RUnlock() + } + for k, v := range other.m { + gm.m[k] = v + } +} diff --git a/g/container/gmap/gmap_string_interface_map.go b/g/container/gmap/gmap_string_interface_map.go index 3fab490e9..5c354ea89 100644 --- a/g/container/gmap/gmap_string_interface_map.go +++ b/g/container/gmap/gmap_string_interface_map.go @@ -20,21 +20,21 @@ type StringInterfaceMap struct { // NewStringInterfaceMap returns an empty StringInterfaceMap object. // The param used to specify whether using map with un-concurrent-safety, // which is false in default, means concurrent-safe. -func NewStringInterfaceMap(unsafe...bool) *StringInterfaceMap { +func NewStringInterfaceMap(unsafe ...bool) *StringInterfaceMap { return &StringInterfaceMap{ - m : make(map[string]interface{}), - mu : rwmutex.New(unsafe...), + m: make(map[string]interface{}), + mu: rwmutex.New(unsafe...), } } // NewStringInterfaceMapFrom returns an StringInterfaceMap object from given map . // Notice that, the param map is a type of pointer, // there might be some concurrent-safe issues when changing the map outside. -func NewStringInterfaceMapFrom(m map[string]interface{}, unsafe...bool) *StringInterfaceMap { - return &StringInterfaceMap{ - m : m, - mu : rwmutex.New(unsafe...), - } +func NewStringInterfaceMapFrom(m map[string]interface{}, unsafe ...bool) *StringInterfaceMap { + return &StringInterfaceMap{ + m: m, + mu: rwmutex.New(unsafe...), + } } // NewStringInterfaceMapFromArray returns an StringInterfaceMap object from given array. @@ -43,25 +43,25 @@ func NewStringInterfaceMapFrom(m map[string]interface{}, unsafe...bool) *StringI // // If length of is greater than that of , // the corresponding overflow map values will be the default value of its type. -func NewStringInterfaceMapFromArray(keys []string, values []interface{}, unsafe...bool) *StringInterfaceMap { - m := make(map[string]interface{}) - l := len(values) - for i, k := range keys { - if i < l { - m[k] = values[i] - } else { - m[k] = interface{}(nil) - } - } - return &StringInterfaceMap{ - m : m, - mu : rwmutex.New(unsafe...), - } +func NewStringInterfaceMapFromArray(keys []string, values []interface{}, unsafe ...bool) *StringInterfaceMap { + m := make(map[string]interface{}) + l := len(values) + for i, k := range keys { + if i < l { + m[k] = values[i] + } else { + m[k] = interface{}(nil) + } + } + return &StringInterfaceMap{ + m: m, + mu: rwmutex.New(unsafe...), + } } // Iterator iterates the hash map with custom callback function . // If f returns true, then continue iterating; or false to stop. -func (gm *StringInterfaceMap) Iterator(f func (k string, v interface{}) bool) { +func (gm *StringInterfaceMap) Iterator(f func(k string, v interface{}) bool) { gm.mu.RLock() defer gm.mu.RUnlock() for k, v := range gm.m { @@ -73,18 +73,18 @@ func (gm *StringInterfaceMap) Iterator(f func (k string, v interface{}) bool) { // Clone returns a new hash map with copy of current map data. func (gm *StringInterfaceMap) Clone() *StringInterfaceMap { - return NewStringInterfaceMapFrom(gm.Map(), !gm.mu.IsSafe()) + return NewStringInterfaceMapFrom(gm.Map(), !gm.mu.IsSafe()) } // Map returns a copy of the data of the hash map. func (gm *StringInterfaceMap) Map() map[string]interface{} { - m := make(map[string]interface{}) - gm.mu.RLock() - for k, v := range gm.m { - m[k] = v - } - gm.mu.RUnlock() - return m + m := make(map[string]interface{}) + gm.mu.RLock() + for k, v := range gm.m { + m[k] = v + } + gm.mu.RUnlock() + return m } // Set sets key-value to the hash map. @@ -96,11 +96,11 @@ func (gm *StringInterfaceMap) Set(key string, val interface{}) { // BatchSet batch sets key-values to the hash map. func (gm *StringInterfaceMap) BatchSet(m map[string]interface{}) { - gm.mu.Lock() - for k, v := range m { - gm.m[k] = v - } - gm.mu.Unlock() + gm.mu.Lock() + for k, v := range m { + gm.m[k] = v + } + gm.mu.Unlock() } // Get returns the value by given . @@ -123,37 +123,37 @@ func (gm *StringInterfaceMap) Get(key string) interface{} { func (gm *StringInterfaceMap) doSetWithLockCheck(key string, value interface{}) interface{} { gm.mu.Lock() defer gm.mu.Unlock() - if v, ok := gm.m[key]; ok { - return v - } - if f, ok := value.(func() interface {}); ok { - value = f() - } - if value != nil { - gm.m[key] = value - } - return value + if v, ok := gm.m[key]; ok { + return v + } + if f, ok := value.(func() interface{}); ok { + value = f() + } + if value != nil { + gm.m[key] = value + } + return value } // GetOrSet returns the value by key, // or set value with given if not exist and returns this value. func (gm *StringInterfaceMap) GetOrSet(key string, value interface{}) interface{} { - if v := gm.Get(key); v == nil { - return gm.doSetWithLockCheck(key, value) - } else { - return v - } + if v := gm.Get(key); v == nil { + return gm.doSetWithLockCheck(key, value) + } else { + return v + } } // GetOrSetFunc returns the value by key, // or sets value with return value of callback function if not exist // and returns this value. func (gm *StringInterfaceMap) GetOrSetFunc(key string, f func() interface{}) interface{} { - if v := gm.Get(key); v == nil { - return gm.doSetWithLockCheck(key, f()) - } else { - return v - } + if v := gm.Get(key); v == nil { + return gm.doSetWithLockCheck(key, f()) + } else { + return v + } } // GetOrSetFuncLock returns the value by key, @@ -163,21 +163,21 @@ func (gm *StringInterfaceMap) GetOrSetFunc(key string, f func() interface{}) int // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function // with mutex.Lock of the hash map. func (gm *StringInterfaceMap) GetOrSetFuncLock(key string, f func() interface{}) interface{} { - if v := gm.Get(key); v == nil { - return gm.doSetWithLockCheck(key, f) - } else { - return v - } + if v := gm.Get(key); v == nil { + return gm.doSetWithLockCheck(key, f) + } else { + return v + } } // SetIfNotExist sets to the map if the does not exist, then return true. // It returns false if exists, and would be ignored. func (gm *StringInterfaceMap) SetIfNotExist(key string, value interface{}) bool { - if !gm.Contains(key) { - gm.doSetWithLockCheck(key, value) - return true - } - return false + if !gm.Contains(key) { + gm.doSetWithLockCheck(key, value) + return true + } + return false } // SetIfNotExistFunc sets value with return value of callback function , then return true. @@ -205,11 +205,11 @@ func (gm *StringInterfaceMap) SetIfNotExistFuncLock(key string, f func() interfa // BatchRemove batch deletes values of the map by keys. func (gm *StringInterfaceMap) BatchRemove(keys []string) { - gm.mu.Lock() - for _, key := range keys { - delete(gm.m, key) - } - gm.mu.Unlock() + gm.mu.Lock() + for _, key := range keys { + delete(gm.m, key) + } + gm.mu.Unlock() } // Remove deletes value from map by given , and return this deleted value. @@ -230,7 +230,7 @@ func (gm *StringInterfaceMap) Keys() []string { for key, _ := range gm.m { keys = append(keys, key) } - gm.mu.RUnlock() + gm.mu.RUnlock() return keys } @@ -273,9 +273,9 @@ func (gm *StringInterfaceMap) IsEmpty() bool { // Clear deletes all data of the map, it will remake a new underlying map data map. func (gm *StringInterfaceMap) Clear() { - gm.mu.Lock() - gm.m = make(map[string]interface{}) - gm.mu.Unlock() + gm.mu.Lock() + gm.m = make(map[string]interface{}) + gm.mu.Unlock() } // LockFunc locks writing with given callback function and mutex.Lock. @@ -315,4 +315,4 @@ func (gm *StringInterfaceMap) Merge(other *StringInterfaceMap) { for k, v := range other.m { gm.m[k] = v } -} \ No newline at end of file +} diff --git a/g/container/gmap/gmap_z_int_string_test.go b/g/container/gmap/gmap_z_int_string_test.go index 8c1ddb3f5..386e3a710 100644 --- a/g/container/gmap/gmap_z_int_string_test.go +++ b/g/container/gmap/gmap_z_int_string_test.go @@ -37,10 +37,9 @@ func Test_IntStringMap_Basic(t *testing.T) { //反转之后不成为以下 map,flip 操作只是翻转原 map //gtest.Assert(m.Map(), map[string]int{"a": 1, "c": 3}) m_f := gmap.NewIntStringMap() - m_f.Set(1,"2") + m_f.Set(1, "2") m_f.Flip() - gtest.Assert(m_f.Map(),map[int]string{2:"1"}) - + gtest.Assert(m_f.Map(), map[int]string{2: "1"}) m.Clear() gtest.Assert(m.Size(), 0) @@ -48,7 +47,7 @@ func Test_IntStringMap_Basic(t *testing.T) { m2 := gmap.NewIntStringMapFrom(map[int]string{1: "a", 2: "b"}) gtest.Assert(m2.Map(), map[int]string{1: "a", 2: "b"}) - m3 := gmap.NewIntStringMapFromArray([]int{1, 2}, []string{"a","b"}) + m3 := gmap.NewIntStringMapFromArray([]int{1, 2}, []string{"a", "b"}) gtest.Assert(m3.Map(), map[int]string{1: "a", 2: "b"}) }) @@ -67,16 +66,16 @@ func Test_IntStringMap_Set_Fun(t *testing.T) { func Test_IntStringMap_Batch(t *testing.T) { m := gmap.NewIntStringMap() - m.BatchSet(map[int]string{1: "a", 2: "b",3:"c"}) + m.BatchSet(map[int]string{1: "a", 2: "b", 3: "c"}) m.Iterator(intStringCallBack) - gtest.Assert(m.Map(), map[int]string{1: "a", 2: "b"}) + gtest.Assert(m.Map(), map[int]string{1: "a", 2: "b",3: "c"}) m.BatchRemove([]int{1, 2}) gtest.Assert(m.Map(), map[int]interface{}{3: "c"}) } func Test_IntStringMap_Clone(t *testing.T) { //clone 方法是深克隆 - m := gmap.NewIntStringMapFrom(map[int]string{1: "a", 2: "b",3:"c"}) + m := gmap.NewIntStringMapFrom(map[int]string{1: "a", 2: "b", 3: "c"}) m_clone := m.Clone() m.Remove(1) diff --git a/g/container/gmap/gmap_z_string_int_test.go b/g/container/gmap/gmap_z_string_int_test.go index 69892eba1..7c1050d46 100644 --- a/g/container/gmap/gmap_z_string_int_test.go +++ b/g/container/gmap/gmap_z_string_int_test.go @@ -6,7 +6,6 @@ import ( "testing" ) - func stringIntCallBack(string, int) bool { return true } @@ -33,10 +32,9 @@ func Test_StringIntMap_Basic(t *testing.T) { gtest.AssertIN(1, m.Values()) m_f := gmap.NewStringIntMap() - m_f.Set("1",2) + m_f.Set("1", 2) m_f.Flip() - gtest.Assert(m_f.Map(),map[string]int{"2":1}) - + gtest.Assert(m_f.Map(), map[string]int{"2": 1}) m.Clear() gtest.Assert(m.Size(), 0) @@ -44,7 +42,7 @@ func Test_StringIntMap_Basic(t *testing.T) { m2 := gmap.NewStringIntMapFrom(map[string]int{"a": 1, "b": 2}) gtest.Assert(m2.Map(), map[string]int{"a": 1, "b": 2}) - m3 := gmap.NewStringIntMapFromArray([]string{"a","b"}, []int{1, 2}) + m3 := gmap.NewStringIntMapFromArray([]string{"a", "b"}, []int{1, 2}) gtest.Assert(m3.Map(), map[string]int{"a": 1, "b": 2}) }) @@ -63,16 +61,16 @@ func Test_StringIntMap_Set_Fun(t *testing.T) { func Test_StringIntMap_Batch(t *testing.T) { m := gmap.NewStringIntMap() - m.BatchSet(map[string]int{"a": 1, "b": 2,"c":3}) + m.BatchSet(map[string]int{"a": 1, "b": 2, "c": 3}) m.Iterator(stringIntCallBack) - gtest.Assert(m.Map(), map[string]int{"a": 1, "b": 2,"c":3}) - m.BatchRemove([]string{"a","b"}) + gtest.Assert(m.Map(), map[string]int{"a": 1, "b": 2, "c": 3}) + m.BatchRemove([]string{"a", "b"}) gtest.Assert(m.Map(), map[string]int{"c": 3}) } func Test_StringIntMap_Clone(t *testing.T) { //clone 方法是深克隆 - m := gmap.NewStringIntMapFrom(map[string]int{"a": 1, "b": 2,"c":3}) + m := gmap.NewStringIntMapFrom(map[string]int{"a": 1, "b": 2, "c": 3}) m_clone := m.Clone() m.Remove("a") @@ -89,5 +87,5 @@ func Test_StringIntMap_Merge(t *testing.T) { m1.Set("a", 1) m2.Set("b", 2) m1.Merge(m2) - gtest.Assert(m1.Map(),map[string]int{"a": 1, "b": 2}) + gtest.Assert(m1.Map(), map[string]int{"a": 1, "b": 2}) } diff --git a/g/container/gmap/gmap_z_string_interface_test.go b/g/container/gmap/gmap_z_string_interface_test.go index 45407c186..74e94115d 100644 --- a/g/container/gmap/gmap_z_string_interface_test.go +++ b/g/container/gmap/gmap_z_string_interface_test.go @@ -6,7 +6,6 @@ import ( "testing" ) - func stringInterfaceCallBack(string, interface{}) bool { return true } @@ -60,11 +59,11 @@ func Test_StringInterfaceMap_Set_Fun(t *testing.T) { func Test_StringInterfaceMap_Batch(t *testing.T) { m := gmap.NewStringInterfaceMap() - m.BatchSet(map[string]interface{}{"a": 1, "b": "2","c":3}) + m.BatchSet(map[string]interface{}{"a": 1, "b": "2", "c": 3}) m.Iterator(stringInterfaceCallBack) - gtest.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2","c":3}) - m.BatchRemove([]string{"a","b"}) - gtest.Assert(m.Map(), map[string]interface{}{"c":3}) + gtest.Assert(m.Map(), map[string]interface{}{"a": 1, "b": "2", "c": 3}) + m.BatchRemove([]string{"a", "b"}) + gtest.Assert(m.Map(), map[string]interface{}{"c": 3}) } func Test_StringInterfaceMap_Clone(t *testing.T) {