Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
617 changes: 124 additions & 493 deletions container/garray/garray_normal_any.go

Large diffs are not rendered by default.

608 changes: 126 additions & 482 deletions container/garray/garray_normal_int.go

Large diffs are not rendered by default.

596 changes: 130 additions & 466 deletions container/garray/garray_normal_str.go

Large diffs are not rendered by default.

642 changes: 504 additions & 138 deletions container/garray/garray_normal_t.go

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions container/garray/garray_sorted_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package garray
import (
"fmt"
"sort"
"sync"

"github.com/gogf/gf/v2/util/gconv"
)
Expand All @@ -20,13 +21,16 @@ import (
// when its initialization and cannot be changed then.
type SortedArray struct {
*SortedTArray[any]
once sync.Once
}

// lazyInit lazily initializes the array.
func (a *SortedArray) lazyInit() {
if a.SortedTArray == nil {
a.SortedTArray = NewSortedTArraySize[any](0, nil, false)
}
a.once.Do(func() {
if a.SortedTArray == nil {
a.SortedTArray = NewSortedTArraySize[any](0, nil, false)
}
})
}

// NewSortedArray creates and returns an empty sorted array.
Expand Down
12 changes: 8 additions & 4 deletions container/garray/garray_sorted_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package garray

import (
"fmt"
"sync"

"github.com/gogf/gf/v2/util/gconv"
)
Expand All @@ -19,14 +20,17 @@ import (
// when its initialization and cannot be changed then.
type SortedIntArray struct {
*SortedTArray[int]
once sync.Once
}

// lazyInit lazily initializes the array.
func (a *SortedIntArray) lazyInit() {
if a.SortedTArray == nil {
a.SortedTArray = NewSortedTArraySize(0, defaultComparatorInt, false)
a.SetSorter(quickSortInt)
}
a.once.Do(func() {
if a.SortedTArray == nil {
a.SortedTArray = NewSortedTArraySize(0, defaultComparatorInt, false)
a.SetSorter(quickSortInt)
}
})
}

// NewSortedIntArray creates and returns an empty sorted array.
Expand Down
12 changes: 8 additions & 4 deletions container/garray/garray_sorted_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package garray
import (
"bytes"
"strings"
"sync"

"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
Expand All @@ -21,14 +22,17 @@ import (
// when its initialization and cannot be changed then.
type SortedStrArray struct {
*SortedTArray[string]
once sync.Once
}

// lazyInit lazily initializes the array.
func (a *SortedStrArray) lazyInit() {
if a.SortedTArray == nil {
a.SortedTArray = NewSortedTArraySize(0, defaultComparatorStr, false)
a.SetSorter(quickSortStr)
}
a.once.Do(func() {
if a.SortedTArray == nil {
a.SortedTArray = NewSortedTArraySize(0, defaultComparatorStr, false)
a.SetSorter(quickSortStr)
}
})
}

// NewSortedStrArray creates and returns an empty sorted array.
Expand Down
29 changes: 17 additions & 12 deletions container/garray/garray_z_unit_normal_t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ func Test_TArray_Basic(t *testing.T) {
v, ok = array.Remove(0) // 1, 2, 3
t.Assert(v, 100)
t.Assert(ok, true)
t.Assert(array.Slice(), []int{1, 2, 3})

v, ok = array.Remove(-1)
t.Assert(v, 0)
t.Assert(ok, false)
t.Assert(array.Slice(), []int{1, 2, 3})

v, ok = array.Remove(100000)
t.Assert(v, 0)
t.Assert(ok, false)
t.Assert(array.Slice(), []int{1, 2, 3})

v, ok = array2.Remove(3) // 0 1 2
t.Assert(v, 3)
Expand All @@ -81,14 +84,16 @@ func Test_TArray_Basic(t *testing.T) {
t.Assert(ok, true)

t.Assert(array.Contains(100), false)
array.Append(4) // 1, 2, 3 ,4
array.Append(4) // 2, 2, 3, 4
t.Assert(array.Slice(), []int{2, 2, 3, 4})
t.Assert(array.Len(), 4)
array.InsertBefore(0, 100) // 100, 1, 2, 3, 4
array.InsertAfter(0, 200) // 100, 200, 1, 2, 3, 4
t.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 4})
array.InsertBefore(0, 100) // 100, 2, 2, 3, 4
t.Assert(array.Slice(), []int{100, 2, 2, 3, 4})
array.InsertAfter(0, 200) // 100, 200, 2, 2, 3, 4
t.Assert(array.Slice(), []int{100, 200, 2, 2, 3, 4})
array.InsertBefore(5, 300)
array.InsertAfter(6, 400)
t.Assert(array.Slice(), []int{100, 200, 1, 2, 3, 300, 4, 400})
t.Assert(array.Slice(), []int{100, 200, 2, 2, 3, 300, 4, 400})
t.Assert(array.Clear().Len(), 0)
err = array.InsertBefore(99, 9900)
t.AssertNE(err, nil)
Expand Down Expand Up @@ -588,27 +593,27 @@ func TestTArray_RLockFunc(t *testing.T) {
ch1 := make(chan int64, 3)
ch2 := make(chan int64, 1)
// go1
go a1.RLockFunc(func(n1 []any) { // 读锁
time.Sleep(2 * time.Second) // 暂停1秒
go a1.RLockFunc(func(n1 []any) { // read lock
time.Sleep(2 * time.Second) // sleep 2 s
n1[2] = "g"
ch2 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
})

// go2
go func() {
time.Sleep(100 * time.Millisecond) // 故意暂停0.01秒,等go1执行锁后,再开始执行.
time.Sleep(100 * time.Millisecond) // wait go1 do line lock for 0.01s. Then do.
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
a1.Len()
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
}()

t1 := <-ch1
t2 := <-ch1
<-ch2 // 等待go1完成
<-ch2 // wait for go1 done.

// 防止ci抖动,以豪秒为单位
t.AssertLT(t2-t1, 20) // go1加的读锁,所go2读的时候,并没有阻塞。
t.Assert(a1.Contains("g"), false)
// Prevent CI jitter, in milliseconds.
t.AssertLT(t2-t1, 20) // Go1 acquired a read lock, so when Go2 reads, it is not blocked.
t.Assert(a1.Contains("g"), true)
})
}

Expand Down
Loading