如何使用通用结构体?

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

How use universal struct

问题

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

  1. type Post struct { // 举个例子
  2. P string
  3. O int
  4. Prev *Post
  5. Next *Post
  6. }
  7. type Act struct {
  8. I int
  9. Prev *Act
  10. Next *Act
  11. }
  12. type Map struct {
  13. Act map[int]*Act
  14. First *Act
  15. Last *Act
  16. }
  17. func (m *Map) New(a *Act) int {
  18. Ida++
  19. f := Ida
  20. a.Id = Ida
  21. m.Act[Ida] = a
  22. if m.First == nil {
  23. m.First = a
  24. } else {
  25. m.Last.Next = a
  26. a.Prev = m.Last
  27. }
  28. m.Last = a
  29. return f
  30. }
  31. func (m *Map) Del(s int) {
  32. if _, ok := m.Act[s]; ok {
  33. if m.Last == m.First {
  34. m.Last = nil
  35. m.First = nil
  36. delete(m.Act, s)
  37. return
  38. }
  39. if m.Last == m.Act[s] {
  40. m.Last = m.Act[s].Prev
  41. m.Act[s].Prev.Next = nil
  42. delete(m.Act, s)
  43. return
  44. }
  45. if m.First == m.Act[s] {
  46. m.First = m.Act[s].Next
  47. m.Act[s].Next.Prev = nil
  48. delete(m.Act, s)
  49. return
  50. }
  51. m.Act[s].Prev.Next = m.Act[s].Next
  52. m.Act[s].Next.Prev = m.Act[s].Prev
  53. delete(m.Act, s)
  54. return
  55. }
  56. }
英文:

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.

  1. type Post struct { //for example
  2. P string
  3. O int
  4. Prev, Next *Post
  5. }
  6. type Act struct {
  7. I int
  8. Prev, Next *Act
  9. }
  10. type Map struct {
  11. Act map[int]*Act
  12. First, Last *Act
  13. }
  14. func (m *Map) New(a *Act) int {
  15. Ida++
  16. f := Ida
  17. a.Id = Ida
  18. m.Act[Ida] = a
  19. if m.First == nil {
  20. m.First = a
  21. } else {
  22. m.Last.Next = a
  23. a.Prev = m.Last
  24. }
  25. m.Last = a
  26. return f
  27. }
  28. func (m *Map) Del(s int) {
  29. if _, ok := m.Act
    展开收缩
    ; ok {
  30. if m.Last == m.First {
  31. m.Last = nil
  32. m.First = nil
  33. delete(m.Act, s)
  34. return
  35. }
  36. if m.Last == m.Act
    展开收缩
    {
  37. m.Last = m.Act
    展开收缩
    .Prev
  38. m.Act
    展开收缩
    .Prev.Next = nil
  39. delete(m.Act, s)
  40. return
  41. }
  42. if m.First == m.Act
    展开收缩
    {
  43. m.First = m.Act
    展开收缩
    .Next
  44. m.Act
    展开收缩
    .Next.Prev = nil
  45. delete(m.Act, s)
  46. return
  47. }
  48. m.Act
    展开收缩
    .Prev.Next = m.Act
    展开收缩
    .Next
  49. m.Act
    展开收缩
    .Next.Prev = m.Act
    展开收缩
    .Prev
  50. delete(m.Act, s)
  51. return
  52. }
  53. }

答案1

得分: 2

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

  1. type List interface{
  2. Next() List
  3. Prev() List
  4. First() List
  5. Last() List
  6. Value() interface{}
  7. SetNext(List)
  8. SetPrev(List)
  9. }
  10. func Del(l List, elem_to_delete interface{}){
  11. for e := l.First(); e != l.Last(); e = l.Next() {
  12. if l.Value() == elem_to_delete {
  13. l.Prev().SetNext(l.Next())
  14. l.Next().SetPrev(l.Prev())
  15. break
  16. }
  17. }
  18. }
  19. //然后为你的类型实现这些方法
  20. type Post struct { //举个例子
  21. P string
  22. O int
  23. Prev, Next *Post
  24. }
  25. func (p Post) Next() List {
  26. return p.Next
  27. }
  28. ...
  29. //然后你可以使用任何这些类型调用Del()函数
  30. 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

  1. type List interface{
  2. Next() List
  3. Prev() List
  4. First() List
  5. Last() List
  6. Value() interface{}
  7. SetNext(List)
  8. SetPrev(List)
  9. }
  10. func Del(l List, elem_to_delete interface{}){
  11. for e:=l.First(); e != l.Last(); e = l.Next()
  12. if l.Value() == elem_to_delete{
  13. l.Prev().SetNext(l.Next())
  14. l.Next().SetPrev(l.Prev())
  15. break
  16. }
  17. }
  18. }
  19. //and implement those methods for your types
  20. type Post struct { //for example
  21. P string
  22. O int
  23. Prev, Next *Post
  24. }
  25. func (p Post) Next() List{
  26. return p.Next
  27. }
  28. ...
  29. //and then you can call Del() with any of this types
  30. Del(post, 5)

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

答案2

得分: 1

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

  1. type Act struct {
  2. I int
  3. Prev, Next *Act
  4. }
  5. func (a *Act) GetNext() *Act {
  6. return a.Next
  7. }

你可以定义一个接口:

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

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

  1. func Del(m *PrevNext, s int) {
  2. if _, ok := m.Act[s]; ok {
  3. if m.GetLast() == m.GetFirst() {
  4. m.SetLast(nil)
  5. m.SetFirst(nil)
  6. delete(m.Act, s)
  7. return
  8. }
  9. // ....
  10. }
  11. }
英文:

If to define Next and Prev operations as methods:

  1. type Act struct {
  2. I int
  3. Prev, Next *Act
  4. }
  5. func (a *Act) GetNext(){
  6. return a.Next
  7. }

you could have an interface:

  1. type PrevNext interface {
  2. GetNext() *Act
  3. GetPrev() *Act
  4. // ... and all other required actions
  5. }

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.

  1. func Del(m *PrevNext, s int) {
  2. if _, ok := m.Act
    展开收缩
    ; ok {
  3. if m.GetLast() == m.GetFirst() {
  4. m.SetLast(nil)
  5. m.SetFirst(nil)
  6. delete(m.Act, s)
  7. return
  8. }
  9. // ....
  10. }
  11. }

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:

确定