diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index 9288a0702..f2fd289e7 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -286,11 +286,17 @@ func (a *SortedIntArray) Contains(value int) bool { // Search searches array by , returns the index of , // or returns -1 if not exists. func (a *SortedIntArray) Search(value int) (index int) { - index, _ = a.binSearch(value, true) - return + 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 equals to 0, it means the value at is equals to . +// If lesser than 0, it means the value at is lesser than . +// If greater than 0, it means the value at is greater than . func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) { if len(a.array) == 0 { return -1, -2 diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index c392fc468..25d0a7f17 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -287,11 +287,17 @@ func (a *SortedArray) Contains(value interface{}) bool { // Search searches array by , returns the index of , // or returns -1 if not exists. func (a *SortedArray) Search(value interface{}) (index int) { - index, _ = a.binSearch(value, true) - return + 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 equals to 0, it means the value at is equals to . +// If lesser than 0, it means the value at is lesser than . +// If greater than 0, it means the value at is greater than . func (a *SortedArray) binSearch(value interface{}, lock bool)(index int, result int) { if len(a.array) == 0 { return -1, -2 diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index 28e7a2fa0..1fa4fe6ec 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -281,11 +281,17 @@ func (a *SortedStringArray) Contains(value string) bool { // Search searches array by , returns the index of , // or returns -1 if not exists. func (a *SortedStringArray) Search(value string) (index int) { - index, _ = a.binSearch(value, true) - return + 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 equals to 0, it means the value at is equals to . +// If lesser than 0, it means the value at is lesser than . +// If greater than 0, it means the value at is greater than . func (a *SortedStringArray) binSearch(value string, lock bool) (index int, result int) { if len(a.array) == 0 { return -1, -2 diff --git a/geg/other/test.go b/geg/other/test.go index 9f3000841..c2217cd37 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,11 +1,12 @@ package main import ( - "fmt" - "github.com/gogf/gf/g/os/gtime" + "github.com/gogf/gf/g/container/garray" + "github.com/gogf/gf/g/test/gtest" ) func main() { - fmt.Println(gtime.Now().Format("U")) - fmt.Println(gtime.Second()) + a2 := []int{1} + array2 := garray.NewSortedIntArrayFrom(a2) + gtest.Assert(array2.Search(2),-1) } \ No newline at end of file