2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-04-16 21:01: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 21:01:23 +08:00
|
|
|
|
|
|
|
package garray
|
|
|
|
|
|
|
|
import (
|
2019-02-20 14:18:11 +08:00
|
|
|
"bytes"
|
2019-04-24 22:23:32 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/g/container/gtype"
|
2019-02-02 16:18:25 +08:00
|
|
|
"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 21:01:23 +08:00
|
|
|
)
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// It's using increasing order in default.
|
2018-04-16 21:01:23 +08:00
|
|
|
type SortedIntArray struct {
|
2019-04-24 22:23:32 +08:00
|
|
|
mu *rwmutex.RWMutex
|
|
|
|
array []int
|
|
|
|
unique *gtype.Bool // Whether enable unique feature(false)
|
2019-05-09 22:53:42 +08:00
|
|
|
comparator func(v1, v2 int) int // Comparison function(it returns -1: v1 < v2; 0: v1 == v2; 1: v1 > v2)
|
2018-04-16 21:01:23 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// NewSortedIntArray creates and returns an empty sorted array.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <unsafe> used to specify whether using array in un-concurrent-safety,
|
2019-04-24 22:23:32 +08:00
|
|
|
// which is false in default.
|
2019-02-01 22:00:58 +08:00
|
|
|
func NewSortedIntArray(unsafe...bool) *SortedIntArray {
|
|
|
|
return NewSortedIntArraySize(0, unsafe...)
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// NewSortedIntArraySize create and returns an sorted array with given size and cap.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <unsafe> used to specify whether using array in un-concurrent-safety,
|
2019-04-24 22:23:32 +08:00
|
|
|
// which is false in default.
|
2019-02-01 22:00:58 +08:00
|
|
|
func NewSortedIntArraySize(cap int, unsafe...bool) *SortedIntArray {
|
2018-09-30 16:49:34 +08:00
|
|
|
return &SortedIntArray {
|
2019-01-12 23:36:22 +08:00
|
|
|
mu : rwmutex.New(unsafe...),
|
2018-09-30 16:49:34 +08:00
|
|
|
array : make([]int, 0, cap),
|
2018-04-16 21:01:23 +08:00
|
|
|
unique : gtype.NewBool(),
|
2019-05-09 22:53:42 +08:00
|
|
|
comparator : func(v1, v2 int) int {
|
2018-04-16 21:01:23 +08:00
|
|
|
if v1 < v2 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if v1 > v2 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// NewIntArrayFrom creates and returns an sorted array with given slice <array>.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <unsafe> used to specify whether using array in un-concurrent-safety,
|
2019-04-24 22:23:32 +08:00
|
|
|
// which is false in default.
|
2019-01-30 21:27:03 +08:00
|
|
|
func NewSortedIntArrayFrom(array []int, unsafe...bool) *SortedIntArray {
|
2019-02-01 22:00:58 +08:00
|
|
|
a := NewSortedIntArraySize(0, unsafe...)
|
2019-01-30 21:27:03 +08:00
|
|
|
a.array = array
|
|
|
|
sort.Ints(a.array)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// NewSortedIntArrayFromCopy creates and returns an sorted array from a copy of given slice <array>.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <unsafe> used to specify whether using array in un-concurrent-safety,
|
2019-04-24 22:23:32 +08:00
|
|
|
// which is false in default.
|
2019-02-20 19:06:08 +08:00
|
|
|
func NewSortedIntArrayFromCopy(array []int, unsafe...bool) *SortedIntArray {
|
|
|
|
newArray := make([]int, len(array))
|
|
|
|
copy(newArray, array)
|
2019-06-12 22:45:13 +08:00
|
|
|
return NewSortedIntArrayFrom(newArray, unsafe...)
|
2019-02-20 19:06:08 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// SetArray sets the underlying slice array with the given <array>.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) SetArray(array []int) *SortedIntArray {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
a.array = array
|
|
|
|
sort.Ints(a.array)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Sort sorts the array in increasing order.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <reverse> controls whether sort
|
2019-04-24 22:23:32 +08:00
|
|
|
// in increasing order(default) or decreasing order.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) Sort() *SortedIntArray {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
sort.Ints(a.array)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Add adds one or multiple values to sorted array, the array always keeps sorted.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) Add(values...int) *SortedIntArray {
|
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 21:01: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([]int{}, a.array[index : ]...)
|
|
|
|
a.array = append(a.array[0 : index], value)
|
|
|
|
a.array = append(a.array, rear...)
|
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-04-24 22:23:32 +08:00
|
|
|
// Get returns the value of the specified index,
|
|
|
|
// the caller should notice the boundary of the array.
|
2018-04-16 21:01:23 +08:00
|
|
|
func (a *SortedIntArray) Get(index int) int {
|
|
|
|
a.mu.RLock()
|
2018-07-01 00:27:33 +08:00
|
|
|
defer a.mu.RUnlock()
|
2018-04-16 21:01:23 +08:00
|
|
|
value := a.array[index]
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Remove removes an item by index.
|
2018-10-16 18:12:50 +08:00
|
|
|
func (a *SortedIntArray) Remove(index int) int {
|
2018-04-16 21:01:23 +08:00
|
|
|
a.mu.Lock()
|
2018-07-01 00:27:33 +08:00
|
|
|
defer a.mu.Unlock()
|
2019-04-24 22:23:32 +08:00
|
|
|
// Determine array boundaries when deleting to improve deletion efficiency.
|
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
|
|
|
|
}
|
2019-04-24 22:23:32 +08:00
|
|
|
// If it is a non-boundary delete,
|
|
|
|
// it will involve the creation of an array,
|
|
|
|
// then the deletion is less efficient.
|
2018-10-16 18:12:50 +08:00
|
|
|
value := a.array[index]
|
2018-04-16 21:01:23 +08:00
|
|
|
a.array = append(a.array[ : index], a.array[index + 1 : ]...)
|
2018-10-16 18:12:50 +08:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// PopLeft pops and returns an item from the beginning of array.
|
2018-10-16 18:12:50 +08:00
|
|
|
func (a *SortedIntArray) PopLeft() int {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
value := a.array[0]
|
|
|
|
a.array = a.array[1 : ]
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// PopRight pops and returns an item from the end of array.
|
2018-10-16 18:12:50 +08:00
|
|
|
func (a *SortedIntArray) PopRight() int {
|
|
|
|
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 21:01:23 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// PopRand randomly pops and return an item out of array.
|
2019-02-02 14:22:32 +08:00
|
|
|
func (a *SortedIntArray) PopRand() int {
|
|
|
|
return a.Remove(grand.Intn(len(a.array)))
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// PopRands randomly pops and returns <size> items out of array.
|
2019-02-20 19:06:08 +08:00
|
|
|
func (a *SortedIntArray) PopRands(size int) []int {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
if size > len(a.array) {
|
|
|
|
size = len(a.array)
|
|
|
|
}
|
|
|
|
array := make([]int, 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-04-24 22:23:32 +08:00
|
|
|
// PopLefts pops and returns <size> items from the beginning of array.
|
2019-02-02 14:22:32 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// PopRights pops and returns <size> items from the end of array.
|
2019-02-02 14:22:32 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Range picks and returns items by range, like array[start:end].
|
|
|
|
// Notice, if in concurrent-safe usage, it returns a copy of slice;
|
2019-02-02 14:22:32 +08:00
|
|
|
// else a pointer to the underlying data.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Len returns the length of array.
|
2018-04-16 21:01:23 +08:00
|
|
|
func (a *SortedIntArray) Len() int {
|
|
|
|
a.mu.RLock()
|
|
|
|
length := len(a.array)
|
|
|
|
a.mu.RUnlock()
|
|
|
|
return length
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Sum returns the sum of values in an array.
|
2019-02-01 17:30:23 +08:00
|
|
|
func (a *SortedIntArray) Sum() (sum int) {
|
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
for _, v := range a.array {
|
|
|
|
sum += v
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Slice returns the underlying data of array.
|
|
|
|
// Notice, if in concurrent-safe usage, it returns a copy of slice;
|
2019-02-02 14:22:32 +08:00
|
|
|
// else a pointer to the underlying data.
|
2018-04-16 21:01:23 +08:00
|
|
|
func (a *SortedIntArray) Slice() []int {
|
2018-09-16 10:51:02 +08:00
|
|
|
array := ([]int)(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([]int, 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 21:01:23 +08:00
|
|
|
return array
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Contains checks whether a value exists in the array.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) Contains(value int) bool {
|
2019-06-12 22:45:13 +08:00
|
|
|
return a.Search(value) != -1
|
2019-01-30 21:27:03 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Search searches array by <value>, returns the index of <value>,
|
|
|
|
// or returns -1 if not exists.
|
2019-02-02 14:22:32 +08:00
|
|
|
func (a *SortedIntArray) Search(value int) (index int) {
|
2019-06-12 10:11:54 +08:00
|
|
|
if i, r := a.binSearch(value, true); r == 0 {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return -1
|
2018-10-16 18:12:50 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 14:22:32 +08:00
|
|
|
// Binary search.
|
2019-06-12 10:11:54 +08:00
|
|
|
// It returns the last compared index and the result.
|
|
|
|
// If <result> equals to 0, it means the value at <index> is equals to <value>.
|
|
|
|
// If <result> lesser than 0, it means the value at <index> is lesser than <value>.
|
|
|
|
// If <result> greater than 0, it means the value at <index> is greater than <value>.
|
2018-10-30 18:52:47 +08:00
|
|
|
func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) {
|
2018-04-16 21:01: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 21:01: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)
|
2019-05-09 22:53:42 +08:00
|
|
|
cmp = a.comparator(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 21:01:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mid, cmp
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// SetUnique sets unique mark to the array,
|
2019-02-02 14:22:32 +08:00
|
|
|
// 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 *SortedIntArray) SetUnique(unique bool) *SortedIntArray {
|
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-04-24 22:23:32 +08:00
|
|
|
// Unique uniques the array, clear repeated items.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) Unique() *SortedIntArray {
|
2018-04-16 21:01:23 +08:00
|
|
|
a.mu.Lock()
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
if i == len(a.array) - 1 {
|
|
|
|
break
|
|
|
|
}
|
2019-05-09 22:53:42 +08:00
|
|
|
if a.comparator(a.array[i], a.array[i + 1]) == 0 {
|
2018-04-16 21:01:23 +08:00
|
|
|
a.array = append(a.array[ : i + 1], a.array[i + 1 + 1 : ]...)
|
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.mu.Unlock()
|
2019-01-30 21:27:03 +08:00
|
|
|
return a
|
2018-04-16 21:01:23 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Clone returns a new array, which is a copy of current array.
|
2019-02-01 22:00:58 +08:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Clear deletes all items of current array.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) Clear() *SortedIntArray {
|
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([]int, 0)
|
2018-11-16 18:20:09 +08:00
|
|
|
}
|
2018-04-16 21:01: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-04-24 22:23:32 +08:00
|
|
|
// LockFunc locks writing by callback function <f>.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray {
|
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-04-24 22:23:32 +08:00
|
|
|
// RLockFunc locks reading by callback function <f>.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray {
|
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-04-24 22:23:32 +08:00
|
|
|
// Merge merges <array> into current array.
|
|
|
|
// The parameter <array> can be any garray or slice type.
|
|
|
|
// The difference between Merge and Append is Append supports only specified slice type,
|
|
|
|
// but Merge supports more parameter types.
|
2019-02-20 14:18:11 +08:00
|
|
|
func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray {
|
2019-02-22 09:08:46 +08:00
|
|
|
switch v := array.(type) {
|
|
|
|
case *Array: a.Add(gconv.Ints(v.Slice())...)
|
|
|
|
case *IntArray: a.Add(gconv.Ints(v.Slice())...)
|
|
|
|
case *StringArray: a.Add(gconv.Ints(v.Slice())...)
|
|
|
|
case *SortedArray: a.Add(gconv.Ints(v.Slice())...)
|
|
|
|
case *SortedIntArray: a.Add(gconv.Ints(v.Slice())...)
|
|
|
|
case *SortedStringArray: a.Add(gconv.Ints(v.Slice())...)
|
|
|
|
default:
|
|
|
|
a.Add(gconv.Ints(array)...)
|
2019-01-30 21:27:03 +08:00
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Chunk splits an array into multiple arrays,
|
|
|
|
// the size of each array is determined by <size>.
|
2019-02-02 14:22:32 +08:00
|
|
|
// The last chunk may contain less than size elements.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) Chunk(size int) [][]int {
|
|
|
|
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 [][]int
|
|
|
|
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-04-24 22:23:32 +08:00
|
|
|
// SubSlice returns a slice of elements from the array as specified
|
|
|
|
// by the <offset> and <size> parameters.
|
|
|
|
// If in concurrent safe usage, it returns a copy of the slice; else a pointer.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) SubSlice(offset, size int) []int {
|
|
|
|
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([]int, size)
|
|
|
|
copy(s, a.array[offset:])
|
|
|
|
return s
|
|
|
|
} else {
|
|
|
|
return a.array[offset:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Rand randomly returns one item from array(no deleting).
|
2019-02-20 19:06:08 +08:00
|
|
|
func (a *SortedIntArray) Rand() int {
|
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
return a.array[grand.Intn(len(a.array))]
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Rands randomly returns <size> items from array(no deleting).
|
2019-02-20 19:06:08 +08:00
|
|
|
func (a *SortedIntArray) Rands(size int) []int {
|
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([]int, size)
|
|
|
|
for i, v := range grand.Perm(len(a.array)) {
|
|
|
|
n[i] = a.array[v]
|
|
|
|
if i == size - 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:23:32 +08:00
|
|
|
// Join joins array elements with a string <glue>.
|
2019-01-30 21:27:03 +08:00
|
|
|
func (a *SortedIntArray) 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()
|
2019-04-24 22:23:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CountValues counts the number of occurrences of all values in the array.
|
|
|
|
func (a *SortedIntArray) CountValues() map[int]int {
|
|
|
|
m := make(map[int]int)
|
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
for _, v := range a.array {
|
|
|
|
m[v]++
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns current array as a string.
|
|
|
|
func (a *SortedIntArray) String() string {
|
|
|
|
a.mu.RLock()
|
|
|
|
defer a.mu.RUnlock()
|
|
|
|
return fmt.Sprint(a.array)
|
2018-04-16 21:01:23 +08:00
|
|
|
}
|