merge master

This commit is contained in:
john
2018-10-08 09:44:53 +08:00
5 changed files with 42 additions and 61 deletions

View File

@ -15,7 +15,6 @@ import (
type SortedIntArray struct {
mu *rwmutex.RWMutex // 互斥锁
cap int // 初始化设置的数组容量
size int // 初始化设置的数组大小
array []int // 底层数组
unique *gtype.Bool // 是否要求不能重复
compareFunc func(v1, v2 int) int // 比较函数,返回值 -1: v1 < v20: v1 == v21: v1 > v2
@ -23,8 +22,9 @@ type SortedIntArray struct {
// 创建一个排序的int数组
func NewSortedIntArray(size int, cap int, safe...bool) *SortedIntArray {
a := &SortedIntArray {
return &SortedIntArray {
mu : rwmutex.New(safe...),
array : make([]int, 0, cap),
unique : gtype.NewBool(),
compareFunc : func(v1, v2 int) int {
if v1 < v2 {
@ -36,14 +36,6 @@ func NewSortedIntArray(size int, cap int, safe...bool) *SortedIntArray {
return 0
},
}
a.size = size
if cap > 0 {
a.cap = cap
a.array = make([]int, size, cap)
} else {
a.array = make([]int, size)
}
return a
}
// 添加加数据项
@ -54,13 +46,13 @@ func (a *SortedIntArray) Add(values...int) {
for _, value := range values {
index, cmp := a.Search(value)
if a.unique.Val() && cmp == 0 {
return
continue
}
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 {
a.array = append(a.array, value)
return
a.mu.Unlock()
continue
}
// 加到指定索引后面
if cmp > 0 {
@ -69,6 +61,7 @@ func (a *SortedIntArray) Add(values...int) {
rear := append([]int{}, a.array[index : ]...)
a.array = append(a.array[0 : index], value)
a.array = append(a.array, rear...)
a.mu.Unlock()
}
}
@ -167,11 +160,7 @@ func (a *SortedIntArray) doUnique() {
// 清空数据数组
func (a *SortedIntArray) Clear() {
a.mu.Lock()
if a.cap > 0 {
a.array = make([]int, a.size, a.cap)
} else {
a.array = make([]int, a.size)
}
a.array = make([]int, 0, a.cap)
a.mu.Unlock()
}

View File

@ -15,17 +15,16 @@ import (
type SortedArray struct {
mu *rwmutex.RWMutex // 互斥锁
cap int // 初始化设置的数组容量
size int // 初始化设置的数组大小
array []interface{} // 底层数组
unique *gtype.Bool // 是否要求不能重复
compareFunc func(v1, v2 interface{}) int // 比较函数,返回值 -1: v1 < v20: v1 == v21: v1 > v2
}
func NewSortedArray(size int, cap int, compareFunc func(v1, v2 interface{}) int, safe...bool) *SortedArray {
func NewSortedArray(cap int, compareFunc func(v1, v2 interface{}) int, safe...bool) *SortedArray {
return &SortedArray{
mu : rwmutex.New(safe...),
unique : gtype.NewBool(),
array : make([]interface{}, size, cap),
array : make([]interface{}, 0, cap),
compareFunc : compareFunc,
}
}
@ -38,13 +37,13 @@ func (a *SortedArray) Add(values...interface{}) {
for _, value := range values {
index, cmp := a.Search(value)
if a.unique.Val() && cmp == 0 {
return
continue
}
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 {
a.array = append(a.array, value)
return
a.mu.Unlock()
continue
}
// 加到指定索引后面
if cmp > 0 {
@ -53,6 +52,7 @@ func (a *SortedArray) Add(values...interface{}) {
rear := append([]interface{}{}, a.array[index : ]...)
a.array = append(a.array[0 : index], value)
a.array = append(a.array, rear...)
a.mu.Unlock()
}
}
@ -151,11 +151,7 @@ func (a *SortedArray) doUnique() {
// 清空数据数组
func (a *SortedArray) Clear() {
a.mu.Lock()
if a.cap > 0 {
a.array = make([]interface{}, a.size, a.cap)
} else {
a.array = make([]interface{}, a.size)
}
a.array = make([]interface{}, 0, a.cap)
a.mu.Unlock()
}

View File

@ -16,28 +16,20 @@ import (
type SortedStringArray struct {
mu *rwmutex.RWMutex // 互斥锁
cap int // 初始化设置的数组容量
size int // 初始化设置的数组大小
array []string // 底层数组
unique *gtype.Bool // 是否要求不能重复
compareFunc func(v1, v2 string) int // 比较函数,返回值 -1: v1 < v20: v1 == v21: v1 > v2
}
func NewSortedStringArray(size int, cap int, safe...bool) *SortedStringArray {
a := &SortedStringArray {
mu : rwmutex.New(safe...),
func NewSortedStringArray(cap int, safe...bool) *SortedStringArray {
return &SortedStringArray {
mu : rwmutex.New(safe...),
array : make([]string, 0, cap),
unique : gtype.NewBool(),
compareFunc : func(v1, v2 string) int {
return strings.Compare(v1, v2)
},
}
a.size = size
if cap > 0 {
a.cap = cap
a.array = make([]string, size, cap)
} else {
a.array = make([]string, size)
}
return a
}
// 添加加数据项
@ -46,13 +38,13 @@ func (a *SortedStringArray) Add(values...string) {
for _, value := range values {
index, cmp := a.Search(value)
if a.unique.Val() && cmp == 0 {
return
continue
}
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 {
a.array = append(a.array, value)
return
a.mu.Unlock()
continue
}
// 加到指定索引后面
if cmp > 0 {
@ -61,6 +53,7 @@ func (a *SortedStringArray) Add(values...string) {
rear := append([]string{}, a.array[index : ]...)
a.array = append(a.array[0 : index], value)
a.array = append(a.array, rear...)
a.mu.Unlock()
}
}
}
@ -160,11 +153,7 @@ func (a *SortedStringArray) doUnique() {
// 清空数据数组
func (a *SortedStringArray) Clear() {
a.mu.Lock()
if a.cap > 0 {
a.array = make([]string, a.size, a.cap)
} else {
a.array = make([]string, a.size)
}
a.array = make([]string, 0, a.cap)
a.mu.Unlock()
}

View File

@ -1,12 +0,0 @@
package main
import (
"gitee.com/johng/gf/g/encoding/gjson"
)
func main() {
j := gjson.New(nil)
j.Set("array", []int{1,2,3})
j.Append("array", 4)
j.Dump()
}

19
geg/other/test_test.go Normal file
View File

@ -0,0 +1,19 @@
package main
import (
"testing"
"gitee.com/johng/gf/g/container/garray"
)
func TestGFArray001(t *testing.T) {
var source = []string{"59705a2c1fd50736a4c768a1", "597a95ff1fd5073e48bb2272", "597a960f1fd5073e48bb2274"}
var CacheChannelKeys = garray.NewSortedStringArray(0)
CacheChannelKeys.Add(source...)
t.Logf("%#v\n", CacheChannelKeys)
CacheChannelKeys.Clear()
CacheChannelKeys = garray.NewSortedStringArray(len(source))
t.Logf("%#v\n", CacheChannelKeys)
CacheChannelKeys.Add(source...)
t.Logf("%#v\n", CacheChannelKeys)
}