diff --git a/g/container/garray/garray_normal_int.go b/g/container/garray/garray_normal_int.go index 452f74d39..6b2778cc8 100644 --- a/g/container/garray/garray_normal_int.go +++ b/g/container/garray/garray_normal_int.go @@ -17,27 +17,23 @@ import ( type IntArray struct { mu *rwmutex.RWMutex // 互斥锁 - cap int // 初始化设置的数组容量 - size int // 初始化设置的数组大小 array []int // 底层数组 } -func NewIntArray(size int, cap int, unsafe...bool) *IntArray { - a := &IntArray{ - mu : rwmutex.New(unsafe...), - } - a.size = size - if cap > 0 { - a.cap = cap - a.array = make([]int, size, cap) - } else { - a.array = make([]int, size) - } - return a +// Create an empty array. +// 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 NewIntArray(unsafe...bool) *IntArray { + return NewIntArraySize(0, 0, unsafe...) } -func NewIntArrayEmpty(unsafe...bool) *IntArray { - return NewIntArray(0, 0, unsafe...) +func NewIntArraySize(size int, cap int, unsafe...bool) *IntArray { + return &IntArray{ + mu : rwmutex.New(unsafe...), + array : make([]int, size, cap), + } } func NewIntArrayFrom(array []int, unsafe...bool) *IntArray { @@ -234,16 +230,23 @@ func (a *IntArray) Slice() []int { return array } +// Return a new array, which is a copy of current array. +// +// 克隆当前数组,返回当前数组的一个拷贝。 +func (a *IntArray) Clone() (newArray *IntArray) { + a.mu.RLock() + array := make([]int, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewIntArrayFrom(array, !a.mu.IsSafe()) +} + // 清空数据数组 func (a *IntArray) Clear() *IntArray { a.mu.Lock() if len(a.array) > 0 { - 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.mu.Unlock() return a } @@ -314,6 +317,8 @@ func (a *IntArray) Merge(array *IntArray) *IntArray { } // 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 { a.mu.Lock() defer a.mu.Unlock() @@ -331,6 +336,8 @@ func (a *IntArray) Fill(startIndex int, num int, value int) *IntArray { } // 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 { if size < 1 { panic("size: cannot be less than 1") @@ -355,6 +362,10 @@ func (a *IntArray) Chunk(size int) [][]int { // If size is positive then the array is padded on the right, or negative on the left. // If the absolute value of size is less than or equal to the length of the array // then no padding takes place. +// +// 返回数组的一个拷贝,并用value将其填补到size指定的长度。 +// 如果size为正数,则填补到数组的右侧,如果为负数则从左侧开始填补。 +// 如果size的绝对值小于或等于数组的长度则没有任何填补。 func (a *IntArray) Pad(size int, value int) *IntArray { a.mu.Lock() defer a.mu.Unlock() @@ -380,6 +391,8 @@ func (a *IntArray) Pad(size int, value int) *IntArray { // 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 { a.mu.RLock() defer a.mu.RUnlock() @@ -399,6 +412,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. +// +// 从数组中随机取出size个元素项,构成slice返回。 func (a *IntArray) Rand(size int) []int { a.mu.RLock() defer a.mu.RUnlock() @@ -416,6 +431,8 @@ func (a *IntArray) Rand(size int) []int { } // Randomly shuffles the array. +// +// 随机打乱当前数组。 func (a *IntArray) Shuffle() *IntArray { a.mu.Lock() defer a.mu.Unlock() @@ -426,6 +443,8 @@ func (a *IntArray) Shuffle() *IntArray { } // Make array with elements in reverse order. +// +// 将当前数组反转。 func (a *IntArray) Reverse() *IntArray { a.mu.Lock() defer a.mu.Unlock() @@ -436,6 +455,8 @@ func (a *IntArray) Reverse() *IntArray { } // Join array elements with a string. +// +// 使用glue字符串串连当前数组的元素项,构造成新的字符串返回。 func (a *IntArray) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() diff --git a/g/container/garray/garray_normal_interface.go b/g/container/garray/garray_normal_interface.go index 5876dcf17..e17fc392f 100644 --- a/g/container/garray/garray_normal_interface.go +++ b/g/container/garray/garray_normal_interface.go @@ -16,32 +16,28 @@ import ( ) type Array struct { - mu *rwmutex.RWMutex // 互斥锁 - cap int // 初始化设置的数组容量 - size int // 初始化设置的数组大小 - array []interface{} // 底层数组 + mu *rwmutex.RWMutex // 互斥锁 + array []interface{} // 底层数组 } -func New(size int, cap int, unsafe...bool) *Array { - return NewArray(size, cap, unsafe...) +// Create an empty array. +// 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 New(unsafe...bool) *Array { + return NewArraySize(0, 0, unsafe...) } -func NewArray(size int, cap int, unsafe...bool) *Array { - a := &Array{ - mu : rwmutex.New(unsafe...), +func NewArray(unsafe...bool) *Array { + return NewArraySize(0, 0, unsafe...) +} + +func NewArraySize(size int, cap int, unsafe...bool) *Array { + return &Array{ + mu : rwmutex.New(unsafe...), + array : make([]interface{}, size, cap), } - a.size = size - if cap > 0 { - a.cap = cap - a.array = make([]interface{}, size, cap) - } else { - a.array = make([]interface{}, size) - } - return a -} - -func NewArrayEmpty(unsafe...bool) *Array { - return NewArray(0, 0, unsafe...) } func NewArrayFrom(array []interface{}, unsafe...bool) *Array { @@ -219,15 +215,22 @@ func (a *Array) Slice() []interface{} { return array } +// Return a new array, which is a copy of current array. +// +// 克隆当前数组,返回当前数组的一个拷贝。 +func (a *Array) Clone() (newArray *Array) { + a.mu.RLock() + array := make([]interface{}, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewArrayFrom(array, !a.mu.IsSafe()) +} + // 清空数据数组 func (a *Array) Clear() *Array { a.mu.Lock() if len(a.array) > 0 { - 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.mu.Unlock() return a @@ -299,6 +302,8 @@ func (a *Array) Merge(array *Array) *Array { } // 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 { a.mu.Lock() defer a.mu.Unlock() @@ -316,6 +321,8 @@ func (a *Array) Fill(startIndex int, num int, value interface{}) *Array { } // 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{} { if size < 1 { panic("size: cannot be less than 1") @@ -341,6 +348,10 @@ func (a *Array) Chunk(size int) [][]interface{} { // if it's negative then on the left. // If the absolute value of size is less than or equal to the length of the array // then no padding takes place. +// +// 返回数组的一个拷贝,并用value将其填补到size指定的长度。 +// 如果size为正数,则填补到数组的右侧,如果为负数则从左侧开始填补。 +// 如果size的绝对值小于或等于数组的长度则没有任何填补。 func (a *Array) Pad(size int, val interface{}) *Array { a.mu.Lock() defer a.mu.Unlock() @@ -366,6 +377,8 @@ func (a *Array) Pad(size int, val interface{}) *Array { // 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 *Array) SubSlice(offset, size int) []interface{} { a.mu.RLock() defer a.mu.RUnlock() @@ -385,6 +398,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. +// +// 从数组中随机取出size个元素项,构成slice返回。 func (a *Array) Rand(size int) []interface{} { a.mu.RLock() defer a.mu.RUnlock() @@ -402,6 +417,8 @@ func (a *Array) Rand(size int) []interface{} { } // Randomly shuffles the array. +// +// 随机打乱当前数组。 func (a *Array) Shuffle() *Array { a.mu.Lock() defer a.mu.Unlock() @@ -412,6 +429,8 @@ func (a *Array) Shuffle() *Array { } // Make array with elements in reverse order. +// +// 将当前数组反转。 func (a *Array) Reverse() *Array { a.mu.Lock() defer a.mu.Unlock() @@ -422,6 +441,8 @@ func (a *Array) Reverse() *Array { } // Join array elements with a string. +// +// 使用glue字符串串连当前数组的元素项,构造成新的字符串返回。 func (a *Array) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() diff --git a/g/container/garray/garray_normal_string.go b/g/container/garray/garray_normal_string.go index 205bf3281..0ce9ebd04 100644 --- a/g/container/garray/garray_normal_string.go +++ b/g/container/garray/garray_normal_string.go @@ -17,27 +17,23 @@ import ( type StringArray struct { mu *rwmutex.RWMutex // 互斥锁 - cap int // 初始化设置的数组容量 - size int // 初始化设置的数组大小 array []string // 底层数组 } -func NewStringArray(size int, cap int, unsafe...bool) *StringArray { - a := &StringArray{ - mu : rwmutex.New(unsafe...), - } - a.size = size - if cap > 0 { - a.cap = cap - a.array = make([]string, size, cap) - } else { - a.array = make([]string, size) - } - return a +// Create an empty array. +// 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 NewStringArray(unsafe...bool) *StringArray { + return NewStringArraySize(0, 0, unsafe...) } -func NewStringArrayEmpty(unsafe...bool) *StringArray { - return NewStringArray(0, 0, unsafe...) +func NewStringArraySize(size int, cap int, unsafe...bool) *StringArray { + return &StringArray{ + mu : rwmutex.New(unsafe...), + array : make([]string, size, cap), + } } func NewStringArrayFrom(array []string, unsafe...bool) *StringArray { @@ -234,17 +230,24 @@ func (a *StringArray) Slice() []string { return array } +// Return a new array, which is a copy of current array. +// +// 克隆当前数组,返回当前数组的一个拷贝。 +func (a *StringArray) Clone() (newArray *StringArray) { + a.mu.RLock() + array := make([]string, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewStringArrayFrom(array, !a.mu.IsSafe()) +} + // 清空数据数组 func (a *StringArray) Clear() *StringArray { - a.mu.Lock() - if len(a.array) > 0 { - if a.cap > 0 { - a.array = make([]string, a.size, a.cap) - } else { - a.array = make([]string, a.size) - } - } - a.mu.Unlock() + a.mu.Lock() + if len(a.array) > 0 { + a.array = make([]string, 0) + } + a.mu.Unlock() return a } @@ -313,6 +316,8 @@ func (a *StringArray) Merge(array *StringArray) *StringArray { } // 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 { a.mu.Lock() defer a.mu.Unlock() @@ -330,6 +335,8 @@ func (a *StringArray) Fill(startIndex int, num int, value string) *StringArray { } // 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 { if size < 1 { panic("size: cannot be less than 1") @@ -355,6 +362,10 @@ func (a *StringArray) Chunk(size int) [][]string { // if it's negative then on the left. // If the absolute value of size is less than or equal to the length of the array // then no padding takes place. +// +// 返回数组的一个拷贝,并用value将其填补到size指定的长度。 +// 如果size为正数,则填补到数组的右侧,如果为负数则从左侧开始填补。 +// 如果size的绝对值小于或等于数组的长度则没有任何填补。 func (a *StringArray) Pad(size int, value string) *StringArray { a.mu.Lock() defer a.mu.Unlock() @@ -380,6 +391,8 @@ func (a *StringArray) Pad(size int, value string) *StringArray { // 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 { a.mu.RLock() defer a.mu.RUnlock() @@ -399,6 +412,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. +// +// 从数组中随机取出size个元素项,构成slice返回。 func (a *StringArray) Rand(size int) []string { a.mu.RLock() defer a.mu.RUnlock() @@ -416,6 +431,8 @@ func (a *StringArray) Rand(size int) []string { } // Randomly shuffles the array. +// +// 随机打乱当前数组。 func (a *StringArray) Shuffle() *StringArray { a.mu.Lock() defer a.mu.Unlock() @@ -426,6 +443,8 @@ func (a *StringArray) Shuffle() *StringArray { } // Make array with elements in reverse order. +// +// 将当前数组反转。 func (a *StringArray) Reverse() *StringArray { a.mu.Lock() defer a.mu.Unlock() @@ -436,6 +455,8 @@ func (a *StringArray) Reverse() *StringArray { } // Join array elements with a string. +// +// 使用glue字符串串连当前数组的元素项,构造成新的字符串返回。 func (a *StringArray) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index c726fa482..daa254b60 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -19,14 +19,21 @@ import ( // 默认按照从小到大进行排序 type SortedIntArray struct { mu *rwmutex.RWMutex // 互斥锁 - cap int // 初始化设置的数组容量 array []int // 底层数组 unique *gtype.Bool // 是否要求不能重复(默认false) compareFunc func(v1, v2 int) int // 比较函数,返回值 -1: v1 < v2;0: v1 == v2;1: v1 > v2 } -// 创建一个排序的int数组 -func NewSortedIntArray(cap int, unsafe...bool) *SortedIntArray { +// Create an empty sorted array. +// 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 NewSortedIntArray(unsafe...bool) *SortedIntArray { + return NewSortedIntArraySize(0, unsafe...) +} + +func NewSortedIntArraySize(cap int, unsafe...bool) *SortedIntArray { return &SortedIntArray { mu : rwmutex.New(unsafe...), array : make([]int, 0, cap), @@ -43,12 +50,8 @@ func NewSortedIntArray(cap int, unsafe...bool) *SortedIntArray { } } -func NewSortedIntArrayEmpty(unsafe...bool) *SortedIntArray { - return NewSortedIntArray(0, unsafe...) -} - func NewSortedIntArrayFrom(array []int, unsafe...bool) *SortedIntArray { - a := NewSortedIntArray(0, unsafe...) + a := NewSortedIntArraySize(0, unsafe...) a.array = array sort.Ints(a.array) return a @@ -244,11 +247,22 @@ func (a *SortedIntArray) Unique() *SortedIntArray { return a } +// Return a new array, which is a copy of current array. +// +// 克隆当前数组,返回当前数组的一个拷贝。 +func (a *SortedIntArray) Clone() (newArray *SortedIntArray) { + a.mu.RLock() + array := make([]int, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewSortedIntArrayFrom(array, !a.mu.IsSafe()) +} + // 清空数据数组 func (a *SortedIntArray) Clear() *SortedIntArray { a.mu.Lock() if len(a.array) > 0 { - a.array = make([]int, 0, a.cap) + a.array = make([]int, 0) } a.mu.Unlock() return a @@ -284,6 +298,8 @@ func (a *SortedIntArray) Merge(array *SortedIntArray) *SortedIntArray { } // 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 { if size < 1 { panic("size: cannot be less than 1") @@ -306,6 +322,8 @@ func (a *SortedIntArray) Chunk(size int) [][]int { // 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 { a.mu.RLock() defer a.mu.RUnlock() @@ -325,6 +343,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. +// +// 从数组中随机取出size个元素项,构成slice返回。 func (a *SortedIntArray) Rand(size int) []int { a.mu.RLock() defer a.mu.RUnlock() @@ -342,6 +362,8 @@ func (a *SortedIntArray) Rand(size int) []int { } // Join array elements with a string. +// +// 使用glue字符串串连当前数组的元素项,构造成新的字符串返回。 func (a *SortedIntArray) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index c08c6da18..b8737f567 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -19,13 +19,29 @@ import ( // 默认按照从小到大进行排序 type SortedArray struct { mu *rwmutex.RWMutex // 互斥锁 - cap int // 初始化设置的数组容量 array []interface{} // 底层数组 unique *gtype.Bool // 是否要求不能重复 compareFunc func(v1, v2 interface{}) int // 比较函数,返回值 -1: v1 < v2;0: v1 == v2;1: v1 > v2 } -func NewSortedArray(cap int, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray { +// Create an empty sorted array. +// The param used to specify whether using array with un-concurrent-safety, +// which is false in default, means concurrent-safe in default. +// The param used to compare values to sort in array, +// if it returns value < 0, means v1 < v2; +// if it returns value = 0, means v1 = v2; +// if it returns value > 0, means v1 > v2; +// +// 创建一个空的排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。 +// 参数compareFunc用于指定排序方法: +// 如果返回值 < 0, 表示 v1 < v2; +// 如果返回值 = 0, 表示 v1 = v2; +// 如果返回值 > 0, 表示 v1 > v2; +func NewSortedArray(compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray { + return NewSortedArraySize(0, compareFunc, unsafe...) +} + +func NewSortedArraySize(cap int, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray { return &SortedArray{ mu : rwmutex.New(unsafe...), unique : gtype.NewBool(), @@ -34,12 +50,8 @@ func NewSortedArray(cap int, compareFunc func(v1, v2 interface{}) int, unsafe... } } -func NewSortedArrayEmpty(compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray { - return NewSortedArray(0, compareFunc, unsafe...) -} - func NewSortedArrayFrom(array []interface{}, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray { - a := NewSortedArray(0, compareFunc, unsafe...) + a := NewSortedArraySize(0, compareFunc, unsafe...) a.array = array sort.Slice(a.array, func(i, j int) bool { return a.compareFunc(a.array[i], a.array[j]) < 0 @@ -243,11 +255,22 @@ func (a *SortedArray) Unique() *SortedArray { return a } +// Return a new array, which is a copy of current array. +// +// 克隆当前数组,返回当前数组的一个拷贝。 +func (a *SortedArray) Clone() (newArray *SortedArray) { + a.mu.RLock() + array := make([]interface{}, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewSortedArrayFrom(array, a.compareFunc, !a.mu.IsSafe()) +} + // 清空数据数组 func (a *SortedArray) Clear() *SortedArray { a.mu.Lock() if len(a.array) > 0 { - a.array = make([]interface{}, 0, a.cap) + a.array = make([]interface{}, 0) } a.mu.Unlock() return a @@ -285,6 +308,8 @@ func (a *SortedArray) Merge(array *SortedArray) *SortedArray { } // 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{} { if size < 1 { panic("size: cannot be less than 1") @@ -307,6 +332,8 @@ func (a *SortedArray) Chunk(size int) [][]interface{} { // 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{} { a.mu.RLock() defer a.mu.RUnlock() @@ -326,6 +353,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. +// +// 从数组中随机取出size个元素项,构成slice返回。 func (a *SortedArray) Rand(size int) []interface{} { a.mu.RLock() defer a.mu.RUnlock() @@ -343,6 +372,8 @@ func (a *SortedArray) Rand(size int) []interface{} { } // Join array elements with a string. +// +// 使用glue字符串串连当前数组的元素项,构造成新的字符串返回。 func (a *SortedArray) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index c2c6d1027..078e93459 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -19,13 +19,21 @@ import ( // 默认按照从小到大进行排序 type SortedStringArray struct { mu *rwmutex.RWMutex // 互斥锁 - cap int // 初始化设置的数组容量 array []string // 底层数组 unique *gtype.Bool // 是否要求不能重复 compareFunc func(v1, v2 string) int // 比较函数,返回值 -1: v1 < v2;0: v1 == v2;1: v1 > v2 } -func NewSortedStringArray(cap int, unsafe...bool) *SortedStringArray { +// Create an empty sorted array. +// 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 NewSortedStringArray(unsafe...bool) *SortedStringArray { + return NewSortedStringArraySize(0, unsafe...) +} + +func NewSortedStringArraySize(cap int, unsafe...bool) *SortedStringArray { return &SortedStringArray { mu : rwmutex.New(unsafe...), array : make([]string, 0, cap), @@ -36,12 +44,8 @@ func NewSortedStringArray(cap int, unsafe...bool) *SortedStringArray { } } -func NewSortedStringArrayEmpty(unsafe...bool) *SortedStringArray { - return NewSortedStringArray(0, unsafe...) -} - func NewSortedStringArrayFrom(array []string, unsafe...bool) *SortedStringArray { - a := NewSortedStringArray(0, unsafe...) + a := NewSortedStringArraySize(0, unsafe...) a.array = array sort.Strings(a.array) return a @@ -237,11 +241,22 @@ func (a *SortedStringArray) Unique() *SortedStringArray { return a } +// Return a new array, which is a copy of current array. +// +// 克隆当前数组,返回当前数组的一个拷贝。 +func (a *SortedStringArray) Clone() (newArray *SortedStringArray) { + a.mu.RLock() + array := make([]string, len(a.array)) + copy(array, a.array) + a.mu.RUnlock() + return NewSortedStringArrayFrom(array, !a.mu.IsSafe()) +} + // 清空数据数组 func (a *SortedStringArray) Clear() *SortedStringArray { a.mu.Lock() if len(a.array) > 0 { - a.array = make([]string, 0, a.cap) + a.array = make([]string, 0) } a.mu.Unlock() return a @@ -277,6 +292,8 @@ func (a *SortedStringArray) Merge(array *SortedStringArray) *SortedStringArray { } // 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 { if size < 1 { panic("size: cannot be less than 1") @@ -299,6 +316,8 @@ func (a *SortedStringArray) Chunk(size int) [][]string { // 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 { a.mu.RLock() defer a.mu.RUnlock() @@ -318,6 +337,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. +// +// 从数组中随机取出size个元素项,构成slice返回。 func (a *SortedStringArray) Rand(size int) []string { a.mu.RLock() defer a.mu.RUnlock() @@ -335,6 +356,8 @@ func (a *SortedStringArray) Rand(size int) []string { } // Join array elements with a string. +// +// 使用glue字符串串连当前数组的元素项,构造成新的字符串返回。 func (a *SortedStringArray) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() diff --git a/g/container/garray/garray_z_bench_test.go b/g/container/garray/garray_z_bench_test.go index 26ac1b0ab..9954a9293 100644 --- a/g/container/garray/garray_z_bench_test.go +++ b/g/container/garray/garray_z_bench_test.go @@ -14,7 +14,7 @@ import ( ) var ( - sortedIntArray = garray.NewSortedIntArray(0) + sortedIntArray = garray.NewSortedIntArray() ) func BenchmarkSortedIntArray_Add(b *testing.B) { diff --git a/g/container/garray/garray_z_unit_basic_test.go b/g/container/garray/garray_z_unit_basic_test.go index 23c07b631..830225296 100644 --- a/g/container/garray/garray_z_unit_basic_test.go +++ b/g/container/garray/garray_z_unit_basic_test.go @@ -18,7 +18,7 @@ import ( func Test_IntArray_Unique(t *testing.T) { expect := []int{1, 2, 3, 4, 5, 6} - array := garray.NewIntArray(0, 0) + array := garray.NewIntArray() array.Append(1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6) array.Unique() gtest.Assert(array.Slice(), expect) @@ -26,7 +26,7 @@ func Test_IntArray_Unique(t *testing.T) { func Test_SortedIntArray1(t *testing.T) { expect := []int{0,1,2,3,4,5,6,7,8,9,10} - array := garray.NewSortedIntArray(0) + array := garray.NewSortedIntArray() for i := 10; i > -1; i-- { array.Add(i) } @@ -35,7 +35,7 @@ func Test_SortedIntArray1(t *testing.T) { func Test_SortedIntArray2(t *testing.T) { expect := []int{0,1,2,3,4,5,6,7,8,9,10} - array := garray.NewSortedIntArray(0) + array := garray.NewSortedIntArray() for i := 0; i <= 10; i++ { array.Add(i) } @@ -44,7 +44,7 @@ func Test_SortedIntArray2(t *testing.T) { func Test_SortedStringArray1(t *testing.T) { expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedStringArray(0) + array := garray.NewSortedStringArray() for i := 10; i > -1; i-- { array.Add(gconv.String(i)) } @@ -53,7 +53,7 @@ func Test_SortedStringArray1(t *testing.T) { func Test_SortedStringArray2(t *testing.T) { expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedStringArray(0) + array := garray.NewSortedStringArray() for i := 0; i <= 10; i++ { array.Add(gconv.String(i)) } @@ -62,7 +62,7 @@ func Test_SortedStringArray2(t *testing.T) { func Test_SortedArray1(t *testing.T) { expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedArray(0, func(v1, v2 interface{}) int { + array := garray.NewSortedArray(func(v1, v2 interface{}) int { return strings.Compare(gconv.String(v1), gconv.String(v2)) }) for i := 10; i > -1; i-- { @@ -73,7 +73,7 @@ func Test_SortedArray1(t *testing.T) { func Test_SortedArray2(t *testing.T) { expect := []string{"0","1","10","2","3","4","5","6","7","8","9"} - array := garray.NewSortedArray(0, func(v1, v2 interface{}) int { + array := garray.NewSortedArray(func(v1, v2 interface{}) int { return strings.Compare(gconv.String(v1), gconv.String(v2)) }) for i := 0; i <= 10; i++ { diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index 962340ed9..d2593254c 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -42,7 +42,7 @@ func TestIntArray_Sort(t *testing.T) { gtest.Case(t, func() { expect1 := []int{0, 1, 2, 3} expect2 := []int{3, 2, 1, 0} - array := garray.NewIntArray(0, 0) + array := garray.NewIntArray() for i := 3; i >= 0; i-- { array.Append(i) } diff --git a/g/container/garray/garray_z_unit_interface_test.go b/g/container/garray/garray_z_unit_interface_test.go index fe82b1861..e4f58c246 100644 --- a/g/container/garray/garray_z_unit_interface_test.go +++ b/g/container/garray/garray_z_unit_interface_test.go @@ -42,7 +42,7 @@ func TestArray_Sort(t *testing.T) { gtest.Case(t, func() { expect1 := []interface{}{0, 1, 2, 3} expect2 := []interface{}{3, 2, 1, 0} - array := garray.NewArray(0, 0) + array := garray.NewArray() for i := 3; i >= 0; i-- { array.Append(i) } diff --git a/g/container/garray/garray_z_unit_string_test.go b/g/container/garray/garray_z_unit_string_test.go index 7e8a02966..43a250acf 100644 --- a/g/container/garray/garray_z_unit_string_test.go +++ b/g/container/garray/garray_z_unit_string_test.go @@ -43,7 +43,7 @@ func TestStringArray_Sort(t *testing.T) { gtest.Case(t, func() { expect1 := []string{"0", "1", "2", "3"} expect2 := []string{"3", "2", "1", "0"} - array := garray.NewStringArray(0, 0) + array := garray.NewStringArray() for i := 3; i >= 0; i-- { array.Append(gconv.String(i)) } diff --git a/g/container/gset/gset_z_unit_int_test.go b/g/container/gset/gset_z_unit_int_test.go index c5e77e0a0..8fdcdbb65 100644 --- a/g/container/gset/gset_z_unit_int_test.go +++ b/g/container/gset/gset_z_unit_int_test.go @@ -41,8 +41,8 @@ func TestIntSet_Iterator(t *testing.T) { s.Add(1).Add(2).Add(3) gtest.Assert(s.Size(), 3) - a1 := garray.New(0, 0) - a2 := garray.New(0, 0) + a1 := garray.New() + a2 := garray.New() s.Iterator(func(v int) bool { a1.Append(1) return false diff --git a/g/container/gset/gset_z_unit_string_test.go b/g/container/gset/gset_z_unit_string_test.go index e9bc60b26..611255eaf 100644 --- a/g/container/gset/gset_z_unit_string_test.go +++ b/g/container/gset/gset_z_unit_string_test.go @@ -41,8 +41,8 @@ func TestStringSet_Iterator(t *testing.T) { s.Add("1").Add("2").Add("3") gtest.Assert(s.Size(), 3) - a1 := garray.New(0, 0) - a2 := garray.New(0, 0) + a1 := garray.New() + a2 := garray.New() s.Iterator(func(v string) bool { a1.Append("1") return false diff --git a/g/container/gset/gset_z_unit_test.go b/g/container/gset/gset_z_unit_test.go index 7a12ad36a..2a630395a 100644 --- a/g/container/gset/gset_z_unit_test.go +++ b/g/container/gset/gset_z_unit_test.go @@ -41,8 +41,8 @@ func TestSet_Iterator(t *testing.T) { s.Add(1).Add(2).Add(3) gtest.Assert(s.Size(), 3) - a1 := garray.New(0, 0) - a2 := garray.New(0, 0) + a1 := garray.New() + a2 := garray.New() s.Iterator(func(v interface{}) bool { a1.Append(1) return false diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index 99491255d..c82ae4af6 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -309,7 +309,7 @@ func (s *Server) GetRouteMap() string { } if _, ok := m[item.domain]; !ok { // 注意排序函数的逻辑 - m[item.domain] = garray.NewSortedArray(100, func(v1, v2 interface{}) int { + m[item.domain] = garray.NewSortedArraySize(100, func(v1, v2 interface{}) int { item1 := v1.(*tableItem) item2 := v2.(*tableItem) r := 0 diff --git a/g/net/ghttp/ghttp_server_config_static.go b/g/net/ghttp/ghttp_server_config_static.go index 07bfc3fb6..2d1c4fcbb 100644 --- a/g/net/ghttp/ghttp_server_config_static.go +++ b/g/net/ghttp/ghttp_server_config_static.go @@ -108,7 +108,7 @@ func (s *Server) AddStaticPath(prefix string, path string) { // 先添加item s.config.StaticPaths = append(s.config.StaticPaths, addItem) // 按照prefix从长到短进行排序 - array := garray.NewSortedArray(0, func(v1, v2 interface{}) int { + array := garray.NewSortedArray(func(v1, v2 interface{}) int { s1 := gconv.String(v1) s2 := gconv.String(v2) r := len(s2) - len(s1) diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index f031e574a..aabea3106 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -45,7 +45,7 @@ func New(path string, file...string) *Config { } c := &Config { name : gtype.NewString(name), - paths : garray.NewStringArray(0, 1), + paths : garray.NewStringArray(), jsons : gmap.NewStringInterfaceMap(), vc : gtype.NewBool(), } diff --git a/g/os/gcron/gcron_cron.go b/g/os/gcron/gcron_cron.go index 3e6312792..6668798ca 100644 --- a/g/os/gcron/gcron_cron.go +++ b/g/os/gcron/gcron_cron.go @@ -159,7 +159,7 @@ func (c *Cron) Size() int { // 获取所有已注册的定时任务项(按照注册时间从小到大进行排序) func (c *Cron) Entries() []*Entry { - array := garray.NewSortedArray(c.entries.Size(), func(v1, v2 interface{}) int { + array := garray.NewSortedArraySize(c.entries.Size(), func(v1, v2 interface{}) int { entry1 := v1.(*Entry) entry2 := v2.(*Entry) if entry1.Time.Nanosecond() > entry2.Time.Nanosecond() { diff --git a/g/os/gcron/gcron_unit_1_test.go b/g/os/gcron/gcron_unit_1_test.go index 8a43e5aa9..7a68981ce 100644 --- a/g/os/gcron/gcron_unit_1_test.go +++ b/g/os/gcron/gcron_unit_1_test.go @@ -18,7 +18,7 @@ import ( func TestCron_Add_Close(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() _, err1 := cron.Add("* * * * * *", func() { //glog.Println("cron1") array.Append(1) @@ -92,7 +92,7 @@ func TestCron_AddSingleton(t *testing.T) { // keep this gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.AddSingleton("* * * * * *", func() { array.Append(1) time.Sleep(50*time.Second) @@ -107,7 +107,7 @@ func TestCron_AddSingleton(t *testing.T) { func TestCron_AddOnce(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.AddOnce("* * * * * *", func() { array.Append(1) }) @@ -124,7 +124,7 @@ func TestCron_AddOnce(t *testing.T) { func TestCron_AddTimes(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.AddTimes("* * * * * *", 2, func() { array.Append(1) }) @@ -137,7 +137,7 @@ func TestCron_AddTimes(t *testing.T) { func TestCron_DelayAdd(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.DelayAdd(500*time.Millisecond, "* * * * * *", func() { array.Append(1) }) @@ -154,7 +154,7 @@ func TestCron_DelayAdd(t *testing.T) { func TestCron_DelayAddSingleton(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.DelayAddSingleton(500*time.Millisecond, "* * * * * *", func() { array.Append(1) time.Sleep(10*time.Second) @@ -169,7 +169,7 @@ func TestCron_DelayAddSingleton(t *testing.T) { func TestCron_DelayAddOnce(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.DelayAddOnce(500*time.Millisecond, "* * * * * *", func() { array.Append(1) }) @@ -186,7 +186,7 @@ func TestCron_DelayAddOnce(t *testing.T) { func TestCron_DelayAddTimes(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() { array.Append(1) }) diff --git a/g/os/gcron/gcron_unit_2_test.go b/g/os/gcron/gcron_unit_2_test.go index 01d5f07ff..818e9d65f 100644 --- a/g/os/gcron/gcron_unit_2_test.go +++ b/g/os/gcron/gcron_unit_2_test.go @@ -21,7 +21,7 @@ func TestCron_Entry_Operations(t *testing.T) { gtest.Case(t, func() { cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() { glog.Println("add times") array.Append(1) @@ -36,7 +36,7 @@ func TestCron_Entry_Operations(t *testing.T) { }) cron := gcron.New() - array := garray.New(0, 0) + array := garray.New() entry, err1 := cron.Add("* * * * * *", func() { glog.Println("add") array.Append(1) diff --git a/g/os/gmlock/gmlock_unit_lock_test.go b/g/os/gmlock/gmlock_unit_lock_test.go index 553061de7..6fa0e8920 100644 --- a/g/os/gmlock/gmlock_unit_lock_test.go +++ b/g/os/gmlock/gmlock_unit_lock_test.go @@ -17,7 +17,7 @@ import ( func TestLocker_Lock_Unlock(t *testing.T) { gtest.Case(t, func() { key := "test1" - array := garray.New(0, 0) + array := garray.New() go func() { gmlock.Lock(key) array.Append(1) @@ -47,7 +47,7 @@ func TestLocker_Lock_Unlock(t *testing.T) { func TestLocker_Lock_Expire(t *testing.T) { gtest.Case(t, func() { key := "test2" - array := garray.New(0, 0) + array := garray.New() go func() { gmlock.Lock(key, 100*time.Millisecond) array.Append(1) @@ -69,7 +69,7 @@ func TestLocker_Lock_Expire(t *testing.T) { func TestLocker_TryLock_Expire(t *testing.T) { gtest.Case(t, func() { key := "test3" - array := garray.New(0, 0) + array := garray.New() go func() { gmlock.Lock(key, 200*time.Millisecond) array.Append(1) diff --git a/g/os/gmlock/gmlock_unit_rlock_test.go b/g/os/gmlock/gmlock_unit_rlock_test.go index 1d8bfe905..1ae9daf53 100644 --- a/g/os/gmlock/gmlock_unit_rlock_test.go +++ b/g/os/gmlock/gmlock_unit_rlock_test.go @@ -17,7 +17,7 @@ import ( func TestLocker_RLock1(t *testing.T) { gtest.Case(t, func() { key := "test100" - array := garray.New(0, 0) + array := garray.New() go func() { gmlock.RLock(key) array.Append(1) @@ -41,7 +41,7 @@ func TestLocker_RLock1(t *testing.T) { func TestLocker_RLock2(t *testing.T) { gtest.Case(t, func() { key := "test200" - array := garray.New(0, 0) + array := garray.New() go func() { gmlock.Lock(key) array.Append(1) diff --git a/g/os/gspath/gspath.go b/g/os/gspath/gspath.go index 0b7d53047..e8583307d 100644 --- a/g/os/gspath/gspath.go +++ b/g/os/gspath/gspath.go @@ -43,7 +43,7 @@ var ( // 创建一个搜索对象 func New(path...string) *SPath { sp := &SPath { - paths : garray.NewStringArray(0, 1), + paths : garray.NewStringArray(), cache : gmap.NewStringStringMap(), } if len(path) > 0 { diff --git a/g/os/gtimer/gtimer_z_unit_0_test.go b/g/os/gtimer/gtimer_z_unit_0_test.go index d7db1db2e..12e920bec 100644 --- a/g/os/gtimer/gtimer_z_unit_0_test.go +++ b/g/os/gtimer/gtimer_z_unit_0_test.go @@ -19,7 +19,7 @@ import ( func TestSetTimeout(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.SetTimeout(200*time.Millisecond, func() { array.Append(1) }) @@ -30,7 +30,7 @@ func TestSetTimeout(t *testing.T) { func TestSetInterval(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.SetInterval(200*time.Millisecond, func() { array.Append(1) }) @@ -41,7 +41,7 @@ func TestSetInterval(t *testing.T) { func TestAddEntry(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.AddEntry(200*time.Millisecond, func() { array.Append(1) }, false, 2, gtimer.STATUS_READY) @@ -52,7 +52,7 @@ func TestAddEntry(t *testing.T) { func TestAddSingleton(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.AddSingleton(200*time.Millisecond, func() { array.Append(1) time.Sleep(10000*time.Millisecond) @@ -64,7 +64,7 @@ func TestAddSingleton(t *testing.T) { func TestAddTimes(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.AddTimes(200*time.Millisecond, 2, func() { array.Append(1) }) @@ -75,7 +75,7 @@ func TestAddTimes(t *testing.T) { func TestDelayAdd(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.DelayAdd(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) }) @@ -88,7 +88,7 @@ func TestDelayAdd(t *testing.T) { func TestDelayAddEntry(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.DelayAddEntry(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) }, false, 2, gtimer.STATUS_READY) @@ -101,7 +101,7 @@ func TestDelayAddEntry(t *testing.T) { func TestDelayAddSingleton(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.DelayAddSingleton(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) time.Sleep(10000*time.Millisecond) @@ -115,7 +115,7 @@ func TestDelayAddSingleton(t *testing.T) { func TestDelayAddOnce(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.DelayAddOnce(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) }) @@ -128,7 +128,7 @@ func TestDelayAddOnce(t *testing.T) { func TestDelayAddTimes(t *testing.T) { gtest.Case(t, func() { - array := garray.New(0, 0) + array := garray.New() gtimer.DelayAddTimes(200*time.Millisecond, 200*time.Millisecond, 2, func() { array.Append(1) }) diff --git a/g/os/gtimer/gtimer_z_unit_1_test.go b/g/os/gtimer/gtimer_z_unit_1_test.go index e992ba22a..318766ffd 100644 --- a/g/os/gtimer/gtimer_z_unit_1_test.go +++ b/g/os/gtimer/gtimer_z_unit_1_test.go @@ -24,7 +24,7 @@ func New() *gtimer.Timer { func TestTimer_Add_Close(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() //fmt.Println("start", time.Now()) timer.Add(200*time.Millisecond, func() { //fmt.Println("entry1", time.Now()) @@ -53,7 +53,7 @@ func TestTimer_Add_Close(t *testing.T) { func TestTimer_Start_Stop_Close(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.Add(200*time.Millisecond, func() { //glog.Println("add...") array.Append(1) @@ -76,7 +76,7 @@ func TestTimer_Start_Stop_Close(t *testing.T) { func TestTimer_AddSingleton(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.AddSingleton(200*time.Millisecond, func() { array.Append(1) time.Sleep(10*time.Second) @@ -92,7 +92,7 @@ func TestTimer_AddSingleton(t *testing.T) { func TestTimer_AddOnce(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.AddOnce(200*time.Millisecond, func() { array.Append(1) }) @@ -114,7 +114,7 @@ func TestTimer_AddOnce(t *testing.T) { func TestTimer_AddTimes(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.AddTimes(200*time.Millisecond, 2, func() { array.Append(1) }) @@ -126,7 +126,7 @@ func TestTimer_AddTimes(t *testing.T) { func TestTimer_DelayAdd(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.DelayAdd(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) }) @@ -140,7 +140,7 @@ func TestTimer_DelayAdd(t *testing.T) { func TestTimer_DelayAddEntry(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.DelayAddEntry(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) }, false, 100, gtimer.STATUS_READY) @@ -154,7 +154,7 @@ func TestTimer_DelayAddEntry(t *testing.T) { func TestTimer_DelayAddSingleton(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.DelayAddSingleton(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) time.Sleep(10*time.Second) @@ -170,7 +170,7 @@ func TestTimer_DelayAddSingleton(t *testing.T) { func TestTimer_DelayAddOnce(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.DelayAddOnce(200*time.Millisecond, 200*time.Millisecond, func() { array.Append(1) }) @@ -188,7 +188,7 @@ func TestTimer_DelayAddOnce(t *testing.T) { func TestTimer_DelayAddTimes(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.DelayAddTimes(200*time.Millisecond, 500*time.Millisecond, 2, func() { array.Append(1) }) @@ -209,7 +209,7 @@ func TestTimer_DelayAddTimes(t *testing.T) { func TestTimer_AddLessThanInterval(t *testing.T) { gtest.Case(t, func() { timer := gtimer.New(10, 100*time.Millisecond) - array := garray.New(0, 0) + array := garray.New() timer.Add(20*time.Millisecond, func() { array.Append(1) }) @@ -227,7 +227,7 @@ func TestTimer_AddLessThanInterval(t *testing.T) { func TestTimer_AddLeveledEntry1(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() //glog.Println("start") timer.DelayAdd(1000*time.Millisecond, 1001*time.Millisecond, func() { //glog.Println("add") @@ -244,7 +244,7 @@ func TestTimer_AddLeveledEntry1(t *testing.T) { func TestTimer_Exit(t *testing.T) { gtest.Case(t, func() { timer := New() - array := garray.New(0, 0) + array := garray.New() timer.Add(200*time.Millisecond, func() { array.Append(1) gtimer.Exit() diff --git a/g/os/gtimer/gtimer_z_unit_2_test.go b/g/os/gtimer/gtimer_z_unit_2_test.go index f625f6c40..be6cdbb18 100644 --- a/g/os/gtimer/gtimer_z_unit_2_test.go +++ b/g/os/gtimer/gtimer_z_unit_2_test.go @@ -18,7 +18,7 @@ import ( func TestEntry_Start_Stop_Close(t *testing.T) { timer := New() - array := garray.New(0, 0) + array := garray.New() entry := timer.Add(200*time.Millisecond, func() { array.Append(1) }) @@ -39,7 +39,7 @@ func TestEntry_Start_Stop_Close(t *testing.T) { func TestEntry_Singleton(t *testing.T) { timer := New() - array := garray.New(0, 0) + array := garray.New() entry := timer.Add(200*time.Millisecond, func() { array.Append(1) time.Sleep(10*time.Second) @@ -56,7 +56,7 @@ func TestEntry_Singleton(t *testing.T) { func TestEntry_SetTimes(t *testing.T) { timer := New() - array := garray.New(0, 0) + array := garray.New() entry := timer.Add(200*time.Millisecond, func() { array.Append(1) }) @@ -67,7 +67,7 @@ func TestEntry_SetTimes(t *testing.T) { func TestEntry_Run(t *testing.T) { timer := New() - array := garray.New(0, 0) + array := garray.New() entry := timer.Add(1000*time.Millisecond, func() { array.Append(1) }) diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 289a11e3b..615b5f49e 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -70,7 +70,7 @@ func ParseContent(content string, params Params) ([]byte, error) { // 生成一个视图对象 func New(path...string) *View { view := &View { - paths : garray.NewStringArray(0, 1), + paths : garray.NewStringArray(), data : make(map[string]interface{}), funcmap : make(map[string]interface{}), delimiters : make([]string, 2), diff --git a/geg/container/garray/garray1.go b/geg/container/garray/garray1.go index e1779e170..882ad7197 100644 --- a/geg/container/garray/garray1.go +++ b/geg/container/garray/garray1.go @@ -8,7 +8,7 @@ import ( func main () { // 创建普通的int类型数组,并关闭默认的并发安全特性 - a := garray.NewIntArray(0, 0, false) + a := garray.NewIntArray(true) // 添加数据项 for i := 0; i < 10; i++ { diff --git a/geg/container/garray/garray2.go b/geg/container/garray/garray2.go index f60e768c5..f7c93516e 100644 --- a/geg/container/garray/garray2.go +++ b/geg/container/garray/garray2.go @@ -8,7 +8,7 @@ import ( func main () { // 自定义排序数组,降序排序(SortedIntArray管理的数据是升序) - a := garray.NewSortedArray(0, 0, func(v1, v2 interface{}) int { + a := garray.NewSortedArray(func(v1, v2 interface{}) int { if v1.(int) < v2.(int) { return 1 } diff --git a/geg/container/garray/garray_slice_safe.go b/geg/container/garray/garray_slice_safe.go index 554ddf217..78867fc80 100644 --- a/geg/container/garray/garray_slice_safe.go +++ b/geg/container/garray/garray_slice_safe.go @@ -7,7 +7,7 @@ import ( func main () { - a := garray.NewIntArray(0, 0) + a := garray.NewIntArray() a.Append(1, 2, 3) v := a.Slice() diff --git a/geg/container/garray/sorted_string_array1.go b/geg/container/garray/sorted_string_array1.go index 8bb72ff2c..2660d209f 100644 --- a/geg/container/garray/sorted_string_array1.go +++ b/geg/container/garray/sorted_string_array1.go @@ -6,7 +6,7 @@ import ( ) func main() { - array := garray.NewSortedStringArray(0, false) + array := garray.NewSortedStringArray() array.Add("1") array.Add("2") array.Add("3") diff --git a/geg/container/garray/sorted_string_array2.go b/geg/container/garray/sorted_string_array2.go index 53316ce5a..117728952 100644 --- a/geg/container/garray/sorted_string_array2.go +++ b/geg/container/garray/sorted_string_array2.go @@ -8,7 +8,7 @@ import ( ) func main() { - array := garray.NewSortedStringArray(0, false) + array := garray.NewSortedStringArray() array.Add("/api/ctl/show") array.Add("/api/ctl/post") array.Add("/api/obj/rest")