【问题标题】:What is the comparable interface called?可比接口叫什么?
【发布时间】:2016-11-02 12:54:55
【问题描述】:

出于学习目的,我正在使用 Go 开发一个简单的链表实现。元素的定义如下:

type Element struct {
    next, prev *Element
    Value      interface{}
}

如您所见,Value 可以是任何满足空接口的东西。现在,作为一项新功能,我想这样做,以便当您将新元素插入列表时,它会以排序方式插入 - 每个元素都将是

为了做到这一点,我写了以下方法:

func (l *LinkedList) Add(val interface{}) *Element {
    this := &l.Root
    e := Element{Value: val}
    for {
        if this.next.Value != nil && this.next.Value < val {  // <-comparison here
            this = this.next
        } else {
            return l.insert(&e, this)
        }
    }
}

编译器抱怨operator &lt; not defined on interface 这是公平的。所以我知道在我的 Element typedef 中,我应该将 Value 限制为可以使用 &lt; 运算符进行比较的类型。我在研究 Go 不支持运算符重载的问题时了解到这一点——我不想这样做。相反,我只是想确保 Element.Value 是一种可以使用 &lt; 运算符进行比较的类型。我该怎么做?

更新:

在我看来,简单地定义一个基于内置的新类型可能并不难,可以通过一些函数进行比较。所以我写了这个烂摊子(以及许多其他尝试做同样事情的方法):

type Comparable interface {
    LessThan(j interface{}) bool // tried (j Comparable), (j MyInt), etc
    EqualTo(j interface{}) bool  // tried (j Comparable), (j MyInt), etc
}

type MyInt int

func (i MyInt) LessThan(j MyInt) bool {
    return i < j
}

func (i MyInt) EqualTo(j MyInt) bool {
    return i == j
}

type Element struct {
    next, prev *Element
    Value      Comparable
}

我真正想要的是定义一个接口,如果为一个类型实现它,它提供函数LessThanEqualTo 操作该类型的两个实例并提供一个布尔值 - 类似于LessThan(i, j WhatEvers) bool可以用来代替&lt;。我在下面意识到它是作为实例方法实现的 - 我尝试了两种方法但没有成功。有了上面的内容,我会在 Add 函数中使用类似:this.next.Value.LessThan(val)。我得到:

linkedlist.MyInt does not implement linkedlist.Comparable (wrong type for EqualTo method)
    have EqualTo(linkedlist.MyInt) bool
    want EqualTo(interface {}) bool

linkedlist.MyInt does not implement linkedlist.Comparable (wrong type for EqualTo method)
    have EqualTo(linkedlist.MyInt) bool
    want EqualTo(linkedlist.Comparable) bool

这是否可以使用接口来要求必须存在对自定义类型的两个实例进行操作的某个函数,还是仅用于方法?

【问题讨论】:

  • 您是否看过标准的linked listsort 实现如何处理这个问题?
  • 你不能。没有像“所有允许通过

标签: go interface linked-list comparison-operators


【解决方案1】:

编辑:
考虑这种用户类型:

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
}

满足这个接口的:

type Comparer interface {
    Compare(b userType) int
}

现在将这些 {1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'} 类型添加到 LinkeList:
示例代码:

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)
}

和输出:

{1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'} 
{4 99} true

因此,如果您准备使用已知类型(例如 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()
}

旧:

Go 中没有这样的接口。你可以写这个Less函数来比较你的类型:

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 

【讨论】:

  • 谢谢,但这不是我想要做的。我不想对构造的列表进行排序。 AFAIK,sort.Interface 在容器上实现。我需要确保元素具有可比性。
  • @domoarrigato:查看新编辑,如果您提供最低工作示例代码,我可以提供更多帮助。
  • 我认为重点是Value 不是“新类型”,因此他们无法向其中添加Compare 函数。
  • 我想过定义一个我自己的Comparable 接口,但据我了解,这意味着不可能使用简单的本机类型作为值,因为它们没有实现这个新接口.当我尝试时,我得到了cannot define new methods on non-local type int
  • 然后使用reflect 包就是答案
【解决方案2】:

这是我能够完成的一个可能的答案,但它太丑了,(将每次比较都减少为一个 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
}

有人请告诉我有更好的方法!

【讨论】:

    猜你喜欢
    • 2013-08-10
    • 2016-02-14
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多