From 1447496efac4eabd3092953fd3cd02233320d90f Mon Sep 17 00:00:00 2001 From: John Date: Sat, 2 Feb 2019 14:22:32 +0800 Subject: [PATCH] comments update --- g/container/garray/garray_normal_int.go | 178 +++++++++++++++--- g/container/garray/garray_normal_interface.go | 156 +++++++++++++-- g/container/garray/garray_normal_string.go | 177 ++++++++++++++--- g/container/garray/garray_sorted_int.go | 169 ++++++++++++++--- g/container/garray/garray_sorted_interface.go | 172 ++++++++++++++--- g/container/garray/garray_sorted_string.go | 169 ++++++++++++++--- g/container/garray/garray_z_unit_int_test.go | 28 +++ .../garray/garray_z_unit_interface_test.go | 28 +++ .../garray/garray_z_unit_string_test.go | 28 +++ g/container/gmap/gmap.go | 69 ++++++- g/text/gstr/gstr.go | 4 +- g/text/gstr/gstr_levenshtein.go | 6 + g/text/gstr/gstr_parse.go | 6 + g/text/gstr/gstr_pos.go | 6 + g/text/gstr/gstr_similartext.go | 6 + g/text/gstr/gstr_soundex.go | 6 + g/text/gstr/gstr_trim.go | 4 +- geg/other/test.go | 38 ++-- geg/other/test2.go | 18 +- 19 files changed, 1079 insertions(+), 189 deletions(-) diff --git a/g/container/garray/garray_normal_int.go b/g/container/garray/garray_normal_int.go index 36c61f30f..22ac3a4a3 100644 --- a/g/container/garray/garray_normal_int.go +++ b/g/container/garray/garray_normal_int.go @@ -29,6 +29,11 @@ func NewIntArray(unsafe...bool) *IntArray { return NewIntArraySize(0, 0, unsafe...) } +// Create an array with given size and cap. +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 创建一个指定大小的数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewIntArraySize(size int, cap int, unsafe...bool) *IntArray { return &IntArray{ mu : rwmutex.New(unsafe...), @@ -36,6 +41,11 @@ func NewIntArraySize(size int, cap int, unsafe...bool) *IntArray { } } +// Create an array with given slice . +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 通过给定的slice变量创建数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewIntArrayFrom(array []int, unsafe...bool) *IntArray { return &IntArray{ mu : rwmutex.New(unsafe...), @@ -43,7 +53,9 @@ func NewIntArrayFrom(array []int, unsafe...bool) *IntArray { } } -// 获取指定索引的数据项, 调用方注意判断数组边界 +// Get value by index. +// +// 获取指定索引的数据项, 调用方注意判断数组边界。 func (a *IntArray) Get(index int) int { a.mu.RLock() defer a.mu.RUnlock() @@ -51,7 +63,9 @@ func (a *IntArray) Get(index int) int { return value } -// 设置指定索引的数据项, 调用方注意判断数组边界 +// Set value by index. +// +// 设置指定索引的数据项, 调用方注意判断数组边界。 func (a *IntArray) Set(index int, value int) *IntArray { a.mu.Lock() defer a.mu.Unlock() @@ -59,6 +73,8 @@ func (a *IntArray) Set(index int, value int) *IntArray { return a } +// Set the underlying slice array with the given param. +// // 设置底层数组变量. func (a *IntArray) SetArray(array []int) *IntArray { a.mu.Lock() @@ -67,6 +83,8 @@ func (a *IntArray) SetArray(array []int) *IntArray { return a } +// Replace the array items by given from the beginning of array. +// // 使用指定数组替换到对应的索引元素值. func (a *IntArray) Replace(array []int) *IntArray { a.mu.Lock() @@ -93,7 +111,11 @@ func (a *IntArray) Sum() (sum int) { return } -// 将数组重新排序. +// Sort the array in increasing order. +// The param controls whether sort +// in increasing order(default) or decreasing order +// +// 将数组排序(默认从低到高). func (a *IntArray) Sort(reverse...bool) *IntArray { a.mu.Lock() defer a.mu.Unlock() @@ -110,6 +132,8 @@ func (a *IntArray) Sort(reverse...bool) *IntArray { return a } +// Sort the array by custom function . +// // 使用自定义的排序函数将数组重新排序. func (a *IntArray) SortFunc(less func(v1, v2 int) bool) *IntArray { a.mu.Lock() @@ -120,7 +144,9 @@ func (a *IntArray) SortFunc(less func(v1, v2 int) bool) *IntArray { return a } -// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界 +// Insert the to the front of . +// +// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界。 func (a *IntArray) InsertBefore(index int, value int) *IntArray { a.mu.Lock() defer a.mu.Unlock() @@ -130,7 +156,9 @@ func (a *IntArray) InsertBefore(index int, value int) *IntArray { return a } -// 在当前索引位置后插入一个数据项, 调用方注意判断数组边界 +// Insert the to the back of . +// +// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界。 func (a *IntArray) InsertAfter(index int, value int) *IntArray { a.mu.Lock() defer a.mu.Unlock() @@ -140,7 +168,9 @@ func (a *IntArray) InsertAfter(index int, value int) *IntArray { return a } -// 删除指定索引的数据项, 调用方注意判断数组边界 +// Remove an item by index. +// +// 删除指定索引的数据项, 调用方注意判断数组边界。 func (a *IntArray) Remove(index int) int { a.mu.Lock() defer a.mu.Unlock() @@ -160,7 +190,9 @@ func (a *IntArray) Remove(index int) int { return value } -// 将数据项添加到数组的最左端(索引为0) +// Push new items to the beginning of array. +// +// 将数据项添加到数组的最左端(索引为0)。 func (a *IntArray) PushLeft(value...int) *IntArray { a.mu.Lock() a.array = append(value, a.array...) @@ -168,7 +200,9 @@ func (a *IntArray) PushLeft(value...int) *IntArray { return a } -// 将数据项添加到数组的最右端(索引为length - 1), 等于: Append +// Push new items to the end of array. +// +// 将数据项添加到数组的最右端(索引为length - 1), 等于: Append。 func (a *IntArray) PushRight(value...int) *IntArray { a.mu.Lock() a.array = append(a.array, value...) @@ -176,7 +210,9 @@ func (a *IntArray) PushRight(value...int) *IntArray { return a } -// 将最左端(索引为0)的数据项移出数组,并返回该数据项 +// Pop an item from the beginning of array. +// +// 将最左端(索引为0)的数据项移出数组,并返回该数据项。 func (a *IntArray) PopLeft() int { a.mu.Lock() defer a.mu.Unlock() @@ -185,7 +221,9 @@ func (a *IntArray) PopLeft() int { return value } -// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项 +// Pop an item from the end of array. +// +// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项。 func (a *IntArray) PopRight() int { a.mu.Lock() defer a.mu.Unlock() @@ -195,12 +233,76 @@ func (a *IntArray) PopRight() int { return value } -// 随机将一个数据项移出数组,并返回该数据项 +// Pop an random item from array. +// +// 随机将一个数据项移出数组,并返回该数据项。 func (a *IntArray) PopRand() int { return a.Remove(grand.Intn(len(a.array))) } -// 追加数据项 +// Pop items from the beginning of array. +// +// 将最左端(首部)的size个数据项移出数组,并返回该数据项。 +func (a *IntArray) PopLefts(size int) []int { + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0 : size] + a.array = a.array[size : ] + return value +} + +// Pop items from the end of array. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *IntArray) PopRights(size int) []int { + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index :] + a.array = a.array[ : index] + return value +} + +// Get items by range, returns array[start:end]. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *IntArray) Range(start, end int) []int { + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]int)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]int, end - start) + copy(array, a.array[start : end]) + } else { + array = a.array[start : end] + } + return array +} + +// See PushRight. +// +// 追加数据项, 等于: PushRight。 func (a *IntArray) Append(value...int) *IntArray { a.mu.Lock() a.array = append(a.array, value...) @@ -208,7 +310,9 @@ func (a *IntArray) Append(value...int) *IntArray { return a } -// 数组长度 +// Get the length of array. +// +// 数组长度。 func (a *IntArray) Len() int { a.mu.RLock() length := len(a.array) @@ -216,7 +320,11 @@ func (a *IntArray) Len() int { return length } -// 返回原始数据数组 +// Get the underlying data of array. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 返回原始数据数组. func (a *IntArray) Slice() []int { array := ([]int)(nil) if a.mu.IsSafe() { @@ -241,7 +349,9 @@ func (a *IntArray) Clone() (newArray *IntArray) { return NewIntArrayFrom(array, !a.mu.IsSafe()) } -// 清空数据数组 +// Clear array. +// +// 清空数据数组。 func (a *IntArray) Clear() *IntArray { a.mu.Lock() if len(a.array) > 0 { @@ -251,12 +361,17 @@ func (a *IntArray) Clear() *IntArray { return a } -// 查找指定数值是否存在 +// Check whether a value exists in the array. +// +// 查找指定数值是否存在。 func (a *IntArray) Contains(value int) bool { return a.Search(value) != -1 } -// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1 + +// Search array by , returns the index of , returns -1 if not exists. +// +// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1。 func (a *IntArray) Search(value int) int { if len(a.array) == 0 { return -1 @@ -274,7 +389,9 @@ func (a *IntArray) Search(value int) int { return result } -// 清理数组中重复的元素项 +// Unique the array, clear repeated values. +// +// 清理数组中重复的元素项。 func (a *IntArray) Unique() *IntArray { a.mu.Lock() for i := 0; i < len(a.array) - 1; i++ { @@ -288,7 +405,9 @@ func (a *IntArray) Unique() *IntArray { return a } -// 使用自定义方法执行加锁修改操作 +// Lock writing by callback function f. +// +// 使用自定义方法执行加锁修改操作。 func (a *IntArray) LockFunc(f func(array []int)) *IntArray { a.mu.Lock(true) defer a.mu.Unlock(true) @@ -296,7 +415,9 @@ func (a *IntArray) LockFunc(f func(array []int)) *IntArray { return a } -// 使用自定义方法执行加锁读取操作 +// Lock reading by callback function f. +// +// 使用自定义方法执行加锁读取操作。 func (a *IntArray) RLockFunc(f func(array []int)) *IntArray { a.mu.RLock(true) defer a.mu.RUnlock(true) @@ -304,6 +425,8 @@ func (a *IntArray) RLockFunc(f func(array []int)) *IntArray { return a } +// Merge two arrays. +// // 合并两个数组. func (a *IntArray) Merge(array *IntArray) *IntArray { a.mu.Lock() @@ -316,7 +439,8 @@ func (a *IntArray) Merge(array *IntArray) *IntArray { return a } -// Fills an array with num entries of the value of the value parameter, keys starting at the startIndex parameter. +// Fills an array with num entries of the value of the value parameter, +// keys starting at the startIndex parameter. // // 用value参数的值将数组填充num个条目,位置由startIndex参数指定的开始。 func (a *IntArray) Fill(startIndex int, num int, value int) *IntArray { @@ -335,7 +459,8 @@ func (a *IntArray) Fill(startIndex int, num int, value int) *IntArray { return a } -// Chunks an array into arrays with size elements. The last chunk may contain less than size elements. +// Chunks an array into arrays with size elements. +// The last chunk may contain less than size elements. // // 将一个数组分割成多个数组,其中每个数组的单元数目由size决定。最后一个数组的单元数目可能会少于size个。 func (a *IntArray) Chunk(size int) [][]int { @@ -389,8 +514,10 @@ func (a *IntArray) Pad(size int, value int) *IntArray { return a } -// Extract a slice of the array(If in concurrent safe usage, it returns a copy of the slice; else a pointer). -// It returns the sequence of elements from the array array as specified by the offset and length parameters. +// Extract a slice of the array(If in concurrent safe usage, +// it returns a copy of the slice; else a pointer). +// It returns the sequence of elements from the array array as specified +// by the offset and length parameters. // // 返回根据offset和size参数所指定的数组中的一段序列。 func (a *IntArray) SubSlice(offset, size int) []int { @@ -411,7 +538,8 @@ func (a *IntArray) SubSlice(offset, size int) []int { } } -// Picks one or more random entries out of an array(a copy), and returns the key (or keys) of the random entries. +// Picks one or more random entries out of an array(a copy), +// and returns the key (or keys) of the random entries. // // 从数组中随机取出size个元素项,构成slice返回。 func (a *IntArray) Rand(size int) []int { diff --git a/g/container/garray/garray_normal_interface.go b/g/container/garray/garray_normal_interface.go index e4ae2ea57..90c7f54e9 100644 --- a/g/container/garray/garray_normal_interface.go +++ b/g/container/garray/garray_normal_interface.go @@ -29,10 +29,18 @@ func New(unsafe...bool) *Array { return NewArraySize(0, 0, unsafe...) } +// See New. +// +// 同New方法。 func NewArray(unsafe...bool) *Array { return NewArraySize(0, 0, unsafe...) } +// Create an array with given size and cap. +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 创建一个指定大小的数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewArraySize(size int, cap int, unsafe...bool) *Array { return &Array{ mu : rwmutex.New(unsafe...), @@ -40,6 +48,11 @@ func NewArraySize(size int, cap int, unsafe...bool) *Array { } } +// Create an array with given slice . +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 通过给定的slice变量创建数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewArrayFrom(array []interface{}, unsafe...bool) *Array { return &Array{ mu : rwmutex.New(unsafe...), @@ -47,6 +60,8 @@ func NewArrayFrom(array []interface{}, unsafe...bool) *Array { } } +// Get value by index. +// // 获取指定索引的数据项, 调用方注意判断数组边界 func (a *Array) Get(index int) interface{} { a.mu.RLock() @@ -55,6 +70,8 @@ func (a *Array) Get(index int) interface{} { return value } +// Set value by index. +// // 设置指定索引的数据项, 调用方注意判断数组边界 func (a *Array) Set(index int, value interface{}) *Array { a.mu.Lock() @@ -63,6 +80,8 @@ func (a *Array) Set(index int, value interface{}) *Array { return a } +// Set the underlying slice array with the given param. +// // 设置底层数组变量. func (a *Array) SetArray(array []interface{}) *Array { a.mu.Lock() @@ -71,6 +90,8 @@ func (a *Array) SetArray(array []interface{}) *Array { return a } +// Replace the array items by given from the beginning of array. +// // 使用指定数组替换到对应的索引元素值. func (a *Array) Replace(array []interface{}) *Array { a.mu.Lock() @@ -97,6 +118,8 @@ func (a *Array) Sum() (sum int) { return } +// Sort the array by custom function . +// // 使用自定义的排序函数将数组重新排序. func (a *Array) SortFunc(less func(v1, v2 interface{}) bool) *Array { a.mu.Lock() @@ -107,7 +130,9 @@ func (a *Array) SortFunc(less func(v1, v2 interface{}) bool) *Array { return a } -// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界 +// Insert the to the front of . +// +// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界。 func (a *Array) InsertBefore(index int, value interface{}) *Array { a.mu.Lock() defer a.mu.Unlock() @@ -117,7 +142,9 @@ func (a *Array) InsertBefore(index int, value interface{}) *Array { return a } -// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界 +// Insert the to the back of . +// +// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界。 func (a *Array) InsertAfter(index int, value interface{}) *Array { a.mu.Lock() defer a.mu.Unlock() @@ -127,7 +154,9 @@ func (a *Array) InsertAfter(index int, value interface{}) *Array { return a } -// 删除指定索引的数据项, 调用方注意判断数组边界 +// Remove an item by index. +// +// 删除指定索引的数据项, 调用方注意判断数组边界。 func (a *Array) Remove(index int) interface{} { a.mu.Lock() defer a.mu.Unlock() @@ -147,7 +176,9 @@ func (a *Array) Remove(index int) interface{} { return value } -// 将数据项添加到数组的最左端(索引为0) +// Push new items to the beginning of array. +// +// 将数据项添加到数组的最左端(索引为0)。 func (a *Array) PushLeft(value...interface{}) *Array { a.mu.Lock() a.array = append(value, a.array...) @@ -155,7 +186,9 @@ func (a *Array) PushLeft(value...interface{}) *Array { return a } -// 将数据项添加到数组的最右端(索引为length - 1), 等于: Append +// Push new items to the end of array. +// +// 将数据项添加到数组的最右端(索引为length - 1), 等于: Append。 func (a *Array) PushRight(value...interface{}) *Array { a.mu.Lock() a.array = append(a.array, value...) @@ -163,7 +196,16 @@ func (a *Array) PushRight(value...interface{}) *Array { return a } -// 将最左端(索引为0)的数据项移出数组,并返回该数据项 +// Pop an random item from array. +// +// 随机将一个数据项移出数组,并返回该数据项。 +func (a *Array) PopRand() interface{} { + return a.Remove(grand.Intn(len(a.array))) +} + +// Pop an item from the beginning of array. +// +// 将最左端(索引为0)的数据项移出数组,并返回该数据项。 func (a *Array) PopLeft() interface{} { a.mu.Lock() defer a.mu.Unlock() @@ -172,7 +214,9 @@ func (a *Array) PopLeft() interface{} { return value } -// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项 +// Pop an item from the end of array. +// +// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项。 func (a *Array) PopRight() interface{} { a.mu.Lock() defer a.mu.Unlock() @@ -182,18 +226,77 @@ func (a *Array) PopRight() interface{} { return value } -// 随机将一个数据项移出数组,并返回该数据项 -func (a *Array) PopRand() interface{} { - return a.Remove(grand.Intn(len(a.array))) +// Pop items from the beginning of array. +// +// 将最左端(首部)的size个数据项移出数组,并返回该数据项 +func (a *Array) PopLefts(size int) []interface{} { + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0 : size] + a.array = a.array[size : ] + return value } -// 追加数据项, 等于: PushRight +// Pop items from the end of array. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *Array) PopRights(size int) []interface{} { + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index :] + a.array = a.array[ : index] + return value +} + +// Get items by range, returns array[start:end]. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *Array) Range(start, end int) []interface{} { + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]interface{})(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]interface{}, end - start) + copy(array, a.array[start : end]) + } else { + array = a.array[start : end] + } + return array +} + +// See PushRight. +// +// 追加数据项, 等于: PushRight。 func (a *Array) Append(value...interface{}) *Array { a.PushRight(value...) return a } -// 数组长度 +// Get the length of array. +// +// 数组长度。 func (a *Array) Len() int { a.mu.RLock() length := len(a.array) @@ -201,7 +304,11 @@ func (a *Array) Len() int { return length } -// 返回原始数据数组 +// Get the underlying data of array. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 返回原始数据数组. func (a *Array) Slice() []interface{} { array := ([]interface{})(nil) if a.mu.IsSafe() { @@ -226,6 +333,8 @@ func (a *Array) Clone() (newArray *Array) { return NewArrayFrom(array, !a.mu.IsSafe()) } +// Clear array. +// // 清空数据数组 func (a *Array) Clear() *Array { a.mu.Lock() @@ -236,11 +345,15 @@ func (a *Array) Clear() *Array { return a } +// Check whether a value exists in the array. +// // 查找指定数值是否存在 func (a *Array) Contains(value interface{}) bool { return a.Search(value) != -1 } +// Search array by , returns the index of , returns -1 if not exists. +// // 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1 func (a *Array) Search(value interface{}) int { if len(a.array) == 0 { @@ -259,6 +372,8 @@ func (a *Array) Search(value interface{}) int { return result } +// Unique the array, clear repeated values. +// // 清理数组中重复的元素项 func (a *Array) Unique() *Array { a.mu.Lock() @@ -273,6 +388,8 @@ func (a *Array) Unique() *Array { return a } +// Lock writing by callback function f. +// // 使用自定义方法执行加锁修改操作 func (a *Array) LockFunc(f func(array []interface{})) *Array { a.mu.Lock(true) @@ -281,6 +398,8 @@ func (a *Array) LockFunc(f func(array []interface{})) *Array { return a } +// Lock reading by callback function f. +// // 使用自定义方法执行加锁读取操作 func (a *Array) RLockFunc(f func(array []interface{})) *Array { a.mu.RLock(true) @@ -289,6 +408,8 @@ func (a *Array) RLockFunc(f func(array []interface{})) *Array { return a } +// Merge two arrays. +// // 合并两个数组. func (a *Array) Merge(array *Array) *Array { a.mu.Lock() @@ -301,7 +422,8 @@ func (a *Array) Merge(array *Array) *Array { return a } -// Fills an array with num entries of the value of the value parameter, keys starting at the start_index parameter. +// Fills an array with num entries of the value of the value parameter, +// keys starting at the start_index parameter. // // 用value参数的值将数组填充num个条目,位置由startIndex参数指定的开始。 func (a *Array) Fill(startIndex int, num int, value interface{}) *Array { @@ -320,7 +442,8 @@ func (a *Array) Fill(startIndex int, num int, value interface{}) *Array { return a } -// Chunks an array into arrays with size elements. The last chunk may contain less than size elements. +// Chunks an array into arrays with size elements. +// The last chunk may contain less than size elements. // // 将一个数组分割成多个数组,其中每个数组的单元数目由size决定。最后一个数组的单元数目可能会少于size个。 func (a *Array) Chunk(size int) [][]interface{} { @@ -397,7 +520,8 @@ func (a *Array) SubSlice(offset, size int) []interface{} { } } -// Picks one or more random entries out of an array(a copy), and returns the key (or keys) of the random entries. +// Picks one or more random entries out of an array(a copy), +// and returns the key (or keys) of the random entries. // // 从数组中随机取出size个元素项,构成slice返回。 func (a *Array) Rand(size int) []interface{} { diff --git a/g/container/garray/garray_normal_string.go b/g/container/garray/garray_normal_string.go index a39a92291..f41d849d0 100644 --- a/g/container/garray/garray_normal_string.go +++ b/g/container/garray/garray_normal_string.go @@ -29,6 +29,11 @@ func NewStringArray(unsafe...bool) *StringArray { return NewStringArraySize(0, 0, unsafe...) } +// Create an array with given size and cap. +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 创建一个指定大小的数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewStringArraySize(size int, cap int, unsafe...bool) *StringArray { return &StringArray{ mu : rwmutex.New(unsafe...), @@ -36,6 +41,11 @@ func NewStringArraySize(size int, cap int, unsafe...bool) *StringArray { } } +// Create an array with given slice . +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 通过给定的slice变量创建数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewStringArrayFrom(array []string, unsafe...bool) *StringArray { return &StringArray { mu : rwmutex.New(unsafe...), @@ -43,7 +53,9 @@ func NewStringArrayFrom(array []string, unsafe...bool) *StringArray { } } -// 获取指定索引的数据项, 调用方注意判断数组边界 +// Get value by index. +// +// 获取指定索引的数据项, 调用方注意判断数组边界。 func (a *StringArray) Get(index int) string { a.mu.RLock() defer a.mu.RUnlock() @@ -51,7 +63,9 @@ func (a *StringArray) Get(index int) string { return value } -// 设置指定索引的数据项, 调用方注意判断数组边界 +// Set value by index. +// +// 设置指定索引的数据项, 调用方注意判断数组边界。 func (a *StringArray) Set(index int, value string) *StringArray { a.mu.Lock() defer a.mu.Unlock() @@ -59,6 +73,8 @@ func (a *StringArray) Set(index int, value string) *StringArray { return a } +// Set the underlying slice array with the given param. +// // 设置底层数组变量. func (a *StringArray) SetArray(array []string) *StringArray { a.mu.Lock() @@ -67,6 +83,8 @@ func (a *StringArray) SetArray(array []string) *StringArray { return a } +// Replace the array items by given from the beginning of array. +// // 使用指定数组替换到对应的索引元素值. func (a *StringArray) Replace(array []string) *StringArray { a.mu.Lock() @@ -93,7 +111,11 @@ func (a *StringArray) Sum() (sum int) { return } -// 将数组重新排序(从小到大). +// Sort the array in increasing order. +// The param controls whether sort +// in increasing order(default) or decreasing order +// +// 将数组排序(默认从低到高). func (a *StringArray) Sort(reverse...bool) *StringArray { a.mu.Lock() defer a.mu.Unlock() @@ -110,6 +132,8 @@ func (a *StringArray) Sort(reverse...bool) *StringArray { return a } +// Sort the array by custom function . +// // 使用自定义的排序函数将数组重新排序. func (a *StringArray) SortFunc(less func(v1, v2 string) bool) *StringArray { a.mu.Lock() @@ -120,7 +144,9 @@ func (a *StringArray) SortFunc(less func(v1, v2 string) bool) *StringArray { return a } -// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界 +// Insert the to the front of . +// +// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界。 func (a *StringArray) InsertBefore(index int, value string) *StringArray { a.mu.Lock() defer a.mu.Unlock() @@ -130,7 +156,9 @@ func (a *StringArray) InsertBefore(index int, value string) *StringArray { return a } -// 在当前索引位置后插入一个数据项, 调用方注意判断数组边界 +// Insert the to the back of . +// +// 在当前索引位置前插入一个数据项, 调用方注意判断数组边界。 func (a *StringArray) InsertAfter(index int, value string) *StringArray { a.mu.Lock() defer a.mu.Unlock() @@ -140,7 +168,9 @@ func (a *StringArray) InsertAfter(index int, value string) *StringArray { return a } -// 删除指定索引的数据项, 调用方注意判断数组边界 +// Remove an item by index. +// +// 删除指定索引的数据项, 调用方注意判断数组边界。 func (a *StringArray) Remove(index int) string { a.mu.Lock() defer a.mu.Unlock() @@ -160,7 +190,9 @@ func (a *StringArray) Remove(index int) string { return value } -// 将数据项添加到数组的最左端(索引为0) +// Push new items to the beginning of array. +// +// 将数据项添加到数组的最左端(索引为0)。 func (a *StringArray) PushLeft(value...string) *StringArray { a.mu.Lock() a.array = append(value, a.array...) @@ -168,7 +200,9 @@ func (a *StringArray) PushLeft(value...string) *StringArray { return a } -// 将数据项添加到数组的最右端(索引为length - 1), 等于: Append +// Push new items to the end of array. +// +// 将数据项添加到数组的最右端(索引为length - 1), 等于: Append。 func (a *StringArray) PushRight(value...string) *StringArray { a.mu.Lock() a.array = append(a.array, value...) @@ -176,7 +210,9 @@ func (a *StringArray) PushRight(value...string) *StringArray { return a } -// 将最左端(索引为0)的数据项移出数组,并返回该数据项 +// Pop an item from the beginning of array. +// +// 将最左端(索引为0)的数据项移出数组,并返回该数据项。 func (a *StringArray) PopLeft() string { a.mu.Lock() defer a.mu.Unlock() @@ -185,7 +221,9 @@ func (a *StringArray) PopLeft() string { return value } -// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项 +// Pop an item from the end of array. +// +// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项。 func (a *StringArray) PopRight() string { a.mu.Lock() defer a.mu.Unlock() @@ -195,12 +233,76 @@ func (a *StringArray) PopRight() string { return value } -// 随机将一个数据项移出数组,并返回该数据项 +// Pop an random item from array. +// +// 随机将一个数据项移出数组,并返回该数据项。 func (a *StringArray) PopRand() string { return a.Remove(grand.Intn(len(a.array))) } -// 追加数据项 +// Pop items from the beginning of array. +// +// 将最左端(首部)的size个数据项移出数组,并返回该数据项 +func (a *StringArray) PopLefts(size int) []string { + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0 : size] + a.array = a.array[size : ] + return value +} + +// Pop items from the end of array. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *StringArray) PopRights(size int) []string { + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index :] + a.array = a.array[ : index] + return value +} + +// Get items by range, returns array[start:end]. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *StringArray) Range(start, end int) []string { + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]string)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]string, end - start) + copy(array, a.array[start : end]) + } else { + array = a.array[start : end] + } + return array +} + +// See PushRight. +// +// 追加数据项, 等于: PushRight。 func (a *StringArray) Append(value...string) *StringArray { a.mu.Lock() a.array = append(a.array, value...) @@ -208,7 +310,9 @@ func (a *StringArray) Append(value...string) *StringArray { return a } -// 数组长度 +// Get the length of array. +// +// 数组长度。 func (a *StringArray) Len() int { a.mu.RLock() length := len(a.array) @@ -216,7 +320,11 @@ func (a *StringArray) Len() int { return length } -// 返回原始数据数组 +// Get the underlying data of array. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 返回原始数据数组. func (a *StringArray) Slice() []string { array := ([]string)(nil) if a.mu.IsSafe() { @@ -241,7 +349,9 @@ func (a *StringArray) Clone() (newArray *StringArray) { return NewStringArrayFrom(array, !a.mu.IsSafe()) } -// 清空数据数组 +// Clear array. +// +// 清空数据数组。 func (a *StringArray) Clear() *StringArray { a.mu.Lock() if len(a.array) > 0 { @@ -251,12 +361,16 @@ func (a *StringArray) Clear() *StringArray { return a } -// 查找指定数值是否存在 +// Check whether a value exists in the array. +// +// 查找指定数值是否存在。 func (a *StringArray) Contains(value string) bool { return a.Search(value) != -1 } -// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1 +// Search array by , returns the index of , returns -1 if not exists. +// +// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1。 func (a *StringArray) Search(value string) int { if len(a.array) == 0 { return -1 @@ -273,7 +387,9 @@ func (a *StringArray) Search(value string) int { return result } -// 清理数组中重复的元素项 +// Unique the array, clear repeated values. +// +// 清理数组中重复的元素项。 func (a *StringArray) Unique() *StringArray { a.mu.Lock() for i := 0; i < len(a.array) - 1; i++ { @@ -287,7 +403,9 @@ func (a *StringArray) Unique() *StringArray { return a } -// 使用自定义方法执行加锁修改操作 +// Lock writing by callback function f. +// +// 使用自定义方法执行加锁修改操作。 func (a *StringArray) LockFunc(f func(array []string)) *StringArray { a.mu.Lock(true) defer a.mu.Unlock(true) @@ -295,7 +413,9 @@ func (a *StringArray) LockFunc(f func(array []string)) *StringArray { return a } -// 使用自定义方法执行加锁读取操作 +// Lock reading by callback function f. +// +// 使用自定义方法执行加锁读取操作。 func (a *StringArray) RLockFunc(f func(array []string)) *StringArray { a.mu.RLock(true) defer a.mu.RUnlock(true) @@ -303,6 +423,8 @@ func (a *StringArray) RLockFunc(f func(array []string)) *StringArray { return a } +// Merge two arrays. +// // 合并两个数组. func (a *StringArray) Merge(array *StringArray) *StringArray { a.mu.Lock() @@ -315,7 +437,8 @@ func (a *StringArray) Merge(array *StringArray) *StringArray { return a } -// Fills an array with num entries of the value of the value parameter, keys starting at the start_index parameter. +// Fills an array with num entries of the value of the value parameter, +// keys starting at the start_index parameter. // // 用value参数的值将数组填充num个条目,位置由startIndex参数指定的开始。 func (a *StringArray) Fill(startIndex int, num int, value string) *StringArray { @@ -334,7 +457,8 @@ func (a *StringArray) Fill(startIndex int, num int, value string) *StringArray { return a } -// Chunks an array into arrays with size elements. The last chunk may contain less than size elements. +// Chunks an array into arrays with size elements. +// The last chunk may contain less than size elements. // // 将一个数组分割成多个数组,其中每个数组的单元数目由size决定。最后一个数组的单元数目可能会少于size个。 func (a *StringArray) Chunk(size int) [][]string { @@ -389,8 +513,10 @@ func (a *StringArray) Pad(size int, value string) *StringArray { return a } -// Extract a slice of the array(If in concurrent safe usage, it returns a copy of the slice; else a pointer). -// It returns the sequence of elements from the array array as specified by the offset and length parameters. +// Extract a slice of the array(If in concurrent safe usage, +// it returns a copy of the slice; else a pointer). +// It returns the sequence of elements from the array array as specified +// by the offset and length parameters. // // 返回根据offset和size参数所指定的数组中的一段序列。 func (a *StringArray) SubSlice(offset, size int) []string { @@ -411,7 +537,8 @@ func (a *StringArray) SubSlice(offset, size int) []string { } } -// Picks one or more random entries out of an array(a copy), and returns the key (or keys) of the random entries. +// Picks one or more random entries out of an array(a copy), +// and returns the key (or keys) of the random entries. // // 从数组中随机取出size个元素项,构成slice返回。 func (a *StringArray) Rand(size int) []string { diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index e65d8e0ea..c7c7f258f 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -33,6 +33,11 @@ func NewSortedIntArray(unsafe...bool) *SortedIntArray { return NewSortedIntArraySize(0, unsafe...) } +// Create a sorted array with given size and cap. +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 创建一个指定大小的排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewSortedIntArraySize(cap int, unsafe...bool) *SortedIntArray { return &SortedIntArray { mu : rwmutex.New(unsafe...), @@ -50,6 +55,11 @@ func NewSortedIntArraySize(cap int, unsafe...bool) *SortedIntArray { } } +// Create an array with given slice . +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 通过给定的slice变量创建排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewSortedIntArrayFrom(array []int, unsafe...bool) *SortedIntArray { a := NewSortedIntArraySize(0, unsafe...) a.array = array @@ -57,6 +67,8 @@ func NewSortedIntArrayFrom(array []int, unsafe...bool) *SortedIntArray { return a } +// Set the underlying slice array with the given param. +// // 设置底层数组变量. func (a *SortedIntArray) SetArray(array []int) *SortedIntArray { a.mu.Lock() @@ -66,7 +78,9 @@ func (a *SortedIntArray) SetArray(array []int) *SortedIntArray { return a } -// 将数组重新排序(从小到大). +// Sort the array in increasing order. +// +// 将数组排序(默认从低到高). func (a *SortedIntArray) Sort() *SortedIntArray { a.mu.Lock() defer a.mu.Unlock() @@ -74,7 +88,9 @@ func (a *SortedIntArray) Sort() *SortedIntArray { return a } -// 添加加数据项 +// And values to sorted array, the array always keeps sorted. +// +// 添加数据项. func (a *SortedIntArray) Add(values...int) *SortedIntArray { if len(values) == 0 { return a @@ -101,7 +117,9 @@ func (a *SortedIntArray) Add(values...int) *SortedIntArray { return a } -// 获取指定索引的数据项, 调用方注意判断数组边界 +// Get value by index. +// +// 获取指定索引的数据项, 调用方注意判断数组边界。 func (a *SortedIntArray) Get(index int) int { a.mu.RLock() defer a.mu.RUnlock() @@ -109,7 +127,9 @@ func (a *SortedIntArray) Get(index int) int { return value } -// 删除指定索引的数据项, 调用方注意判断数组边界 +// Remove an item by index. +// +// 删除指定索引的数据项, 调用方注意判断数组边界。 func (a *SortedIntArray) Remove(index int) int { a.mu.Lock() defer a.mu.Unlock() @@ -129,7 +149,9 @@ func (a *SortedIntArray) Remove(index int) int { return value } -// 将最左端(索引为0)的数据项移出数组,并返回该数据项 +// Push new items to the beginning of array. +// +// 将数据项添加到数组的最左端(索引为0)。 func (a *SortedIntArray) PopLeft() int { a.mu.Lock() defer a.mu.Unlock() @@ -138,7 +160,9 @@ func (a *SortedIntArray) PopLeft() int { return value } -// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项 +// Push new items to the end of array. +// +// 将数据项添加到数组的最右端(索引为length - 1)。 func (a *SortedIntArray) PopRight() int { a.mu.Lock() defer a.mu.Unlock() @@ -148,7 +172,76 @@ func (a *SortedIntArray) PopRight() int { return value } -// 数组长度 +// Pop an random item from array. +// +// 随机将一个数据项移出数组,并返回该数据项。 +func (a *SortedIntArray) PopRand() int { + return a.Remove(grand.Intn(len(a.array))) +} + +// Pop items from the beginning of array. +// +// 将最左端(首部)的size个数据项移出数组,并返回该数据项 +func (a *SortedIntArray) PopLefts(size int) []int { + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0 : size] + a.array = a.array[size : ] + return value +} + +// Pop items from the end of array. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *SortedIntArray) PopRights(size int) []int { + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index :] + a.array = a.array[ : index] + return value +} + +// Get items by range, returns array[start:end]. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *SortedIntArray) Range(start, end int) []int { + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]int)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]int, end - start) + copy(array, a.array[start : end]) + } else { + array = a.array[start : end] + } + return array +} + +// Get the length of array. +// +// 数组长度。 func (a *SortedIntArray) Len() int { a.mu.RLock() length := len(a.array) @@ -168,7 +261,11 @@ func (a *SortedIntArray) Sum() (sum int) { return } -// 返回原始数据数组 +// Get the underlying data of array. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 返回原始数据数组. func (a *SortedIntArray) Slice() []int { array := ([]int)(nil) if a.mu.IsSafe() { @@ -182,18 +279,24 @@ func (a *SortedIntArray) Slice() []int { return array } -// 查找指定数值是否存在 +// Check whether a value exists in the array. +// +// 查找指定数值是否存在。 func (a *SortedIntArray) Contains(value int) bool { - _, r := a.Search(value) - return r == 0 + return a.Search(value) == 0 } -// 查找指定数值的索引位置,返回索引位置(具体匹配位置或者最后对比位置)及查找结果 -// 返回值: 最后比较位置, 比较结果 -func (a *SortedIntArray) Search(value int) (index int, result int) { - return a.binSearch(value, true) +// Search array by , returns the index of , returns -1 if not exists. +// +// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1。 +func (a *SortedIntArray) Search(value int) (index int) { + index, _ = a.binSearch(value, true) + return } +// Binary search. +// +// 二分查找. func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) { if len(a.array) == 0 { return -1, -2 @@ -219,7 +322,11 @@ func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) return mid, cmp } -// 设置是否允许数组唯一 +// Set unique mark to the array, +// which means it does not contain any repeated items. +// It also do unique check, remove all repeated items. +// +// 设置是否允许数组唯一. func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray { oldUnique := a.unique.Val() a.unique.Set(unique) @@ -229,7 +336,9 @@ func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray { return a } -// 清理数组中重复的元素项 +// Do unique check, remove all repeated items. +// +// 清理数组中重复的元素项. func (a *SortedIntArray) Unique() *SortedIntArray { a.mu.Lock() i := 0 @@ -258,7 +367,9 @@ func (a *SortedIntArray) Clone() (newArray *SortedIntArray) { return NewSortedIntArrayFrom(array, !a.mu.IsSafe()) } -// 清空数据数组 +// Clear array. +// +// 清空数据数组。 func (a *SortedIntArray) Clear() *SortedIntArray { a.mu.Lock() if len(a.array) > 0 { @@ -268,7 +379,9 @@ func (a *SortedIntArray) Clear() *SortedIntArray { return a } -// 使用自定义方法执行加锁修改操作 +// Lock writing by callback function f. +// +// 使用自定义方法执行加锁修改操作。 func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray { a.mu.Lock(true) defer a.mu.Unlock(true) @@ -276,7 +389,9 @@ func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray { return a } -// 使用自定义方法执行加锁读取操作 +// Lock reading by callback function f. +// +// 使用自定义方法执行加锁读取操作。 func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray { a.mu.RLock(true) defer a.mu.RUnlock(true) @@ -284,6 +399,8 @@ func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray { return a } +// Merge two arrays. +// // 合并两个数组. func (a *SortedIntArray) Merge(array *SortedIntArray) *SortedIntArray { a.mu.Lock() @@ -297,7 +414,8 @@ func (a *SortedIntArray) Merge(array *SortedIntArray) *SortedIntArray { return a } -// Chunks an array into arrays with size elements. The last chunk may contain less than size elements. +// Chunks an array into arrays with size elements. +// The last chunk may contain less than size elements. // // 将一个数组分割成多个数组,其中每个数组的单元数目由size决定。最后一个数组的单元数目可能会少于size个。 func (a *SortedIntArray) Chunk(size int) [][]int { @@ -320,8 +438,10 @@ func (a *SortedIntArray) Chunk(size int) [][]int { return n } -// Extract a slice of the array(If in concurrent safe usage, it returns a copy of the slice; else a pointer). -// It returns the sequence of elements from the array array as specified by the offset and length parameters. +// Extract a slice of the array(If in concurrent safe usage, +// it returns a copy of the slice; else a pointer). +// It returns the sequence of elements from the array array as specified +// by the offset and length parameters. // // 返回根据offset和size参数所指定的数组中的一段序列。 func (a *SortedIntArray) SubSlice(offset, size int) []int { @@ -342,7 +462,8 @@ func (a *SortedIntArray) SubSlice(offset, size int) []int { } } -// Picks one or more random entries out of an array(a copy), and returns the key (or keys) of the random entries. +// Picks one or more random entries out of an array(a copy), +// and returns the key (or keys) of the random entries. // // 从数组中随机取出size个元素项,构成slice返回。 func (a *SortedIntArray) Rand(size int) []int { diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index 31417422e..ad6de0b15 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -41,6 +41,11 @@ func NewSortedArray(compareFunc func(v1, v2 interface{}) int, unsafe...bool) *So return NewSortedArraySize(0, compareFunc, unsafe...) } +// Create a sorted array with given size and cap. +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 创建一个指定大小的排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewSortedArraySize(cap int, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray { return &SortedArray{ mu : rwmutex.New(unsafe...), @@ -50,6 +55,11 @@ func NewSortedArraySize(cap int, compareFunc func(v1, v2 interface{}) int, unsaf } } +// Create an array with given slice . +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 通过给定的slice变量创建排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewSortedArrayFrom(array []interface{}, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray { a := NewSortedArraySize(0, compareFunc, unsafe...) a.array = array @@ -59,6 +69,8 @@ func NewSortedArrayFrom(array []interface{}, compareFunc func(v1, v2 interface{} return a } +// Set the underlying slice array with the given param. +// // 设置底层数组变量. func (a *SortedArray) SetArray(array []interface{}) *SortedArray { a.mu.Lock() @@ -70,7 +82,9 @@ func (a *SortedArray) SetArray(array []interface{}) *SortedArray { return a } -// 将数组重新排序(从小到大). +// Sort the array by comparing function. +// +// 将数组按照比较方法进行排序. func (a *SortedArray) Sort() *SortedArray { a.mu.Lock() defer a.mu.Unlock() @@ -80,7 +94,9 @@ func (a *SortedArray) Sort() *SortedArray { return a } -// 添加加数据项 +// And values to sorted array, the array always keeps sorted. +// +// 添加数据项. func (a *SortedArray) Add(values...interface{}) *SortedArray { if len(values) == 0 { return a @@ -107,7 +123,9 @@ func (a *SortedArray) Add(values...interface{}) *SortedArray { return a } -// 获取指定索引的数据项, 调用方注意判断数组边界 +// Get value by index. +// +// 获取指定索引的数据项, 调用方注意判断数组边界。 func (a *SortedArray) Get(index int) interface{} { a.mu.RLock() defer a.mu.RUnlock() @@ -115,7 +133,9 @@ func (a *SortedArray) Get(index int) interface{} { return value } -// 删除指定索引的数据项, 调用方注意判断数组边界 +// Remove an item by index. +// +// 删除指定索引的数据项, 调用方注意判断数组边界。 func (a *SortedArray) Remove(index int) interface{} { a.mu.Lock() defer a.mu.Unlock() @@ -135,7 +155,9 @@ func (a *SortedArray) Remove(index int) interface{} { return value } -// 将最左端(索引为0)的数据项移出数组,并返回该数据项 +// Push new items to the beginning of array. +// +// 将数据项添加到数组的最左端(索引为0)。 func (a *SortedArray) PopLeft() interface{} { a.mu.Lock() defer a.mu.Unlock() @@ -144,7 +166,9 @@ func (a *SortedArray) PopLeft() interface{} { return value } -// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项 +// Push new items to the end of array. +// +// 将数据项添加到数组的最右端(索引为length - 1)。 func (a *SortedArray) PopRight() interface{} { a.mu.Lock() defer a.mu.Unlock() @@ -154,6 +178,73 @@ func (a *SortedArray) PopRight() interface{} { return value } +// Pop an random item from array. +// +// 随机将一个数据项移出数组,并返回该数据项。 +func (a *SortedArray) PopRand() interface{} { + return a.Remove(grand.Intn(len(a.array))) +} + +// Pop items from the beginning of array. +// +// 将最左端(首部)的size个数据项移出数组,并返回该数据项 +func (a *SortedArray) PopLefts(size int) []interface{} { + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0 : size] + a.array = a.array[size : ] + return value +} + +// Pop items from the end of array. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *SortedArray) PopRights(size int) []interface{} { + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index :] + a.array = a.array[ : index] + return value +} + +// Get items by range, returns array[start:end]. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *SortedArray) Range(start, end int) []interface{} { + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]interface{})(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]interface{}, end - start) + copy(array, a.array[start : end]) + } else { + array = a.array[start : end] + } + return array +} + // Calculate the sum of values in an array. // // 对数组中的元素项求和(将元素值转换为int类型后叠加)。 @@ -166,7 +257,9 @@ func (a *SortedArray) Sum() (sum int) { return } -// 数组长度 +// Get the length of array. +// +// 数组长度。 func (a *SortedArray) Len() int { a.mu.RLock() length := len(a.array) @@ -174,7 +267,11 @@ func (a *SortedArray) Len() int { return length } -// 返回原始数据数组 +// Get the underlying data of array. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 返回原始数据数组. func (a *SortedArray) Slice() []interface{} { array := ([]interface{})(nil) if a.mu.IsSafe() { @@ -188,20 +285,25 @@ func (a *SortedArray) Slice() []interface{} { return array } -// 查找指定数值是否存在 +// Check whether a value exists in the array. +// +// 查找指定数值是否存在。 func (a *SortedArray) Contains(value interface{}) bool { - _, r := a.Search(value) - return r == 0 + return a.Search(value) == 0 } -// 查找指定数值的索引位置,返回索引位置(具体匹配位置或者最后对比位置)及查找结果 -// 返回值: 最后比较位置, 比较结果 -func (a *SortedArray) Search(value interface{}) (index int, result int) { - return a.binSearch(value, true) +// Search array by , returns the index of , returns -1 if not exists. +// +// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1。 +func (a *SortedArray) Search(value interface{}) (index int) { + index, _ = a.binSearch(value, true) + return } -// 查找指定数值的索引位置,返回索引位置(具体匹配位置或者最后对比位置)及查找结果 -// 返回值: 最后比较位置, 比较结果 +// Binary search. +// +// 二分查找。查找指定数值的索引位置,返回索引位置(具体匹配位置或者最后对比位置)及查找结果 +// 返回值: 最后比较位置, 比较结果。 func (a *SortedArray) binSearch(value interface{}, lock bool)(index int, result int) { if len(a.array) == 0 { return -1, -2 @@ -227,7 +329,11 @@ func (a *SortedArray) binSearch(value interface{}, lock bool)(index int, result return mid, cmp } -// 设置是否允许数组唯一 +// Set unique mark to the array, +// which means it does not contain any repeated items. +// It also do unique check, remove all repeated items. +// +// 设置是否允许数组唯一. func (a *SortedArray) SetUnique(unique bool) *SortedArray { oldUnique := a.unique.Val() a.unique.Set(unique) @@ -237,7 +343,9 @@ func (a *SortedArray) SetUnique(unique bool) *SortedArray { return a } -// 清理数组中重复的元素项 +// Do unique check, remove all repeated items. +// +// 清理数组中重复的元素项. func (a *SortedArray) Unique() *SortedArray { a.mu.Lock() defer a.mu.Unlock() @@ -266,7 +374,9 @@ func (a *SortedArray) Clone() (newArray *SortedArray) { return NewSortedArrayFrom(array, a.compareFunc, !a.mu.IsSafe()) } -// 清空数据数组 +// Clear array. +// +// 清空数据数组。 func (a *SortedArray) Clear() *SortedArray { a.mu.Lock() if len(a.array) > 0 { @@ -276,7 +386,9 @@ func (a *SortedArray) Clear() *SortedArray { return a } -// 使用自定义方法执行加锁修改操作 +// Lock writing by callback function f. +// +// 使用自定义方法执行加锁修改操作。 func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray { a.mu.Lock(true) defer a.mu.Unlock(true) @@ -284,7 +396,9 @@ func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray { return a } -// 使用自定义方法执行加锁读取操作 +// Lock reading by callback function f. +// +// 使用自定义方法执行加锁读取操作。 func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray { a.mu.RLock(true) defer a.mu.RUnlock(true) @@ -292,6 +406,8 @@ func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray { return a } +// Merge two arrays. +// // 合并两个数组. func (a *SortedArray) Merge(array *SortedArray) *SortedArray { a.mu.Lock() @@ -307,7 +423,8 @@ func (a *SortedArray) Merge(array *SortedArray) *SortedArray { return a } -// Chunks an array into arrays with size elements. The last chunk may contain less than size elements. +// Chunks an array into arrays with size elements. +// The last chunk may contain less than size elements. // // 将一个数组分割成多个数组,其中每个数组的单元数目由size决定。最后一个数组的单元数目可能会少于size个。 func (a *SortedArray) Chunk(size int) [][]interface{} { @@ -330,8 +447,10 @@ func (a *SortedArray) Chunk(size int) [][]interface{} { return n } -// Extract a slice of the array(If in concurrent safe usage, it returns a copy of the slice; else a pointer). -// It returns the sequence of elements from the array array as specified by the offset and length parameters. +// Extract a slice of the array(If in concurrent safe usage, +// it returns a copy of the slice; else a pointer). +// It returns the sequence of elements from the array array as specified +// by the offset and length parameters. // // 返回根据offset和size参数所指定的数组中的一段序列。 func (a *SortedArray) SubSlice(offset, size int) []interface{} { @@ -352,7 +471,8 @@ func (a *SortedArray) SubSlice(offset, size int) []interface{} { } } -// Picks one or more random entries out of an array(a copy), and returns the key (or keys) of the random entries. +// Picks one or more random entries out of an array(a copy), +// and returns the key (or keys) of the random entries. // // 从数组中随机取出size个元素项,构成slice返回。 func (a *SortedArray) Rand(size int) []interface{} { diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index 4b72132a9..d7b689c19 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -33,6 +33,11 @@ func NewSortedStringArray(unsafe...bool) *SortedStringArray { return NewSortedStringArraySize(0, unsafe...) } +// Create a sorted array with given size and cap. +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 创建一个指定大小的排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewSortedStringArraySize(cap int, unsafe...bool) *SortedStringArray { return &SortedStringArray { mu : rwmutex.New(unsafe...), @@ -44,6 +49,11 @@ func NewSortedStringArraySize(cap int, unsafe...bool) *SortedStringArray { } } +// Create an array with given slice . +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 通过给定的slice变量创建排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func NewSortedStringArrayFrom(array []string, unsafe...bool) *SortedStringArray { a := NewSortedStringArraySize(0, unsafe...) a.array = array @@ -51,6 +61,8 @@ func NewSortedStringArrayFrom(array []string, unsafe...bool) *SortedStringArray return a } +// Set the underlying slice array with the given param. +// // 设置底层数组变量. func (a *SortedStringArray) SetArray(array []string) *SortedStringArray { a.mu.Lock() @@ -60,7 +72,9 @@ func (a *SortedStringArray) SetArray(array []string) *SortedStringArray { return a } -// 将数组重新排序(从小到大). +// Sort the array in increasing order. +// +// 将数组排序(默认从低到高). func (a *SortedStringArray) Sort() *SortedStringArray { a.mu.Lock() defer a.mu.Unlock() @@ -68,7 +82,9 @@ func (a *SortedStringArray) Sort() *SortedStringArray { return a } -// 添加加数据项 +// And values to sorted array, the array always keeps sorted. +// +// 添加数据项. func (a *SortedStringArray) Add(values...string) *SortedStringArray { if len(values) == 0 { return a @@ -95,7 +111,9 @@ func (a *SortedStringArray) Add(values...string) *SortedStringArray { return a } -// 获取指定索引的数据项, 调用方注意判断数组边界 +// Get value by index. +// +// 获取指定索引的数据项, 调用方注意判断数组边界。 func (a *SortedStringArray) Get(index int) string { a.mu.RLock() defer a.mu.RUnlock() @@ -103,7 +121,9 @@ func (a *SortedStringArray) Get(index int) string { return value } -// 删除指定索引的数据项, 调用方注意判断数组边界 +// Remove an item by index. +// +// 删除指定索引的数据项, 调用方注意判断数组边界。 func (a *SortedStringArray) Remove(index int) string { a.mu.Lock() defer a.mu.Unlock() @@ -123,7 +143,9 @@ func (a *SortedStringArray) Remove(index int) string { return value } -// 将最左端(索引为0)的数据项移出数组,并返回该数据项 +// Push new items to the beginning of array. +// +// 将数据项添加到数组的最左端(索引为0)。 func (a *SortedStringArray) PopLeft() string { a.mu.Lock() defer a.mu.Unlock() @@ -132,7 +154,9 @@ func (a *SortedStringArray) PopLeft() string { return value } -// 将最右端(索引为length - 1)的数据项移出数组,并返回该数据项 +// Push new items to the end of array. +// +// 将数据项添加到数组的最右端(索引为length - 1)。 func (a *SortedStringArray) PopRight() string { a.mu.Lock() defer a.mu.Unlock() @@ -142,6 +166,73 @@ func (a *SortedStringArray) PopRight() string { return value } +// Pop an random item from array. +// +// 随机将一个数据项移出数组,并返回该数据项。 +func (a *SortedStringArray) PopRand() string { + return a.Remove(grand.Intn(len(a.array))) +} + +// Pop items from the beginning of array. +// +// 将最左端(首部)的size个数据项移出数组,并返回该数据项 +func (a *SortedStringArray) PopLefts(size int) []string { + a.mu.Lock() + defer a.mu.Unlock() + length := len(a.array) + if size > length { + size = length + } + value := a.array[0 : size] + a.array = a.array[size : ] + return value +} + +// Pop items from the end of array. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *SortedStringArray) PopRights(size int) []string { + a.mu.Lock() + defer a.mu.Unlock() + index := len(a.array) - size + if index < 0 { + index = 0 + } + value := a.array[index :] + a.array = a.array[ : index] + return value +} + +// Get items by range, returns array[start:end]. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 将最右端(尾部)的size个数据项移出数组,并返回该数据项 +func (a *SortedStringArray) Range(start, end int) []string { + a.mu.RLock() + defer a.mu.RUnlock() + length := len(a.array) + if start > length || start > end { + return nil + } + if start < 0 { + start = 0 + } + if end > length { + end = length + } + array := ([]string)(nil) + if a.mu.IsSafe() { + a.mu.RLock() + defer a.mu.RUnlock() + array = make([]string, end - start) + copy(array, a.array[start : end]) + } else { + array = a.array[start : end] + } + return array +} + // Calculate the sum of values in an array. // // 对数组中的元素项求和(将元素值转换为int类型后叠加)。 @@ -154,7 +245,9 @@ func (a *SortedStringArray) Sum() (sum int) { return } -// 数组长度 +// Get the length of array. +// +// 数组长度。 func (a *SortedStringArray) Len() int { a.mu.RLock() length := len(a.array) @@ -162,7 +255,11 @@ func (a *SortedStringArray) Len() int { return length } -// 返回原始数据数组 +// Get the underlying data of array. +// Be aware that, if in concurrent-safe usage, it returns a copy of slice; +// else a pointer to the underlying data. +// +// 返回原始数据数组. func (a *SortedStringArray) Slice() []string { array := ([]string)(nil) if a.mu.IsSafe() { @@ -176,18 +273,24 @@ func (a *SortedStringArray) Slice() []string { return array } -// 查找指定数值是否存在 +// Check whether a value exists in the array. +// +// 查找指定数值是否存在。 func (a *SortedStringArray) Contains(value string) bool { - _, r := a.Search(value) - return r == 0 + return a.Search(value) == 0 } -// 查找指定数值的索引位置,返回索引位置(具体匹配位置或者最后对比位置)及查找结果 -// 返回值: 最后比较位置, 比较结果 -func (a *SortedStringArray) Search(value string) (index int, result int) { - return a.binSearch(value, true) +// Search array by , returns the index of , returns -1 if not exists. +// +// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1。 +func (a *SortedStringArray) Search(value string) (index int) { + index, _ = a.binSearch(value, true) + return } +// Binary search. +// +// 二分查找. func (a *SortedStringArray) binSearch(value string, lock bool) (index int, result int) { if len(a.array) == 0 { return -1, -2 @@ -213,7 +316,11 @@ func (a *SortedStringArray) binSearch(value string, lock bool) (index int, resul return mid, cmp } -// 设置是否允许数组唯一 +// Set unique mark to the array, +// which means it does not contain any repeated items. +// It also do unique check, remove all repeated items. +// +// 设置是否允许数组唯一. func (a *SortedStringArray) SetUnique(unique bool) *SortedStringArray { oldUnique := a.unique.Val() a.unique.Set(unique) @@ -223,7 +330,9 @@ func (a *SortedStringArray) SetUnique(unique bool) *SortedStringArray { return a } -// 清理数组中重复的元素项 +// Do unique check, remove all repeated items. +// +// 清理数组中重复的元素项. func (a *SortedStringArray) Unique() *SortedStringArray { a.mu.Lock() i := 0 @@ -252,7 +361,9 @@ func (a *SortedStringArray) Clone() (newArray *SortedStringArray) { return NewSortedStringArrayFrom(array, !a.mu.IsSafe()) } -// 清空数据数组 +// Clear array. +// +// 清空数据数组。 func (a *SortedStringArray) Clear() *SortedStringArray { a.mu.Lock() if len(a.array) > 0 { @@ -262,7 +373,9 @@ func (a *SortedStringArray) Clear() *SortedStringArray { return a } -// 使用自定义方法执行加锁修改操作 +// Lock writing by callback function f. +// +// 使用自定义方法执行加锁修改操作。 func (a *SortedStringArray) LockFunc(f func(array []string)) *SortedStringArray { a.mu.Lock(true) defer a.mu.Unlock(true) @@ -270,7 +383,9 @@ func (a *SortedStringArray) LockFunc(f func(array []string)) *SortedStringArray return a } -// 使用自定义方法执行加锁读取操作 +// Lock reading by callback function f. +// +// 使用自定义方法执行加锁读取操作。 func (a *SortedStringArray) RLockFunc(f func(array []string)) *SortedStringArray { a.mu.RLock(true) defer a.mu.RUnlock(true) @@ -278,6 +393,8 @@ func (a *SortedStringArray) RLockFunc(f func(array []string)) *SortedStringArray return a } +// Merge two arrays. +// // 合并两个数组. func (a *SortedStringArray) Merge(array *SortedStringArray) *SortedStringArray { a.mu.Lock() @@ -291,7 +408,8 @@ func (a *SortedStringArray) Merge(array *SortedStringArray) *SortedStringArray { return a } -// Chunks an array into arrays with size elements. The last chunk may contain less than size elements. +// Chunks an array into arrays with size elements. +// The last chunk may contain less than size elements. // // 将一个数组分割成多个数组,其中每个数组的单元数目由size决定。最后一个数组的单元数目可能会少于size个。 func (a *SortedStringArray) Chunk(size int) [][]string { @@ -314,8 +432,10 @@ func (a *SortedStringArray) Chunk(size int) [][]string { return n } -// Extract a slice of the array(If in concurrent safe usage, it returns a copy of the slice; else a pointer). -// It returns the sequence of elements from the array array as specified by the offset and length parameters. +// Extract a slice of the array(If in concurrent safe usage, +// it returns a copy of the slice; else a pointer). +// It returns the sequence of elements from the array array as specified +// by the offset and length parameters. // // 返回根据offset和size参数所指定的数组中的一段序列。 func (a *SortedStringArray) SubSlice(offset, size int) []string { @@ -336,7 +456,8 @@ func (a *SortedStringArray) SubSlice(offset, size int) []string { } } -// Picks one or more random entries out of an array(a copy), and returns the key (or keys) of the random entries. +// Picks one or more random entries out of an array(a copy), +// and returns the key (or keys) of the random entries. // // 从数组中随机取出size个元素项,构成slice返回。 func (a *SortedStringArray) Rand(size int) []string { diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index d2593254c..a0624548a 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -76,6 +76,34 @@ func TestIntArray_PushAndPop(t *testing.T) { }) } +func TestIntArray_PopLeftsAndPopRights(t *testing.T) { + gtest.Case(t, func() { + value1 := []int{0,1,2,3,4,5,6} + value2 := []int{0,1,2,3,4,5,6} + array1 := garray.NewIntArrayFrom(value1) + array2 := garray.NewIntArrayFrom(value2) + gtest.Assert(array1.PopLefts(2), []int{0,1}) + gtest.Assert(array1.Slice(), []int{2,3,4,5,6}) + gtest.Assert(array1.PopRights(2), []int{5,6}) + gtest.Assert(array1.Slice(), []int{2,3,4}) + gtest.Assert(array1.PopRights(20), []int{2,3,4}) + gtest.Assert(array1.Slice(), []int{}) + gtest.Assert(array2.PopLefts(20), []int{0,1,2,3,4,5,6}) + gtest.Assert(array2.Slice(), []int{}) + }) +} + +func TestIntArray_Range(t *testing.T) { + gtest.Case(t, func() { + value1 := []int{0,1,2,3,4,5,6} + array1 := garray.NewIntArrayFrom(value1) + gtest.Assert(array1.Range(0, 1), []int{0}) + gtest.Assert(array1.Range(1, 2), []int{1}) + gtest.Assert(array1.Range(0, 2), []int{0, 1}) + gtest.Assert(array1.Range(-1, 10), value1) + }) +} + func TestIntArray_Merge(t *testing.T) { gtest.Case(t, func() { a1 := []int{0, 1, 2, 3} diff --git a/g/container/garray/garray_z_unit_interface_test.go b/g/container/garray/garray_z_unit_interface_test.go index e4f58c246..a84f13cc2 100644 --- a/g/container/garray/garray_z_unit_interface_test.go +++ b/g/container/garray/garray_z_unit_interface_test.go @@ -80,6 +80,34 @@ func TestArray_PushAndPop(t *testing.T) { }) } +func TestArray_PopLeftsAndPopRights(t *testing.T) { + gtest.Case(t, func() { + value1 := []interface{}{0,1,2,3,4,5,6} + value2 := []interface{}{0,1,2,3,4,5,6} + array1 := garray.NewArrayFrom(value1) + array2 := garray.NewArrayFrom(value2) + gtest.Assert(array1.PopLefts(2), []interface{}{0,1}) + gtest.Assert(array1.Slice(), []interface{}{2,3,4,5,6}) + gtest.Assert(array1.PopRights(2), []interface{}{5,6}) + gtest.Assert(array1.Slice(), []interface{}{2,3,4}) + gtest.Assert(array1.PopRights(20), []interface{}{2,3,4}) + gtest.Assert(array1.Slice(), []interface{}{}) + gtest.Assert(array2.PopLefts(20), []interface{}{0,1,2,3,4,5,6}) + gtest.Assert(array2.Slice(), []interface{}{}) + }) +} + +func TestArray_Range(t *testing.T) { + gtest.Case(t, func() { + value1 := []interface{}{0,1,2,3,4,5,6} + array1 := garray.NewArrayFrom(value1) + gtest.Assert(array1.Range(0, 1), []interface{}{0}) + gtest.Assert(array1.Range(1, 2), []interface{}{1}) + gtest.Assert(array1.Range(0, 2), []interface{}{0, 1}) + gtest.Assert(array1.Range(-1, 10), value1) + }) +} + func TestArray_Merge(t *testing.T) { gtest.Case(t, func() { a1 := []interface{}{0, 1, 2, 3} diff --git a/g/container/garray/garray_z_unit_string_test.go b/g/container/garray/garray_z_unit_string_test.go index 43a250acf..84b54f352 100644 --- a/g/container/garray/garray_z_unit_string_test.go +++ b/g/container/garray/garray_z_unit_string_test.go @@ -77,6 +77,34 @@ func TestStringArray_PushAndPop(t *testing.T) { }) } +func TestStringArray_PopLeftsAndPopRights(t *testing.T) { + gtest.Case(t, func() { + value1 := []string{"0","1","2","3","4","5","6"} + value2 := []string{"0","1","2","3","4","5","6"} + array1 := garray.NewStringArrayFrom(value1) + array2 := garray.NewStringArrayFrom(value2) + gtest.Assert(array1.PopLefts(2), []interface{}{"0","1"}) + gtest.Assert(array1.Slice(), []interface{}{"2","3","4","5","6"}) + gtest.Assert(array1.PopRights(2), []interface{}{"5","6"}) + gtest.Assert(array1.Slice(), []interface{}{"2","3","4"}) + gtest.Assert(array1.PopRights(20), []interface{}{"2","3","4"}) + gtest.Assert(array1.Slice(), []interface{}{}) + gtest.Assert(array2.PopLefts(20), []interface{}{"0","1","2","3","4","5","6"}) + gtest.Assert(array2.Slice(), []interface{}{}) + }) +} + +func TestString_Range(t *testing.T) { + gtest.Case(t, func() { + value1 := []string{"0","1","2","3","4","5","6"} + array1 := garray.NewStringArrayFrom(value1) + gtest.Assert(array1.Range(0, 1), []interface{}{"0"}) + gtest.Assert(array1.Range(1, 2), []interface{}{"1"}) + gtest.Assert(array1.Range(0, 2), []interface{}{"0", "1"}) + gtest.Assert(array1.Range(-1, 10), value1) + }) +} + func TestStringArray_Merge(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3"} diff --git a/g/container/gmap/gmap.go b/g/container/gmap/gmap.go index 575e7c3f2..bc22f7fa0 100644 --- a/g/container/gmap/gmap.go +++ b/g/container/gmap/gmap.go @@ -15,16 +15,23 @@ import "gitee.com/johng/gf/g/internal/rwmutex" // 1、这个Map是所有并发安全Map中效率最低的,如果对效率要求比较高的场合,请合理选择对应数据类型的Map; // 2、这个Map的优点是使用简便,由于键值都是interface{}类型,因此对键值的数据类型要求不高; // 3、底层实现比较类似于sync.Map; - type Map struct { mu *rwmutex.RWMutex m map[interface{}]interface{} } +// Create an empty hash map. +// The param used to specify whether using map with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// +// 创建一个空的哈希表,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 func New(unsafe...bool) *Map { return NewMap(unsafe...) } +// See New. +// +// 同New方法。 func NewMap(unsafe...bool) *Map { return &Map{ m : make(map[interface{}]interface{}), @@ -32,6 +39,11 @@ func NewMap(unsafe...bool) *Map { } } +// Create a hash map from given map. +// Be aware that, the param map is a type of pointer, +// there might be some concurrent-safe issues when changing the map outside. +// +// 基于给定的map变量创建哈希表对象,注意由于map是指针类型,外部对map有操作时会有并发安全问题。 func NewFrom(m map[interface{}]interface{}, unsafe...bool) *Map { return &Map{ m : m, @@ -39,6 +51,12 @@ func NewFrom(m map[interface{}]interface{}, unsafe...bool) *Map { } } +// Create a hash map from given arrays. +// The param given as the keys of the map, +// and as the corresponding values. +// +// 基于给定的数组变量创建哈希表对象,keys作为键名, values作为键值。 +// 当keys数组大小比values数组大时,多余的键名将会使用对应类型默认的键值。 func NewFromArray(keys []interface{}, values []interface{}, unsafe...bool) *Map { m := make(map[interface{}]interface{}) l := len(values) @@ -55,6 +73,9 @@ func NewFromArray(keys []interface{}, values []interface{}, unsafe...bool) *Map } } +// Iterate the hash map with custom callback function . +// If f returns true, then continue iterating; or false to stop. +// // 给定回调函数对原始内容进行遍历,回调函数返回true表示继续遍历,否则停止遍历 func (gm *Map) Iterator(f func (k interface{}, v interface{}) bool) { gm.mu.RLock() @@ -66,11 +87,16 @@ func (gm *Map) Iterator(f func (k interface{}, v interface{}) bool) { } } +// Clone current hash map with copy of underlying data, +// return a new hash map. +// // 哈希表克隆. func (gm *Map) Clone() *Map { return NewFrom(gm.Map(), !gm.mu.IsSafe()) } +// Returns copy of the data of the hash map. +// // 返回当前哈希表的数据Map. func (gm *Map) Map() map[interface{}]interface{} { m := make(map[interface{}]interface{}) @@ -82,6 +108,8 @@ func (gm *Map) Map() map[interface{}]interface{} { return m } +// Set key-value to the hash map. +// // 设置键值对 func (gm *Map) Set(key interface{}, val interface{}) { gm.mu.Lock() @@ -89,6 +117,8 @@ func (gm *Map) Set(key interface{}, val interface{}) { gm.mu.Unlock() } +// Batch set key-values to the hash map. +// // 批量设置键值对 func (gm *Map) BatchSet(m map[interface{}]interface{}) { gm.mu.Lock() @@ -98,6 +128,8 @@ func (gm *Map) BatchSet(m map[interface{}]interface{}) { gm.mu.Unlock() } +// Get value by key. +// // 获取键值 func (gm *Map) Get(key interface{}) interface{} { gm.mu.RLock() @@ -121,6 +153,8 @@ func (gm *Map) doSetWithLockCheck(key interface{}, value interface{}) interface{ return value } +// Get the value by key, or set it with given key-value if not exist. +// // 当键名存在时返回其键值,否则写入指定的键值 func (gm *Map) GetOrSet(key interface{}, value interface{}) interface{} { if v := gm.Get(key); v == nil { @@ -130,6 +164,8 @@ func (gm *Map) GetOrSet(key interface{}, value interface{}) interface{} { } } +// Get the value by key, or set the it with return of callback function if not exist. +// // 当键名存在时返回其键值,否则写入指定的键值,键值由指定的函数生成 func (gm *Map) GetOrSetFunc(key interface{}, f func() interface{}) interface{} { if v := gm.Get(key); v == nil { @@ -139,6 +175,9 @@ func (gm *Map) GetOrSetFunc(key interface{}, f func() interface{}) interface{} { } } +// Get the value by key, or set the it with return of callback function if not exist. +// The difference with GetOrSetFunc is, it locks in executing callback function . +// // 与GetOrSetFunc不同的是,f是在写锁机制内执行 func (gm *Map) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} { if v := gm.Get(key); v == nil { @@ -148,6 +187,8 @@ func (gm *Map) GetOrSetFuncLock(key interface{}, f func() interface{}) interface } } +// Set key-value if the key does not exist, then return true; or else return false. +// // 当键名不存在时写入,并返回true;否则返回false。 func (gm *Map) SetIfNotExist(key interface{}, value interface{}) bool { if !gm.Contains(key) { @@ -157,6 +198,8 @@ func (gm *Map) SetIfNotExist(key interface{}, value interface{}) bool { return false } +// Batch remove by keys. +// // 批量删除键值对 func (gm *Map) BatchRemove(keys []interface{}) { gm.mu.Lock() @@ -166,6 +209,8 @@ func (gm *Map) BatchRemove(keys []interface{}) { gm.mu.Unlock() } +// Remove by given key. +// // 返回对应的键值,并删除该键值 func (gm *Map) Remove(key interface{}) interface{} { gm.mu.Lock() @@ -177,7 +222,9 @@ func (gm *Map) Remove(key interface{}) interface{} { return val } -// 返回键列表 +// Return all the keys of hash map as a slice. +// +// 返回键列表. func (gm *Map) Keys() []interface{} { gm.mu.RLock() keys := make([]interface{}, 0) @@ -188,6 +235,8 @@ func (gm *Map) Keys() []interface{} { return keys } +// Return all the values of hash map as a slice. +// // 返回值列表(注意是随机排序) func (gm *Map) Values() []interface{} { gm.mu.RLock() @@ -199,6 +248,8 @@ func (gm *Map) Values() []interface{} { return vals } +// Check whether a key exist. +// // 是否存在某个键 func (gm *Map) Contains(key interface{}) bool { gm.mu.RLock() @@ -207,6 +258,8 @@ func (gm *Map) Contains(key interface{}) bool { return exists } +// Get the size of hash map. +// // 哈希表大小 func (gm *Map) Size() int { gm.mu.RLock() @@ -215,6 +268,8 @@ func (gm *Map) Size() int { return length } +// Check whether the hash map is empty. +// // 哈希表是否为空 func (gm *Map) IsEmpty() bool { gm.mu.RLock() @@ -223,6 +278,8 @@ func (gm *Map) IsEmpty() bool { return empty } +// Clear the hash map, it will remake a new underlying map data map. +// // 清空哈希表 func (gm *Map) Clear() { gm.mu.Lock() @@ -230,6 +287,8 @@ func (gm *Map) Clear() { gm.mu.Unlock() } +// Lock writing with given callback . +// // 并发安全锁操作,使用自定义方法执行加锁修改操作 func (gm *Map) LockFunc(f func(m map[interface{}]interface{})) { gm.mu.Lock(true) @@ -237,6 +296,8 @@ func (gm *Map) LockFunc(f func(m map[interface{}]interface{})) { f(gm.m) } +// Lock reading with given callback . +// // 并发安全锁操作,使用自定义方法执行加锁读取操作 func (gm *Map) RLockFunc(f func(m map[interface{}]interface{})) { gm.mu.RLock(true) @@ -244,6 +305,8 @@ func (gm *Map) RLockFunc(f func(m map[interface{}]interface{})) { f(gm.m) } +// Exchange key-value in the hash map, it will result key-value to value-key. +// // 交换Map中的键和值. func (gm *Map) Flip() { gm.mu.Lock() @@ -255,6 +318,8 @@ func (gm *Map) Flip() { gm.m = n } +// Merge two hash maps. +// // 合并两个Map. func (gm *Map) Merge(m *Map) { gm.mu.Lock() diff --git a/g/text/gstr/gstr.go b/g/text/gstr/gstr.go index d063af2cc..6b5b5d1ab 100644 --- a/g/text/gstr/gstr.go +++ b/g/text/gstr/gstr.go @@ -4,9 +4,9 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -// Package gstr provides useful API for string handling. +// Package gstr provides functions for string handling. // -// 字符串操作. +// 字符串处理. package gstr import ( diff --git a/g/text/gstr/gstr_levenshtein.go b/g/text/gstr/gstr_levenshtein.go index dca1f0700..c0870ca5d 100644 --- a/g/text/gstr/gstr_levenshtein.go +++ b/g/text/gstr/gstr_levenshtein.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// 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://gitee.com/johng/gf. + package gstr // Calculate Levenshtein distance between two strings. diff --git a/g/text/gstr/gstr_parse.go b/g/text/gstr/gstr_parse.go index d4154de0f..8628d26c4 100644 --- a/g/text/gstr/gstr_parse.go +++ b/g/text/gstr/gstr_parse.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// 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://gitee.com/johng/gf. + package gstr import ( diff --git a/g/text/gstr/gstr_pos.go b/g/text/gstr/gstr_pos.go index a872811ab..9af1bd6dd 100644 --- a/g/text/gstr/gstr_pos.go +++ b/g/text/gstr/gstr_pos.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// 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://gitee.com/johng/gf. + package gstr import "strings" diff --git a/g/text/gstr/gstr_similartext.go b/g/text/gstr/gstr_similartext.go index 43cbe5eaa..1c2761b97 100644 --- a/g/text/gstr/gstr_similartext.go +++ b/g/text/gstr/gstr_similartext.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// 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://gitee.com/johng/gf. + package gstr // Calculate the similarity between two strings. diff --git a/g/text/gstr/gstr_soundex.go b/g/text/gstr/gstr_soundex.go index 4975bf777..7acc96c69 100644 --- a/g/text/gstr/gstr_soundex.go +++ b/g/text/gstr/gstr_soundex.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// 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://gitee.com/johng/gf. + package gstr // Calculate the soundex key of a string. diff --git a/g/text/gstr/gstr_trim.go b/g/text/gstr/gstr_trim.go index 1d0e402be..ffd0e4dda 100644 --- a/g/text/gstr/gstr_trim.go +++ b/g/text/gstr/gstr_trim.go @@ -32,7 +32,7 @@ func TrimLeft(str string, characterMask ...string) string { return strings.TrimLeft(str, mask) } -// Strip all of the given cut string from the beginning of a string. +// Strip all of the given string from the beginning of a string. // // 去除字符串首的给定字符串。 func TrimLeftStr(str string, cut string) string { @@ -55,7 +55,7 @@ func TrimRight(str string, characterMask ...string) string { return strings.TrimRight(str, mask) } -// Strip all of the given cut string from the end of a string. +// Strip all of the given string from the end of a string. // // 去除字符串尾的给定字符串。 func TrimRightStr(str string, cut string) string { diff --git a/geg/other/test.go b/geg/other/test.go index b236095c2..18de00c30 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,32 +2,18 @@ package main import ( "fmt" -<<<<<<< HEAD - "gitee.com/johng/gf/g/text/gstr" + "gitee.com/johng/gf/g/container/garray" ) func main() { - fmt.Println(gstr.TrimLeftStr("gogo我爱gogo", "go")) - fmt.Println(gstr.TrimRightStr("gogo我爱gogo", "go")) -} -======= -<<<<<<< HEAD - "gitee.com/johng/gf/g/net/ghttp" - "strings" - "time" -) - -func main() { - for { - time.Sleep(500*time.Millisecond) - fmt.Println(strings.TrimSpace(ghttp.GetContent("http://127.0.0.1:8881"))) - } -======= - "gitee.com/johng/gf/g/os/gfile" -) - -func main() { - fmt.Println(gfile.RealPath("config")) ->>>>>>> master -} ->>>>>>> qiangg_reuseport + value1 := []interface{}{0,1,2,3,4,5,6} + fmt.Println(value1[1:2]) + return + array1 := garray.NewArrayFrom(value1) + a := array1.Range(0, 1) + fmt.Println(a) + fmt.Println(array1.Slice()) + a = append(a, 100) + fmt.Println(a) + fmt.Println(array1.Slice()) +} \ No newline at end of file diff --git a/geg/other/test2.go b/geg/other/test2.go index b562a0c86..49f61ef67 100644 --- a/geg/other/test2.go +++ b/geg/other/test2.go @@ -1,21 +1,6 @@ package main -<<<<<<< HEAD -func main() { - //listener, err := reuseport.Listen("tcp", ":8881") - //if err != nil { - // panic(err) - //} - //defer listener.Close() - // - //server := &http.Server{} - //http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - // fmt.Println(gproc.Pid()) - // fmt.Fprintf(w, "%d\n", gproc.Pid()) - //}) - // - //panic(server.Serve(listener)) -======= + import ( "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/net/ghttp" @@ -33,5 +18,4 @@ func main() { }) s.SetPort(6789) s.Run() ->>>>>>> master } \ No newline at end of file