Go 可比较的接口叫什么?
我正在学习Go中的一个简单链表实现。元素的定义如下:Go 可比较的接口叫什么?,go,interface,linked-list,comparison-operators,Go,Interface,Linked List,Comparison Operators,我正在学习Go中的一个简单链表实现。元素的定义如下: type Element struct { next, prev *Element Value interface{} } 如您所见,该值可以是满足空接口的任何内容。现在,作为一个新功能,我想让它成为这样,当您将新元素插入列表时,它会以排序方式插入它-每个元素都将被编辑: 考虑这个用户类型: type userType struct { frequency int value rune }
type Element struct {
next, prev *Element
Value interface{}
}
如您所见,该值可以是满足空接口的任何内容。现在,作为一个新功能,我想让它成为这样,当您将新元素插入列表时,它会以排序方式插入它-每个元素都将被编辑:考虑这个用户类型:
type userType struct {
frequency int
value rune
}
假设要将这些类型添加到链接列表中:它应该先按频率排序,然后如果频率相同,看看char值。因此,
Compare
功能将是:
func (a userType) Compare(b userType) int {
if a.frequency > b.frequency {
return 1
}
if a.frequency < b.frequency {
return -1
}
if a.value > b.value {
return 1
}
if a.value < b.value {
return -1
}
return 0
}
现在将这些{1,'d'}{2,'b'}{3,'c'}{4,'a'}{4,'b'}{4,'c'}
类型添加到链接列表:示例代码:
package main
import (
"container/list"
"fmt"
)
type Comparer interface {
Compare(b userType) int
}
type userType struct {
frequency int
value rune
}
// it should sort by frequency first, then if the frequencies are the same, look at the char value.
func (a userType) Compare(b userType) int {
if a.frequency > b.frequency {
return 1
}
if a.frequency < b.frequency {
return -1
}
if a.value > b.value {
return 1
}
if a.value < b.value {
return -1
}
return 0
}
func Insert(val userType, l *list.List) {
e := l.Front()
if e == nil {
l.PushFront(val)
return
}
for ; e != nil; e = e.Next() {
var ut userType = e.Value.(userType)
if val.Compare(ut) < 0 {
l.InsertBefore(val, e)
return
}
}
l.PushBack(val)
}
func main() {
l := list.New()
Insert(userType{4, 'c'}, l)
Insert(userType{4, 'a'}, l)
Insert(userType{4, 'b'}, l)
Insert(userType{2, 'b'}, l)
Insert(userType{3, 'c'}, l)
Insert(userType{1, 'd'}, l)
for e := l.Front(); e != nil; e = e.Next() {
ut := e.Value.(userType)
fmt.Printf("{%d,%q} ", ut.frequency, ut.value)
}
fmt.Println()
var t interface{} = userType{4, 'c'}
i, ok := t.(Comparer)
fmt.Println(i, ok)
}
package main
import (
"container/list"
"fmt"
)
func Less(a, b interface{}) bool {
switch a.(type) {
case int:
if ai, ok := a.(int); ok {
if bi, ok := b.(int); ok {
return ai < bi
}
}
case string:
if ai, ok := a.(string); ok {
if bi, ok := b.(string); ok {
return ai < bi
}
}
default:
panic("Unknown")
}
return false
}
func Insert(val interface{}, l *list.List) *list.Element {
e := l.Front()
if e == nil {
return l.PushFront(val)
}
for ; e != nil; e = e.Next() {
if Less(val, e.Value) {
return l.InsertBefore(val, e)
}
}
return l.PushBack(val)
}
func main() {
l := list.New()
Insert(4, l)
Insert(2, l)
Insert(3, l)
Insert(1, l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println()
Insert("C", l)
Insert("A", l)
Insert("AB", l)
Insert("C", l)
Insert("C2", l)
Insert("C1", l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println()
}
因此,如果您准备使用已知类型(例如,
int
),请参阅以下示例:
package main
import (
"container/list"
"fmt"
)
func Insert(val int, l *list.List) {
e := l.Front()
if e == nil {
l.PushFront(val)
return
}
for ; e != nil; e = e.Next() {
v := e.Value.(int)
if val < v {
l.InsertBefore(val, e)
return
}
}
l.PushBack(val)
}
func main() {
l := list.New()
Insert(4, l)
Insert(2, l)
Insert(3, l)
Insert(1, l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ") // 1 2 3 4
}
fmt.Println()
}
测试样本代码:
package main
import (
"container/list"
"fmt"
)
type Comparer interface {
Compare(b userType) int
}
type userType struct {
frequency int
value rune
}
// it should sort by frequency first, then if the frequencies are the same, look at the char value.
func (a userType) Compare(b userType) int {
if a.frequency > b.frequency {
return 1
}
if a.frequency < b.frequency {
return -1
}
if a.value > b.value {
return 1
}
if a.value < b.value {
return -1
}
return 0
}
func Insert(val userType, l *list.List) {
e := l.Front()
if e == nil {
l.PushFront(val)
return
}
for ; e != nil; e = e.Next() {
var ut userType = e.Value.(userType)
if val.Compare(ut) < 0 {
l.InsertBefore(val, e)
return
}
}
l.PushBack(val)
}
func main() {
l := list.New()
Insert(userType{4, 'c'}, l)
Insert(userType{4, 'a'}, l)
Insert(userType{4, 'b'}, l)
Insert(userType{2, 'b'}, l)
Insert(userType{3, 'c'}, l)
Insert(userType{1, 'd'}, l)
for e := l.Front(); e != nil; e = e.Next() {
ut := e.Value.(userType)
fmt.Printf("{%d,%q} ", ut.frequency, ut.value)
}
fmt.Println()
var t interface{} = userType{4, 'c'}
i, ok := t.(Comparer)
fmt.Println(i, ok)
}
package main
import (
"container/list"
"fmt"
)
func Less(a, b interface{}) bool {
switch a.(type) {
case int:
if ai, ok := a.(int); ok {
if bi, ok := b.(int); ok {
return ai < bi
}
}
case string:
if ai, ok := a.(string); ok {
if bi, ok := b.(string); ok {
return ai < bi
}
}
default:
panic("Unknown")
}
return false
}
func Insert(val interface{}, l *list.List) *list.Element {
e := l.Front()
if e == nil {
return l.PushFront(val)
}
for ; e != nil; e = e.Next() {
if Less(val, e.Value) {
return l.InsertBefore(val, e)
}
}
return l.PushBack(val)
}
func main() {
l := list.New()
Insert(4, l)
Insert(2, l)
Insert(3, l)
Insert(1, l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println()
Insert("C", l)
Insert("A", l)
Insert("AB", l)
Insert("C", l)
Insert("C2", l)
Insert("C1", l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println()
}
这是一个可能的答案,我已经能够使工作,但它是如此丑陋,(减少每一个比较到一个int值-可能适用于字符串,但将血腥的其他类型很难)-我不想接受它:
type MyInt int
type Valuer interface {
ValueOf() int
}
func LessThan(i, j Valuer) bool {
return i.ValueOf() < j.ValueOf()
}
func EqualTo(i, j Valuer) bool {
return i.ValueOf() == j.ValueOf()
}
func (i MyInt) ValueOf() int {
return int(i)
}
type Element struct {
next, prev *Element
Value Valuer
}
键入MyInt
类型估价师接口{
ValueOf()int
}
func LessThan(i,j估价师)bool{
返回i.ValueOf()
有人请告诉我有更好的方法 谢谢,但这不是我想要做的。我不想对构造的列表进行排序。AFAIK,
sort.Interface
在容器上实现。我需要确保元素具有可比性。@DomoArigato:请参见新编辑,如果您提供最低限度的工作示例代码,我可以提供更多帮助。我想重点是Value
不是一个“新类型”,因此他们无法向其添加Compare
函数。我曾考虑定义自己的compariable
接口,但据我所知,这意味着不可能使用简单的本机类型作为值,因为它们不实现这个新接口。当我尝试时,我得到了无法在非本地类型int上定义新方法的答案,然后使用reflect
包就是答案。你看了标准和实现如何处理这个问题吗?你不能。没有像“任何东西都可以通过网络进行比较”这样的类型
{1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'}
{4 99} true
package main
import (
"container/list"
"fmt"
)
func Insert(val int, l *list.List) {
e := l.Front()
if e == nil {
l.PushFront(val)
return
}
for ; e != nil; e = e.Next() {
v := e.Value.(int)
if val < v {
l.InsertBefore(val, e)
return
}
}
l.PushBack(val)
}
func main() {
l := list.New()
Insert(4, l)
Insert(2, l)
Insert(3, l)
Insert(1, l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ") // 1 2 3 4
}
fmt.Println()
}
func Less(a, b interface{}) bool {
switch a.(type) {
case int:
if ai, ok := a.(int); ok {
if bi, ok := b.(int); ok {
return ai < bi
}
}
case string:
if ai, ok := a.(string); ok {
if bi, ok := b.(string); ok {
return ai < bi
}
}
// ...
default:
panic("Unknown")
}
return false
}
package main
import (
"container/list"
"fmt"
)
func Less(a, b interface{}) bool {
switch a.(type) {
case int:
if ai, ok := a.(int); ok {
if bi, ok := b.(int); ok {
return ai < bi
}
}
case string:
if ai, ok := a.(string); ok {
if bi, ok := b.(string); ok {
return ai < bi
}
}
default:
panic("Unknown")
}
return false
}
func Insert(val interface{}, l *list.List) *list.Element {
e := l.Front()
if e == nil {
return l.PushFront(val)
}
for ; e != nil; e = e.Next() {
if Less(val, e.Value) {
return l.InsertBefore(val, e)
}
}
return l.PushBack(val)
}
func main() {
l := list.New()
Insert(4, l)
Insert(2, l)
Insert(3, l)
Insert(1, l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println()
Insert("C", l)
Insert("A", l)
Insert("AB", l)
Insert("C", l)
Insert("C2", l)
Insert("C1", l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
fmt.Println()
}
1 2 3 4
1 2 3 4 A AB C C C1 C2
type MyInt int
type Valuer interface {
ValueOf() int
}
func LessThan(i, j Valuer) bool {
return i.ValueOf() < j.ValueOf()
}
func EqualTo(i, j Valuer) bool {
return i.ValueOf() == j.ValueOf()
}
func (i MyInt) ValueOf() int {
return int(i)
}
type Element struct {
next, prev *Element
Value Valuer
}