Files
gf/.example/container/garray/sorted_array_basic.go

41 lines
741 B
Go
Raw Normal View History

2018-08-23 17:24:24 +08:00
package main
import (
2019-04-03 00:03:46 +08:00
"fmt"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/garray"
2018-08-23 17:24:24 +08:00
)
2019-04-03 00:03:46 +08:00
func main() {
// 自定义排序数组,降序排序(SortedIntArray管理的数据是升序)
a := garray.NewSortedArray(func(v1, v2 interface{}) int {
if v1.(int) < v2.(int) {
return 1
}
if v1.(int) > v2.(int) {
return -1
}
return 0
})
2018-08-23 17:24:24 +08:00
2019-04-03 00:03:46 +08:00
// 添加数据
a.Add(2)
a.Add(3)
a.Add(1)
fmt.Println(a.Slice())
2018-09-06 18:42:35 +08:00
2019-04-03 00:03:46 +08:00
// 添加重复数据
a.Add(3)
fmt.Println(a.Slice())
2018-09-06 18:42:35 +08:00
2019-04-03 00:03:46 +08:00
// 检索数据,返回最后对比的索引位置,检索结果
// 检索结果0: 匹配; <0:参数小于对比值; >0:参数大于对比值
fmt.Println(a.Search(1))
2018-09-06 18:42:35 +08:00
2019-04-03 00:03:46 +08:00
// 设置不可重复
a.SetUnique(true)
fmt.Println(a.Slice())
a.Add(1)
fmt.Println(a.Slice())
2018-08-23 17:24:24 +08:00
}