可比接口称为什么? [英] What is the comparable interface called?
问题描述
出于学习目的,我正在Go中进行一个简单的链表实现.元素的定义如下:
I'm working on a simple linked list implementation in Go for learning purposes. The definition of an element is below:
type Element struct {
next, prev *Element
Value interface{}
}
如您所见,Value可以是满足空接口的任何值.现在,作为一项新功能,我要使其成为一个新元素,以便在将新元素插入列表时,它以有序方式插入-每个元素将是< =下一个元素.
As you can see, the Value can be anything that satisfies the empty interface. Now, as a new feature, I would like to make it so that when you insert a new element into the list, it inserts it in a sorted manner - each element will be <= the next.
为此,我编写了以下方法:
In order to do this, I wrote the following method:
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 < not defined on interface
这是公平的.因此,我知道在Element typedef中,我应该将Value限制为可以使用<
运算符进行比较的类型.我在研究Go不支持运算符重载的问题时了解了这一点-我不是在尝试这样做.相反,我只是试图确保Element.Value是可以使用<
运算符进行比较的类型.我该怎么做?
The compiler complains operator < not defined on interface
which is fair. So I understand that in my Element typedef, I should restrict Value to types that can be compared using the <
operator. I learned this while researching this problem that Go does not support operator overloading - I am not trying to do that. Instead, I am just trying to make sure that Element.Value is a type that can be compared using the <
operator. How do I do this?
在我看来,简单地定义一个基于内置类型的新类型可能并不难,可以通过某些功能进行比较.所以我写了这个烂摊子(以及尝试做同一件事的其他方法):
It occurs to me that it might not be too difficult to simple define a new type, based on a built-in, that can be compared through some function. so I wrote this mess (as well as a bunch of other ways of trying to do the same thing):
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
}
我真正想要的是定义一个接口,如果为一种类型实现,则提供功能LessThan
和EqualTo
,它们在该类型的两个实例上运行并提供bool-类似LessThan(i, j WhatEvers) bool
这样的接口可以代替<
使用.我意识到下面它是作为实例方法实现的-我已经尝试了两种方法,但都没有成功.通过以上操作,我将在Add函数中使用类似this.next.Value.LessThan(val)
的命令.我得到了:
What I would really like is to define an interface, which, if implemented for a type, provides functions LessThan
and EqualTo
that operate on two instances of that type and provides a bool - something like LessThan(i, j WhatEvers) bool
which can be used in place of <
. I realize below it's implemented as an instance method - I've tried both ways but no success. With the above, I would use it something like: this.next.Value.LessThan(val)
in the Add function. I get either:
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
是否有可能使用接口来要求必须存在对自定义类型的两个实例进行操作的某个函数,或者仅用于方法?
Is this possible to use an interface to require that a certain function must exist that operates on two instances of a custom type, or is it only for Methods?
推荐答案
考虑此用户类型:
consider this user type:
type userType struct {
frequency int
value rune
}
,并假设您想将此类型添加到链接列表中:
并且应该首先按频率排序,然后如果频率相同,请查看char值.因此Compare
函数将为:
and suppose you want to add this types to your Linked List:
and It should be sorted by frequency first, then if the frequencies are the same, look at the char value. so the Compare
function will be:
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:
示例代码:
now add these {1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'}
types to LinkeList:
sample code:
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
),请参见以下示例:
so if you ready to use known type (e.g. int
), see this sample:
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()
}
旧:
old:
Go中没有这样的界面.您可以编写此Less
函数来比较您的类型:
There is no such interface in Go. You may write this Less
function to compare your types:
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
这篇关于可比接口称为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!