英文:
Container types in Go
问题
我正在尝试熟悉Go语言,所以尝试实现一些搜索功能,但是在查看容器类型的文档时,没有一个内置类型实现了contains
方法。我是否遗漏了什么,如果没有,我该如何测试成员资格?我必须实现自己的方法还是必须遍历所有元素?如果是这样,为什么容器类型会省略这个基本方法的原因是什么?
英文:
I am trying to familiarize myself with Go and so was trying to implements some search function but looking through the docs for the container types, none of the inbuilt type implements a contains
method. Am i missing something and if not how do i go about testing for membership? Do I have to implement my own method or i have to iterate through all elements. If this is so what is the rationale behind the omission of this elementary method for container types?
答案1
得分: 6
标准库的容器类型在提取元素时需要进行类型断言。这些容器本身无法进行成员测试,因为它们不知道它们所包含的类型,并且无法进行比较。
Ric Szopa的跳表实现可能是你正在寻找的。它有一个Set类型,实现了Contains方法。
https://github.com/ryszard/goskiplist
我已经在生产环境中使用它,并且非常满意。
英文:
The standard library's container types require you do type assertions when pulling elements out. The containers themselves have no way of doing tests for membership because they don't know the types they're containing and have no way of doing a comparison.
Ric Szopa's skip list implementation might be what you're looking for. It has a Set type which implements a Contains method.
https://github.com/ryszard/goskiplist
I've been using it in production and am quite happy with it.
答案2
得分: 4
地图是一种内置类型,它具有一个“包含”结构,而不是一个方法。
http://play.golang.org/p/ddpmiskxqS
package main
import (
"fmt"
)
func main() {
a := map[string]string{"foo": "bar"}
_, k := a["asd"]
fmt.Println(k)
_, k = a["foo"]
fmt.Println(k)
}
英文:
Maps are a built-in type which has a "contains" construct, not a method, though.
http://play.golang.org/p/ddpmiskxqS
package main
import (
"fmt"
)
func main() {
a := map[string]string{"foo": "bar"}
_, k := a["asd"]
fmt.Println(k)
_, k = a["foo"]
fmt.Println(k)
}
答案3
得分: 3
使用container/list包,您可以编写自己的循环来搜索内容。不提供此功能的原因可能如Dystroy所说,这将隐藏一个O(n)的操作。
您无法添加方法,所以只需编写一个循环。
for e := l.Front(); e != nil; e = e.Next() {
data := e.Value.(dataType) // 类型断言
if /* 对data进行测试 */ {
// 做一些操作
break
}
}
这很简单,O(n)的复杂度也很明显。
在您对Go提供的支持搜索的数据结构进行评估时,不要错过sort包。该包中的函数允许对切片进行O(n log(n))的排序,然后在O(log(n))的时间内进行二分搜索。
最后,正如Daniel建议的那样,考虑使用第三方包。有一些受欢迎且成熟的容器类型的包可供使用。
英文:
With the container/list package, you write your own loop to search for things. The reasoning for not having this provided in the package is probably as Dystroy said, that would hide an O(n) operation.
You can't add a method, so you just write a loop.
for e := l.Front(); e != nil; e = e.Next() {
data := e.Value.(dataType) // type assertion
if /* test on data */ {
// do something
break
}
}
It's simple enough and the O(n) complexity is obvious.
In your review of data structures supplied with Go that support searching, don't miss the sort package. Functions there allow a slice to be sorted in O(n log(n)) and then binary searched in O(log(n)) time.
Finally as Daniel suggested, consider third-party packages. There are some popular and mature packages for container types.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论