2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-04-16 17:59:23 +08:00
|
|
|
|
//
|
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-04-16 17:59:23 +08:00
|
|
|
|
|
|
|
|
|
package garray
|
|
|
|
|
|
|
|
|
|
import (
|
2019-02-20 14:18:11 +08:00
|
|
|
|
"bytes"
|
2019-02-02 16:18:25 +08:00
|
|
|
|
"github.com/gogf/gf/g/container/gtype"
|
|
|
|
|
"github.com/gogf/gf/g/internal/rwmutex"
|
|
|
|
|
"github.com/gogf/gf/g/util/gconv"
|
|
|
|
|
"github.com/gogf/gf/g/util/grand"
|
2019-01-30 21:27:03 +08:00
|
|
|
|
"math"
|
|
|
|
|
"sort"
|
2018-04-16 17:59:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// 默认按照从小到大进行排序
|
2018-04-16 17:59:23 +08:00
|
|
|
|
type SortedArray struct {
|
2018-09-05 18:34:41 +08:00
|
|
|
|
mu *rwmutex.RWMutex // 互斥锁
|
2018-04-16 17:59:23 +08:00
|
|
|
|
array []interface{} // 底层数组
|
|
|
|
|
unique *gtype.Bool // 是否要求不能重复
|
|
|
|
|
compareFunc func(v1, v2 interface{}) int // 比较函数,返回值 -1: v1 < v2;0: v1 == v2;1: v1 > v2
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 22:00:58 +08:00
|
|
|
|
// Create an empty sorted array.
|
|
|
|
|
// The param <unsafe> used to specify whether using array with un-concurrent-safety,
|
|
|
|
|
// which is false in default, means concurrent-safe in default.
|
|
|
|
|
// The param <compareFunc> 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...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Create a sorted array with given size and cap.
|
|
|
|
|
// The param <unsafe> used to specify whether using array with un-concurrent-safety,
|
|
|
|
|
// which is false in default, means concurrent-safe in default.
|
|
|
|
|
//
|
|
|
|
|
// 创建一个指定大小的排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。
|
2019-02-01 22:00:58 +08:00
|
|
|
|
func NewSortedArraySize(cap int, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray {
|
2018-04-16 17:59:23 +08:00
|
|
|
|
return &SortedArray{
|
2019-01-12 23:36:22 +08:00
|
|
|
|
mu : rwmutex.New(unsafe...),
|
2018-04-16 17:59:23 +08:00
|
|
|
|
unique : gtype.NewBool(),
|
2018-09-30 16:49:34 +08:00
|
|
|
|
array : make([]interface{}, 0, cap),
|
2018-04-16 17:59:23 +08:00
|
|
|
|
compareFunc : compareFunc,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Create an array with given slice <array>.
|
|
|
|
|
// The param <unsafe> used to specify whether using array with un-concurrent-safety,
|
|
|
|
|
// which is false in default, means concurrent-safe in default.
|
|
|
|
|
//
|
|
|
|
|
// 通过给定的slice变量创建排序数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func NewSortedArrayFrom(array []interface{}, compareFunc func(v1, v2 interface{}) int, unsafe...bool) *SortedArray {
|
2019-02-01 22:00:58 +08:00
|
|
|
|
a := NewSortedArraySize(0, compareFunc, unsafe...)
|
2019-01-30 21:27:03 +08:00
|
|
|
|
a.array = array
|
|
|
|
|
sort.Slice(a.array, func(i, j int) bool {
|
|
|
|
|
return a.compareFunc(a.array[i], a.array[j]) < 0
|
|
|
|
|
})
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 19:06:08 +08:00
|
|
|
|
// Create an array from a copy of given slice <array>.
|
|
|
|
|
// The param <unsafe> used to specify whether using array with un-concurrent-safety,
|
|
|
|
|
// which is false in default, means concurrent-safe in default.
|
|
|
|
|
//
|
|
|
|
|
// 通过给定的slice拷贝创建数组对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。
|
|
|
|
|
func NewSortedArrayFromCopy(array []interface{}, unsafe...bool) *SortedArray {
|
|
|
|
|
newArray := make([]interface{}, len(array))
|
|
|
|
|
copy(newArray, array)
|
|
|
|
|
return &SortedArray{
|
|
|
|
|
mu : rwmutex.New(unsafe...),
|
|
|
|
|
array : newArray,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Set the underlying slice array with the given <array> param.
|
|
|
|
|
//
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// 设置底层数组变量.
|
|
|
|
|
func (a *SortedArray) SetArray(array []interface{}) *SortedArray {
|
|
|
|
|
a.mu.Lock()
|
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
a.array = array
|
|
|
|
|
sort.Slice(a.array, func(i, j int) bool {
|
|
|
|
|
return a.compareFunc(a.array[i], a.array[j]) < 0
|
|
|
|
|
})
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Sort the array by comparing function.
|
|
|
|
|
//
|
|
|
|
|
// 将数组按照比较方法进行排序.
|
2019-02-01 18:14:12 +08:00
|
|
|
|
func (a *SortedArray) Sort() *SortedArray {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
a.mu.Lock()
|
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
sort.Slice(a.array, func(i, j int) bool {
|
|
|
|
|
return a.compareFunc(a.array[i], a.array[j]) < 0
|
|
|
|
|
})
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// And values to sorted array, the array always keeps sorted.
|
|
|
|
|
//
|
|
|
|
|
// 添加数据项.
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) Add(values...interface{}) *SortedArray {
|
2018-09-03 23:28:02 +08:00
|
|
|
|
if len(values) == 0 {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return a
|
2018-04-16 17:59:23 +08:00
|
|
|
|
}
|
2018-10-16 18:12:50 +08:00
|
|
|
|
a.mu.Lock()
|
|
|
|
|
defer a.mu.Unlock()
|
2018-09-03 23:28:02 +08:00
|
|
|
|
for _, value := range values {
|
2018-10-16 18:12:50 +08:00
|
|
|
|
index, cmp := a.binSearch(value, false)
|
2018-09-03 23:28:02 +08:00
|
|
|
|
if a.unique.Val() && cmp == 0 {
|
2018-09-30 16:32:12 +08:00
|
|
|
|
continue
|
2018-09-03 23:28:02 +08:00
|
|
|
|
}
|
|
|
|
|
if index < 0 {
|
|
|
|
|
a.array = append(a.array, value)
|
2018-09-30 16:32:12 +08:00
|
|
|
|
continue
|
2018-09-03 23:28:02 +08:00
|
|
|
|
}
|
|
|
|
|
// 加到指定索引后面
|
|
|
|
|
if cmp > 0 {
|
|
|
|
|
index++
|
|
|
|
|
}
|
|
|
|
|
rear := append([]interface{}{}, a.array[index : ]...)
|
|
|
|
|
a.array = append(a.array[0 : index], value)
|
|
|
|
|
a.array = append(a.array, rear...)
|
2018-04-16 17:59:23 +08:00
|
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return a
|
2018-04-16 17:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Get value by index.
|
|
|
|
|
//
|
|
|
|
|
// 获取指定索引的数据项, 调用方注意判断数组边界。
|
2018-04-16 17:59:23 +08:00
|
|
|
|
func (a *SortedArray) Get(index int) interface{} {
|
|
|
|
|
a.mu.RLock()
|
2018-07-01 00:27:33 +08:00
|
|
|
|
defer a.mu.RUnlock()
|
2018-04-16 17:59:23 +08:00
|
|
|
|
value := a.array[index]
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Remove an item by index.
|
|
|
|
|
//
|
|
|
|
|
// 删除指定索引的数据项, 调用方注意判断数组边界。
|
2018-10-16 18:12:50 +08:00
|
|
|
|
func (a *SortedArray) Remove(index int) interface{} {
|
2018-04-16 17:59:23 +08:00
|
|
|
|
a.mu.Lock()
|
2018-07-01 00:27:33 +08:00
|
|
|
|
defer a.mu.Unlock()
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 边界删除判断,以提高删除效率
|
|
|
|
|
if index == 0 {
|
|
|
|
|
value := a.array[0]
|
|
|
|
|
a.array = a.array[1 : ]
|
|
|
|
|
return value
|
|
|
|
|
} else if index == len(a.array) - 1 {
|
|
|
|
|
value := a.array[index]
|
|
|
|
|
a.array = a.array[: index]
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
// 如果非边界删除,会涉及到数组创建,那么删除的效率差一些
|
2018-10-16 18:12:50 +08:00
|
|
|
|
value := a.array[index]
|
2018-04-16 17:59:23 +08:00
|
|
|
|
a.array = append(a.array[ : index], a.array[index + 1 : ]...)
|
2018-10-16 18:12:50 +08:00
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Push new items to the beginning of array.
|
|
|
|
|
//
|
|
|
|
|
// 将数据项添加到数组的最左端(索引为0)。
|
2018-10-16 18:12:50 +08:00
|
|
|
|
func (a *SortedArray) PopLeft() interface{} {
|
|
|
|
|
a.mu.Lock()
|
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
value := a.array[0]
|
|
|
|
|
a.array = a.array[1 : ]
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Push new items to the end of array.
|
|
|
|
|
//
|
|
|
|
|
// 将数据项添加到数组的最右端(索引为length - 1)。
|
2018-10-16 18:12:50 +08:00
|
|
|
|
func (a *SortedArray) PopRight() interface{} {
|
|
|
|
|
a.mu.Lock()
|
|
|
|
|
defer a.mu.Unlock()
|
2018-11-16 18:20:09 +08:00
|
|
|
|
index := len(a.array) - 1
|
|
|
|
|
value := a.array[index]
|
|
|
|
|
a.array = a.array[: index]
|
2018-10-16 18:12:50 +08:00
|
|
|
|
return value
|
2018-04-16 17:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 19:06:08 +08:00
|
|
|
|
// PopRand picks an random item out of array.
|
2019-02-02 14:22:32 +08:00
|
|
|
|
//
|
|
|
|
|
// 随机将一个数据项移出数组,并返回该数据项。
|
|
|
|
|
func (a *SortedArray) PopRand() interface{} {
|
|
|
|
|
return a.Remove(grand.Intn(len(a.array)))
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 19:06:08 +08:00
|
|
|
|
// PopRands picks <size> items out of array.
|
|
|
|
|
//
|
|
|
|
|
// 随机将size个数据项移出数组,并返回该数据项。
|
|
|
|
|
func (a *SortedArray) PopRands(size int) []interface{} {
|
|
|
|
|
a.mu.Lock()
|
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
if size > len(a.array) {
|
|
|
|
|
size = len(a.array)
|
|
|
|
|
}
|
|
|
|
|
array := make([]interface{}, size)
|
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
|
index := grand.Intn(len(a.array))
|
|
|
|
|
array[i] = a.array[index]
|
|
|
|
|
a.array = append(a.array[ : index], a.array[index + 1 : ]...)
|
|
|
|
|
}
|
|
|
|
|
return array
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Pop <size> 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 <size> 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
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 17:30:23 +08:00
|
|
|
|
// Calculate the sum of values in an array.
|
|
|
|
|
//
|
|
|
|
|
// 对数组中的元素项求和(将元素值转换为int类型后叠加)。
|
|
|
|
|
func (a *SortedArray) Sum() (sum int) {
|
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
|
for _, v := range a.array {
|
|
|
|
|
sum += gconv.Int(v)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Get the length of array.
|
|
|
|
|
//
|
|
|
|
|
// 数组长度。
|
2018-04-16 17:59:23 +08:00
|
|
|
|
func (a *SortedArray) Len() int {
|
|
|
|
|
a.mu.RLock()
|
|
|
|
|
length := len(a.array)
|
|
|
|
|
a.mu.RUnlock()
|
|
|
|
|
return length
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
// 返回原始数据数组.
|
2018-04-16 17:59:23 +08:00
|
|
|
|
func (a *SortedArray) Slice() []interface{} {
|
2018-09-16 10:51:02 +08:00
|
|
|
|
array := ([]interface{})(nil)
|
|
|
|
|
if a.mu.IsSafe() {
|
|
|
|
|
a.mu.RLock()
|
2019-01-30 21:27:03 +08:00
|
|
|
|
defer a.mu.RUnlock()
|
2018-09-16 10:51:02 +08:00
|
|
|
|
array = make([]interface{}, len(a.array))
|
2019-01-30 21:27:03 +08:00
|
|
|
|
copy(array, a.array)
|
2018-09-16 10:51:02 +08:00
|
|
|
|
} else {
|
|
|
|
|
array = a.array
|
|
|
|
|
}
|
2018-04-16 17:59:23 +08:00
|
|
|
|
return array
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Check whether a value exists in the array.
|
|
|
|
|
//
|
|
|
|
|
// 查找指定数值是否存在。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) Contains(value interface{}) bool {
|
2019-02-02 14:22:32 +08:00
|
|
|
|
return a.Search(value) == 0
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Search array by <value>, returns the index of <value>, returns -1 if not exists.
|
|
|
|
|
//
|
|
|
|
|
// 查找指定数值的索引位置,返回索引位置,如果查找不到则返回-1。
|
|
|
|
|
func (a *SortedArray) Search(value interface{}) (index int) {
|
|
|
|
|
index, _ = a.binSearch(value, true)
|
|
|
|
|
return
|
2018-10-16 18:12:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Binary search.
|
|
|
|
|
//
|
|
|
|
|
// 二分查找。查找指定数值的索引位置,返回索引位置(具体匹配位置或者最后对比位置)及查找结果
|
|
|
|
|
// 返回值: 最后比较位置, 比较结果。
|
2018-10-30 18:52:47 +08:00
|
|
|
|
func (a *SortedArray) binSearch(value interface{}, lock bool)(index int, result int) {
|
2018-04-16 17:59:23 +08:00
|
|
|
|
if len(a.array) == 0 {
|
|
|
|
|
return -1, -2
|
|
|
|
|
}
|
2018-10-16 18:12:50 +08:00
|
|
|
|
if lock {
|
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
|
}
|
2018-04-16 17:59:23 +08:00
|
|
|
|
min := 0
|
|
|
|
|
max := len(a.array) - 1
|
|
|
|
|
mid := 0
|
|
|
|
|
cmp := -2
|
2018-12-20 21:55:05 +08:00
|
|
|
|
for min <= max {
|
2018-08-23 17:24:24 +08:00
|
|
|
|
mid = int((min + max) / 2)
|
|
|
|
|
cmp = a.compareFunc(value, a.array[mid])
|
2019-02-01 17:44:58 +08:00
|
|
|
|
switch {
|
|
|
|
|
case cmp < 0 : max = mid - 1
|
|
|
|
|
case cmp > 0 : min = mid + 1
|
|
|
|
|
default :
|
2018-12-20 21:55:05 +08:00
|
|
|
|
return mid, cmp
|
2018-04-16 17:59:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mid, cmp
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Set unique mark to the array,
|
|
|
|
|
// which means it does not contain any repeated items.
|
|
|
|
|
// It also do unique check, remove all repeated items.
|
|
|
|
|
//
|
|
|
|
|
// 设置是否允许数组唯一.
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) SetUnique(unique bool) *SortedArray {
|
2018-04-16 21:01:23 +08:00
|
|
|
|
oldUnique := a.unique.Val()
|
|
|
|
|
a.unique.Set(unique)
|
|
|
|
|
if unique && oldUnique != unique {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
a.Unique()
|
2018-04-16 21:01:23 +08:00
|
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return a
|
2018-04-16 21:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Do unique check, remove all repeated items.
|
|
|
|
|
//
|
|
|
|
|
// 清理数组中重复的元素项.
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) Unique() *SortedArray {
|
2018-04-16 17:59:23 +08:00
|
|
|
|
a.mu.Lock()
|
2018-10-16 18:12:50 +08:00
|
|
|
|
defer a.mu.Unlock()
|
2018-04-16 21:01:23 +08:00
|
|
|
|
i := 0
|
|
|
|
|
for {
|
2018-04-16 17:59:23 +08:00
|
|
|
|
if i == len(a.array) - 1 {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-04-16 21:01:23 +08:00
|
|
|
|
if a.compareFunc(a.array[i], a.array[i + 1]) == 0 {
|
|
|
|
|
a.array = append(a.array[ : i + 1], a.array[i + 1 + 1 : ]...)
|
|
|
|
|
} else {
|
|
|
|
|
i++
|
2018-04-16 17:59:23 +08:00
|
|
|
|
}
|
2018-04-16 21:01:23 +08:00
|
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return a
|
2018-04-16 21:01:23 +08:00
|
|
|
|
}
|
2018-04-16 17:59:23 +08:00
|
|
|
|
|
2019-02-01 22:00:58 +08:00
|
|
|
|
// 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())
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Clear array.
|
|
|
|
|
//
|
|
|
|
|
// 清空数据数组。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) Clear() *SortedArray {
|
2018-04-16 21:01:23 +08:00
|
|
|
|
a.mu.Lock()
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if len(a.array) > 0 {
|
2019-02-01 22:00:58 +08:00
|
|
|
|
a.array = make([]interface{}, 0)
|
2018-11-16 18:20:09 +08:00
|
|
|
|
}
|
2018-04-16 17:59:23 +08:00
|
|
|
|
a.mu.Unlock()
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return a
|
2018-04-16 21:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Lock writing by callback function f.
|
|
|
|
|
//
|
|
|
|
|
// 使用自定义方法执行加锁修改操作。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray {
|
2019-03-12 00:24:31 +08:00
|
|
|
|
a.mu.Lock()
|
|
|
|
|
defer a.mu.Unlock()
|
2018-04-16 21:01:23 +08:00
|
|
|
|
f(a.array)
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return a
|
2018-04-16 21:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Lock reading by callback function f.
|
|
|
|
|
//
|
|
|
|
|
// 使用自定义方法执行加锁读取操作。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray {
|
2019-03-12 00:24:31 +08:00
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
2018-04-16 21:01:23 +08:00
|
|
|
|
f(a.array)
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 19:06:08 +08:00
|
|
|
|
// Merge two arrays. The parameter <array> can be any garray type or slice type.
|
2019-02-22 09:08:46 +08:00
|
|
|
|
// The difference between Merge and Add is Add supports only specified slice type,
|
|
|
|
|
// but Merge supports more variable types.
|
2019-02-02 14:22:32 +08:00
|
|
|
|
//
|
2019-02-22 09:08:46 +08:00
|
|
|
|
// 合并两个数组, 支持任意的garray数组类型及slice类型.
|
2019-02-20 14:18:11 +08:00
|
|
|
|
func (a *SortedArray) Merge(array interface{}) *SortedArray {
|
2019-02-22 09:08:46 +08:00
|
|
|
|
switch v := array.(type) {
|
|
|
|
|
case *Array: a.Add(gconv.Interfaces(v.Slice())...)
|
|
|
|
|
case *IntArray: a.Add(gconv.Interfaces(v.Slice())...)
|
|
|
|
|
case *StringArray: a.Add(gconv.Interfaces(v.Slice())...)
|
|
|
|
|
case *SortedArray: a.Add(gconv.Interfaces(v.Slice())...)
|
|
|
|
|
case *SortedIntArray: a.Add(gconv.Interfaces(v.Slice())...)
|
|
|
|
|
case *SortedStringArray: a.Add(gconv.Interfaces(v.Slice())...)
|
|
|
|
|
default:
|
|
|
|
|
a.Add(gconv.Interfaces(array)...)
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// Chunks an array into arrays with size elements.
|
|
|
|
|
// The last chunk may contain less than size elements.
|
2019-02-01 22:00:58 +08:00
|
|
|
|
//
|
|
|
|
|
// 将一个数组分割成多个数组,其中每个数组的单元数目由size决定。最后一个数组的单元数目可能会少于size个。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) Chunk(size int) [][]interface{} {
|
|
|
|
|
if size < 1 {
|
2019-02-02 09:07:40 +08:00
|
|
|
|
return nil
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
|
length := len(a.array)
|
|
|
|
|
chunks := int(math.Ceil(float64(length) / float64(size)))
|
|
|
|
|
var n [][]interface{}
|
|
|
|
|
for i, end := 0, 0; chunks > 0; chunks-- {
|
|
|
|
|
end = (i + 1) * size
|
|
|
|
|
if end > length {
|
|
|
|
|
end = length
|
|
|
|
|
}
|
|
|
|
|
n = append(n, a.array[i*size : end])
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
|
// 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.
|
2019-02-01 22:00:58 +08:00
|
|
|
|
//
|
|
|
|
|
// 返回根据offset和size参数所指定的数组中的一段序列。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) SubSlice(offset, size int) []interface{} {
|
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
|
if offset > len(a.array) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if offset + size > len(a.array) {
|
|
|
|
|
size = len(a.array) - offset
|
|
|
|
|
}
|
|
|
|
|
if a.mu.IsSafe() {
|
|
|
|
|
s := make([]interface{}, size)
|
|
|
|
|
copy(s, a.array[offset:])
|
|
|
|
|
return s
|
|
|
|
|
} else {
|
|
|
|
|
return a.array[offset:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 19:06:08 +08:00
|
|
|
|
// Rand gets one random entry from array.
|
|
|
|
|
//
|
|
|
|
|
// 从数组中随机获得1个元素项(不删除)。
|
|
|
|
|
func (a *SortedArray) Rand() interface{} {
|
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
|
return a.array[grand.Intn(len(a.array))]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rands gets one or more random entries from array(a copy).
|
2019-02-01 22:00:58 +08:00
|
|
|
|
//
|
2019-02-20 19:06:08 +08:00
|
|
|
|
// 从数组中随机拷贝size个元素项,构成slice返回。
|
|
|
|
|
func (a *SortedArray) Rands(size int) []interface{} {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
|
if size > len(a.array) {
|
|
|
|
|
size = len(a.array)
|
|
|
|
|
}
|
|
|
|
|
n := make([]interface{}, size)
|
|
|
|
|
for i, v := range grand.Perm(len(a.array)) {
|
|
|
|
|
n[i] = a.array[v]
|
|
|
|
|
if i == size - 1 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Join array elements with a string.
|
2019-02-01 22:00:58 +08:00
|
|
|
|
//
|
|
|
|
|
// 使用glue字符串串连当前数组的元素项,构造成新的字符串返回。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (a *SortedArray) Join(glue string) string {
|
|
|
|
|
a.mu.RLock()
|
|
|
|
|
defer a.mu.RUnlock()
|
2019-02-20 14:18:11 +08:00
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
|
for k, v := range a.array {
|
|
|
|
|
buffer.WriteString(gconv.String(v))
|
|
|
|
|
if k != len(a.array) - 1 {
|
|
|
|
|
buffer.WriteString(glue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return buffer.String()
|
2018-04-16 17:59:23 +08:00
|
|
|
|
}
|