mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 21:59:05 +08:00
577 lines
15 KiB
Go
577 lines
15 KiB
Go
// Copyright 2018 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
|
|
|
package garray
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"math"
|
|
"sort"
|
|
|
|
"github.com/gogf/gf/container/gtype"
|
|
"github.com/gogf/gf/internal/rwmutex"
|
|
"github.com/gogf/gf/util/gconv"
|
|
"github.com/gogf/gf/util/grand"
|
|
)
|
|
|
|
// It's using increasing order in default.
|
|
type SortedIntArray struct {
|
|
mu *rwmutex.RWMutex
|
|
array []int
|
|
unique *gtype.Bool // Whether enable unique feature(false)
|
|
comparator func(a, b int) int // Comparison function(it returns -1: a < b; 0: a == b; 1: a > b)
|
|
}
|
|
|
|
// NewSortedIntArray creates and returns an empty sorted array.
|
|
// The parameter <safe> is used to specify whether using array in concurrent-safety,
|
|
// which is false in default.
|
|
func NewSortedIntArray(safe ...bool) *SortedIntArray {
|
|
return NewSortedIntArraySize(0, safe...)
|
|
}
|
|
|
|
// NewSortedIntArrayComparator creates and returns an empty sorted array with specified comparator.
|
|
// The parameter <safe> is used to specify whether using array in concurrent-safety which is false in default.
|
|
func NewSortedIntArrayComparator(comparator func(a, b int) int, safe ...bool) *SortedIntArray {
|
|
array := NewSortedIntArray(safe...)
|
|
array.comparator = comparator
|
|
return array
|
|
}
|
|
|
|
// NewSortedIntArraySize create and returns an sorted array with given size and cap.
|
|
// The parameter <safe> is used to specify whether using array in concurrent-safety,
|
|
// which is false in default.
|
|
func NewSortedIntArraySize(cap int, safe ...bool) *SortedIntArray {
|
|
return &SortedIntArray{
|
|
mu: rwmutex.New(safe...),
|
|
array: make([]int, 0, cap),
|
|
unique: gtype.NewBool(),
|
|
comparator: defaultComparatorInt,
|
|
}
|
|
}
|
|
|
|
// NewIntArrayFrom creates and returns an sorted array with given slice <array>.
|
|
// The parameter <safe> is used to specify whether using array in concurrent-safety,
|
|
// which is false in default.
|
|
func NewSortedIntArrayFrom(array []int, safe ...bool) *SortedIntArray {
|
|
a := NewSortedIntArraySize(0, safe...)
|
|
a.array = array
|
|
sort.Ints(a.array)
|
|
return a
|
|
}
|
|
|
|
// NewSortedIntArrayFromCopy creates and returns an sorted array from a copy of given slice <array>.
|
|
// The parameter <safe> is used to specify whether using array in concurrent-safety,
|
|
// which is false in default.
|
|
func NewSortedIntArrayFromCopy(array []int, safe ...bool) *SortedIntArray {
|
|
newArray := make([]int, len(array))
|
|
copy(newArray, array)
|
|
return NewSortedIntArrayFrom(newArray, safe...)
|
|
}
|
|
|
|
// SetArray sets the underlying slice array with the given <array>.
|
|
func (a *SortedIntArray) SetArray(array []int) *SortedIntArray {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.array = array
|
|
sort.Ints(a.array)
|
|
return a
|
|
}
|
|
|
|
// Sort sorts the array in increasing order.
|
|
// The parameter <reverse> controls whether sort
|
|
// in increasing order(default) or decreasing order.
|
|
func (a *SortedIntArray) Sort() *SortedIntArray {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
sort.Ints(a.array)
|
|
return a
|
|
}
|
|
|
|
// Add adds one or multiple values to sorted array, the array always keeps sorted.
|
|
func (a *SortedIntArray) Add(values ...int) *SortedIntArray {
|
|
if len(values) == 0 {
|
|
return a
|
|
}
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
for _, value := range values {
|
|
index, cmp := a.binSearch(value, false)
|
|
if a.unique.Val() && cmp == 0 {
|
|
continue
|
|
}
|
|
if index < 0 {
|
|
a.array = append(a.array, value)
|
|
continue
|
|
}
|
|
if cmp > 0 {
|
|
index++
|
|
}
|
|
rear := append([]int{}, a.array[index:]...)
|
|
a.array = append(a.array[0:index], value)
|
|
a.array = append(a.array, rear...)
|
|
}
|
|
return a
|
|
}
|
|
|
|
// Get returns the value of the specified index,
|
|
// the caller should notice the boundary of the array.
|
|
func (a *SortedIntArray) Get(index int) int {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
value := a.array[index]
|
|
return value
|
|
}
|
|
|
|
// Remove removes an item by index.
|
|
func (a *SortedIntArray) Remove(index int) int {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
// Determine array boundaries when deleting to improve deletion efficiency.
|
|
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
|
|
}
|
|
// If it is a non-boundary delete,
|
|
// it will involve the creation of an array,
|
|
// then the deletion is less efficient.
|
|
value := a.array[index]
|
|
a.array = append(a.array[:index], a.array[index+1:]...)
|
|
return value
|
|
}
|
|
|
|
// PopLeft pops and returns an item from the beginning of array.
|
|
func (a *SortedIntArray) PopLeft() int {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
value := a.array[0]
|
|
a.array = a.array[1:]
|
|
return value
|
|
}
|
|
|
|
// PopRight pops and returns an item from the end of array.
|
|
func (a *SortedIntArray) PopRight() int {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
index := len(a.array) - 1
|
|
value := a.array[index]
|
|
a.array = a.array[:index]
|
|
return value
|
|
}
|
|
|
|
// PopRand randomly pops and return an item out of array.
|
|
func (a *SortedIntArray) PopRand() int {
|
|
return a.Remove(grand.Intn(len(a.array)))
|
|
}
|
|
|
|
// PopRands randomly pops and returns <size> items out of array.
|
|
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
|
|
}
|
|
|
|
// PopLefts pops and returns <size> items from the beginning of array.
|
|
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
|
|
}
|
|
|
|
// PopRights pops and returns <size> items from the end of array.
|
|
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
|
|
}
|
|
|
|
// Range picks and returns items by range, like array[start:end].
|
|
// Notice, if in concurrent-safe usage, it returns a copy of slice;
|
|
// else a pointer to the underlying data.
|
|
//
|
|
// If <end> is negative, then the offset will start from the end of array.
|
|
// If <end> is omitted, then the sequence will have everything from start up
|
|
// until the end of the array.
|
|
func (a *SortedIntArray) Range(start int, end ...int) []int {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
offsetEnd := len(a.array)
|
|
if len(end) > 0 && end[0] < offsetEnd {
|
|
offsetEnd = end[0]
|
|
}
|
|
if start > offsetEnd {
|
|
return nil
|
|
}
|
|
if start < 0 {
|
|
start = 0
|
|
}
|
|
array := ([]int)(nil)
|
|
if a.mu.IsSafe() {
|
|
array = make([]int, offsetEnd-start)
|
|
copy(array, a.array[start:offsetEnd])
|
|
} else {
|
|
array = a.array[start:offsetEnd]
|
|
}
|
|
return array
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// If offset is non-negative, the sequence will start at that offset in the array.
|
|
// If offset is negative, the sequence will start that far from the end of the array.
|
|
//
|
|
// If length is given and is positive, then the sequence will have up to that many elements in it.
|
|
// If the array is shorter than the length, then only the available array elements will be present.
|
|
// If length is given and is negative then the sequence will stop that many elements from the end of the array.
|
|
// If it is omitted, then the sequence will have everything from offset up until the end of the array.
|
|
//
|
|
// Any possibility crossing the left border of array, it will fail.
|
|
func (a *SortedIntArray) SubSlice(offset int, length ...int) []int {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
size := len(a.array)
|
|
if len(length) > 0 {
|
|
size = length[0]
|
|
}
|
|
if offset > len(a.array) {
|
|
return nil
|
|
}
|
|
if offset < 0 {
|
|
offset = len(a.array) + offset
|
|
if offset < 0 {
|
|
return nil
|
|
}
|
|
}
|
|
if size < 0 {
|
|
offset += size
|
|
size = -size
|
|
if offset < 0 {
|
|
return nil
|
|
}
|
|
}
|
|
end := offset + size
|
|
if end > len(a.array) {
|
|
end = 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:end]
|
|
}
|
|
}
|
|
|
|
// Len returns the length of array.
|
|
func (a *SortedIntArray) Len() int {
|
|
a.mu.RLock()
|
|
length := len(a.array)
|
|
a.mu.RUnlock()
|
|
return length
|
|
}
|
|
|
|
// Sum returns the sum of values in an array.
|
|
func (a *SortedIntArray) Sum() (sum int) {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
for _, v := range a.array {
|
|
sum += v
|
|
}
|
|
return
|
|
}
|
|
|
|
// Slice returns the underlying data of array.
|
|
// Note that, if it's in concurrent-safe usage, it returns a copy of underlying data,
|
|
// or else a pointer to the underlying data.
|
|
func (a *SortedIntArray) Slice() []int {
|
|
array := ([]int)(nil)
|
|
if a.mu.IsSafe() {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
array = make([]int, len(a.array))
|
|
copy(array, a.array)
|
|
} else {
|
|
array = a.array
|
|
}
|
|
return array
|
|
}
|
|
|
|
// Interfaces returns current array as []interface{}.
|
|
func (a *SortedIntArray) Interfaces() []interface{} {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
array := make([]interface{}, len(a.array))
|
|
for k, v := range a.array {
|
|
array[k] = v
|
|
}
|
|
return array
|
|
}
|
|
|
|
// Contains checks whether a value exists in the array.
|
|
func (a *SortedIntArray) Contains(value int) bool {
|
|
return a.Search(value) != -1
|
|
}
|
|
|
|
// Search searches array by <value>, returns the index of <value>,
|
|
// or returns -1 if not exists.
|
|
func (a *SortedIntArray) Search(value int) (index int) {
|
|
if i, r := a.binSearch(value, true); r == 0 {
|
|
return i
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// Binary search.
|
|
// 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>.
|
|
func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) {
|
|
if len(a.array) == 0 {
|
|
return -1, -2
|
|
}
|
|
if lock {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
}
|
|
min := 0
|
|
max := len(a.array) - 1
|
|
mid := 0
|
|
cmp := -2
|
|
for min <= max {
|
|
mid = int((min + max) / 2)
|
|
cmp = a.comparator(value, a.array[mid])
|
|
switch {
|
|
case cmp < 0:
|
|
max = mid - 1
|
|
case cmp > 0:
|
|
min = mid + 1
|
|
default:
|
|
return mid, cmp
|
|
}
|
|
}
|
|
return mid, cmp
|
|
}
|
|
|
|
// SetUnique sets 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)
|
|
if unique && oldUnique != unique {
|
|
a.Unique()
|
|
}
|
|
return a
|
|
}
|
|
|
|
// Unique uniques the array, clear repeated items.
|
|
func (a *SortedIntArray) Unique() *SortedIntArray {
|
|
a.mu.Lock()
|
|
i := 0
|
|
for {
|
|
if i == len(a.array)-1 {
|
|
break
|
|
}
|
|
if a.comparator(a.array[i], a.array[i+1]) == 0 {
|
|
a.array = append(a.array[:i+1], a.array[i+1+1:]...)
|
|
} else {
|
|
i++
|
|
}
|
|
}
|
|
a.mu.Unlock()
|
|
return a
|
|
}
|
|
|
|
// Clone returns 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())
|
|
}
|
|
|
|
// Clear deletes all items of current array.
|
|
func (a *SortedIntArray) Clear() *SortedIntArray {
|
|
a.mu.Lock()
|
|
if len(a.array) > 0 {
|
|
a.array = make([]int, 0)
|
|
}
|
|
a.mu.Unlock()
|
|
return a
|
|
}
|
|
|
|
// LockFunc locks writing by callback function <f>.
|
|
func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
f(a.array)
|
|
return a
|
|
}
|
|
|
|
// RLockFunc locks reading by callback function <f>.
|
|
func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
f(a.array)
|
|
return a
|
|
}
|
|
|
|
// 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.
|
|
func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray {
|
|
switch v := array.(type) {
|
|
case *Array:
|
|
a.Add(gconv.Ints(v.Slice())...)
|
|
case *IntArray:
|
|
a.Add(gconv.Ints(v.Slice())...)
|
|
case *StrArray:
|
|
a.Add(gconv.Ints(v.Slice())...)
|
|
case *SortedArray:
|
|
a.Add(gconv.Ints(v.Slice())...)
|
|
case *SortedIntArray:
|
|
a.Add(gconv.Ints(v.Slice())...)
|
|
case *SortedStrArray:
|
|
a.Add(gconv.Ints(v.Slice())...)
|
|
default:
|
|
a.Add(gconv.Ints(array)...)
|
|
}
|
|
return a
|
|
}
|
|
|
|
// Chunk splits an array into multiple arrays,
|
|
// the size of each array is determined by <size>.
|
|
// The last chunk may contain less than size elements.
|
|
func (a *SortedIntArray) Chunk(size int) [][]int {
|
|
if size < 1 {
|
|
return nil
|
|
}
|
|
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
|
|
}
|
|
|
|
// Rand randomly returns one item from array(no deleting).
|
|
func (a *SortedIntArray) Rand() int {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
return a.array[grand.Intn(len(a.array))]
|
|
}
|
|
|
|
// Rands randomly returns <size> items from array(no deleting).
|
|
func (a *SortedIntArray) Rands(size int) []int {
|
|
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
|
|
}
|
|
|
|
// Join joins array elements with a string <glue>.
|
|
func (a *SortedIntArray) Join(glue string) string {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
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()
|
|
}
|
|
|
|
// 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, which implements like json.Marshal does.
|
|
func (a *SortedIntArray) String() string {
|
|
return "[" + a.Join(",") + "]"
|
|
}
|
|
|
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
|
func (a *SortedIntArray) MarshalJSON() ([]byte, error) {
|
|
a.mu.RLock()
|
|
defer a.mu.RUnlock()
|
|
return json.Marshal(a.array)
|
|
}
|
|
|
|
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
|
func (a *SortedIntArray) UnmarshalJSON(b []byte) error {
|
|
if a.mu == nil {
|
|
a.mu = rwmutex.New()
|
|
a.array = make([]int, 0)
|
|
a.unique = gtype.NewBool()
|
|
a.comparator = defaultComparatorInt
|
|
}
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
if err := json.Unmarshal(b, &a.array); err != nil {
|
|
return err
|
|
}
|
|
sort.Ints(a.array)
|
|
return nil
|
|
}
|