doupa9062 2018-11-16 03:19
浏览 41
已采纳

我想知道如何将set struct实现为地图值[关闭]

I want to use set as map value on golang. So I coded like this:

import (
   "fmt"
   "reflect"
)

type TestSet struct {
   Items []Test
}

func (ts *TestSet) Add(t *Test) {
   ok := true
   for _, item := range ts.Items {
      if item.Equal(t) {
         ok = false
         break
      }
   }
   if ok {
      ts.Items = append(ts.Items, *t)
   }
}

type Test struct {
   phoneNumber string
   name        string
   friends     []string // i add this field! (**edit**)
}

func (t *Test) Equal(t2 *Test) bool {
   if t.phoneNumber != t2.phoneNumber || t.name != t2.name {
      return false
   }
   if !reflect.DeepEqual(t.friends, t2.friends) {
      return false
   }
   return true
}

And I want to use structure like below code:

val := make(map[int]*TestSet)
val[1] = &TestSet{}
val[1].Add(&Test{phoneNumber: "8210", name: "minji", friends: []string{"myself"})

However my TestSet always has to iterate over the entire item to exist its value. So Add() time complexity O(n).

I want to reduce that time complexity to O(1). (like python set in)

But, I do not know what to do. Should I use another map?

Any good ideas?

  • 写回答

3条回答 默认 最新

  • doupeng6890 2018-11-16 04:20
    关注

    Sets are often implemented as maps with no value. A struct{} is effectively empty in Go.

    type Empty struct {}
    
    type TestSet struct {
       set map[Test]Empty
    }
    

    In order for this to work, Test must be comparable.

    Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

    So Test is comparable.

    package main;
    
    import (
        "fmt"
    )
    
    type Empty struct {}
    
    type TestSet struct {
        set map[Test]Empty
    }
    
    func (ts *TestSet) Add(t Test) bool {
        if _, present := ts.set[t]; present {
            return false
        } else {
            ts.set[t] = Empty{}
            return true
        }
    }
    
    type Test struct {
        phoneNumber string
        name        string
    }
    
    func main() {
        set := TestSet{ set: make(map[Test]Empty) }
        test1 := Test{ phoneNumber: "555-555-5555", name: "Yarrow Hock" }
        test2 := Test{ phoneNumber: "555-555-5555", name: "Yarrow Hock" }
        test3 := Test{ phoneNumber: "123-555-5555", name: "Yarrow Hock" }
        if set.Add( test1 ) {
            fmt.Println("Added 1")
        }
        if set.Add( test2 ) {
            fmt.Println("Added 2")
        }
        if set.Add( test3 ) {
            fmt.Println("Added 3")
        }
    
        for test := range set.set {
            fmt.Println(test.phoneNumber)
        }
    }
    

    You can also use the golang-set library.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))