refact Merge function for garray; add more frequently-used type alias for gf

This commit is contained in:
John
2019-02-22 09:08:46 +08:00
parent 4564f38e1a
commit 0f854e46d8
7 changed files with 94 additions and 102 deletions

View File

@ -458,23 +458,20 @@ func (a *IntArray) RLockFunc(f func(array []int)) *IntArray {
}
// Merge two arrays. The parameter <array> can be any garray type or slice type.
// The difference between Merge and Append is Append supports only specified slice type,
// but Merge supports more variable types.
//
// 合并两个数组.
// 合并两个数组, 支持任意的garray数组类型及slice类型.
func (a *IntArray) Merge(array interface{}) *IntArray {
a.mu.Lock()
defer a.mu.Unlock()
if a == array {
a.array = append(a.array, a.array...)
} else {
if f, ok := array.(apiSliceInt); ok {
a.array = append(a.array, f.Slice()...)
} else if f, ok := array.(apiSliceInterface); ok {
a.array = append(a.array, gconv.Ints(f.Slice())...)
} else if f, ok := array.(apiSliceString); ok {
a.array = append(a.array, gconv.Ints(f.Slice())...)
} else {
a.array = append(a.array, gconv.Ints(array)...)
}
switch v := array.(type) {
case *Array: a.Append(gconv.Ints(v.Slice())...)
case *IntArray: a.Append(gconv.Ints(v.Slice())...)
case *StringArray: a.Append(gconv.Ints(v.Slice())...)
case *SortedArray: a.Append(gconv.Ints(v.Slice())...)
case *SortedIntArray: a.Append(gconv.Ints(v.Slice())...)
case *SortedStringArray: a.Append(gconv.Ints(v.Slice())...)
default:
a.Append(gconv.Ints(array)...)
}
return a
}

View File

@ -450,23 +450,20 @@ func (a *Array) RLockFunc(f func(array []interface{})) *Array {
}
// Merge two arrays. The parameter <array> can be any garray type or slice type.
// The difference between Merge and Append is Append supports only specified slice type,
// but Merge supports more variable types.
//
// 合并两个数组.
// 合并两个数组, 支持任意的garray数组类型及slice类型.
func (a *Array) Merge(array interface{}) *Array {
a.mu.Lock()
defer a.mu.Unlock()
if a == array {
a.array = append(a.array, a.array...)
} else {
if f, ok := array.(apiSliceInterface); ok {
a.array = append(a.array, f.Slice()...)
} else if f, ok := array.(apiSliceInt); ok {
a.array = append(a.array, gconv.Interfaces(f.Slice())...)
} else if f, ok := array.(apiSliceString); ok {
a.array = append(a.array, gconv.Interfaces(f.Slice())...)
} else {
a.array = append(a.array, gconv.Interfaces(array)...)
}
switch v := array.(type) {
case *Array: a.Append(gconv.Interfaces(v.Slice())...)
case *IntArray: a.Append(gconv.Interfaces(v.Slice())...)
case *StringArray: a.Append(gconv.Interfaces(v.Slice())...)
case *SortedArray: a.Append(gconv.Interfaces(v.Slice())...)
case *SortedIntArray: a.Append(gconv.Interfaces(v.Slice())...)
case *SortedStringArray: a.Append(gconv.Interfaces(v.Slice())...)
default:
a.Append(gconv.Interfaces(array)...)
}
return a
}

View File

@ -457,23 +457,20 @@ func (a *StringArray) RLockFunc(f func(array []string)) *StringArray {
}
// Merge two arrays. The parameter <array> can be any garray type or slice type.
// The difference between Merge and Append is Append supports only specified slice type,
// but Merge supports more variable types.
//
// 合并两个数组.
// 合并两个数组, 支持任意的garray数组类型及slice类型.
func (a *StringArray) Merge(array interface{}) *StringArray {
a.mu.Lock()
defer a.mu.Unlock()
if a == array {
a.array = append(a.array, a.array...)
} else {
if f, ok := array.(apiSliceString); ok {
a.array = append(a.array, f.Slice()...)
} else if f, ok := array.(apiSliceInterface); ok {
a.array = append(a.array, gconv.Strings(f.Slice())...)
} else if f, ok := array.(apiSliceInt); ok {
a.array = append(a.array, gconv.Strings(f.Slice())...)
} else {
a.array = append(a.array, gconv.Strings(array)...)
}
switch v := array.(type) {
case *Array: a.Append(gconv.Strings(v.Slice())...)
case *IntArray: a.Append(gconv.Strings(v.Slice())...)
case *StringArray: a.Append(gconv.Strings(v.Slice())...)
case *SortedArray: a.Append(gconv.Strings(v.Slice())...)
case *SortedIntArray: a.Append(gconv.Strings(v.Slice())...)
case *SortedStringArray: a.Append(gconv.Strings(v.Slice())...)
default:
a.Append(gconv.Strings(array)...)
}
return a
}

View File

@ -432,25 +432,21 @@ func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray {
}
// Merge two arrays. The parameter <array> can be any garray type or slice type.
// The difference between Merge and Add is Add supports only specified slice type,
// but Merge supports more variable types.
//
// 合并两个数组.
// 合并两个数组, 支持任意的garray数组类型及slice类型.
func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray {
a.mu.Lock()
defer a.mu.Unlock()
if a == array {
a.array = append(a.array, a.array...)
} else {
if f, ok := array.(apiSliceInt); ok {
a.array = append(a.array, f.Slice()...)
} else if f, ok := array.(apiSliceInterface); ok {
a.array = append(a.array, gconv.Ints(f.Slice())...)
} else if f, ok := array.(apiSliceString); ok {
a.array = append(a.array, gconv.Ints(f.Slice())...)
} else {
a.array = append(a.array, gconv.Ints(array)...)
}
switch v := array.(type) {
case *Array: a.Add(gconv.Ints(v.Slice())...)
case *IntArray: a.Add(gconv.Ints(v.Slice())...)
case *StringArray: a.Add(gconv.Ints(v.Slice())...)
case *SortedArray: a.Add(gconv.Ints(v.Slice())...)
case *SortedIntArray: a.Add(gconv.Ints(v.Slice())...)
case *SortedStringArray: a.Add(gconv.Ints(v.Slice())...)
default:
a.Add(gconv.Ints(array)...)
}
sort.Ints(a.array)
return a
}

View File

@ -439,27 +439,21 @@ func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray {
}
// Merge two arrays. The parameter <array> can be any garray type or slice type.
// The difference between Merge and Add is Add supports only specified slice type,
// but Merge supports more variable types.
//
// 合并两个数组.
// 合并两个数组, 支持任意的garray数组类型及slice类型.
func (a *SortedArray) Merge(array interface{}) *SortedArray {
a.mu.Lock()
defer a.mu.Unlock()
if a == array {
a.array = append(a.array, a.array...)
} else {
if f, ok := array.(apiSliceInterface); ok {
a.array = append(a.array, f.Slice()...)
} else if f, ok := array.(apiSliceInt); ok {
a.array = append(a.array, gconv.Interfaces(f.Slice())...)
} else if f, ok := array.(apiSliceString); ok {
a.array = append(a.array, gconv.Interfaces(f.Slice())...)
} else {
a.array = append(a.array, gconv.Interfaces(array)...)
}
switch v := array.(type) {
case *Array: a.Add(gconv.Interfaces(v.Slice())...)
case *IntArray: a.Add(gconv.Interfaces(v.Slice())...)
case *StringArray: a.Add(gconv.Interfaces(v.Slice())...)
case *SortedArray: a.Add(gconv.Interfaces(v.Slice())...)
case *SortedIntArray: a.Add(gconv.Interfaces(v.Slice())...)
case *SortedStringArray: a.Add(gconv.Interfaces(v.Slice())...)
default:
a.Add(gconv.Interfaces(array)...)
}
sort.Slice(a.array, func(i, j int) bool {
return a.compareFunc(a.array[i], a.array[j]) < 0
})
return a
}

View File

@ -427,25 +427,21 @@ func (a *SortedStringArray) RLockFunc(f func(array []string)) *SortedStringArray
}
// Merge two arrays. The parameter <array> can be any garray type or slice type.
// The difference between Merge and Add is Add supports only specified slice type,
// but Merge supports more variable types.
//
// 合并两个数组.
// 合并两个数组, 支持任意的garray数组类型及slice类型.
func (a *SortedStringArray) Merge(array interface{}) *SortedStringArray {
a.mu.Lock()
defer a.mu.Unlock()
if a == array {
a.array = append(a.array, a.array...)
} else {
if f, ok := array.(apiSliceString); ok {
a.array = append(a.array, f.Slice()...)
} else if f, ok := array.(apiSliceInterface); ok {
a.array = append(a.array, gconv.Strings(f.Slice())...)
} else if f, ok := array.(apiSliceInt); ok {
a.array = append(a.array, gconv.Strings(f.Slice())...)
} else {
a.array = append(a.array, gconv.Strings(array)...)
}
switch v := array.(type) {
case *Array: a.Add(gconv.Strings(v.Slice())...)
case *IntArray: a.Add(gconv.Strings(v.Slice())...)
case *StringArray: a.Add(gconv.Strings(v.Slice())...)
case *SortedArray: a.Add(gconv.Strings(v.Slice())...)
case *SortedIntArray: a.Add(gconv.Strings(v.Slice())...)
case *SortedStringArray: a.Add(gconv.Strings(v.Slice())...)
default:
a.Add(gconv.Strings(array)...)
}
sort.Strings(a.array)
return a
}

29
g/g.go
View File

@ -3,34 +3,49 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// 常用数据类型以及对象封装
package g
import "github.com/gogf/gf/g/container/gvar"
// 框架动态变量可以用该类型替代interface{}类型
type Var = gvar.Var
// Universal variable type, like generics.
//
// 动态变量类型可以用该类型替代interface{}类型
type Var = gvar.Var
// Frequently-used map type alias.
//
// 常用map数据结构(使用别名)
type Map = map[string]interface{}
type Map = map[interface{}]interface{}
type MapAnyStr = map[interface{}]string
type MapAnyInt = map[interface{}]int
type MapStrAny = map[string]interface{}
type MapStrStr = map[string]string
type MapStrInt = map[string]int
type MapIntAny = map[int]interface{}
type MapIntStr = map[int]string
type MapIntInt = map[int]int
// Frequently-used slice type alias.
//
// 常用list数据结构(使用别名)
type List = []Map
type ListAnyStr = []map[interface{}]string
type ListAnyInt = []map[interface{}]int
type ListStrAny = []map[string]interface{}
type ListStrStr = []map[string]string
type ListStrInt = []map[string]int
type ListIntAny = []map[int]interface{}
type ListIntStr = []map[int]string
type ListIntInt = []map[int]int
// 常用slice数据结构(使用别名)
type Slice = []interface{}
type SliceAny = []interface{}
type SliceStr = []string
type SliceInt = []int
type Array = Slice
type ArrayStr = SliceStr
type ArrayInt = SliceInt
type Array = []interface{}
type ArrayAny = []interface{}
type ArrayStr = []string
type ArrayInt = []int