From bd0df135c8f65441143a71e16a5bd94847d41514 Mon Sep 17 00:00:00 2001 From: john Date: Sun, 30 Sep 2018 15:44:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dgarray=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84Add=E5=8F=98=E5=8F=82=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E6=AD=BB=E9=94=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_sorted_int.go | 3 ++- g/container/garray/garray_sorted_interface.go | 3 ++- g/container/garray/garray_sorted_string.go | 5 +++-- geg/other/test.go | 16 ++++++++++------ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index 2206ce3f2..fcbbee9f9 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -57,9 +57,9 @@ func (a *SortedIntArray) Add(values...int) { return } a.mu.Lock() - defer a.mu.Unlock() if index < 0 { a.array = append(a.array, value) + a.mu.Unlock() return } // 加到指定索引后面 @@ -69,6 +69,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() } } diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index 1a51ad405..d923bf034 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -41,9 +41,9 @@ func (a *SortedArray) Add(values...interface{}) { return } a.mu.Lock() - defer a.mu.Unlock() if index < 0 { a.array = append(a.array, value) + a.mu.Unlock() return } // 加到指定索引后面 @@ -53,6 +53,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() } } diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index f010f5539..c35743839 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -24,7 +24,7 @@ type SortedStringArray struct { func NewSortedStringArray(size int, cap int, safe...bool) *SortedStringArray { a := &SortedStringArray { - mu : rwmutex.New(safe...), + mu : rwmutex.New(safe...), unique : gtype.NewBool(), compareFunc : func(v1, v2 string) int { return strings.Compare(v1, v2) @@ -49,9 +49,9 @@ func (a *SortedStringArray) Add(values...string) { return } a.mu.Lock() - defer a.mu.Unlock() if index < 0 { a.array = append(a.array, value) + a.mu.Unlock() return } // 加到指定索引后面 @@ -61,6 +61,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() } } } diff --git a/geg/other/test.go b/geg/other/test.go index 95f307477..8dc34cff3 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,11 +1,15 @@ package main -import ( - "fmt" - "gitee.com/johng/gf/g/util/gregex" -) +import "gitee.com/johng/gf/g/container/garray" + +type S struct { -func main() { - fmt.Println(gregex.IsMatchString("g/os/glog/glog.+$", "g/os/glog/glog_logger.go")) +} + +func main() { + var source = []string{"59705a2c1fd50736a4c768a1", "597a95ff1fd5073e48bb2272", "597a960f1fd5073e48bb2274"} + var CacheChannelKeys *garray.SortedStringArray + CacheChannelKeys = garray.NewSortedStringArray(9999, 9999) + CacheChannelKeys.Add(source...) } From 0be5cd590bc6b69ba526dd970f4e128eef9b5171 Mon Sep 17 00:00:00 2001 From: john Date: Sun, 30 Sep 2018 16:32:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dgarray=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84Add=E5=8F=AF=E5=8F=98=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_sorted_int.go | 4 ++-- g/container/garray/garray_sorted_interface.go | 4 ++-- g/container/garray/garray_sorted_string.go | 4 ++-- geg/other/test.go | 8 ++++++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index fcbbee9f9..36bdaf147 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -54,13 +54,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() if index < 0 { a.array = append(a.array, value) a.mu.Unlock() - return + continue } // 加到指定索引后面 if cmp > 0 { diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index d923bf034..3599e63ce 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -38,13 +38,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() if index < 0 { a.array = append(a.array, value) a.mu.Unlock() - return + continue } // 加到指定索引后面 if cmp > 0 { diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index c35743839..b5b546414 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -46,13 +46,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() if index < 0 { a.array = append(a.array, value) a.mu.Unlock() - return + continue } // 加到指定索引后面 if cmp > 0 { diff --git a/geg/other/test.go b/geg/other/test.go index 8dc34cff3..3e7a07cfb 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,6 +1,9 @@ package main -import "gitee.com/johng/gf/g/container/garray" +import ( + "gitee.com/johng/gf/g/container/garray" + "fmt" +) type S struct { @@ -9,7 +12,8 @@ type S struct { func main() { var source = []string{"59705a2c1fd50736a4c768a1", "597a95ff1fd5073e48bb2272", "597a960f1fd5073e48bb2274"} var CacheChannelKeys *garray.SortedStringArray - CacheChannelKeys = garray.NewSortedStringArray(9999, 9999) + CacheChannelKeys = garray.NewSortedStringArray(0, 0) CacheChannelKeys.Add(source...) + fmt.Println(CacheChannelKeys.Slice()) } From 16dd4a92c1712c56eaa5d2d45a860595b6aa3a9c Mon Sep 17 00:00:00 2001 From: john Date: Sun, 30 Sep 2018 16:49:34 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=8E=BB=E6=8E=89garray=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84length=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/container/garray/garray_sorted_int.go | 18 +++-------------- g/container/garray/garray_sorted_interface.go | 11 +++------- g/container/garray/garray_sorted_string.go | 20 ++++--------------- geg/other/test.go | 7 ++++--- geg/other/test2.go | 12 ----------- geg/other/test_test.go | 19 ++++++++++++++++++ 6 files changed, 33 insertions(+), 54 deletions(-) delete mode 100644 geg/other/test2.go create mode 100644 geg/other/test_test.go diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index 36bdaf147..e7ad776fc 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -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 < v2;0: v1 == v2;1: 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 } // 添加加数据项 @@ -168,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() } diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index 3599e63ce..a2970477e 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -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 < v2;0: v1 == v2;1: 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, } } @@ -152,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() } diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index b5b546414..687b07256 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -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 < v2;0: v1 == v2;1: v1 > v2 } -func NewSortedStringArray(size int, cap int, safe...bool) *SortedStringArray { - a := &SortedStringArray { +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 } // 添加加数据项 @@ -161,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() } diff --git a/geg/other/test.go b/geg/other/test.go index 3e7a07cfb..4e1dd2b7d 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -9,11 +9,12 @@ type S struct { } + + func main() { var source = []string{"59705a2c1fd50736a4c768a1", "597a95ff1fd5073e48bb2272", "597a960f1fd5073e48bb2274"} - var CacheChannelKeys *garray.SortedStringArray - CacheChannelKeys = garray.NewSortedStringArray(0, 0) + var CacheChannelKeys = garray.NewSortedStringArray(3) CacheChannelKeys.Add(source...) - fmt.Println(CacheChannelKeys.Slice()) + fmt.Println(CacheChannelKeys.Len()) } diff --git a/geg/other/test2.go b/geg/other/test2.go deleted file mode 100644 index 8b6c1388b..000000000 --- a/geg/other/test2.go +++ /dev/null @@ -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() -} \ No newline at end of file diff --git a/geg/other/test_test.go b/geg/other/test_test.go new file mode 100644 index 000000000..8fb18c4b0 --- /dev/null +++ b/geg/other/test_test.go @@ -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) +} \ No newline at end of file