如何使用通用结构体?

huangapple go评论93阅读模式
英文:

How use universal struct

问题

我有一个用于订单列表的结构体。我想对map进行排序并找到它的方式。但我不想为我拥有的所有类型都创建这个功能。也许我需要另一段代码?或者我如何重写它以实现通用性,以便我可以在所有具有PrevNext的类型中使用它。

type Post struct { // 举个例子
    P    string
    O    int
    Prev *Post
    Next *Post
}

type Act struct {
    I    int
    Prev *Act
    Next *Act
}

type Map struct {
    Act   map[int]*Act
    First *Act
    Last  *Act
}

func (m *Map) New(a *Act) int {
    Ida++
    f := Ida
    a.Id = Ida
    m.Act[Ida] = a
    if m.First == nil {
        m.First = a
    } else {
        m.Last.Next = a
        a.Prev = m.Last
    }
    m.Last = a
    return f
}

func (m *Map) Del(s int) {
    if _, ok := m.Act[s]; ok {
        if m.Last == m.First {
            m.Last = nil
            m.First = nil
            delete(m.Act, s)
            return
        }
        if m.Last == m.Act[s] {
            m.Last = m.Act[s].Prev
            m.Act[s].Prev.Next = nil
            delete(m.Act, s)
            return
        }
        if m.First == m.Act[s] {
            m.First = m.Act[s].Next
            m.Act[s].Next.Prev = nil
            delete(m.Act, s)
            return
        }
        m.Act[s].Prev.Next = m.Act[s].Next
        m.Act[s].Next.Prev = m.Act[s].Prev
        delete(m.Act, s)
        return
    }
}
英文:

I have struct for order list. I want order map and find it way. But i don't want create this for all types, that i have. Maybe I need another code? Or how I can rewrite it for universal, that I can use it with all types, that have Prev and Next.

type Post struct { //for example
P string
O int
Prev, Next *Post
}
type Act struct {
I int
Prev, Next *Act
}    
type Map struct {
Act         map[int]*Act
First, Last *Act
}
func (m *Map) New(a *Act) int {
Ida++
f := Ida
a.Id = Ida
m.Act[Ida] = a
if m.First == nil {
m.First = a
} else {
m.Last.Next = a
a.Prev = m.Last
}
m.Last = a
return f
}
func (m *Map) Del(s int) {
if _, ok := m.Act
展开收缩
; ok { if m.Last == m.First { m.Last = nil m.First = nil delete(m.Act, s) return } if m.Last == m.Act
展开收缩
{ m.Last = m.Act
展开收缩
.Prev m.Act
展开收缩
.Prev.Next = nil delete(m.Act, s) return } if m.First == m.Act
展开收缩
{ m.First = m.Act
展开收缩
.Next m.Act
展开收缩
.Next.Prev = nil delete(m.Act, s) return } m.Act
展开收缩
.Prev.Next = m.Act
展开收缩
.Next m.Act
展开收缩
.Next.Prev = m.Act
展开收缩
.Prev delete(m.Act, s) return } }

答案1

得分: 2

你可以定义一个函数,该函数可以与实现了Prev()Next()方法的所有类型一起使用。例如,像这样:

type List interface{
    Next() List
    Prev() List
    First() List
    Last() List
    Value() interface{}
    SetNext(List)
    SetPrev(List)
}

func Del(l List, elem_to_delete interface{}){
    for e := l.First(); e != l.Last(); e = l.Next() {
        if l.Value() == elem_to_delete {
            l.Prev().SetNext(l.Next())
            l.Next().SetPrev(l.Prev())
            break
        }
    }
}

//然后为你的类型实现这些方法
type Post struct { //举个例子
    P string
    O int
    Prev, Next *Post
}

func (p Post) Next() List {
    return p.Next
}
...

//然后你可以使用任何这些类型调用Del()函数
Del(post, 5)

此外,Go语言在stdlib中定义了链表数据结构,你也可以使用它。

英文:

You can define function which would work with all types that implements Prev() and Next() methods. For example something like this

type List interface{
Next() List
Prev() List
First() List
Last() List
Value() interface{}
SetNext(List)
SetPrev(List)
}
func Del(l List, elem_to_delete interface{}){
for e:=l.First(); e != l.Last(); e = l.Next()
if l.Value() == elem_to_delete{
l.Prev().SetNext(l.Next())
l.Next().SetPrev(l.Prev())
break
}
}
}
//and implement those methods for your types
type Post struct { //for example
P string
O int
Prev, Next *Post
}
func (p Post) Next() List{
return p.Next
}
...
//and then you can call Del() with any of this types
Del(post, 5)

Also Go have list data structure defined in stdlib which you can use.

答案2

得分: 1

如果将NextPrev操作定义为方法:

type Act struct {
    I int
    Prev, Next *Act
}

func (a *Act) GetNext() *Act {
    return a.Next
}

你可以定义一个接口:

type PrevNext interface {
    GetNext() *Act
    GetPrev() *Act
    // ... 和其他所有必需的操作
}

这样,你可以将Del()定义为一个通用函数,而不是一个方法,该函数期望PrevNext类型的对象,并将所有必需的操作定义为方法。

func Del(m *PrevNext, s int) {
    if _, ok := m.Act[s]; ok {
        if m.GetLast() == m.GetFirst() {
            m.SetLast(nil)
            m.SetFirst(nil)
            delete(m.Act, s)
            return
        }
        // ....
    }
}
英文:

If to define Next and Prev operations as methods:

type Act struct {
I int
Prev, Next *Act
}
func (a *Act) GetNext(){
return a.Next
}    

you could have an interface:

type PrevNext interface {
GetNext() *Act
GetPrev() *Act
// ... and all other required actions
}

So you could define Del() not as method but as generic function which expects object of PrevNext type with all required operations defined as methods.

func Del(m *PrevNext, s int) {
if _, ok := m.Act
展开收缩
; ok { if m.GetLast() == m.GetFirst() { m.SetLast(nil) m.SetFirst(nil) delete(m.Act, s) return } // .... } }

huangapple
  • 本文由 发表于 2017年1月30日 01:28:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/41923825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定